-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab5_A.c
More file actions
63 lines (63 loc) · 1.43 KB
/
Copy pathLab5_A.c
File metadata and controls
63 lines (63 loc) · 1.43 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
#include <stdio.h>
#include <ctype.h>
#include <math.h>
int stack[50], top = -1;
void push(int elem)
{
stack[++top] = elem;
}
main()
{
char postfix[50], ch;
int i = 0, op1, op2;
printf("Enter a Suffix expression with single digit operands and operators:");
scanf("%s", postfix);
while ((ch = postfix[i++]) != '\0')
{
if (isalpha(ch))
{
printf("Invalid expression\n");
return;
}
else if (isdigit(ch))
push(ch - 48);
else
{
op2 = stack[top--];
if (top <= -1)
{
printf("Invalid Expression\n");
return;
}
op1 = stack[top--];
switch (ch)
{
case '+':
push(op1 + op2);
break;
case '-':
push(op1 - op2);
break;
case '*':
push(op1 * op2);
break;
case '/':
push(op1 / op2);
break;
case '%':
push(op1 % op2);
break;
case '^':
push(pow(op1, op2));
break;
default:
printf("Invalid operator\n");
return;
}
}
}
if (top != 0)
printf("invalid expression\n");
else
printf("Result = %d\n", stack[top]);
}