Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stack>
- using namespace std;
- typedef struct{
- int index;
- int prev;
- }oper;
- int main()
- {
- int n,m;
- scanf("%d %d",&n,&m);
- int memory[n+1];for(int i=0;i<=n;i++)memory[i] = 0;
- stack<oper> st;
- for(int i=0;i<m;i++){
- char c;
- scanf(" %c",&c);
- bool pushs = true;
- oper op;
- switch(c){
- case('='):{
- int x,y;
- scanf("%d %d",&x,&y);
- op = {x,memory[x]};
- memory[x] = y;
- break;
- }
- case('+'):{
- int x,y,z;
- scanf("%d %d %d",&x,&y,&z);
- op = {z,memory[z]};
- memory[z] = memory[x] + memory[y];
- break;
- }
- case('-'):{
- int x,y,z;
- scanf("%d %d %d",&x,&y,&z);
- op = {z,memory[z]};
- memory[z] = memory[x] - memory[y];
- break;
- }
- case('C'):{
- int x,y;
- scanf("%d %d",&x,&y);
- op = {y,memory[y]};
- memory[y] = memory[x];
- break;
- }
- case('P'):{
- int x;
- scanf("%d",&x);
- printf("%d\n",memory[x]);
- pushs = false;
- break;
- }
- case('U'):{
- int t;
- scanf("%d",&t);
- while(t--){
- int index = st.top().index;
- int val = st.top().prev;
- memory[index] = val;
- st.pop();
- }
- pushs = false;
- break;
- }
- }
- if(pushs)st.push(op);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment