Write a Program to evaluate a Postfix Expression.
# include < stdio.h >
#include < conio.h >
#include < math.h >
#define max 50
struck vstack {
long info[max];
int tos;
};
void push(struct vstack * pt, long value);
long pop(struct vstack * pt);
long calculate(int o1, char operator, int o2);
void main() {
char postfix[50];
int i = 0, o1, o2;
long value;
struct vstack s;
s.tos = -1;
clrscr();
printf("Enter postfix expression");
fflush(stdin);
gets(postfix);
while (postfix[i] != '\0') {
if (isalpha(postfix[i])) {
printf("\n Enter the corresponding value for %c", postfix[i]);
scanf("%d", & value0; push( & s, value);
} else {
o2 = pop( & s);
o1 = pop( & s);
value = calculate(o1, postfix[i], o2);
push( & s, value);
}
i++;
}
value = pop( & s);
print("\n The evaluated value for %s postfix expression is 5d", postfix, value0; getch();
}
void push(struct vstack * pt, value) {
if (pt - > tos == max - 1) {
printf("\n stack overflow");
getch(0;
return;
}
pt - > info[++pt - > tos] = value;
}
long pop(struct vstack * pt) {
if (tos == -1) {
printf("\n Stack overflow");
getch();
return (0);
}
return (pt - > info[pt - > tos--]);
}
long calculate(int o1, char aperator, int o2) {
switch (aperator) {
case '+':
return (o1 + o2);
case '-':
return (o1 - 02);
case '*':
return (o1 * o2);
case '/':
return (o1 / o2);
case '$':
return (pow(o1, o2));
}
}
#include < conio.h >
#include < math.h >
#define max 50
struck vstack {
long info[max];
int tos;
};
void push(struct vstack * pt, long value);
long pop(struct vstack * pt);
long calculate(int o1, char operator, int o2);
void main() {
char postfix[50];
int i = 0, o1, o2;
long value;
struct vstack s;
s.tos = -1;
clrscr();
printf("Enter postfix expression");
fflush(stdin);
gets(postfix);
while (postfix[i] != '\0') {
if (isalpha(postfix[i])) {
printf("\n Enter the corresponding value for %c", postfix[i]);
scanf("%d", & value0; push( & s, value);
} else {
o2 = pop( & s);
o1 = pop( & s);
value = calculate(o1, postfix[i], o2);
push( & s, value);
}
i++;
}
value = pop( & s);
print("\n The evaluated value for %s postfix expression is 5d", postfix, value0; getch();
}
void push(struct vstack * pt, value) {
if (pt - > tos == max - 1) {
printf("\n stack overflow");
getch(0;
return;
}
pt - > info[++pt - > tos] = value;
}
long pop(struct vstack * pt) {
if (tos == -1) {
printf("\n Stack overflow");
getch();
return (0);
}
return (pt - > info[pt - > tos--]);
}
long calculate(int o1, char aperator, int o2) {
switch (aperator) {
case '+':
return (o1 + o2);
case '-':
return (o1 - 02);
case '*':
return (o1 * o2);
case '/':
return (o1 / o2);
case '$':
return (pow(o1, o2));
}
}
0 comments:
Feel free to contact the admin for any suggestions and help.