标签:des style blog http io ar color os sp
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12832 Accepted Submission(s): 4222
写得丑点就过不到呀~
反正思路就是见到乘除号就马上把它搞定~再弄加减号这样。
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cmath> #include <vector> #include <queue> #include <map> #include <set> #include <stack> #include <algorithm> using namespace std; typedef long long LL; const int N = 3010; const int inf = 1e7+7; const double PI = acos(-1.0); const double eps = 1e-6 ; char str[N]; int top1,top2,pos,len; bool is_digit( char s ) { if( s < ‘0‘ || s > ‘9‘ ) return false ; return true ; } double cal( double a , char s , double b ) { if( s == ‘+‘ ) return a+b ; else if( s==‘-‘) return a-b; else if( s==‘*‘) return a*b ; else if( s==‘/‘) return a / b ; } double get(){ double a , b ; char opp; a = 0 ; while( pos < len && is_digit( str[pos] ) ) a = a * 10 + ( str[pos] - ‘0‘ ) , pos++; pos++; while( pos < len && ( str[pos] == ‘*‘ || str[pos] == ‘/‘ ) ) { opp = str[pos] ; pos += 2 ; b = 0 ; while( pos < len && is_digit( str[pos] ) ) b = b * 10 + ( str[pos] - ‘0‘ ) , pos++; pos++; a = cal( a , opp , b ); } return a ; } int main() { #ifdef LOCAL freopen("in.txt","r",stdin); #endif // LOCAL ios::sync_with_stdio(false); while( gets(str) ) { if( str[0] == ‘0‘ && strlen(str) == 1 ) break ; double a , b ; char opp ; pos = 0 ; len = strlen(str) ; a = get(); while( pos < len ) { opp = str[pos] ; pos += 2 ; b = get() ; a = cal( a , opp , b ); } printf("%.2lf\n",a ); } return 0; }
标签:des style blog http io ar color os sp
原文地址:http://www.cnblogs.com/hlmark/p/4109613.html