• c++:stack


    #ifndef  STACK_CLASS
    #define STACK_CLASS
    #include<iostream>
    #include<cstdlib>
    using namespace std;
    const int MaxStackSize = 50;
    template<class T>
    class Stack{
    private:
    	T stacklist[MaxStackSize];
    	int top;
    public:
    	Stack(void);
    	void Push(const T& item);
    	T Pop(void);
    	void ClearStack(void);
    	T Peek(void) const;
    	int StackEmpty(void) const;
    	int StackFull(void) const;
    };
    template<class T>
    Stack<T>::Stack(void) :top(-1){}
    template<class T>
    void Stack<T>::Push(const T& item){
    	if (top == MaxStackSize - 1){
    		std::cerr << "Stack overflow" << endl;
    		exit(1);
    	}
    	top++;
    	stacklist[top] = item;
    }
    template<class T>
    T Stack<T>::Pop(void){
    	T temp;
    	if (top == -1){
    		std::cerr << "attempt to pop an empty stack!" << endl;
    		exit(1);
    	}
    	temp = stacklist[top];
    	top--;
    	return temp;
    }
    template<class T>
    T Stack<T>::Peek(void)  const{
    	if (top == -1){
    		std::cerr << "attempt to peek at an empty stack" << endl;
    		exit(1);
    	}
    	return stacklist[top];
    }
    template<class T>
    int Stack<T>::StackEmpty(void) const{
    	return top == -1;
    }
    template<class T>
    int Stack<T>::StackFull(void) const{
    	return top == MaxStackSize - 1;
    }
    template<class T>
    void Stack<T>::ClearStack(void){
    	top = -1;
    }
    #endif
    <pre class="cpp" name="code">#include<iostream>
    #include<cmath>
    #include<cstdlib>
    #include<cstring>
    using namespace std;
    enum Boolean{False,True};
    #include"Stack.h"
    class Calculator{
    private:
    	Stack<int> S;
    	void Enter(int num);
    	Boolean GetTwoOperands(int &opnd1, int &opnd2);
    	void Compute(char op);
    public:
    	Calculator(void);
    	void Run(void);
    	void Clear(void);
    };
    void Calculator::Enter(int num){
    	S.Push(num);
    }
    Boolean Calculator::GetTwoOperands(int &opnd1, int &opnd2){
    	if (S.StackEmpty()){
    		cerr << "Missing operand!" << endl;
    		return False;
    	}
    	opnd1 = S.Pop();
    	if (S.StackEmpty()){
    		cerr << "Missing operand;;" << endl;
    		return False;
    	}
    	opnd2 = S.Pop();
    	return True;
    }
    void Calculator::Compute(char op){
    	Boolean result;
    	int operand1, operand2;
    	result = GetTwoOperands(operand1, operand2);
    	if (result == True){
    		switch (op){
    		case '+':S.Push(operand1 + operand2);
    			break;
    		case '-':S.Push(operand1 - operand2);
    			break;
    		case '*':S.Push(operand1 * operand2);
    			break;
    		case '/':if (operand1 == 0){
    					 cerr << "divide by 0" << endl;
    					 S.ClearStack();
    		}
    else
    				 S.Push(operand2 / operand1);
    			break;
    		case '^':S.Push(pow(operand2, operand1));
    			break;
    		}
    		cout << '=' << S.Peek() << ' ';
    	}
    else
    	S.ClearStack();
    }
    Calculator::Calculator(void){  }
    void Calculator::Run(void){
    	char c[20];
    	while (cin >> c, *c != 'q')
    		switch (*c){
    		case 'c':S.ClearStack();
    			break;
    		case '-':if (strlen(c) > 1)
    			Enter(atoi(c));
    				 else
    					 Compute(*c);
    			break;
    		case '+':
    		case '*':
    		case '/':
    		case '^':
    			Compute(*c);
    			break;
    		default:
    			Enter(atoi(c));
    			break;
    	}
    }
    void Calculator::Clear(void){
    	S.ClearStack();
    }
    #include"Calculator.h"
    int main(){
    	Calculator Calculator;
    	Calculator.Run();
    }


    
    
  • 相关阅读:
    Hdu3037 Saving Beans
    bzoj3994 [SDOI2015]约数个数和
    bzoj2154 Crash的数字表格
    bzoj3529 [Sdoi2014]数表
    bzoj2820 YY的GCD
    bzoj2301 [HAOI2011]Problem b
    bzoj2440 [中山市选2011]完全平方数
    Java基础知识强化之IO流笔记20:FileOutputStream写出数据实现换行和追加写入
    Java基础知识强化之IO流笔记19:FileOutputStream的三个write方法
    Java基础知识强化之IO流笔记18:FileOutputStream写入数据
  • 原文地址:https://www.cnblogs.com/javafly/p/6037239.html
Copyright © 2020-2023  润新知