在课上一个半小时写出来的,还望各位不吝赐教~
首先百度百科上抄一下定义:后缀表达式,指的是不包含括号,运算符放在两个运算对象的后面,所有的计算按运算符出现的顺序,严格从左向右进行(不再考虑运算符的优先规则)。
计算过程是这样的:建立一个栈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一样~我就直接贴鲍哥的图了