C++Stack应用-表达式计算器

/*@file test.cpp*/
/*mingGW compiled ok*/

#include <iostream>

#include "Stack.h"
#include "calculator.h"
using namespace std;
 
int main()
{
     Calculator C1;
     C1.run();

    return 0;
}

/**************************************************************************/

运行结果如下:

Please input expression: (end by =)
8 5 6 + * 2 / 6 8 * + =
Result: 92

 

/**************************************************************************/

/*@file "Stack.h"*/

#ifndef STACK_H
#define STACK_H

#define SIZE_INIT 100
#define SIZE_INCREMENT 100
template <typename Type> class Stack
{
    public:
       Stack();
        ~Stack();
        int getLength() const { return length; }
        Type getTop();
        bool empty(){ return (top == base); }
        bool full(){ return (length >= size); }
        void push(const Type &val);
        Type pop();
        void makeEmpty();
        void visit();
    private:
        Type *base;
        Type *top;
        int length;
        int size;
};

template <typename Type> Stack<Type>::Stack() : length(0), size(SIZE_INIT)
{
    base = new Type[SIZE_INIT];
    top = base;
}

template <typename Type> Stack<Type>::~Stack()
{
    delete [] base;

}

template <typename Type> Type Stack<Type>::getTop()
{
    if(empty())
    {
        cerr << "Stack empty: getTop()/n";
        exit(-1);
    }

    return *(top-1);
}

template <typename Type> void Stack<Type>::push(const Type &val)
{
    if(full())
    {
        cerr << "Stack full: push()/n";
        exit(-1);
    }

    *top = val;
    top++;
    length++;
}

template <typename Type> Type Stack<Type>::pop()
{
    if(empty())
    {
        cerr << "Stack empty: pop()/n";
        exit(-1);
    }

    Type val = *(top-1);
    top--;
    length--;

    return val;
}

template <typename Type> void Stack<Type>::makeEmpty()
{
    if(empty())
    {
        return;
    }

    while(top != base)
    {
        pop();
    }
   
}


template <typename Type> void Stack<Type>::visit()
{
    if(empty())
    {
        cout << "empty, no element visited/n";
        return;
    }

    Type *ptr = base;

    cout << "[bottom: ";
    while(ptr != top)
    {
        cout << *ptr << " ";
        ptr++;
  }
    cout << ":top]/n";

}

#endif

/**************************************************************************/

/*Calculator.h*/
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include <math.h>
using namespace std;

class Calculator
{
 public:
     Calculator(){ }
     ~Calculator(){ }
 
     void run();
     void clear();
 
 private:
     void addOperand(double val){ iStack.push(val); }
     bool getOperand(double &left, double &right);
     void doOperator(char op);
     Stack<double> iStack; 
 
};


bool Calculator::getOperand(double &left, double &right)
{
     if(iStack.empty())
     {
         cerr << "Missing Operand!" << endl;
         return false;
     }

     right = (double)iStack.pop();
 
     if(iStack.empty())
     {
         cerr << "Missing Operand!" << endl;
         return false;
     }
 
     left = (double)iStack.pop();

     return true;
 }
 
 void Calculator::doOperator(char op)
 {
     double left, right;
     if(getOperand(left, right))
     {
         switch(op)
         {
             case '+': iStack.push(left + right); break;
             case '-': iStack.push(left - right); break;
             case '*': iStack.push(left * right); break;
             case '/': if(right == 0.0)
                       {
                           cerr << "Divide by 0!" << endl;
                           clear();
                       }
                       else
                       {
                           iStack.push(left/right);
                           break;
                       }
             case '^': iStack.push(pow(left, right)); break;
         }
     }
     else clear();
 
 }
 
 void Calculator::run()
 {
     char ch;
     double newop;
     cout << "Please input expression: (end by =)/n";
     while(cin >> ch, ch != '=')
     {
         switch(ch)
         {
             case '+':
             case '-':
             case '*':
             case '/':
             case '^':
                      doOperator(ch); break;
 
             default: cin.putback(ch);
                      cin >> newop;
                      addOperand(newop);
                      break;
         }
     }
 
     cout << "Result: " << (double)iStack.pop() << "/n";
 }
 
 void Calculator::clear()
 {
     iStack.makeEmpty();
 
 }

#endif

你可能感兴趣的:(C++Stack应用-表达式计算器)