-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram5a
More file actions
58 lines (53 loc) · 1.16 KB
/
program5a
File metadata and controls
58 lines (53 loc) · 1.16 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
#include<stdio.h>
#include<math.h>
#include<ctype.h>
float stack[50];
int top = -1;
void push(float x) {
stack[++top] = x;
}
float pop() {
return stack[top--];
}
float eval(char postfix[]) {
float op1, op2, res;
char ch;
int i = 0;
while ((ch = postfix[i]) != '\0') {
if (isdigit(ch)) {
push(ch - '0');
} else {
op2 = pop();
op1 = pop();
res = oper(ch, op1, op2);
push(res);
}
i++;
}
return pop();
}
float oper(char symb, float op1, float op2) {
switch(symb) {
case '$':
case '^': return pow(op1, op2);
case '*': return op1 * op2;
case '/': return op1 / op2;
case '+': return op1 + op2;
case '-': return op1 - op2;
default: return 0;
}
}
int main() {
char postfix[50];
int choice;
float res;
do {
printf("Enter postfix expression: ");
scanf("%s", postfix);
res = eval(postfix);
printf("Result = %f\n", res);
printf("Do you want to enter another expression? (1/0): ");
scanf("%d", &choice);
} while (choice != 0);
return 0;
}