6-1 Evaluate Postfix Expression (25 分)
Write a program to evaluate a postfix expression. You only have to handle four kinds of operators: +, -, x, and /.
Format of functions:
ElementType EvalPostfix( char *expr );
where expr
points to a string that stores the postfix expression. It is guaranteed that there is exactly one space between any two operators or operands. The function EvalPostfix
is supposed to return the value of the expression. If it is not a legal postfix expression, EvalPostfix
must return a special value Infinity
which is defined by the judge program.
Sample program of judge:
#include <stdio.h>
#include <stdlib.h>
typedef double ElementType;
#define Infinity 1e8
#define Max_Expr 30 /* max size of expression */
ElementType EvalPostfix( char *expr );
int main()
{
ElementType v;
char expr[Max_Expr];
gets(expr);
v = EvalPostfix( expr );
if ( v < Infinity )
printf("%f\n", v);
else
printf("ERROR\n");
return 0;
}
/* Your function will be put here */
Sample Input 1:
11 -2 5.5 * + 23 7 / -
Sample Output 1:
-3.285714
Sample Input 2:
11 -2 5.5 * + 23 0 / -
Sample Output 2:
ERROR
Sample Input 3:
11 -2 5.5 * + 23 7 / - *
Sample Output 3:
ERROR
Special thanks to Sirou Zhu (朱思柔) for providing a valuable test case.
ElementType EvalPostfix(char *expr) {
int len = strlen(expr);
double num[30] = { 0 };
int numlast=0;
int i = 0;
int count = 0;
int opernum = 0;
double result= Infinity;
while (i<=len&&expr[i]!='\0') {
if (expr[i] == '+' || expr[i] == '-' || expr[i] == '*' || expr[i] == '/') {//操作符
if (expr[i] == '-'&&expr[i + 1] <= '9'&&expr[i + 1] >= '0') {//存入负数
i++;
num[count] = 0;
int point = 0;
int ispoint = 0;
while (expr[i] != ' '&&expr[i]!='\0') {
if (expr[i] == '.')ispoint = 1;
else {
if (ispoint == 0) {
num[count] = 10 * num[count] + (double)expr[i] - 48;
}
else {
num[count] = 10 * num[count] + (double)expr[i] - 48;
point++;
}
}
i++;
}
for (int j = 0; j < point; j++) {
num[count] *= (0.1);
}
numlast = count;
num[count] *= (-1);
count++;
}
else if(!(expr[i + 1] <= '9'&&expr[i + 1] >= '0')){
if (numlast > 0) {
if (expr[i] == '+')
num[numlast - 1] = num[numlast - 1] + num[numlast];
else if (expr[i] == '-')
num[numlast - 1] = num[numlast - 1] - num[numlast];
else if (expr[i] == '*')
num[numlast - 1] = num[numlast - 1] * num[numlast];
else if (expr[i] == '/') {
if (num[numlast] == 0)return Infinity;
num[numlast - 1] = num[numlast - 1] / num[numlast];
}//零不能为分母
opernum++;
numlast--;
num[numlast + 1] = 0;
result = num[numlast];
count--;
i++;
}
else return Infinity;//余下操作符
}
}
else if (expr[i] <= '9'&&expr[i] >= '0') {//存入数据
num[count] = 0;
int point = 0;
int ispoint = 0;
while (expr[i] != ' '&&expr[i]!='\0') {
if (expr[i] == '.')ispoint = 1;
else {
if (ispoint == 0) {
num[count] = 10 * num[count] + (double)expr[i] - 48;
}
else {
num[count] = 10 * num[count] + (double)expr[i] - 48;
point++;
}
}
i++;
}
for (int j = 0; j < point; j++) {
num[count] *= (0.1);
}
numlast = count;
count++;
}
else if (expr[i] == ' ')i++;
else return Infinity;
}
if (opernum == 0)return num[0];
if (i >= Max_Expr*2-1)return Infinity;
if(numlast==0)return result;
else return Infinity;
}
注意*:
当输入只有一个double数字时,输出为该数字
数字剩余时输出ERROR,即要让numlast的位置等于零
该函数中count与numlast的作用相同,设置重复了