用栈实现中缀表达式c++
来源:互联网 发布:lol韩服网络错误 编辑:程序博客网 时间:2024/06/08 19:55
```#include "isotream"using namespace std;class stack{public:int maxsize; int top; char *ListArray; stack(int size = 0); void push(char s); bool pop(char &x); void clear(); void display(); bool lsEmpty(); bool getTop(char &x);};class expression{private: string infix; string postfix;public: int isp(char ch); int icp(char ch); expression(); void get_infix(); void show_infix(); void converttopostfix(); void show_postfix();};#include "iostream"#include "string"#include "stack.h"using namespace std;stack::stack(int size){ maxsize = size; top = 0; ListArray = new char[maxsize];}void stack::push(char s){ ListArray[top] = s; top++;}bool stack::pop(char &x){ if (top == 0) return false; else x = ListArray[top - 1]; top--; return true;}void stack::display(){ int i = top; while (i > 0) { i = i - 1; cout << ListArray[i] << " "; } cout << endl;}void stack::clear(){ top = 0;}bool stack::lsEmpty(){ if (top == 0) return true;}int expression::isp(char ch)//栈内优先级{ switch (ch) { case '#': return 0; case '(': return 1; case '+': case '-': return 3; case '*': case '/': case '%': return 5; case ')': return 6; default: return -1; }}int expression::icp(char ch)//栈外优先级{ switch (ch) { case '#': return 0; case '(': return 6; case '+': case '-': return 2; case '*': case '/': case '%': return 4; case ')': return 1; default: return -1; }}bool stack::getTop(char &x){ if (top == 0) return false; x = ListArray[top - 1]; return true;}void expression::get_infix(){ cout << "input infix:"; cin >> infix;}expression::expression(){ infix = ""; postfix = "";}void expression::show_infix(){ cout << infix << endl;}/*主要的思想:如果栈外的优先级高于栈内的优先级,那么就要将栈外的符号压入栈内,反之就将栈内的元素弹出,赋值给后缀表达式,栈外的符号继续与栈内的符号比较直到栈内的符号优先级小于栈外的,此时将栈外的符号压入栈内,如果优先级相同就判断是否是括号,如果是就将括号弹出*/void expression::converttopostfix(){ stack s; char ch = '#', ch1, op; int i = 0; int length = infix.length();//字符串的长度,用来判断字符串什么时候读完 s.push(ch); ch = infix.at(0); while ((ch != '#') || !s.lsEmpty()) { if (isdigit(ch))//如果ch是数字 { postfix += ch; //成为后缀表达式的一部分 //cout << postfix << endl; i++; if (i < length) ch = infix.at(i);//继续读取下一个字符 else //此时是表示字符串中的元素已经读取完了,然而栈内此时栈内的符号还没有完全弹出赋值给后缀表达式 { while (s.top>1) { s.pop(op); postfix += op;//将符号弹出赋值给后缀表达式,直至到'#' } ch='#';//结束的标志 } } else //不是数字 { s.getTop(ch1); //得到栈内的第一个元素ch1 if (isp(ch1) < icp(ch))//栈外优先级大于栈内 { s.push(ch); //进栈 i++; if (i<length) ch = infix.at(i);//读取下一个字符 else { while (s.top>1) { s.pop(op); postfix += op; } ch = '#'; } } else if (isp(ch1)>icp(ch))//栈内优先级大与栈外 { s.pop(op);//出栈 postfix += op;//成为后缀表达式的一部分 //cout << postfix << endl; } else //优先级相同 { s.pop(op); if (op == '(') i++; if (i<length) ch = infix.at(i); else { while (s.top>1) { s.pop(op); postfix += op; } ch = '#'; } } } }}void expression::show_postfix(){ cout << postfix << endl;}
0 0
- C用栈实现中缀表达式转后缀表达式
- 用栈实现中缀表达式c++
- 中缀表达式转成后缀表达式 C实现
- 用数组顺序栈实现表达式运算(中缀表达式)
- 栈实现中缀表达式转为后缀表达式
- 用栈和队列实现中缀表达式的求解
- 栈的应用:中缀表达式转为后缀表达式(c语言实现)
- C语言 实现中缀表达式转后缀表达式并求值
- 中缀转后缀表达式(栈实现)
- 中缀表达式变后缀:栈实现
- stack 实现中缀表达式
- 栈练习之C语言中实现中缀转后缀表达式
- C/C++ 算法 中缀转后缀表达式实现1
- 栈的应用-中缀表达式转后缀表达式(C版)
- 数据结构(九) 用顺序栈实现中缀表达式转为后缀表达式
- 【数据结构】中缀表达式转后缀表达式(带括号)用栈实现
- 栈的应用--后缀表达式和中缀表达式的实现
- 利用栈实现中缀表达式转后缀表达式
- bzoj1009: [HNOI2008]GT考试
- hosts文件详解
- configure: error: Cannot find php-config
- Android保存用户名和密码
- git clone时出现Please make sure you have the correct access rights and the repository exists.错误
- 用栈实现中缀表达式c++
- Rust 1.12.1 发布
- Android事件的分发、传递和消耗
- Android UI适配(dp,dpi,px介绍)
- 法线纹理(贴图)Normal Mapping
- ubuntu上安装mysql
- NSAttributedString描述和应用
- Android 打开各种文件的方法
- 数据表与简单Java类映射-依靠代码链