怎么能把此程序改成可以运算3个数2个运算符的
答案:3 悬赏:60 手机版
解决时间 2021-03-08 15:46
- 提问者网友:风月客
- 2021-03-08 08:04
怎么能把此程序改成可以运算3个数2个运算符的
最佳答案
- 五星知识达人网友:未来江山和你
- 2021-03-08 09:15
#include
void main()
{
int a,b,c;
int d;
char op,ap;
printf("qing shu ru yi ge shu");
scanf("%d",&a);
printf("\nshu ru yun suan fu:");
scanf("%s",&op);
printf("\nqing shu ru ling yi ge shu");
scanf("%d",&b);
printf("\nshu ru yun suan fu:");
scanf("%s",&ap);
printf("\nqing shu ru di san fe shu");
scanf("%d",&c);
switch(op)
{case'+':
printf("\n=%d\n",d=a+b);
break;
case'-':
printf("\n=%d\n",d=a-b);
break;
case'*':
printf("\n=%d\n",d=a*b);
break;
case'/':
printf("\n=%d\n",d=a/b);
break;
default:
printf("\nwrong\n");
}
switch(ap)
{case'+':
printf("\n=%d\n",d+c);
break;
case'-':
printf("\n=%d\n",d-c);
break;
case'*':
printf("\n=%d\n",d*c);
break;
case'/':
printf("\n=%d\n",d/c);
break;
default:
printf("\nwrong\n");
}
}
这样用一个值保存第一次的运算结果就行了。另外你的程序中没有考虑除数为0的情况,需要加上。
void main()
{
int a,b,c;
int d;
char op,ap;
printf("qing shu ru yi ge shu");
scanf("%d",&a);
printf("\nshu ru yun suan fu:");
scanf("%s",&op);
printf("\nqing shu ru ling yi ge shu");
scanf("%d",&b);
printf("\nshu ru yun suan fu:");
scanf("%s",&ap);
printf("\nqing shu ru di san fe shu");
scanf("%d",&c);
switch(op)
{case'+':
printf("\n=%d\n",d=a+b);
break;
case'-':
printf("\n=%d\n",d=a-b);
break;
case'*':
printf("\n=%d\n",d=a*b);
break;
case'/':
printf("\n=%d\n",d=a/b);
break;
default:
printf("\nwrong\n");
}
switch(ap)
{case'+':
printf("\n=%d\n",d+c);
break;
case'-':
printf("\n=%d\n",d-c);
break;
case'*':
printf("\n=%d\n",d*c);
break;
case'/':
printf("\n=%d\n",d/c);
break;
default:
printf("\nwrong\n");
}
}
这样用一个值保存第一次的运算结果就行了。另外你的程序中没有考虑除数为0的情况,需要加上。
全部回答
- 1楼网友:渊鱼
- 2021-03-08 11:30
你这个只能从右边向左边运算的呀!!要是2-3*4就不行了吧!还得用栈来算才行
- 2楼网友:孤独入客枕
- 2021-03-08 10:35
#include
void main()
{int a,b,c;
char op,ap;
printf("qing shu ru yi ge shu");
scanf("%d",&a);
printf("\nshu ru yun suan fu:");
scanf("%s",&op);
printf("\nqing shu ru ling yi ge shu");
scanf("%d",&b);
printf("\nshu ru yun suan fu:");
scanf("%s",&ap);
printf("\nqing shu ru di san fe shu");
scanf("%d",&c);
switch(ap)
{case'*':
b=b*c;
printf("\n=%d\n",b);
break;
case'/':
b=b*c;
printf("\n=%d\n",b);
break;
default:
;
}
switch(op)
{case'+':
printf("\n=%d\n",a+b);
break;
case'-':
printf("\n=%d\n",a-b);
break;
case'*':
printf("\n=%d\n",a*b);
break;
case'/':
printf("\n=%d\n",a/b);
break;
default:
printf("\nwrong\n");
}
switch(ap)
{case'+':
printf("\n=%d\n",b+c);
break;
case'-':
printf("\n=%d\n",b-c);
break;
default:
printf("\nwrong\n");
}
}
注意运算优先级~
void main()
{int a,b,c;
char op,ap;
printf("qing shu ru yi ge shu");
scanf("%d",&a);
printf("\nshu ru yun suan fu:");
scanf("%s",&op);
printf("\nqing shu ru ling yi ge shu");
scanf("%d",&b);
printf("\nshu ru yun suan fu:");
scanf("%s",&ap);
printf("\nqing shu ru di san fe shu");
scanf("%d",&c);
switch(ap)
{case'*':
b=b*c;
printf("\n=%d\n",b);
break;
case'/':
b=b*c;
printf("\n=%d\n",b);
break;
default:
;
}
switch(op)
{case'+':
printf("\n=%d\n",a+b);
break;
case'-':
printf("\n=%d\n",a-b);
break;
case'*':
printf("\n=%d\n",a*b);
break;
case'/':
printf("\n=%d\n",a/b);
break;
default:
printf("\nwrong\n");
}
switch(ap)
{case'+':
printf("\n=%d\n",b+c);
break;
case'-':
printf("\n=%d\n",b-c);
break;
default:
printf("\nwrong\n");
}
}
注意运算优先级~
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯