Skip to content

Instantly share code, notes, and snippets.

@cs-fedy
Created August 6, 2020 10:25
Show Gist options
  • Save cs-fedy/30d4e01838e822dd36f3a7841b9cb48d to your computer and use it in GitHub Desktop.
Save cs-fedy/30d4e01838e822dd36f3a7841b9cb48d to your computer and use it in GitHub Desktop.
Write code to efficiently evaluate given postfix expression.
// 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