标签:ide else == bre turn oid 桌面 div VID
#include <iostream> #include <vector> #include <cmath> using namespace std; vector<int> v1; vector<char> v2; int a; int b; void compute1() { while(!v2.empty()&&v2.back()!=‘(‘) { a= v1.back(); v1.pop_back(); b= v1.back(); v1.pop_back(); if(v2.back()==‘+‘) b+=a; else if(v2.back()==‘-‘) b-=a; else if(v2.back()==‘*‘) b*=a; else if(v2.back()==‘/‘){ if(a==0){ cout << "can not be divided by zero"; return; } b/=a; } else if(v2.back()==‘^‘) b=pow(b,a); v1.push_back(b); v2.pop_back(); } } void compute2() { while(!v2.empty()&&v2.back()!=‘(‘) { if(v2.back()==‘*‘||v2.back()==‘/‘||v2.back()==‘^‘) { a= v1.back(); v1.pop_back(); b= v1.back(); v1.pop_back(); if(v2.back()==‘*‘) b*=a; else if(v2.back()==‘/‘){ if(a==0){ cout << "can not be divided by zero"; return; } b/=a; } else if(v2.back()==‘^‘) b=pow(b,a); v1.push_back(b); v2.pop_back(); } else break; } } void infix() { char c; int num; while(cin.get(c)) { if(isdigit(c)) { cin.putback(c); cin>>num; v1.push_back(num); } else if(c==‘+‘||c==‘-‘||c==‘*‘||c==‘/‘||c==‘^‘||c==‘(‘||c==‘)‘) { switch(c) { case ‘+‘: compute1(); v2.push_back(c); break; case ‘-‘: compute1(); v2.push_back(c); break; case ‘*‘: compute2(); v2.push_back(c); break; case ‘/‘: compute2(); v2.push_back(c); break; case ‘^‘: v2.push_back(c); break; case ‘(‘: v2.push_back(c); break; case ‘)‘: compute1(); v2.pop_back(); break; } } else if(c==‘\n‘) { compute1(); cout << v1.back() << endl; v1.pop_back(); } } } int main() { infix(); return 0; }
标签:ide else == bre turn oid 桌面 div VID
原文地址:https://www.cnblogs.com/lhb666aboluo/p/12813738.html