5 solutions
-
4
注:个人认为这个人的思路挺好的(虽然他有部分是源于网上的)
那我就直接贴代码了:
#include <bits/stdc++.h> using namespace std; string s; stack<char> stk; stack<int> stk_num; string str; int pri[100]; void init() { pri[')'] = 1; pri['+'] = 2; pri['-'] = 2; pri['*'] = 3; pri['/'] = 3; pri['^'] = 4; pri['('] = 5; } void transform() { bool numStatus = true; int num = 0; for (int i = 0; i < s.size(); ++i) { if (s[i] >= '0' && s[i] <= '9') { numStatus = true; num = num * 10 + (s[i] - '0'); } else if (s[i] == '(') { if (numStatus) { str += (to_string(num) + " "); num = 0; numStatus = false; } stk.push(s[i]); } else if (s[i] == ')') { if (numStatus) { str += (to_string(num) + " "); num = 0; numStatus = false; } while (stk.top() != '(') { str += stk.top(); stk.pop(); } stk.pop(); } else { if (numStatus) { str += (to_string(num) + " "); num = 0; numStatus = false; } while (!stk.empty() && pri[s[i]] <= pri[stk.top()] && stk.top() != '(') { str += stk.top(); stk.pop(); } stk.push(s[i]); } } if (numStatus) str += (to_string(num) + " "); while (!stk.empty()) { str += stk.top(); stk.pop(); } } void cal() { for (int i = 0; i < str.size(); ++i) { int num = 0; if (str[i] >= '0' && str[i] <= '9') { while (str[i] != ' ') { num = num * 10 + (str[i] - '0'); i++; } stk_num.push(num); } else { int a = stk_num.top(); stk_num.pop(); int b = stk_num.top(); stk_num.pop(); switch (str[i]) { case '+': stk_num.push(b + a);break; case '-': stk_num.push(b - a);break; case '*': stk_num.push(b * a);break; case '/': stk_num.push(b / a);break; case '^': stk_num.push(pow(b, a));break; } } } } int main() { cin >> s; init(); transform(); cal(); cout << stk_num.top(); return 0; }
(115行,可以跟高精度比了QwQ)
-
-1
#include<iostream> #include<stack> #include<cmath> #include<string> using namespace std; stack<int> nums; stack<char> ops; int num,x,y; string line; bool isnum=false; size_t len; int priority(char op) { switch(op) { case '(': return 5; case '^': return 4; case '*': case '/': return 3; case '+': case '-': return 2; case ')': return 1; // default: // return -114514; } } int calc(int a,int b,char op) { switch(op) { case '+': return a+b; case '-': return a-b; case '*': return a*b; case '/': return a/b; case '^': return (int)pow(a,b); // default: // return -114514; } } signed main() { cin>>line; line.push_back(')'); len=line.length(); for(size_t i=0;i<len;++i) { if(line[i]>='0'&&line[i]<='9') { isnum=true; num=num*10+(line[i]-'0'); } else { if(isnum) { nums.push(num); num=0; isnum=false; } while(!(ops.empty()||priority(line[i])>priority(ops.top())||ops.top()=='(')) { x=nums.top();nums.pop();y=nums.top();nums.pop(); nums.push(calc(y,x,ops.top())); ops.pop(); } if(!ops.empty()&&line[i]==')'&&ops.top()=='(') ops.pop(); else ops.push(line[i]); } } printf("%d",nums.top()); return 0; }
-
-1
声明,c++的来源于网上的(改了一下,也加了注释,我也不是很会)
发个python的
a=input()#输入 a=a.replace('^','**')#将乘方替换为python的乘方 a=a.replace('/','//')#将除法替换为python的整除(因为题目要求用整数除法) print(eval(a))#将字符串换为表达式的值输出
开个玩笑,请勿模仿👀️
说实话,python的真简单😄 ,足足两个系统函数可以用
#include<iostream> #include<string> #include<stack> #include<cmath> using namespace std; const int N=1100; int p[N];//优先级数组 int calc(int a,int b,char c)//用于两个数计算 { if(c=='+')//不必多言,懂得都懂,就运算 { return a+b; } else if(c=='-') { return a-b; } else if(c=='*') { return a*b; } else if(c=='/') { return a/b; } else if(c=='^') { return int(pow(a,b)); } } int main() { p['(']=5;//设置优先级 p['^']=4; p['*']=3; p['/']=3; p['+']=2; p['-']=2; p[')']=1; int n,num=0; string s; cin>>s;//输入 int len=s.size();//获取长度 s[len]=')';//方便结束 stack<int >nu;//数字栈 stack<char >ch;//符号栈 bool ig=false;//是否是在构建数字 for(int i=0;i<=len;i++) { if(s[i]>='0' && s[i]<='9')//数字时 { ig=true;//标记在构建数字 num=num*10+s[i]-'0'; } else//其他时 { if(ig)//如果在构建数字 { nu.push(num);//压进数字栈 num=0;//重置 ig=false;//标记不在构建数字 } while(!(ch.empty() || p[s[i]]>p[ch.top()] || ch.top()=='(')) { int b=nu.top();//栈顶数字 nu.pop(); int a=nu.top();//对于上面来说,是栈顶上一个数字 nu.pop(); char c=ch.top();//符号 ch.pop(); nu.push(calc(a,b,c));//做运算 } if(ch.size() && ch.top()=='(' && s[i]==')') { ch.pop();//新的括号域 } else { ch.push(s[i]);//新的符号 } } } cout<<nu.top();//输出栈顶 return 0;//完结散花 }
-
-3
#include <cmath> #include <iostream> using namespace std; const int N = 255; int n[N] = {0}; char c[N] = {0}; bool Priority(char ch1, char ch2)//判断优先级 { if (ch1 == '^') { return true; } if (ch2 == '^') { return false; } if (ch2 == ')') { return true; } if (ch1 == '-' || ch1 == '+') { if (ch2 == '-' || ch2 == '+') { return true; } return false; } if (ch1 == '*' || ch1 == '/') { return true; } return false; } int calc(int a, int b, char op) { switch (op) { case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': return a / b; case '^': return pow(a, b); } return 0; } int main() { char ch = 0; int a = 0, b; int ntop = 0, ctop = 0; while (1) { cin.get(ch); switch (ch) { case '+': case '-': case '*': case '/': case '^': n[ntop++] = a; while (ctop > 0 && Priority(c[ctop - 1], ch)) { b = n[--ntop]; a = n[--ntop]; n[ntop++] = calc(a, b, c[--ctop]); } c[ctop++] = ch; a = 0; break; case '(': c[ctop++] = ch; break; case ')': n[ntop++] = a; while (ctop > 0 && c[ctop - 1] != '(' && Priority(c[ctop - 1], ch)) { b = n[--ntop]; a = n[--ntop]; n[ntop++] = calc(a, b, c[--ctop]); } if (c[ctop - 1] == '(')//弹出( { a = n[--ntop]; ctop--; } break; default: if (ch >= '0' && ch <= '9') { a = a * 10 + (ch - '0'); } else//结束 { n[ntop++] = a; while (ctop > 0) { b = n[--ntop]; a = n[--ntop]; n[ntop++] = calc(a, b, c[--ctop]); } cout << n[--ntop]; return 0; } break; } } return 0; }
- 1
Information
- ID
- 841
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 7
- Tags
- # Submissions
- 109
- Accepted
- 24
- Uploaded By