-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
Data Structures and Algorithms (English)/6-5_Evaluate Postfix Expression (25).c
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
ElementType EvalPostfix(char *expr) { | ||
ElementType stack[Max_Expr+10]; | ||
int top = -1; | ||
char *p = expr; | ||
char token[Max_Expr]; | ||
|
||
while (*p) { | ||
int i = 0; | ||
// Skip spaces | ||
while (*p == ' ') p++; | ||
if (!*p) break; | ||
|
||
// Get next token | ||
while (*p && *p != ' ') { | ||
token[i++] = *p++; | ||
} | ||
token[i] = '\0'; | ||
|
||
// Process token | ||
if (strlen(token) == 1 && (token[0] == '+' || token[0] == '-' || token[0] == '*' || token[0] == '/')) { | ||
// Not enough operands | ||
if (top < 1) return Infinity; | ||
|
||
ElementType op2 = stack[top--]; | ||
ElementType op1 = stack[top--]; | ||
|
||
switch (token[0]) { | ||
case '+': | ||
stack[++top] = op1 + op2; | ||
break; | ||
case '-': | ||
stack[++top] = op1 - op2; | ||
break; | ||
case '*': | ||
stack[++top] = op1 * op2; | ||
break; | ||
case '/': | ||
// Division by zero check | ||
if (op2 == 0) return Infinity; | ||
stack[++top] = op1 / op2; | ||
break; | ||
} | ||
} else { | ||
// Convert string to number | ||
ElementType num; | ||
if (sscanf(token, "%lf", &num) != 1) return Infinity; | ||
stack[++top] = num; | ||
} | ||
} | ||
|
||
// Final stack should have exactly one value | ||
if (top != 0) return Infinity; | ||
|
||
return stack[top]; | ||
} |