#include "mystack.h"
#include <iostream>
using namespace std;
char get_command()
{
char command;
bool waiting = true;
cout << "Select command and press <ENTER> : ";
while (waiting){
cin >> 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
<< "[+][-][*][/]" << endl
<< "[q]exit." << 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 << "Waring :stack full,lost numbers" <<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) == overfloww)
cout << "Warring : 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;
numbers.push(p);
}
else{
numbers.pop();
if (numbers.push(p-q) == overfloww)
cout << "Warring : 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;
numbers.push(p);
}
else{
numbers.pop();
if (numbers.push(p*q) == overfloww)
cout << "Warring : 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;
numbers.push(p);
}
else{ if (q == 0)
cout << "againt / precept!" <<endl;
else numbers.pop();
if (numbers.push(p/q) == overfloww)
out << "Warring : stack full,lost result" << endl;
}
}
case 'q';
cout << "calculation finished.\n";
return false;
}
return true;
}
void main()
{
Stack numbers;
while (do_command(get_command(),stored_numbers));
}
后缀运算器(已补全)
最新推荐文章于 2024-06-21 01:18:42 发布