啊哟喂我好傻 2022-04-11 14:15 采纳率: 75%
浏览 124
已结题

c++写一个计算器程序

用面向对象方法设计一个计算器控制台程序。

1.1设计一个父类Operation, 数据成员包含两个操作数m_numberA,m_numberB。 成员函数包括set(),get()函数设置操作数的值,虚函数GetResult()用于输出结果。

1.2分别设计派生类 OperationAdd,OperationSub,OperationMul, OperationDiv实现加减乘除操作,注意除法操作除数不能为0。

1.3派生类统一继承自父类Operation, 重写虚函数GetResult()输出操作后的结果。

1.4 OperationFactory类已经写好,可以直接使用。
class OperationFactory {

public:

static Operation * CreateOperate(char oper)

{

   Operation * poper = NULL;

                 switch (oper) 

                    {

                               case '+':

                          poper = new OperationAdd;

                             break;

                               case '-':

                             poper = new OperationSub;

                             break;

                               case '*':

                                      poper = new OperationMul;

                                      break;

                               case '/':

                                      poper = new OperationDiv;

                                      break;

                     }

        return poper;

   }

};

int main()

{

   int flag = 1;

   while (flag)

   {

          cout << "input A:";

          double numA;

          cin >> numA;

          cout << "input operator:";

          char oper;

          cin >> oper;

          cout << "input B:";

          double numB;

          cin >> numB;

          Operation* poper = OperationFactory::CreateOperate(oper);

          poper->SetNumA(numA);

          poper->SetNumB(numB);

          cout << poper->GetNumA() << ' ' << oper << ' ' << poper->GetNumB() << " = " << poper->GetResult() << endl;

          cout << "是否需要继续: (0/1)";

          cin >> flag;

   }

   return 0;

}

  • 写回答

2条回答 默认 最新

  • CSDN专家-link 2022-04-11 14:37
    关注
    class Operation
    {
        double m_numberA,m_numberB;
    public:
        double GetNumA() {return m_numberA;}
        double GetNumB() {return m_numberB;}
        void SetNumA(double A) {m_numberA = A;}
        void SetNumB(double B) {m_numberB = B;}
        virtual double GetResult() {return 0;}
    };
    
    class OperationAdd : public Operation
    {
    public:
        virtual double GetResult() {return GetNumA() + GetNumB();}
    };
    
    class OperationSub : public Operation
    {
    public:
        virtual double GetResult() {return GetNumA() - GetNumB();}
    };
    
    class OperationMul : public Operation
    {
    public:
        virtual double GetResult() {return GetNumA() * GetNumB();}
    };
    
    class OperationDiv : public Operation
    {
    public:
        virtual double GetResult() { if(GetNumB() == 0) return 0; return GetNumA() / GetNumB();}
    };
    
    
    class OperationFactory {
    public:
    
    static Operation * CreateOperate(char oper)
    
    {
    
       Operation * poper = NULL;
     
                     switch (oper) 
     
                        {
     
                                   case '+':
     
                              poper = new OperationAdd;
     
                                 break;
     
                                   case '-':
     
                                 poper = new OperationSub;
     
                                 break;
     
                                   case '*':
     
                                          poper = new OperationMul;
     
                                          break;
     
                                   case '/':
     
                                          poper = new OperationDiv;
     
                                          break;
     
                         }
     
            return poper;
     
       }
    };
    
    int main()
    
    {
       int flag = 1;
     
       while (flag)
     
       {
     
              cout << "input A:";
     
              double numA;
     
              cin >> numA;
     
              cout << "input operator:";
     
              char oper;
     
              cin >> oper;
     
              cout << "input B:";
     
              double numB;
     
              cin >> numB;
     
              Operation* poper = OperationFactory::CreateOperate(oper);
     
              poper->SetNumA(numA);
     
              poper->SetNumB(numB);
     
              cout << poper->GetNumA() << ' ' << oper << ' ' << poper->GetNumB() << " = " << poper->GetResult() << endl;
     
              cout << "是否需要继续: (0/1)";
     
              cin >> flag;
     
       }
     
       return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月19日
  • 已采纳回答 4月11日
  • 创建了问题 4月11日

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里