Evaluate Postfix Expression

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的作用相同,設置重複了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章