include stdafx include stdio include stdlib include iostream include 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
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
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <conio.h>
#include <string.h>
using namespace std;
char stack[20];
int index = 0;
char pop (void)
{
if (!index)
{
printf ("Stack is empty.\n");
return 0;
}
index--;
return stack[index];
}
void push (char ch)
{
if (index == 20)
{
printf ("Stack is full.\n");
return;
}
stack[index] = ch;
index++;
}
int main (void)
{
char s[20]="1 2 * 3 + 2 /";
for(int i=1;i<strlen(s);i++)
{
char c=s[i];
push(c);
}
while(index!=0){
char c=pop();
switch (c) {
case ' ':
case '\n':
break;
case '=':
printf("Result = %d\n", stack[index]);
break;
case '+':
stack[index-1] = stack[index-1] + stack[index];
break;
case '-':
stack[index-1] = stack[index-1] - stack[index];
break;
case '*':
stack[index-1] = stack[index] * stack[index-1];
break;
case '/':
stack[index-1] = stack[index-1] / stack[index];
break;
default:break;
}
}
printf("Result = %d\n",stack[index]);
system("pause");
return 0;
}