zoukankan      html  css  js  c++  java
  • 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();
    }


    
    
  • 相关阅读:
    leetcode Convert Sorted List to Binary Search Tree
    leetcode Convert Sorted Array to Binary Search Tree
    leetcode Binary Tree Level Order Traversal II
    leetcode Construct Binary Tree from Preorder and Inorder Traversal
    leetcode[105] Construct Binary Tree from Inorder and Postorder Traversal
    证明中序遍历O(n)
    leetcode Maximum Depth of Binary Tree
    限制 button 在 3 秒内不可重复点击
    HTML 和 CSS 画三角形和画多边行基本原理及实践
    在线前端 JS 或 HTML 或 CSS 编写 Demo 处 JSbin 与 jsFiddle 比较
  • 原文地址:https://www.cnblogs.com/javafly/p/6037239.html
Copyright © 2011-2022 走看看