永发信息网

C语言编写加减计算程序

答案:7  悬赏:20  手机版
解决时间 2021-02-01 06:21
  • 提问者网友:棒棒糖
  • 2021-01-31 09:36
C语言编写加减计算程序
最佳答案
  • 五星知识达人网友:洎扰庸人
  • 2021-01-31 10:16
简单的加减运算C语言很好处理。
#include
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();

}
全部回答
  • 1楼网友:愁杀梦里人
  • 2021-01-31 14:20
#include
main (void)
{
int a,b,c;
char op;
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);//这句不需要
if(op=='+')
{c=a+b;}

if(op=='-')
{c=a-b;}
加上一句
printf("\n%d",c);//输出结果

}
  • 2楼网友:狂恋
  • 2021-01-31 13:55
#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{
brr[c]=ptr[b]-48;
b++;
c++;
}
f=c-1;
if(c==1)
{
*num=b-1;
return brr[0];
}
else
{
for(d=0;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楼网友:夜余生
  • 2021-01-31 13:39
首先你的printf中应无&符!
其次那条语句也无用。
你应该在if 语句后写条printf(“a %c b = %d\n”, op, c);
  • 4楼网友:底特律间谍
  • 2021-01-31 12:32
首先你的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
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 = ?
  • 5楼网友:患得患失的劫
  • 2021-01-31 12:13
#include
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```
}
  • 6楼网友:春色三分
  • 2021-01-31 11:27
#include
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我
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯