-
Notifications
You must be signed in to change notification settings - Fork 0
/
9.c
69 lines (63 loc) · 1.65 KB
/
9.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
#include<stdio.h>
#include<conio.h>
#include<math.h>
struct node {
int data;
struct node *lchild;
struct node *rchild;
};
typedef struct node *NODE;
NODE root = 0;
NODE create_tree(char postfix[]);
float eval(NODE root);
void main() {
char postfix[20];
float result;
clrscr();
printf("Enter the postfix\n");
scanf("%s", postfix);
root = create_tree(postfix);
result = eval(root);
printf("Result=%f\n", result);
getch();
}
NODE create_tree(char postfix[]) {
NODE temp, stack[20];
int i = 0, j = 0;
char symbol;
for (i = 0; (symbol = postfix[i]) != 0; i++) {
temp = (NODE)malloc(sizeof(struct node));
temp->lchild = temp->rchild = 0;
temp->data = symbol;
if (isalnum(symbol))
stack[j++] = temp;
else {
temp->rchild = stack[--j];
temp->lchild = stack[--j];
stack[j++] = temp;
}
}
return (stack[--j]);
}
float eval(NODE root) {
float num;
switch (root->data) {
case '+':
return eval(root->lchild) + eval(root->rchild);
case '-':
return eval(root->lchild) - eval(root->rchild);
case '/':
return eval(root->lchild) / eval(root->rchild);
case '*':
return eval(root->lchild) * eval(root->rchild);
case '^':
return pow(eval(root->lchild), eval(root->rchild));
default:
if (isalpha(root->data)) {
printf("Enter the value of %c\n", root->data);
scanf("%f", &num);
return (num);
} else
return (root->data - '0');
}
}