新手第一篇-逆波蘭計算器

昨天剛剛過了二十歲生日,決定今天開始開始寫博客,記錄自己的成長,同時也爲了以後複習用。迴歸正題,先上代碼

#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後出棧,體現了棧的先入後出的特點。

優點:任何表達式,不管多複雜,都可以不用圓括號進行描述

第一次寫,難免有不足之處,有任何問題請網友們批評指正

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