Created
August 6, 2020 10:25
-
-
Save cs-fedy/30d4e01838e822dd36f3a7841b9cb48d to your computer and use it in GitHub Desktop.
Write code to efficiently evaluate given postfix expression.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Write code to efficiently evaluate given postfix expression. | |
int evaluate_exp(char exp[], stck * st) { | |
int index = 0, x, y; | |
char ch; | |
while (exp[index] != '\0') { | |
ch = exp[index]; | |
if (ch >= '0' && ch <= '9') { | |
push(ch - '0', st); | |
} | |
else { | |
x = get_peek(*st); | |
pop(st); | |
y = get_peek(*st); | |
pop(st); | |
if (ch == '+') push(y + x, st); | |
else if (ch == '-') push(y - x, st); | |
else if (ch == '*') push(y * x, st); | |
else if (ch == '/') push(y / x, st); | |
} | |
index++; | |
} | |
return get_peek(*st); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment