表达式求值
来源:互联网 发布:蛙5火箭知乎 编辑:程序博客网 时间:2024/06/05 18:42
http://acm.nyist.net/JudgeOnline/problem.php?pid=35
#include<bits/stdc++.h>using namespace std;const int N=1e3+7;const int inf=0x3f3f3f3f;char str[N];stack<double> num;stack<char> op;int priority(char c){ if(c=='+') return 1; if(c=='-') return 1; if(c=='*') return 2; if(c=='/') return 2; return 0;}void calculate(){ double b=num.top();num.pop(); double a=num.top();num.pop(); switch(op.top()) { case '+': num.push(a+b);break; case '-': num.push(a-b);break; case '*': num.push(a*b);break; case '/': num.push(a/b);break; } op.pop();}int main(){ int i,j,t,n,m,tt=0; scanf("%d",&t); while(t--) { scanf("\n%s",&str); while(!op.empty()) op.pop(); while(!num.empty()) num.pop(); int len=strlen(str); for(i=0;i<len;i++) { if(isdigit(str[i])) { double tmp; sscanf(str+i,"%lf%n",&tmp,&n); i+=(n-1); num.push(tmp); } else if(str[i]=='(') op.push(str[i]); else if(str[i]==')') { while(op.top()!='(') calculate(); op.pop(); } else if(op.empty()||priority(str[i])>priority(op.top())) op.push(str[i]); else { while(!op.empty()&&priority(str[i])<=priority(op.top()))calculate(); op.push(str[i]); } } op.top(); printf("%.2lf\n",num.top()); num.pop(); } return 0;}http://acm.nyist.net/JudgeOnline/problem.php?pid=1272
#include<bits/stdc++.h>using namespace std;const int N=1e3+7;const int inf=0x3f3f3f3f;char str[N];stack<int> num;stack<char> op;int priority(char c){ if(c=='*') return 2; if(c=='+') return 1; return 0;}void calculate(){ int suma=0,sumb=0; int b=num.top();num.pop(); int a=num.top();num.pop(); switch(op.top()) { case '+': num.push(a+b);break; case '*': num.push(a*b);break; case 'S': while(b!=0) sumb+=b%10,b/=10; while(a!=0) suma+=a%10,a/=10; num.push(max(suma,sumb));break; } op.pop();}int main(){ int i,j,t,n,m,tt=0,tmp; scanf("%d",&t); while(t--) { scanf("\n%s",&str); while(!op.empty()) op.pop(); while(!num.empty()) num.pop(); int len=strlen(str); for(i=0;i<=len;i++) { if(isdigit(str[i])) { sscanf(str+i,"%d%n",&tmp,&n); i+=(n-1); num.push(tmp); } else if(str[i]=='(') op.push(str[i]); else if(str[i]==')') { while(op.top()!='(') calculate(); op.pop(); } else if(str[i]=='S') { op.push('(');op.push('S'); i+=4; } else if(str[i]==',') {} else if(op.empty()||priority(str[i])>priority(op.top())) op.push(str[i]); else { while(!op.empty()&&priority(str[i])<=priority(op.top())) calculate(); op.push(str[i]); } } printf("%d\n",num.top()); } return 0;}
0 0
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- java关于位移运算的面试题
- 欢迎使用CSDN-markdown编辑器
- hdu 5731 Solid Dominoes Tilings(多米诺骨牌)
- 对linux下chown命令基本功能的实现
- 很认真的聊一聊程序员的自我修养
- 表达式求值
- 自学360之滚动的TextView
- HDU 5724 博弈,SG函数
- 【 Codeforces 615A】A. Bulbs
- Android之基于位置的服务
- WPF编程宝典读书笔记——第10章 资源(一)
- 取出一个字符串中字母出现的次数。如:字符串:"abcde%^kka27qoq" ,输出格式为: a(2)b(1)k(2)...
- Alice and Bob
- POJ 2406 Power Strings (kmp求循环节)