逆波瀾式計算器

棧的應用:逆波瀾式計算器

// Section 2.3
//03.2 計算器 
#include<iostream> 
#include"Stack_double.cpp"
using namespace std;

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.
*/

{
   Stack_entry 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)/*_________
	  								    |_q_|_p__   p+q    */
         cout << "Stack empty" << endl;
      else {
         numbers.pop();/*___
	  				    |_q_   */
         if (numbers.top(q) == underflow) {
            cout << "Stack has just one entry" << endl;
            numbers.push(p);
         }

         else {
            numbers.pop();//此時p和q都沒了 
            if (numbers.push(q + p) == overflow)//把p+q壓入棧 
               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;
		
		
	
   //   Add options for further user commands.

    case 'q':
      cout << "Calculation finished.\n";
      return false;
   }
   return true;
}


main()
{
   Stack stored_numbers;//存數據的棧 
   while (do_command(get_command(), stored_numbers));//先獲得命令get_command(),再執行命令do_command ,只有按q才false 
}

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