您的位置:首页 > 其它

算数表达式求值

2015-04-11 16:32 239 查看
不带括号时,直接遍历一次,再累和

#include <stdio.h>
#include <string.h>

int main(){
char ch;
int a[200], temp;
while(scanf("%d", &a[0]) != EOF){
int i = 0;
while(scanf("%c", &ch) != EOF && ch != '\n'){
scanf("%d", &temp);
if(ch == '+') a[++i] = temp;
else if(ch == '-') a[++i] = -temp;
else if(ch == '*') a[i] *= temp;
else if(ch == '/') a[i] /= temp;
}
for(int j = 1; j <= i; ++j)
a[0] += a[j];
printf("%d\n", a[0]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: