5 solutions
-
5
#include<bits/stdc++.h> using namespace std; stack<int> stk1; //存数字 stack<char> stk2; //存运算符 void cal() //做一次运算 { int b=stk1.top();stk1.pop(); int a=stk1.top();stk1.pop(); char ch=stk2.top();stk2.pop(); if(ch=='+') stk1.push(a+b); if(ch=='-') stk1.push(a-b); if(ch=='*') stk1.push(a*b); if(ch=='/') stk1.push(a/b); if(ch=='^') { int t=1; for(int i=1;i<=b;i++) t=t*a; stk1.push(t); } } int main() { string str; cin>>str; map<char,int> h={{'+',1},{'-',1},{'*',2},{'/',2},{'^',3}}; //定义符号优先级 int n=str.size(); for(int i=0;i<n;i++) { if(str[i]=='@') break; //结束 if(str[i]>='0'&&str[i]<='9') //取出连续的一段数字 { int t=0; while(str[i]>='0'&&str[i]<='9') { t=t*10+str[i]-'0'; i++; } stk1.push(t); i--; continue; } if(str[i]=='(') //左括号 { stk2.push('('); continue; } if(str[i]==')') //有括号 { while(stk2.top()!='(') //不是左括号就一直算 cal(); stk2.pop();//删除左括号 continue; } //到这里以后肯定是+-*/^ while(stk2.size()&&h[str[i]]<=h[stk2.top()]) //当前元素的优先级小的时候,就先算之前的 cal(); stk2.push(str[i]); } while(stk2.size()) //算出剩余的表达式 cal(); cout<<stk1.top(); return 0; }
-
1
#include<bits/stdc++.h> using namespace std; stack<int> stk1;//存数字 stack<char> stk2;//存运算符 void cal()//做一次运算 { int b=stk1.top();stk1.pop(); int a=stk1.top();stk1.pop(); char ch=stk2.top();stk2.pop(); if(ch=='+') { stk1.push(a+b); } if(ch=='-') { stk1.push(a-b); } if(ch=='*') { stk1.push(a*b); } if(ch=='/') { stk1.push(a/b); } if(ch=='^') { int t=1; for(int i=1;i<=b;i++) { t=t*a; } stk1.push(t); } } int main() { string str; cin>>str; map<char,int> h={{'+',1},{'-',1},{'*',2},{'/',2},{'^',3}};//定义符号优先级 int n=str.size(); for(int i=0;i<n;i++) { if(str[i]=='@')//结束 { break; } if(str[i]>='0'&&str[i]<='9')//取出连续的一段数字 { int t=0; while(str[i]>='0'&&str[i]<='9') { t=t*10+str[i]-'0'; i++; } stk1.push(t); i--; continue; } if(str[i]=='(')//左括号 { stk2.push('('); continue; } if(str[i]==')')//有括号 { while(stk2.top()!='(')//不是左括号就一直算 { cal(); } stk2.pop();//删除左括号 continue; } //到这里以后肯定是+-*/^ while(stk2.size()&&h[str[i]]<=h[stk2.top()])//当前元素的优先级小的时候,就先算之前的 { cal(); } stk2.push(str[i]);//把符号加入栈 } while(stk2.size())//算出剩余的表达式 { cal(); } cout<<stk1.top(); return 0; }
-
-1
#include<bits/stdc++.h> using namespace std; stack<int> stk1; stack<char> stk2; void cal() { int b=stk1.top();stk1.pop(); int a=stk1.top();stk1.pop(); char ch=stk2.top();stk2.pop(); if(ch=='+') { stk1.push(a+b); } if(ch=='-') { stk1.push(a-b); } if(ch=='*') { stk1.push(a*b); } if(ch=='/') { stk1.push(a/b); } if(ch=='^') { int t=1; for(int i=1;i<=b;i++) { t=t*a; } stk1.push(t); } } int main() { string str; cin>>str; map<char,int> h={{'+',1},{'-',1},{'*',2},{'/',2},{'^',3}}; int n=str.size(); for(int i=0;i<n;i++) { if(str[i]=='@') { break; } if(str[i]>='0'&&str[i]<='9') { int t=0; while(str[i]>='0'&&str[i]<='9') { t=t*10+str[i]-'0'; i++; } stk1.push(t); i--; continue; } if(str[i]=='(') { stk2.push('('); continue; } if(str[i]==')') { while(stk2.top()!='(') { cal(); } stk2.pop(); continue; } while(stk2.size()&&h[str[i]]<=h[stk2.top()]) { cal(); } stk2.push(str[i]); } while(stk2.size()) { cal(); } cout<<stk1.top(); return 0; }
- 1
Information
- ID
- 171
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 5
- Tags
- # Submissions
- 78
- Accepted
- 14
- Uploaded By