-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackmachine.c
More file actions
70 lines (70 loc) · 1.27 KB
/
stackmachine.c
File metadata and controls
70 lines (70 loc) · 1.27 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main()
{
long n;
scanf("%ld\n", &n);
long stack[n];
long top = -1;
for (long i = 0; i < n; i++)
{
char s[25];
gets(s);
int l = (int) strlen(s);
if (l > 4)
{
long h = strtol(s+5, NULL, 10);
stack[++top] = h;
}
else if (l == 4)
{
long c = stack[top];
stack[top] = stack[top-1];
stack[top-1] = c;
}
else if (s[0]=='A' && s[1]=='A' && s[2]=='D')
{
stack[top-1]+=stack[top];
top--;
}
else if (s[0]=='S' && s[1]=='U' && s[2]=='B')
{
stack[top-1]=stack[top]-stack[top-1];
top--;
}
else if (s[0]=='M' && s[1]=='U' && s[2]=='L')
{
stack[top-1]*=stack[top];
top--;
}
else if (s[0]=='D' && s[1]=='I' && s[2]=='V')
{
stack[top-1]=stack[top]/stack[top-1];
top--;
}
else if (s[0]=='M' && s[1]=='A' && s[2]=='X')
{
stack[top-1]=((stack[top-1]) > (stack[top])) ? (stack[top-1]) : (stack[top]);
top--;
}
else if (s[0]=='M' && s[1]=='I' && s[2]=='N')
{
stack[top-1]=((stack[top-1]) < (stack[top])) ? (stack[top-1]) : (stack[top]);
top--;
}
else if (s[0]=='N' && s[1]=='E' && s[2]=='G')
{
stack[top]*=-1;
}
else
{
stack[top+1]=stack[top];
top++;
}
}
printf("%ld", stack[0]);
return 0;
}