-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseBolan.c
More file actions
77 lines (67 loc) · 1.42 KB
/
Copy pathreverseBolan.c
File metadata and controls
77 lines (67 loc) · 1.42 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
71
72
73
74
75
76
77
#include <stdlib.h>
#include <stdio.h>
typedef struct {
int data[20];
int top;
}stack;
void initStack(stack *s) {
s->top = -1;
}
void push(stack *s, int data)
{
s->top++;
s->data[s->top] = data;
}
void pop(stack *S, int *data)
{
*data = S->data[S->top];
S->top = S->top - 1;
}
int main(void) {
stack s; //栈
int num; //循环次数
scanf("%d", &num);
getchar();
initStack(&s); //初始化栈
int result1 = 0;
int result2 = 0;
int num1,num2;//进行运算的两个数
num1 = 0;
num2 = 0;
while (num != 0) {
char ch[20];
scanf("%s", ch);
getchar();
if (ch[0] == '+') {
pop(&s, &num1);
pop(&s, &num2);
result1 = num1 + num2;
push(&s,result1);
}
else if (ch[0] == '-') {
pop(&s, &num2);
pop(&s, &num1);
result1 = num1 - num2;
push(&s,result1);
}
else if (ch[0] == '*') {
pop(&s, &num1);
pop(&s, &num2);
result1 = num1 * num2;
push(&s,result1);
}
else if (ch[0] == '/') {
pop(&s, &num2);
pop(&s, &num1);
result1 = num1 / num2;
push(&s,result1);
}
else {
push(&s, atoi(ch));
}
num--;
}
result2 = s.data[s.top];
printf("%d", result2);
return 0;
}