数据结构课程设计 算术表达式求值
来源:互联网 发布:php pdo sqlserver 编辑:程序博客网 时间:2024/05/17 03:10
题目:编制一个用算符优先法对算术表达式求值的程序
//Evaluate Expression#include<iostream>#include<cstring>#include<cctype>using namespace std;int A[7][7] = {3,3,2,2,2,3,3, 3,3,2,2,2,3,3, 3,3,3,3,2,3,3, 3,3,3,3,2,3,3, 2,2,2,2,2,1,0, 3,3,3,3,0,3,3, 2,2,2,2,2,0,1}; //存放运算符的优先顺序,3为>,2为<,1为=bool isoperator(char optr) //判断是否操作符 { switch(optr) { case '+': case '-': case '*': case '/': case '(': case ')': case '#':return true; default:return false; }}int crd(char c) //寻找字符所在的行列坐标{ switch(c) { case'+': return 0; case'-': return 1; case'*': return 2; case'/': return 3; case'(': return 4; case')': return 5; case'#': return 6; }}char preced(char a, char b) //判断运算符的优先关系 { int m, n; m = crd(a); n = crd(b); switch(A[m][n]) { case 3:return '>'; case 2:return '<'; case 1:return '='; }}int push_opnd(double *&opnd, char *&expr, int top_opnd, int i) //操作数压栈 {int j;double suma = 0.0, sumb = 0.0;double e = 0.1, tmp;for(j = i; isdigit(expr[j]); j++){tmp = expr[j] - '0';suma = suma * 10 + tmp;}if(expr[j] == '.'){ for(j++; isdigit(expr[j]); j++) { sumb += (expr[j] - '0') * e; e /= 10; } }opnd[top_opnd] = suma + sumb;return j;}void operate(double *&opnd, char *&optr, int top_opnd, int top_optr) { //计算并把结果压栈 switch(optr[top_optr - 1]) { case '+': opnd[top_opnd - 2] += opnd[top_opnd - 1];break; case '-': opnd[top_opnd - 2] -= opnd[top_opnd - 1];break; case '*': opnd[top_opnd - 2] *= opnd[top_opnd - 1];break; case '/': if(opnd[top_opnd - 1] == 0) { cout << "Error! The divisor is 0" << endl; system("pause"); exit(1); } opnd[top_opnd - 2] /= opnd[top_opnd - 1]; break; }}int main(){double *OPND;//运算数栈char *OPTR;//运算符栈char *EXPR;//表达式char str[100];int top_opnd = 0;//运算数栈顶计数器int top_optr = 0;//运算符栈顶计数器int len;//表达式长度 int i; //下标 cout << "Input the EXPRession: ";cin >> str;len = strlen(str);EXPR = new char[len + 3];OPND = new double[len];OPTR = new char[len];//在表达式前后加上'#'EXPR[0] = '#';for(i = 0; str[i] != '\0'; i++)EXPR[i + 1] = str[i];EXPR[len + 1] = '#';EXPR[len + 2] = '\0'; //cout << EXPR << endl; OPTR[top_optr++] = '#'; i = 1; while(EXPR[i] != '#' || OPTR[top_optr - 1] != '#'){if(isdigit(EXPR[i])){i = push_opnd(OPND, EXPR, top_opnd, i);top_opnd++;}else if(isoperator(EXPR[i])){switch(preced(OPTR[top_optr - 1], EXPR[i])){ case '<': OPTR[top_optr++] = EXPR[i]; i++; break; case '=': top_optr--; i++; break; case '>': operate(OPND, OPTR, top_opnd, top_optr); top_opnd--; top_optr--; break; }}}cout << OPND[0] << endl;delete EXPR;delete OPND;delete OPTR;system("pause");return 0; }
- 数据结构课程设计 算术表达式求值
- 课程设计之算术表达式求值
- 数据结构课程设计-表达式求值
- 数据结构课程设计---------用栈来实现表达式求值
- 数据结构课程设计---------用栈来实现表达式求值
- 算术表达式求值
- C#算术表达式求值
- 算术表达式求值
- 字符串算术表达式求值
- 算术表达式求值
- 算术表达式求值
- 图解--算术表达式求值
- 算术表达式求值(Dijkstra)
- 简单算术表达式求值
- 算术表达式求值
- 算术表达式求值
- 算术表达式求值
- 简单算术表达式求值
- 每天一博:看起来很美,但实际上很脆弱的群体
- linux2.6.29 CFS调度详细分析(一)
- 数据结构课程设计 约瑟夫环
- Java设计模式-----Abstract Factory抽象工厂模式
- [ARM Linux] Qt: Qtopia 2.2 桌面和程序 在ARM-LINUX上发布
- 数据结构课程设计 算术表达式求值
- JQuery页面随滚动条动态加载效果实现
- POJ1275 Cashier Employment
- 数据结构课程设计 表达式类型的实现
- 查看数据库死锁的进程及清除sleeping进程(Kill__Sleeping_Processes)
- php中array_multisort()对多维数组进行排序,相当于数据库查询是order by多字段排序。。超好用
- QWidget,QMainWindow和QDialog的区别
- gdb里面的堆栈调试命令
- 获取RichEditCtrl图片及如何使用在你的程序中~