⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 commands.cpp

📁 数据结构与程序设计教材源码 数据结构与程序设计教材源码
💻 CPP
字号:
void introduction()
{
   cout << "Reverse Polish Calculator Program." 
        << "\n----------------------------------" 
        << endl << endl;
}
 
void instructions()
{
    cout << "User commands are entered to read in and operate on integers." 
         << endl;
    cout << "The valid commands are as follows:"   << endl
         << "[Q]uit." << endl
         << "[?] to enter an integer onto a stack."  << endl
         << "[=] to print the top integer in the stack." << endl
         << "[+] [-] [*] [/] are arithmetic operations." << endl
         << "These operations apply to the top pair of stacked integers." 
         << endl;
}
 
char get_command()
{
   char command;
   bool waiting = true;
   cout << "Select command and press <Enter>:";

   while (waiting) {
      cin >> command;
      command = tolower(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
              << "[+] [-] [*] [/]   are arithmetic operations" << endl
              << "[Q]uit." << endl;
      }
   }
   return command;
}
 
bool do_command(char command, Stack &numbers)
/* 
 
Pre:  The first parameter specifies a valid
calculator command.
Post:
The command specified by the first parameter
has been applied to the Stack of numbers
given by the second parameter.
A result of true is returned unless
command == 'q'.
Uses:
The class Stack.
 
*/

{
   double p, q;
   switch (command) {
   case '?':
      cout << "Enter a real number: " << flush;
      cin >> p;
      if (numbers.push(p) == overflow)
         cout << "Warning: Stack full, lost number" << 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(q + p) == overflow)
               cout << "Warning: Stack full, lost result" << endl;
         }
      }
      break;


   //   Add options for further user commands.
 
   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(q - p) == overflow)
               cout << "Warning: 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(q * p) == overflow)
               cout << "Warning: Stack full, lost result" << endl;
         }
      }
      break;

   case '/':
      if (numbers.top(p) == underflow) cout << "Stack empty" << endl;
      else if (p == 0.0) {
          cerr << "Division by 0 fails; no action done." << endl;
          numbers.push(p);     //  Restore stack to its prior state.
      }
      else {
         numbers.pop();
         if (numbers.top(q) == underflow) {
            cout << "Stack has just one entry" << endl;
            numbers.push(p);
         }
         else {
            numbers.pop();
            if (numbers.push(q / p) == overflow)
               cout << "Warning: Stack full, lost result" << endl;
         }
      }
      break;
 
   case 'q':
      cout << "Calculation finished.\n";
      return false;
   }
   return true;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -