后缀运算器(已补全)

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值