遇到的一些bug(这个是一个数据结构实验课实验嘤嘤嘤):
1.书上的代码用的是链式栈,我写的是一个普通的栈,链式栈的pop函数的参数是elemtype &item,我写的普通的么拉参数,因为这个pop函数不一样导致clear函数也不一样,还有一个top函数也不太一样
2.top的参数也是item不是栈!我一直以为是一个栈的top所以直接用来opnd,太菜了
3.我写的这个是中缀算术表达式求值,所以分不分左右运算符没多大影响 4.operate函数里面已经有一个push函数了,在run函数里面最后一句又写了一个push,所以才会把后半部分的算式重复一遍,变成后面的算式×2(被push了两次),注释掉push就好啦!
4.还有一个我问过助教的智障问题,就是那个模板类,运算符的栈直接设置成char类型,运算数就是template,运算数不确定是整型还是double但是符号肯定是char啊,我一开始没设置好后面代码里面就以为我的运算符栈也是double型,太菜了
calculator.h
#include"Utility.h"
#ifndef CALCULATOR_H_INCLUDED
#define CALCULATOR_H_INCLUDED
//计算器模板
template<class ElemType>
class Calculator{
private:
//计算器的数据成员
Stack<ElemType> opnd;//操作数栈
Stack<char> optr;//操作符栈
//辅助函数模板
int OperPrior(char op);//操作符优先级
void Get2Operands(ElemType &left, ElemType &right);//从栈opnd中退出两个操作数
ElemType Operate(ElemType left, char op, ElemType right);//执行运算left op right
bool IsOperator(char ch);//判断ch是否为操作符
public:
//计算器类方法声明
Calculator(){};//无参数的构造函数
virtual ~Calculator(){};//析构函数
void Run();
char GetChar();//从输入流中读入字符
};
//辅助函数的实现
template<class ElemType>
int Calculator<ElemType>::OperPrior(char op){
//返回操作符优先级
switch (op){
case '=':
return 1;
break;
case '(':
case ')':
return 2;
break;
case '+':
case '-':
return 3;
break;
case '*':
case '/':
case '%':
return 4;
break;
case '^':
return 5;
break;
default:
return -1;
break;
}
}
template<class ElemType>
void Calculator<ElemType>::Get2Operands(ElemType &left, ElemType &right){
//从opnd栈中取两个操作数
if (opnd.pop(right) == UNDER_FLOW) throw Error("表达式有错!");//抛出异常
if (opnd.pop(left) == UNDER_FLOW) throw Error("表达式有错!");//抛出异常
}
template<class ElemType>//if (optr.top(&optrTop) == UNDER_FLOW) throw Error("表达式有错!");//抛出异常
ElemType Calculator<ElemType>::Operate(ElemType left, char op, ElemType right){
//执行left op right
switch (op){
case '+':
opnd.push(left + right);
return left + right;
break;
case '-':
opnd.push(left - right);
return left - right;
break;
case '*':
opnd.push(left * right);
return left * right;
break;
case '/':
if (right == 0) throw Error("除数为0!");//抛出异常
opnd.push(left / right);
return left / right;
break;
case '%':
opnd.push(int(left) % int(right));
return int(left) % int(right);
break;
case '^':
opnd.push(pow(left,right));
return pow(left, right);
break;
}
}
template<class ElemType>
bool Calculator<ElemType>::IsOperator(char ch){
//判断是否为操作符
if (ch == '=' || ch == '(' || ch == ')' || ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%' || ch == '^')
return true;
else return false;
}
template <class ElemType>
void Calculator<ElemType>::Run(){
//操作结果:运算表达式
optr.Clear();
opnd.Clear();//清空optr和opnd栈
optr.push('=');//在optr栈中加入一个‘=’
char ch;//临时字符
char priorChar;//当前输入的前一个字符,如不为操作符,则令其值为‘0’
char optrTop;//临时optr栈的栈顶字符
ElemType operand;//操作数
char op;//操作符
priorChar = '=';//前一个字符
ch = GetChar();//读入一个字符
if (optr.top(optrTop) == UNDER_FLOW) throw Error("表达式有错!");//抛出异常
//取出optr的栈顶
while (optrTop != '=' || ch != '='){
//当前表达式还未运算结束,继续运算
if (isdigit(ch) || ch == '.'){
//ch为一个操作数的第一个字符
cin.putback(ch);//将字符ch放回输入流
cin >> operand;//读入操作数
opnd.push(operand);//操作数入opnd栈
priorChar = '0';
ch = GetChar();
}
else if (!IsOperator(ch)){
//既不是操作符,也不属于操作数
throw Error("表达式有错!");//抛出异常
}
else{
//ch为操作符
if ((priorChar == '=' || priorChar == '(') && (ch == '+' || ch == '-')){
opnd.push(0);//ch为单目运算符+-,在其前面加上操作数0
priorChar = '0';//前一个字符不是操作数,规定前一字符为0
}
if (optrTop == ')'&&ch == '(' || optrTop == '('&&ch == '=' || optrTop == '='&&ch == ')')
throw Error("表达式有错!");
else if (optrTop == '('&&ch == ')'){
//去括号
if (optr.pop(optrTop) == UNDER_FLOW)
throw Error("表达式有错!");
ch = GetChar();//读入新字符
priorChar = ')';//新的前一字符为)
}
else if (ch == '(' || OperPrior(optrTop) < OperPrior(ch)){
//optrTop为(,或optrTop比ch的优先级还低
optr.push(ch);//ch入optr栈
priorChar = ch;//新的前一字符为ch
ch = GetChar();//读入新字符
}
else{
//optrTop的大于或等于ch的优先级
if (optr.pop(op) == UNDER_FLOW)
throw Error("表达式有错!");
ElemType left, right;//操作数
Get2Operands(left, right);//从opnd栈中取操作数
//opnd.push(Operate(left, op, right));//运算结果入opnd栈
Operate(left, op, right);
}
}
if (optr.top(optrTop) == UNDER_FLOW)
throw Error("表达式有错!");
}
if (opnd.top(operand) == UNFER_FLOW)
throw Error("表达式有错!");
cout << operand << endl;//显示表达式的值
}
template <class ElemType>
char Calculator<ElemType>::GetChar(){
char ch;
cin >> ch;
while (ch == ' ' || ch == '\n')
cin >> ch;
return ch;
}
#endif // CALCULATOR_H_INCLUDED
stack.h
#include"Utility.h"
#ifndef STACK_H_INCLUDED
#define STACK_H_INCLUDED
template<class ElemType>
struct Node{
ElemType data;//定义一个结点元素
Node<ElemType> *next;//定义一个结点指针
Node();//无参构造函数
Node(ElemType item, Node<ElemType> *add_on = NULL);//含参构造函数
};
template<class ElemType>
class Stack{
public:
Stack();//无参构造函数
bool Empty() const;//判断堆栈是否为空
Error_code push(const ElemType &item);//往堆栈中传入元素
Error_code pop(ElemType &item);//删除堆栈的栈顶元素
Error_code top(ElemType &item) const;//得到堆栈的栈顶元素
void Clear();//清空堆栈的所有元素
~Stack();//析构函数
Stack(const Stack<ElemType> &original);//有参构造函数
void operator = (const Stack<ElemType> &original);//操作符重载
protected:
Node<ElemType> *top_node;//定义一个指针
};
template<class ElemType>
Node<ElemType>::Node(){
//构造函数
next = NULL;
}
template<class ElemType>
Node<ElemType>::Node(ElemType item, Node<ElemType> *add_on){
//含参构造函数
data = item;
next = add_on;
}
template<class ElemType>
Stack<ElemType>::Stack(){
//堆栈的构造函数
top_node = NULL;
}
template<class ElemType>
bool Stack<ElemType>::Empty() const{
//判断堆栈是否为空
if (top_node == NULL)
return true;
else
return false;
}
template<class ElemType>
Error_code Stack<ElemType>::push(const ElemType &item){
//向堆栈中添加元素
Node<ElemType>*new_top = new Node<ElemType>(item, top_node);
if (new_top == NULL)
return UNFER_FLOW;
top_node = new_top;
return SUCCESS;
}
template<class ElemType>
Error_code Stack<ElemType>::pop(ElemType &item){
//删除堆栈中的栈顶元素
if (Empty()){
//栈空
return UNDER_FLOW;
}
else{
//操作成功
Node<ElemType>*old_top = top_node;
if (top_node == NULL)
return UNDER_FLOW;
item = old_top->data;
top_node = old_top->next;
delete old_top;
return SUCCESS;
}
}
template<class ElemType>
Error_code Stack<ElemType>::top(ElemType &item) const{
//得到堆栈的栈顶元素
if (Empty())
return UNDER_FLOW;
else{
item = top_node->data;
return SUCCESS;
}
}
template<class ElemType>
void Stack<ElemType>::Clear(){
//清空堆栈的所有元素
ElemType tmpElem;
while (!Empty())
pop(tmpElem);
}
template<class ElemType>
Stack<ElemType>::~Stack(){
//析构函数
Clear();
}
#endif // STACK_H_INCLUDED
utility.h
#ifndef UTILITY_H_INCLUDED
#define UTILITY_H_INCLUDED
#include<string.h>
#include<iostream>
#include<limits.h>
#include<math.h>
#include<fstream>
#include<ctype.h>
#include<time.h>
#include<conio.h>
using namespace std;
enum Error_code{ SUCCESS, fail, UNDER_FLOW, UNFER_FLOW };
class Error :public std::logic_error{
public:
Error(const std::string &s) :std::logic_error(s){}
virtual ~Error() throw(){}
};
#endif // UTILITY_H_INCLUDED
main.cpp
#include"Utility.h"
#include"Stack.h"
#include"Calculator.h"
using namespace std;
int main()
{
//cout << "Hello world!" << endl;
Calculator<double> item;
char iscontinue = 'Y';
while (iscontinue == 'Y'){
cout << "输入表达式(以等号(=)结束):" << endl;
item.Run();
cout << "是否继续(Y/N):" << endl;
cin >> iscontinue;
iscontinue = toupper(iscontinue);
}
return 0;
}
//https://wenku.baidu.com/view/1f7f8884bceb19e8b8f6baa9.html