C语言编写加减计算程序

我老师要我编写一个加减计算的程序
下面是要求:从键盘得到 数据a b 和运算符号 c
比如输入 1 + 3
就返回4
输入 4 - 1
就返回 3
至少要实现 加减运算

运算符号用%c得到 注意得到的是char类型 自己用if语句判断得到的c是 '+' 还是 ‘-’
然后进行对应的运算 注意'+' 的''符号 是一个' 代表是一个字符常量
大体过程:int a,b,c
char op;

scanf ("%d %c %d", a, op, b);

if (op == '+') {
c = a + b;
}
if (op == '-' ) {
c = a - b;
}

大体就这样 判断操作符号是'+' 还是 '-' 然后进行对应的操作

下面是我编写的程序:
#include <stdio.h>
main (void)

{
int a,b,c;
char op;
scanf ("%d %c %d", a, op, b);

printf("a=%d,op=%c,b=%d\n",&a,&op,&b);

if(op=='+')
{c=a+b;}

if(op=='-')
{c=a-b;}

}
这个程序肯定有错误。我老师说:printf("a=%d,op=%c,b=%d\n",a,op,b); 这句是干什么? 你应该输出a + b = ?
但是我不知道如何做,请各位大大帮我看下错误提点下.

#include <stdio.h>

main (void)

{
int a,b,c;
char op;
scanf ("%d%c%d",&a,&op,&b);//最好空格去掉,这样就可以直接写成a+b或者a-b。如果有空格的话要记得空格也要打a + b。要不会出现错误 ,而且输入的时候需要用地址符&····

if(op=='+')
{c=a+b;}

if(op=='-')
{c=a-b;}
printf("%d%c%d=%d\n",a,op,b,c);//楼主这句应该放在数据处理完后。。要不你输出的a,op,b都是在没处理过的数,也就是原来的值,而且最好是把,去掉,这样更美观 ,而且printf后面不需要&。。。切记。而且没必要那样输出。请楼主看我的printf```
}
还不清楚 来hi我
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-10-07
#include <stdio.h>

main (void)

{
int a,b,c;
char op;
scanf ("%d%c%d",&a,&op,&b);//最好空格去掉,这样就可以直接写成a+b或者a-b。如果有空格的话要记得空格也要打a + b。要不会出现错误 ,而且输入的时候需要用地址符&····

if(op=='+')
{c=a+b;}

if(op=='-')
{c=a-b;}
printf("%d%c%d=%d\n",a,op,b,c);//楼主这句应该放在数据处理完后。。要不你输出的a,op,b都是在没处理过的数,也就是原来的值,而且最好是把,去掉,这样更美观 ,而且printf后面不需要&。。。切记。而且没必要那样输出。请楼主看我的printf```
}
第2个回答  2009-08-07
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
struct node{
int arr;
struct node *next;
};
typedef struct node Node;
typedef Node *Stack;
struct stacktop{
Stack top;
};
typedef struct stacktop *Top;
int jisuan(int sr1,int sr2,int sr3);
int opinion4(char *ptr,int *num);
int opinion3(char ch);
int opinion2(char ch);
int opinion(char ch);
void StackPush(int ch,Top ptop);
void StackPop(int *pi,Top ptop);
int main(void)
{

int value1;
int value2;
int value3;
char ch[100];
int R,a;
int i=0;
Top svalue;
Top sctype;
svalue=(Top)malloc(sizeof(struct stacktop));
svalue->top=NULL;
sctype=(Top)malloc(sizeof(struct stacktop));
sctype->top=NULL;
StackPush('a',sctype);
printf("请输入一个表达式:\n-->");
gets(ch);
while(ch!=NULL&&ch[i]!='\0')
{
if(opinion(ch[i])==1)//如果是操作符
{
if(opinion2(ch[i])<=opinion2(sctype->top->arr))
{
StackPop(&value1,svalue);
StackPop(&value2,svalue);
StackPop(&value3,sctype);

R=jisuan(value1,value2,value3);
StackPush(R,svalue);
}
StackPush(ch[i],sctype);
}
else if(opinion3(ch[i])==1)//不是操作符,是操作数
{
a=opinion4(ch,&i);
StackPush(a,svalue);
}
else
{
break;
}
i++;
}

while(sctype->top->arr!='a')
{
StackPop(&value1,svalue);
StackPop(&value2,svalue);
StackPop(&value3,sctype);
R=jisuan(value1,value2,value3);
StackPush(R,svalue);
}
printf("%d",R);
return 0;
}

int jisuan(int sr1,int sr2,int sr3)
{
switch((char)sr3)
{
case '+':return sr2+sr1;
case '-':return sr2-sr1;
case '*':return sr2*sr1;
case '/':return sr2/sr1;
}
}

/*将字符转换成数字*/
int opinion4(char *ptr,int *num)
{
int x;
int b=*num;
int c=0;
int d,e,f;
int brr[50];
int fang=1;
int total=0;
x=strlen(ptr);
while((opinion3(ptr[b])==1)&&b<x)
{
brr[c]=ptr[b]-48;
b++;
c++;
}

f=c-1;
if(c==1)
{
*num=b-1;
return brr[0];
}
else
{
for(d=0;d<c;d++)
{
for(e=f;e>0;e--)
fang=fang*10;
total=total+brr[d]*fang;
f--;
fang=1;
}
*num=b-1;
return total;
}
}

/*判断是否为数字*/
int opinion3(char ch)
{
if(ch>='0'&&ch<='9')
return 1;
else
return 0;
}

/*判断运算符的优先级*/
int opinion2(char ch)
{
switch(ch)
{
case '*':
case '/':return 2;
case '+':
case '-':return 1;
default:return 0;
}
}

void StackPop(int *pi,Top ptop)
{
Stack pnew;
pnew=ptop->top;
*pi=pnew->arr;
ptop->top=pnew->next;
free(pnew);
}

void StackPush(int ch,Top ptop)
{
Stack pnew;
pnew=(Stack)malloc(sizeof(Node));
if(pnew==NULL)
exit(1);
pnew->arr=ch;
if(ptop->top==NULL)
pnew->next=NULL;
else
pnew->next=ptop->top;
ptop->top=pnew;

}

/*判断是否为运算符*/
int opinion(char ch)
{
switch(ch)
{
case '+':
case '-':
case '*':
case '/': return 1;
default:
return 0;
}
}

不明白加我百度HI
第3个回答  2009-08-07
首先你的printf 以及 scanf 函数都有语法错误
scanf ("%d %c %d", a, op, b);
这句应该是
scanf ("%d %c %d", &a, &op,&b);
要记得不要少&
&是取地址运算符,是用于取出变量在内存中的地址用的。
然后
printf("a=%d,op=%c,b=%d\n",&a,&op,&b);
这句中就不需要&符号
以下是参考程序
#include <stdio.h>
main (void)

{
int a,b,c;
char op;
scanf ("%d%c%d", &a, &op, &b);

if(op=='+')
{c=a+b;}

if(op=='-')
{c=a-b;}
printf("%d %c %d = %d",a,op,b,c); //输出结果
}
pur_e 的程序输出错了他的只有结果,而题目要求输出的是a + b = ?
第4个回答  推荐于2017-10-03
简单的加减运算C语言很好处理。
#include <stdio.h>
main (void)
{
int a,b,result;
char opprator;
printf("Please input a op b(as 1 + 2)");
scanf ("%d%c%d",&a,&opprator,&b);
if(op=='+')result=a+b;
if(op=='-')result=a-b;
printf("%d%c%d=%d\n",a,opprator,b,c);
getch();

}

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网