转载请注明出处:http://blog.csdn.net/u012860063
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1237
1 + 2 4 + 2 * 5 - 7 / 11 0
3.00 13.36
思路还是很简单的就是先计算 乘法和除法!
具体见代码;
代码如下:
#include <cstdio> int main() { char c[147]; double num[147]; int i; while(~scanf("%lf%c",&num[0],&c[0])) { if(num[0] == 0 && c[0] == ‘\n‘) break; else if(num[0] != 0 && c[0] == ‘\n‘) { printf("%.2lf\n",num[0]); continue; } scanf("%c%*c",&c[0]); for(i = 1 ; ; i++) { scanf("%lf%c",&num[i],&c[i]); if(c[i] == ‘\n‘) { break; } scanf("%c%*c",&c[i]); } char sign =‘+‘ ;//初始化为‘+‘是为了防止后面没有经过‘+‘,‘-‘直接是‘*‘,‘/‘ for(i = 1 ; ; i++) { if(c[i-1] == ‘+‘) sign = ‘+‘; else if( c[i-1] == ‘-‘) sign = ‘-‘; if(c[i-1] == ‘*‘) { num[i] = num[i-1]*num[i]; num[i-1] = 0; c[i-1] = sign; } else if(c[i-1] == ‘/‘) { num[i] = num[i-1]/num[i]; num[i-1] = 0; c[i-1] = sign; } if(c[i] == ‘\n‘) break; } for( i = 1 ; ; i++) { if(c[i-1] == ‘+‘) { num[i] = num[i-1] + num[i]; } else if(c[i-1] == ‘-‘) { num[i] = num[i-1] - num[i]; } if(c[i] == ‘\n‘) break; } printf("%.2lf\n",num[i]); } return 0; }
原文地址:http://blog.csdn.net/u012860063/article/details/25654051