SuitNdtie

Computer st of op

Mar 28th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stack>
  3. using namespace std;
  4.  
  5. typedef struct{
  6.     int index;
  7.     int prev;
  8. }oper;
  9.  
  10. int main()
  11. {
  12.     int n,m;
  13.     scanf("%d %d",&n,&m);
  14.     int memory[n+1];for(int i=0;i<=n;i++)memory[i] = 0;
  15.    
  16.     stack<oper> st;
  17.     for(int i=0;i<m;i++){
  18.         char c;
  19.         scanf(" %c",&c);
  20.         bool pushs = true;
  21.         oper op;
  22.         switch(c){
  23.             case('='):{
  24.                 int x,y;
  25.                 scanf("%d %d",&x,&y);
  26.                 op = {x,memory[x]};
  27.                 memory[x] = y;
  28.                 break;
  29.             }
  30.             case('+'):{
  31.                 int x,y,z;
  32.                 scanf("%d %d %d",&x,&y,&z);
  33.                 op = {z,memory[z]};
  34.                 memory[z] = memory[x] + memory[y];
  35.                 break;
  36.             }
  37.             case('-'):{
  38.                 int x,y,z;
  39.                 scanf("%d %d %d",&x,&y,&z);
  40.                 op = {z,memory[z]};
  41.                 memory[z] = memory[x] - memory[y];
  42.                 break;
  43.             }
  44.             case('C'):{
  45.                 int x,y;
  46.                 scanf("%d %d",&x,&y);
  47.                 op = {y,memory[y]};
  48.                 memory[y] = memory[x];
  49.                 break;
  50.             }
  51.             case('P'):{
  52.                 int x;
  53.                 scanf("%d",&x);
  54.                 printf("%d\n",memory[x]);
  55.                 pushs = false;
  56.                 break;
  57.             }
  58.             case('U'):{
  59.                 int t;
  60.                 scanf("%d",&t);
  61.                 while(t--){
  62.                     int index = st.top().index;
  63.                     int val = st.top().prev;
  64.                     memory[index] = val;
  65.                     st.pop();
  66.                 }
  67.                 pushs = false;
  68.                 break;
  69.             }
  70.         }
  71.         if(pushs)st.push(op);
  72.     }
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment