栈的应用-简单计算器(中缀表达式转后缀表达式)
Posted jiangnansytle
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了栈的应用-简单计算器(中缀表达式转后缀表达式)相关的知识,希望对你有一定的参考价值。
1 // 2 // Stack.h 3 // 顺序栈 4 // 5 // Created by geshenglu on 2020/3/21. 6 // Copyright © 2020 geshenglu. All rights reserved. 7 // 8 9 #ifndef Stack_h 10 #define Stack_h 11 template<class Elemtype> 12 class Stack 13 { 14 public: 15 virtual bool IsEmpty() const =0; 16 virtual void Push(const Elemtype&x)=0; 17 virtual Elemtype Pop()=0; 18 virtual Elemtype Top()const =0; 19 virtual ~Stack(){}; 20 }; 21 #endif /* Stack_h */
1 // 2 // SeqStack.h 3 // 顺序栈 4 // 5 // Created by geshenglu on 2020/3/21. 6 // Copyright © 2020 geshenglu. All rights reserved. 7 // 8 9 #ifndef SeqStack_h 10 #define SeqStack_h 11 #include "Stack.h" 12 template<class Elemtype> 13 class SeqStack:public Stack<Elemtype> 14 { 15 private: 16 Elemtype *data; 17 int maxSize; 18 int top_p; 19 void doubleSpace(); 20 public: 21 SeqStack(int initSize = 10) 22 { 23 maxSize = initSize; 24 data = new Elemtype[maxSize]; 25 top_p = -1; 26 } 27 ~SeqStack() 28 { 29 delete [] data; 30 } 31 virtual bool IsEmpty() const override; 32 virtual void Push(const Elemtype&x)override; 33 virtual Elemtype Pop()override; 34 virtual Elemtype Top()const override; 35 }; 36 37 template<class Elemtype> 38 void SeqStack<Elemtype>::doubleSpace() 39 { 40 Elemtype *tmp = data; 41 data = new Elemtype[maxSize*2]; 42 for (int i=0; i<maxSize; ++i) 43 data[i] = tmp[i]; 44 maxSize *= 2; 45 delete tmp; 46 } 47 48 template<class Elemtype> 49 bool SeqStack<Elemtype>::IsEmpty() const 50 { 51 return top_p == -1; 52 } 53 54 template<class Elemtype> 55 void SeqStack<Elemtype>::Push(const Elemtype&x) 56 { 57 if(top_p == maxSize - 1) 58 { 59 doubleSpace(); 60 } 61 data[++top_p]=x; 62 } 63 64 template<class Elemtype> 65 Elemtype SeqStack<Elemtype>::Pop() 66 { 67 return data[top_p--]; 68 } 69 70 71 template<class Elemtype> 72 Elemtype SeqStack<Elemtype>::Top() const 73 { 74 return data[top_p]; 75 } 76 77 #endif /* SeqStack_h */
1 // 2 // Created by geshenglu on 2020/4/4. 3 // 4 5 #ifndef CALC_H 6 #define CALC_H 7 #include "SeqStack.h" 8 #include <string> 9 class Calc { 10 char *expression; 11 enum class token{ 12 OPAREN,ADD,SUB,MULTI,DIV,EXP,CPAREN,VALUE,EOL 13 }; 14 void BinaryOp(token op,SeqStack<int>&dataStack); 15 token GetOp(int &value); 16 public: 17 Calc(char *s){ 18 expression = new char[strlen(s)+1]; 19 strcpy(expression,s); 20 } 21 ~Calc(){ 22 delete expression; 23 } 24 int result(); 25 }; 26 #endif //CALC_H
1 // 2 // Created by geshenglu on 2020/4/4. 3 // 4 5 #include "Calc.h" 6 #include <iostream> 7 #include <cmath> 8 void Calc::BinaryOp(Calc::token op, SeqStack<int> &dataStack) { 9 int num1,num2; 10 if(dataStack.IsEmpty()){ 11 std::cerr<<"缺少右操作数!"<<std::endl; 12 exit(1); 13 } 14 else 15 num2 = dataStack.Pop(); 16 17 if(dataStack.IsEmpty()){ 18 std::cerr<<"缺少左操作数!"<<std::endl; 19 exit(1); 20 } 21 else 22 num1 = dataStack.Pop(); 23 24 switch(op){ 25 case token::ADD: 26 dataStack.Push(num1+num2); 27 break; 28 case token::SUB: 29 dataStack.Push(num1-num2); 30 break; 31 case token::MULTI: 32 dataStack.Push(num1*num2); 33 break; 34 case token::DIV: 35 dataStack.Push(num1/num2); 36 break; 37 case token::EXP: 38 dataStack.Push(pow(num1,num2)); 39 break; 40 } 41 } 42 43 Calc::token Calc::GetOp(int &value) { 44 while (*expression && *expression == ‘ ‘){ 45 ++expression; 46 } 47 if(*expression==‘