/* 栈实现计算器,主要思路就是设置一个符号栈和一个数字栈,在字符串首尾各加一个'#',然后扫描字符串,
* 如果是数字进数字栈,如果是运算符号先判断符号优先级,若栈外符号优先级大于栈内符号优先级则进栈,
* 小于栈内优先级则符号栈出栈一位,数字栈出栈两位进行计算,结果重新存进数字栈,直到栈外优先级大于栈内,
*/
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAX = 30;
const int DONE = 1;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//栈定义
template <class T>
class Stack{
public:
Stack(int MaxStackSize=10);
~Stack() { delete [] stack;}
bool IsEmpty() const {return top==-1;}
bool IsFull() const {return top==MaxTop;}
T Top() const;
Stack<T>& Add(const T& x);
Stack<T>& Del(T& x);
void MakeEmpty(){top=-1;} //清空栈
void print(){
for(int i; i < top + 1; i ++){
cout<<stack[i]<<'\t';
}
cout<<endl;
}
private:
int top;//栈顶
int MaxTop;//最大的栈顶值
T *stack;//堆栈元素数组
};
template<class T>
Stack<T>::Stack(int MaxStackSize){
MaxTop=MaxStackSize-1;
stack=new T[MaxStackSize];
top=-1;
}
template<class T>
Stack<T>& Stack<T>::Ad
c++栈实现简单计算器
最新推荐文章于 2025-03-15 21:26:42 发布