Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stack>
- #include <string>
- #include <vector>
- #include <algorithm>
- using namespace std;
- vector <string> sig = {"*", "/", "+", "-"};
- bool sgn(string x){
- bool r = 0;
- for (auto y: sig){
- if (y == x){
- r = 1;
- }
- }
- return r;
- }
- int opr(string x, int a, int b){
- int res;
- if (x == "*") res = a * b;
- else if (x == "/") res = a / b;
- else if (x == "+") res = a + b;
- else if (x == "-") res = a - b;
- return res;
- }
- int main()
- {
- vector <string> al;
- stack <int> go;
- string s;
- getline(cin, s);
- /*for (char l: s){
- cout<<l<<'\n';
- }*/
- int id = 0;
- string x = "";
- for (int i=0;i<s.size();++i){
- char l = s[i];
- if (l == ' ') {
- //cout<<i<<'\n';
- al.push_back(x);
- x = "";
- }
- else{
- x += l;
- }
- }
- al.push_back(x);
- //for (auto x: al) cout<<x<<'\n';
- int dg;
- int a,b;
- int ans = 0;
- for (auto x: al){
- if (sgn(x)){
- //cout<<x<<'\n';
- a = go.top();
- go.pop();
- b = go.top();
- go.pop();
- ans = opr(x, a, b);
- go.push(ans);
- }
- else {
- dg = stoi(x);
- go.push(dg);
- }
- }
- cout<<"ans= "<<ans<<'\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement