hdu 1237

来源:互联网 发布:同志网络剧国产2017 编辑:程序博客网 时间:2024/06/05 02:31

递归的思路,不过要注意:加减和乘除的顺序都是从左到右的,在递归的时候要注意


#include "stdio.h"#include "string.h"char line[1000];int p, l;double get(){double a, b;char opt;a = 0.0;while(line[p]>='0' && line[p]<='9'){a *= 10;a += line[p++]-48;}p++;while(p<l && (line[p]=='*' || line[p]=='/')){opt = line[p++];p++;b = 0.0;while(line[p]>='0' && line[p]<='9'){b *= 10;b += line[p++]-48;}p++;if(opt=='*')a *= b;elsea /= b;}return a;}void main(){double a, b;char opt;freopen("in.txt", "r", stdin);while(gets(line), strcmp(line, "0")){l = strlen(line);p = 0;a = get();while(p<l){opt = line[p++];p++;b = get();if(opt=='+')a += b;elsea -= b;}printf("%.2lf\n", a);}}