字尾表示式計算器的C++實現
阿新 • • 發佈:2018-11-30
在課上一個半小時寫出來的,還望各位不吝賜教~
首先百度百科上抄一下定義:字尾表示式,指的是不包含括號,運算子放在兩個運算物件的後面,所有的計算按運算子出現的順序,嚴格從左向右進行(不再考慮運算子的優先規則)。
計算過程是這樣的:建立一個棧S 。從左到右讀表示式,如果讀到運算元就將它壓入棧S中,如果讀到n元運算子(即需要引數個數為n的運算子)則取出由棧頂向下的n項按運算元運算,再將運算的結果代替原棧頂的n項,壓入棧S中 。如果字尾表示式未讀完,則重複上面過程,最後輸出棧頂的數值則為結束。
這次課堂練習的時候助教說最好自己對棧進行一個實現。那麼我也就用了自己實現的棧。
MyStack.h
#ifndef MYSTACK_H_INCLUDED #define MYSTACK_H_INCLUDED #include <iostream> #include <stdlib.h> using namespace std; enum Error_code{underflow, overflow, success}; const int maxStack = 100; template<class Stack_entry> 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<class Stack_entry> MyStack<Stack_entry>::MyStack() { count_ = 0; } template<class Stack_entry> bool MyStack<Stack_entry>::empty() const { return (count_ == 0); } template<class Stack_entry> Error_code MyStack<Stack_entry>::pop() { if (count_ < 1) { return underflow; } else { count_--; return success; } } template<class Stack_entry> Error_code MyStack<Stack_entry>::top(Stack_entry &item) const { if (count_ < 1) { return underflow; } else { item = entry[count_ - 1]; return success; } } template<class Stack_entry> Error_code MyStack<Stack_entry>::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 <math.h>
template<class Stack_entry>
class PolishCal {
public:
void introduction();
void instructions(MyStack<Stack_entry> &numbers);
char get_command();
bool do_command(char command, MyStack<Stack_entry> &numbers);
};
#endif // POLISHCAL_H_INCLUDED
PolishCal.cpp
#include "PolishCal.h"
template<class Stack_entry>
void PolishCal<Stack_entry>::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<class Stack_entry>
void PolishCal<Stack_entry>::instructions(MyStack<Stack_entry> &numbers)
{
char input;
input = get_command();
while (input != 'Q' && input != 'q') {
do_command(input, numbers);
input = get_command();
}
}
template<class Stack_entry>
char PolishCal<Stack_entry>::get_command()
{
char input;
cout << "Select command and press <Enter>:";
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<class Stack_entry>
bool PolishCal<Stack_entry>::do_command(char command, MyStack<Stack_entry> &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<double> stored_numbers;
PolishCal<double> c;
c.introduction();
c.instructions(stored_numbers);
return 0;
}
執行結果和老師給的PPT一樣~我就直接貼鮑哥的圖了