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

📄 cal.cpp

📁 经典c++程序的实现
💻 CPP
字号:
//typedef double DataType; 

#include <stdio.h>
#include <assert.h>
#include <iostream.h>

typedef double ELEM;             // calculator accepts real data

#include "..\include\book.h"
#include "..\include\astack.h"

class Calculator {
private:
  Stack S;

  void Enter(double num);       // store data value on the stack

  // fetch operands from stack and assign value to parameters.
  // print message and return False if two operands not present
  bool GetTwoOperands(double& opnd1, double& opnd2);

  void Compute(char op);    // evaluate an operation

public:
  calculator(void);             // constuctor

  // read chars and evaluate a postfix expression. stop on '='.
  void Run(void);
  void Clear(void);
};

// store data value on the stack
void Calculator::Enter(double num) {
  S.push(num);
}

// fetch operands from stack and assign value to parameters.
// print message and return False if two operands not present
bool Calculator::GetTwoOperands(double& opnd1, double& opnd2)  {
  if (S.isEmpty())   {           // check for presence of operand
    cerr << "Missing opreand!";  // << end1;
    return FALSE;
  }
  opnd1 = S.pop();              // fetch right-hand oprand
  if (S.isEmpty())  {
    cerr << "Missing opreand!";  // << end1;
    return FALSE;
  }
  opnd2 = S.pop();              // fetch left-hand oprand
  return TRUE;
}

void Calculator::Compute(char op)  {      // evaluate an operation
  bool result;
  double operand1, operand2;

  // fetch two operands and identify sucess or failure
  result = GetTwoOperands(operand1, operand2);

  // if success, evaluate the operator and push value on stack
  // otherwise, clear calculator stack. check for divide by 0.
  if (result == TRUE)
    switch(op) {
        case '+' : S.push(operand2 + operand1);
                   break;
        case '-' : S.push(operand2 - operand1);
                   break;
        case '*' : S.push(operand2 * operand1);
                   break;
        case '/' : if (operand1 == 0.0)  {
                      cerr << "Divide by 0!";  // << end1;
                      S.clear();
                   }
                   else
                      S.push(operand2 / operand1);
                   break;
    }
  else                  // error! clear calculator
    S.clear();           
}

// read chars and evaluate a postfix expression. stop on '='.
void Calculator::Run(void)  {
  char c;
  double newoperand;

  while (cin >> c, c!= '=') {     // read until '='  (Quit)
    switch(c) {
        case '+' :                         // check possible operator
        case '-' :
        case '*' :
        case '/' :
                    Compute(c);           // have operator, evaluate it
                    break;
        default :                         // not operator, must be operand,
                   cin.putback(c);         // put char back
                   cin >> newoperand;     // read the operand
                   Enter(newoperand);     // store the operand on the stack
                   break;
    }
  }

  // answer stored on top of stack, print using topValue
  if (!S.isEmpty())
    cout << S.topValue();  // << end1;
}

// clear operand stack
void Calculator::Clear(void)  {
  S.clear();
}

// #include "calc.h"

void main(void)  {
  Calculator Calc;

  Calc.Run();
}

⌨️ 快捷键说明

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