6-2 Evaluate Postfix Expression(25 分)_后缀表达式求值
来源:互联网 发布:java游戏程序设计教程 编辑:程序博客网 时间:2024/05/28 17:06
6-2 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){ double stk[1111], p1, p2; int len = strlen(expr), i, j, f = 0,top=-1; char *tmp; for (i = 0; expr[i]; i++) { if (!i) { if (expr[0] == '-' && expr[1] == '\0') return Infinity; if (!isdigit(expr[0])&&expr[0]!='-') f = 1; } else if (expr[i] != '.' && !isdigit(expr[i])) { f = 1; } } expr[len] = ' ', expr[len + 1] = '\0';// cout << f << endl; if (!f) return atof(expr); i = 0;// 11 -2 5.5 * +23 7 / - while (expr[i]!='\0') { if (expr[i] == ' ') { expr[i++] = '\0'; tmp = expr; expr += i; i = 0; if (!strchr("0123456789.+-*/", tmp[0])) return Infinity; if (strchr("*/+-", tmp[0]) && tmp[1] == '\0') { if (top - 2 < -1) return Infinity; p1 = stk[top--]; p2 = stk[top--]; if (tmp[0] == '+') stk[++top] = p1 + p2; else if (tmp[0] == '-') stk[++top] = p2 - p1; else if (tmp[0] == '*') stk[++top] = p2*p1; else if (tmp[0] == '/') { if (p1 == 0) return Infinity; stk[++top] = p2 / p1; } } else stk[++top] = atof(tmp); } else i++; } if (top - 1 == -1) return stk[0]; return Infinity;}
- 6-2 Evaluate Postfix Expression(25 分)_后缀表达式求值
- Evaluate Postfix Expression_后缀表达式求解
- Sequence_postfix expression(后缀表达式求值)
- Evaluate Postfix Expression
- Evaluate Postfix Expression
- 4-5-Evaluate Postfix Expression
- Expression Evaluation(表达式求值)
- [DS][3-19]Evaluate a postfix expression
- 表达式求值(中缀转后缀及后缀表达式求值)
- 表达式求值(后缀表达式)--栈
- nyist 35 表达式求值(后缀表达式)
- 【数据结构与算法基础】栈及其应用后缀、中缀表达式 / Stack and postfix/infix expression
- 表达式求值(前缀、中缀、后缀)
- 任意实数后缀表达式求值(链栈)
- 后缀表达式求值(顺序栈)
- 后缀表达式的求值
- 后缀表达式求值
- 后缀表达式求值
- OkGo添加公共参数
- 在Nanopi-NEO上编译带Cedrus硬件编码器的ffmpeg动态链接库
- jQuery 遍历
- 如何居中一个元素(正常、绝对定位、浮动元素)
- Ubuntu软件操作的相关命令
- 6-2 Evaluate Postfix Expression(25 分)_后缀表达式求值
- 1.如何在logback中显示mybatis查询日志
- Windows server安装Oracle 11g 停在2%
- git学习四:eclipse使用git提交项目
- java除法保留n位有效位
- shellinabox
- java Collection.sort
- jarvis oj DebugMe writeup
- tomcat启动错误:Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds