-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.c
39 lines (38 loc) · 941 Bytes
/
Test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
#include <stdlib.h>
int main() {
int stack[100];
int top = -1;
char *opcode;
while (scanf("%s", opcode) != EOF) {
if (strcmp(opcode, "push") == 0) {
int value;
scanf("%d", &value);
stack[++top] = value;
} else if (strcmp(opcode, "pop") == 0) {
top--;
} else if (strcmp(opcode, "add") == 0) {
stack[top] += stack[top - 1];
top--;
} else if (strcmp(opcode, "sub") == 0) {
stack[top] -= stack[top - 1];
top--;
} else if (strcmp(opcode, "mul") == 0) {
stack[top] *= stack[top - 1];
top--;
} else if (strcmp(opcode, "div") == 0) {
stack[top] /= stack[top - 1];
top--;
} else if (strcmp(opcode, "pall") == 0) {
for (int i = top; i >= 0; i--) {
printf("%d ", stack[i]);
}
printf("\n");
} else {
printf("Unknown opcode: %s\n", opcode);
}
}
return 0;
}
//monty1l5
//datatm////3