后缀表达式计算器的C++实现

在课上一个半小时写出来的,还望各位不吝赐教~

首先百度百科上抄一下定义:后缀表达式,指的是不包含括号,运算符放在两个运算对象的后面,所有的计算按运算符出现的顺序,严格从左向右进行(不再考虑运算符的优先规则)。

计算过程是这样的:建立一个栈S 。从左到右读表达式,如果读到操作数就将它压入栈S中,如果读到n元运算符(即需要参数个数为n的运算符)则取出由栈顶向下的n项按操作数运算,再将运算的结果代替原栈顶的n项,压入栈S中 。如果后缀表达式未读完,则重复上面过程,最后输出栈顶的数值则为结束。

这次课堂练习的时候助教说最好自己对栈进行一个实现。那么我也就用了自己实现的栈。

MyStack.h

#ifndef MYSTACK_H_INCLUDED
#define MYSTACK_H_INCLUDED

#include 
#include 

using namespace std;

enum Error_code{underflow, overflow, success};

const int maxStack = 100;

template
class MyStack {

public:
    MyStack();
    bool empty() const;
    Error_code pop();
    Error_code top(Stack_entry &item) const;
    Error_code push(const Stack_entry &item);

private:
    int count_;
    Stack_entry entry[maxStack];
};

#endif // MYSTACK_H_INCLUDED

MyStack.cpp

#include "MyStack.h"

template
MyStack::MyStack()
{
    count_ = 0;
}

template
bool MyStack::empty() const
{
    return (count_ == 0);
}

template
Error_code MyStack::pop()
{
    if (count_ < 1) {
        return underflow;
    }
    else {
        count_--;
        return success;
    }
}

template
Error_code MyStack::top(Stack_entry &item) const
{
    if (count_ < 1) {
        return underflow;
    }
    else {
        item = entry[count_ - 1];
        return success;
    }
}

template
Error_code MyStack::push(const Stack_entry &item)
{
    if (count_ >= maxStack) {
        return overflow;
    }
    else {
        entry[count_++] = item;
        return success;
    }
}

PolishCal.h

#ifndef POLISHCAL_H_INCLUDED
#define POLISHCAL_H_INCLUDED

#include "MyStack.h"
#include 

template
class PolishCal {

public:
    void introduction();
    void instructions(MyStack &numbers);
    char get_command();
    bool do_command(char command, MyStack &numbers);

};

#endif // POLISHCAL_H_INCLUDED

PolishCal.cpp

#include "PolishCal.h"

template
void PolishCal::introduction()
{
    cout << "This is a reverse Polish Calculator." << endl;
    cout << "Please enter a valid command:" << endl;
    cout << "[?]push to stack  [=]print top" << endl;
    cout << "[+] [-] [*] [/] [^] are arithmetic operations" << endl;
    cout << "[Q]quit" << endl;
}

template
void PolishCal::instructions(MyStack &numbers)
{
    char input;
    input = get_command();
    while (input != 'Q' && input != 'q') {
        do_command(input, numbers);
        input = get_command();
    }
}

template
char PolishCal::get_command()
{
    char input;
    cout << "Select command and press :";
    cin >> input;
    cin.clear();
    cin.sync();
    while (input != '?' && input != '=' && input != '+' && input != '-' && input != '*' && input != '/' && input != '^' && input != 'Q' && input != 'q') {
        cout << "Input error. Please input again:";
        cin >> input;
        cin.clear();
        cin.sync();
    }
    return input;
}

template
bool PolishCal::do_command(char command, MyStack &numbers)
{
    Stack_entry num;
    Stack_entry top;
    Stack_entry a, b;

    switch (command)
    {
    case '?':
        cout << "Enter a real number: ";
        cin >> num;
        numbers.push(num);
        break;
    case '=':
        numbers.top(top);
        cout << top << endl;
        break;
    case '+':
        numbers.top(a);
        numbers.pop();
        numbers.top(b);
        numbers.pop();
        numbers.push(a + b);
        break;
    case '-':
        numbers.top(a);
        numbers.pop();
        numbers.top(b);
        numbers.pop();
        numbers.push(b - a);
        break;
    case '*':
        numbers.top(a);
        numbers.pop();
        numbers.top(b);
        numbers.pop();
        numbers.push(a * b);
        break;
    case '/':
        numbers.top(a);
        numbers.pop();
        numbers.top(b);
        numbers.pop();
        numbers.push(b / a);
        break;
    case '^':
        numbers.top(a);
        numbers.pop();
        numbers.top(b);
        numbers.pop();
        numbers.push(pow(b, a));
        break;
    case 'Q':
    case 'q':
        break;
    default:
        break;
    }

    return true;
}

main.cpp

#include "MyStack.cpp"
#include "PolishCal.cpp"

int main(void)
{
    MyStack stored_numbers;
    PolishCal c;
    c.introduction();
    c.instructions(stored_numbers);
    return 0;
}

运行结果和老师给的PPT一样~我就直接贴鲍哥的图了

后缀表达式计算器的C++实现_第1张图片

你可能感兴趣的:(C++)