3.2栈的应用举例3.2.5-表达式求值ppt课件
- 格式:ppt
- 大小:586.50 KB
- 文档页数:24
表达式求值: 设计⼀个程序实现输⼊⼀个表达式如3*(3+4),以”#”结尾,求出其值。
分析: 先分析⼀下四则运算的规则: 1. 先乘除后加减; 2. 从左到右计算; 3. 先括号内后括号外; 于是我们要把运算符的优先级确定清楚。
这⾥我只⽤这⼏个运算符:+-*/(), 可以知道+-的优先级低于*/,⽽对于(),当第⼀次遇到’(‘时,’(‘后⾯的就优先计算,直到遇到’)’为⽌,可以先把’(’的优先级定为⽐其它⼏个都⾼,然后遇到’)’时把⾥⾯的先算出来,再算括号外⾯的,具体实现在代码中会表现得很清楚。
考试2⼤提⽰这个程序还是⽤栈来实现,具体代码如下。
代码: #include #include using namespace std; const int STACK_INIT_SIZE=100; //The maximize length of stack template class Stack //A class of stack { public: Stack() //Constructor function { base = new int[STACK_INIT_SIZE]; if (!base) { cerr< exit(-1); } top=base; stacksize=STACK_INIT_SIZE; } ~Stack() //Destructor function { if (base) delete[] base; } T GetTop() { if (top==base) { cerr< exit(-1); } return *(top-1); } void Push(T e) { if (top-base>=stacksize) { base=new int[STACK_INIT_SIZE]; if(!base) { cerr< exit(-1); } top=base+STACK_INIT_SIZE; stacksize+=STACK_INIT_SIZE; } *top++=e; } void Pop(T& e) { if (top==base) { cerr< exit(-1); } e=*--top; } private: int *base; int *top; int stacksize; }; string op("+-*/()#"); //The set of operator bool In(char c,string op) //Judge the character whether belong to the set of operator { string::iterator iter=op.begin(); for (;iter!=op.end();++iter) if (*iter==c) return true; return false; } char Precede(char top,char c) //Confirm the precedence of operator { int grade_top=0,grade_c=0; switch (top) { case '#':grade_top=0;break; case ')':grade_top=1;break; case '+':grade_top=2;break; case '-':grade_top=2;break; case '*':grade_top=3;break; case '/':grade_top=3;break; case '(':grade_top=4;break; } switch (c) { case '#':grade_c=0;break; case ')':grade_c=1;break; case '+':grade_c=2;break; case '-':grade_c=2;break; case '*':grade_c=3;break; case '/':grade_c=3;break; case '(':grade_c=4;break; } if (grade_top>=grade_c) { if (top=='('&&c!=')') return ' else if (top=='('&&c==')') return '='; return '>'; } else if (top=='#'&&c=='#') return '='; else return ' } int Operate(int a,char theta,int b) //Calculate { if (theta=='+') return a+b; else if (theta=='-') return a-b; else if (theta=='*') return a*b; else if (theta=='/') return a/b; return 0; } int EvaluateExpression(Stack& OPTR,Stack& OPND) { int a=0,b=0,n=0; char x; char theta; string c; cin>>c; OPTR.Push('#'); while (c[0]!='#'||OPTR.GetTop()!='#') { if (!In(c[0],op)){ n=atoi(&c[0]); OPND.Push(n); cin>>c; } else switch (Precede(OPTR.GetTop(),c[0])) { case ' OPTR.Push(c[0]); cin>>c; break; case '=': OPTR.Pop(x); cin>>c; break; case '>': OPTR.Pop(theta); OPND.Pop(b); OPND.Pop(a); OPND.Push(Operate(a,theta,b)); break; } } return OPND.GetTop(); } int main() { Stack OPTR; Stack OPND; cout< cout< return 0; }。
数据结构-栈基本运算的实现及其应用下载提示:该文档是本店铺精心编制而成的,希望大家下载后,能够帮助大家解决实际问题。
文档下载后可定制修改,请根据实际需要进行调整和使用,谢谢!本店铺为大家提供各种类型的实用资料,如教育随笔、日记赏析、句子摘抄、古诗大全、经典美文、话题作文、工作总结、词语解析、文案摘录、其他资料等等,想了解不同资料格式和写法,敬请关注!Download tips: This document is carefully compiled by this editor. I hope that after you download it, it can help you solve practical problems. The document can be customized and modified after downloading, please adjust and use it according to actual needs, thank you! In addition, this shop provides you with various types of practical materials, such as educational essays, diary appreciation, sentence excerpts, ancient poems, classic articles, topic composition, work summary, word parsing, copy excerpts, other materials and so on, want to know different data formats and writing methods, please pay attention!数据结构栈基本运算的实现及其应用在计算机科学和编程中,数据结构是非常重要的概念。