c++实现表达式计算问题
来源:互联网 发布:如何才能成为程序员 编辑:程序博客网 时间:2024/06/18 04:40
#include <iostream>#include <Windows.h>#include <string>using namespace std;//栈的定义template<class T>struct Node{T data;Node *next;};template<class T>class LinkStack{public:LinkStack();~LinkStack();T & GetTop();//获取栈顶元素bool Empty();//判断栈是否为空void Push(T);T Pop();void Clear();const int Length();private:Node<T> *head;int length;};template<class T>LinkStack<T>::LinkStack(){length = 0;head = NULL;}template<class T>void LinkStack<T>::Push(T data){Node<T> *p = new Node<T>p->data = data;p->next = head;head = p;length ++;}template<class T>T & LinkStack<T>::GetTop(){if(length < 0){cout << "栈为空" << endl;exit(0);}return head->data;}template<class T>bool LinkStack<T>::Empty(){if (length == 0)return true;elsereturn false;}template<class T>T LinkStack<T>::Pop(){if (length < 0){cout << "栈为空" << endl;exit(0);}T e = head->data;head = head->next;length --;return e;}template<class T>void LinkStack<T>::Clear(){Node<T> *p = head;while(p != NULL){p = p->next;delete head;head = p;}head = NULL;length = 0;}template<class T>const int LinkStack<T>::Length(){return length;}template<class T>LinkStack<T>::~LinkStack(){Clear();//cout << "析构" << endl;}//栈外优先级const int isw(const char &e){int i = 0;switch(e){case '+':case '-':i = 2;break;case '*':case '/':i = 3;break;case '(':i = 5;break;case '#':i = 0;break;}return i;}//栈内优先级const int isn(const char &e){int i = 0;switch(e){case '+':case '-':i = 2;break;case '*':case '/':i = 3;break;case '(':i = 0;break;case '#':i = 0;break;}return i;}LinkStack<double> figure;//存放数字void Calcalate(const char &oper){double num1 = 0,num2 = 0;num1 = figure.Pop();num2 = figure.Pop();switch(oper){case '+':figure.Push( num1+num2 );break;case '-':figure.Push( num2 - num1 );break;case '*':figure.Push( num1 * num2 );break;case '/':if (num1 != 0){figure.Push( num2/num1 );}else{cout << "出错,除数不能为 0 !" << endl;exit(-1);}break;}}void ReadData(){LinkStack<char> mark;mark.Push('#');char c, getMark;string digit_str = "";cout << "请输入表达式,以输入 # 结束" << endl;cin >> c;while(c != '#'){getMark = mark.GetTop();while ((c >= '0' && c <= '9') || c == '.'){digit_str += c;cin >> c;if ((c < '0' || c > '9') && c != '.'){figure.Push( atof( digit_str.c_str() ) );//把字符数字转变成double型数字,后放入figure栈中digit_str = "";}}if (c == '#'){break;}else if (c == ')'){while(getMark != '('){Calcalate( mark.Pop() );//调用Calcalate函数,进行计算getMark = mark.GetTop();}mark.Pop();//把左括号( 扔出栈}else{while (isn(getMark) >= isw(c)){Calcalate( mark.Pop() );getMark = mark.GetTop();}mark.Push(c);}cin >> c;}while( mark.Length() > 1)//排除#号,所以循环次数要大于1{Calcalate( mark.Pop() );}}int main(){ReadData();cout << "result = " << figure.Pop() << endl;return 0;}
阅读全文
0 0
- 算术表达式计算C实现
- c++实现表达式计算问题
- 76C语言实现表达式计算
- C++实现 逆波兰表达式计算问题
- c计算数学表达式
- C语言实现整数四则运算表达式的计算
- 中缀表达式转换为后缀表达式 简易实现(c++)(简易表达式计算)
- 表达式计算的问题
- 完全利用栈实现表达式的计算问题
- 用C++程序实现复杂表达式的计算问题
- 关于用堆栈实现中后缀表达式计算的问题
- 表达式计算java实现
- 栈实现表达式计算
- 表达式计算栈实现
- C语言实现括号匹配,中缀表达式转后缀表达式并计算具体代码实现
- C语言实现括号匹配,中缀表达式转后缀表达式并计算的算法
- C-源代码-计算简单表达式
- UVA 327 计算C表达式
- 递归修剪二叉树的理解
- 2017-12-16 杂记
- 第21节项目1-投票表决器
- Android Studio 常用快捷键分类整理
- codeforces 339A Helpful Maths
- c++实现表达式计算问题
- Ubuntu16.04学习总结
- 数据结构总结之dfs
- 198. House Robber
- FIFO队列(First In First Out)和优先队列
- html_day01
- 迷宫系列(二)搜索、BFS、DFS
- HDU 3339 In Action(最短路(Spfa)+01背包)
- 如何减少浏览器的 Repaint 和 Reflow