表达式求值
来源:互联网 发布:巨人网络2018校招面试 编辑:程序博客网 时间:2024/06/05 16:42
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <iostream>using namespace std;#define MaxSize 20#define MaxOp 7struct //设定运算符的优先级{ char ch; //运算符 int pri; //优先级}lpri[] = {{'=',0},{'(',1},{'*',5},{'/',5},{'+',3},{'-',3},{')',6}}, rpri[] = {{'=',0},{'(',6},{'*',4},{'/',4},{'+',2},{'-',2},{')',1}};int leftpri(char op) //求左运算符op的优先级{ int i; for(i = 0; i<MaxOp; i++) if(lpri[i].ch == op) return lpri[i].pri;}int rightpri(char op) //求右运算符op的优先级{ int i; for(i = 0; i<MaxOp; i++) if(rpri[i].ch == op) return rpri[i].pri;}bool InOp(char ch) //判断ch是否为运算符{ if(ch == '(' || ch == ')' || ch == '+' || ch == '-' || ch == '*' || ch == '/') return true; else return false;}int Precede(char op1, char op2) //op1 和 op2运算符优先级的比较结果{ if(leftpri(op1) == rightpri(op2)) return 0; else if(leftpri(op1) < rightpri(op2)) return -1; else return 1;}void trans(char *exp, char postexp[]){ struct { char data[MaxSize]; int top; }op; int i = 0; op.top = -1; op.top++; op.data[op.top] = '='; while(*exp != '\0') { if(!InOp(*exp)) { while(*exp >= '0' && *exp <='9') { postexp[i++] = *exp; exp++; } postexp[i++] = '#'; } else { switch(Precede(op.data[op.top],*exp)) { case -1: op.top++; op.data[op.top] = *exp; exp++; break; case 0: op.top--; exp++; break; case 1: postexp[i++] = op.data[op.top]; op.top--; break; } } } while(op.data[op.top] != '=') { postexp[i++] = op.data[op.top]; op.top--; } postexp[i] = '\0';}float compvalue(char * postexp){ struct { float data[MaxSize]; int top; }st; float d,a,b,c; st.top = -1; while (*postexp != '\0') { switch(*postexp) { case '+': a = st.data[st.top]; st.top--; b = st.data[st.top]; st.top--; c = a+b; st.top++; st.data[st.top] = c; break; case '-': a = st.data[st.top]; st.top--; b = st.data[st.top]; st.top--; c = b-a; st.top++; st.data[st.top] = c; break; case '*': a = st.data[st.top]; st.top--; b = st.data[st.top]; st.top--; c = a*b; st.top++; st.data[st.top] = c; break; case '/': a = st.data[st.top]; st.top--; b = st.data[st.top]; st.top--; if(a != 0) { c = b/a; st.top++; st.data[st.top] = c; } else { printf("\n\t除零错误!\n"); exit(0); } break; default: d = 0; while(*postexp >= '0' && *postexp <='9') { d = 10 * d + *postexp - '0'; postexp++; } st.top++; st.data[st.top] = d; break; } postexp++; } return (st.data[st.top]);}int main(){ char exp[MaxSize]; char postexp[MaxSize]; while(true) { printf("请输入表达式:"); gets(exp); trans(exp, postexp); printf("表达式的值:%g\n",compvalue(postexp)); } return 0;}
心得体会:栈的东西,一定要注意指针的移动,指向的位置。
阅读全文
0 0
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 一致性算法之Raft
- redis缓存,rabbitMQ队列
- 研发团队管理心得
- PHP开发小技巧①④—PHP判断时间在某一时间段内
- Android Studio解决没有HttpClint及相关类的方法
- 表达式求值
- 中科爱讯WiFi探针模块TZ-1002产品简介
- EditText
- Findbugs错误总结
- linux安装hadoop配置Yarn
- Jemalloc malloc的分配过程,以redis的第一次内存分配malloc(1)展开
- 堆栈的简单实现之二:进制转换方面的简单应用(C语言实现)
- c++ XXXXXX discards qualifiers [-fpermissive] 错误原因
- 悼念512汶川大地震遇难同胞——选拔志愿者 HDU 博弈