Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //13.3
- //main.cpp
- #include <stdio.h>
- #include <cstdlib>
- #include <string.h>
- int comp(char *s)
- {
- if(strcmp(s, "lconst")==0)
- return 1;
- if(strcmp(s, "add")==0)
- return 2;
- if(strcmp(s, "mul")==0)
- return 3;
- if(strcmp(s, "div")==0)
- return 4;
- if(strcmp(s, "print")==0)
- return 5;
- return 0;
- }
- int main()
- {
- int i, n, dim=16;
- int *v;
- v=(int*) malloc(dim * sizeof(int));
- scanf("%d", &n);
- for(int i=1; i<=n; i++)
- {
- char *s;
- int dst, src0, src, val, reg;
- s=(char*) malloc (7 * sizeof(char));
- scanf("%s%[ ]", s);
- switch (comp(s))
- {
- case 1:
- scanf("%d%d", &dst, &val);
- v[dst]=val;
- break;
- case 2:
- scanf("%d%d%d", &dst, &src0, &src);
- v[dst]=v[src]+v[src0];
- break;
- case 3:
- scanf("%d%d%d", &dst, &src0, &src);
- v[dst]=v[src0]*v[src];
- break;
- case 4:
- scanf("%d%d%d", &dst, &src0, &src);
- if(v[src]==0)
- {
- printf("Exception: line %d\n", i);
- i=n;
- }
- else
- v[dst]=v[src0]/v[src];
- break;
- case 5:
- scanf("%d", ®);
- printf("%d\n", v[reg]);
- break;
- default:
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement