Sreekar_0125

Untitled

Sep 13th, 2022
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.81 KB | None | 0 0
  1. #include<iostream>
  2. #include<cctype>
  3. #include<string>
  4. using namespace std;
  5. int stack[100];
  6. int top=-1;
  7. void push(int ch) {
  8.     top++;
  9.     stack[top]=ch;
  10. }
  11. int pop () {
  12.     int c = stack[top];
  13.     top--;
  14.     return c;
  15. }
  16. int result (int a,int b,char c) {
  17.     int a1 = a;
  18.     int b1 = b;
  19.     switch(c) {
  20.         case '+': return a1+b1;
  21.         case '-': return a1-b1;
  22.         case '*': return a1*b1;
  23.         case '/': return a1/b1;
  24.         case '%': return a1%b1;
  25.     }
  26. }
  27. int main () {
  28.     string s;
  29.     cin >> s;
  30.     for(int i=0;i<s.length();i++) {
  31.         if(isdigit(s[i])) {
  32.            
  33.             push(s[i]-'0');
  34.         }
  35.         else {
  36.            int a = pop();
  37.            int b = pop();
  38.            push(result(b,a,s[i]));
  39.         }
  40.     }
  41.     cout << stack[top];
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment