新手第一篇-逆波兰计算器

昨天刚刚过了二十岁生日,决定今天开始开始写博客,记录自己的成长,同时也为了以后复习用。回归正题,先上代码

#include<iostream>
#include<cctype>
using namespace std;
typedef double Stack_entry;


const int maxstack = 10;
enum Error_code{ sucess, overflow, underflow };
typedef double Stack_entry;
class Stack
{

public:
    Stack();
    bool empty()const;//如果Stack为空,返回true,否则返回false
    Error_code pop();//如果Stack非空,删除栈顶元素,如Stack为空,则返回underflow(下溢)
    Error_code push(const Stack_entry &item);//如果Stack不满,则将元素加到Stack的栈顶,如果Stack是满的,则返回overflow(上溢)
    Error_code top(Stack_entry &item)const;//非空的Stack的栈顶元素被复制到item。如果Stack空,返回underflow(下溢)
private:
    int count;//数组下标
    Stack_entry entry[maxstack];//顺序表
};


Stack::Stack()
{
    count = 0;
}

Error_code Stack::push(const Stack_entry &item)
{
    Error_code outcome = sucess;
    if (count >= maxstack)
        outcome = overflow;
    else
        entry[count++] = item;
    return outcome;
}

Error_code Stack::pop()
{
    Error_code outcome = sucess;
    if (count == 0)
        outcome = underflow;
    else
        --count;
    return outcome;
}

Error_code Stack::top(Stack_entry &item)const
{
    Error_code outcome = sucess;
    if (count == 0)
        outcome = underflow;
    else
        item = entry[count - 1];
    return outcome;
}


bool Stack::empty()const
{
    bool outcome = true;
    if (count == 0)
        outcome = true;
    else
        outcome = false;
    return outcome;
}





char get_command()
{
    char command;
    bool waiting = true;//循环控制条件
    cout << "Select command and press[enter]:";
    while (waiting)
    {
        cin >> command;
        command = tolower(command);
        if (command == '?' || command == '=' || command == '+' || command == '-' || command == '*' || command == '/' || command == 'q')
            waiting = false;
        else
        {
            cout << "Please enter a valid command:" << endl
                << "[?]push to stack [=]print top" << endl
                << "[+] [-] [*] [/] are arithmetioc operations" << endl
                << "[Q]uit" << endl;
        }
    }
    return command;
}


bool do_command(char command, Stack &numbers)
{
    double p, q;
    switch (command)
    {
    case '?'://将下一位操作数入栈
        cout << "Enter a real number:" << flush;
        cin >> p;
        if (numbers.push(p) == overflow)
            cout << "Warning:Sttack full,lost number" << endl;
        break;
    case '='://输出操作结果
        if (numbers.top(p) == underflow)
        {
            cout << "Stack empty" << endl;
        }
        else
            cout << p << endl;
        break;
    case'+':
        if (numbers.top(p) == underflow)
            cout << "Stack empty" << endl;
        else
        {
            numbers.pop();
            if (numbers.top(q) == underflow){
                cout << "Stack has just one entry" << endl;
                numbers.push(p);
            }
            else
            {
                numbers.pop();
                if (numbers.push(p + q) == overflow)
                    cout << "Warning:stack full,lost result" << endl;
            }
        }
        break;
    case'-':
        if (numbers.top(p) == underflow)
            cout << "Stack empty" << endl;
        else
        {
            numbers.pop();
            if (numbers.top(q) == underflow)
                cout << "Stack has just one entry" << endl;
            else
            {
                numbers.pop();
                if (numbers.push(p - q) == overflow)
                    cout << "Warning:stack full,lost result" << endl;
            }
        }
        break;
    case'*':
        if (numbers.top(p) == underflow)
            cout << "Stack empty" << endl;
        else
        {
            numbers.pop();
            if (numbers.top(q) == underflow)
                cout << "Stack has just one entry" << endl;
            else
            {
                numbers.pop();
                if (numbers.push(p * q) == overflow)
                    cout << "Warning:stack full,lost result" << endl;
            }
        }
        break;
    case'/':
        if (numbers.top(p) == underflow)
            cout << "Stack empty" << endl;
        else
        {
            numbers.pop();
            if (numbers.top(q) == underflow)
                cout << "Stack has just one entry" << endl;
            else
            {
                numbers.pop();
                if (numbers.push(p / q) == overflow)
                    cout << "Warning:stack full,lost result" << endl;
            }
        }
        break;
    case'q':
        cout << "Calculation finished.\n" << endl;
        return false;
    }
    return true;
}


void introduce()
{
    cout << "Please enter a valid command:" << endl
        << "[?]push to stack [=]print top" << endl
        << "[+] [-] [*] [/] are arithmetioc operations" << endl
        << "[Q]uit" << endl;
}

int main()
{
    Stack stored_numbers;//定义栈类
    introduce();//操作说明
    while (do_command(get_command(), stored_numbers));
}

源自《Data structures and Program Design In c++》

vs2013环境测试结果:
(1)减法
(2)加法
(3)乘法
(4)除法

注意:逆波兰计算器是用栈实现的,可以很好的说明栈的使用。在减法测试中,5先入栈,3后入栈,但结果是-2,说明3先出栈,5后出栈,体现了栈的先入后出的特点。

优点:任何表达式,不管多复杂,都可以不用圆括号进行描述

第一次写,难免有不足之处,有任何问题请网友们批评指正

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章