後綴運算器(已補全)

#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));
}

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