wangxiaokunNO1 2020-10-24 17:38 采纳率: 0%
浏览 145

简单工厂实现简易计算器(c++实现)

1 我想用简单工厂实现简单的计算器,

在主函数输入左右运算数和运算符,在工厂类中通过运算符选择要调用的具体操作——加减乘除,加减乘除各自单独设计一个类,它们都继承自Operation类



2 代码如下

#include <iostream>
#include <cmath>
using namespace std;

class Operation
{
    public:
    Operation(double operandA,double operandB,char operate)
    {
        this->operandA=operandA;
        this->operandB=operandB;
        this->operate=operate;
    }
    protected:
        double operandA;
        double operandB;
        char operate;

    public:
        void setOperandA(double operandA)
        {
            this->operandA=operandA;
        }
        void setOperandB(double operandB)
        {
            this->operandB=operandB;
        }
        void setOperate(char operate)
        {
            this->operate=operate;
        }
        double getOperandA()
        {
            return this->operandA;
        }
        double getOperandB()
        {
            return this->operandB;
        }
        char getOperate()
        {
            return this->operate;
        }
        virtual double getResult()=0;
};
class AddOperation:public Operation
{
    public:
    AddOperation(double operandA,double operandB,char operate):Operation( operandA, operandB, operate){}
    double getResult()
    {
        return operandA+operandB;
    }
};

class MinusOperation:public Operation
{
    public:
    MinusOperation(double operandA,double operandB,char operate):Operation( operandA, operandB, operate){}
    double getResult()
    {
        return operandA-operandB;
    }
};
class MultipleOperation:public Operation
{
    public:
    MultipleOperation(double operandA,double operandB,char operate):Operation( operandA, operandB, operate){}
    double getResult()
    {
        return operandA*operandB;
    }
};
class DivOperation:public Operation
{
    public:
    DivOperation(double operandA,double operandB,char operate):Operation( operandA, operandB, operate){
    }
    double getResult()
    {
//        cout<<operandB<<endl;
        if(fabs(this->operandB)>1e-2)
        {
            return this->operandA/this->operandB;
        }
        return -1;
};
class OperationFactory{


    public :
        Operation *operation=NULL;
    public:
        Operation* createOperation(double operandA,double operandB,char opt)
        {
            switch(opt)
            {
                case '+':
                    {
                        AddOperation add(operandA,operandB,opt);
                        operation=&add;
                    }
                    break;
                case '-':
                    {
                        MinusOperation minus(operandA,operandB,opt);
                        operation=&minus;
                    }
                    break;
                case '*':
                    {
                        MultipleOperation multiple(operandA,operandB,opt);
                        operation=&multiple;
                    }
                    break;
                case '/':
                    {
                        DivOperation div(operandA,operandB,opt);
                        operation=&div;
                    }
                    break;
                default:
                    break;

            }
            return operation;
        }

};
int main()
{
    double oprA,oprB;
    double result;
    char opt;
    cin>>oprA>>oprB>>opt;
//    AddOperation add(oprA,oprB,opt);
//    Operation *op=&add;

    OperationFactory factory;
    Operation *option=factory.createOperation(oprA,oprB,opt);
    result=option->getResult();
    cout<<result;
    return 0;
}

3 问题

在具体操作——加减乘除的实现类中,如DivOperation中,若在该函数体内
判断除数operandB与0的关系语句前,加入
cout<<operandB<<endl;
这样一句话,operandB的值就会错误,导致计算结果也发生错误
比如输入10 10/时
图片说明

不加这句话结果就对了,请问代码哪里出了问题?
  • 写回答

1条回答 默认 最新

  • 江苏的WSH2012 2023-09-13 12:56
    关注
    
    #include<bits/stdc++.h>
    #include<windows.h>
    using namespace std;
    int main()
    {
        long long n,a,b,r=1,x,y;
        cout<<"1·求两数和   2·求两数差         3.求两数积          4.求两数商 "<<endl;
        cout<<"5·求a的b次方 6·求两数最大公约数 7·求两数最小公倍数 8·退出运算"<<endl;
        cout<<"温馨提示:第1·3·5·7题和第4题分别存在数据范围问题和精度问题,请谅解!"<<endl;
        cout<<"_________________________________________"<<endl;
        while (1)
        {
            cin>>n;
            if (n==1||n==2||n==3||n==4||n==5||n==6||n==7)
            {
                cout<<"请输入要进行运算的两个数:"<<endl;
                cin>>a>>b;
                cout<<"正在运算中......"<<endl;
                Sleep(1000);
            }
            switch (n)
            {
                case 1:
                    cout<<a+b;
                    break;
                case 2:
                    cout<<a-b;
                    break;
                case 3:
                    cout<<a*b;
                    break;
                case 4:
                    cout<<double(a)/b;
                    break;
                case 5:
                    while (b!=0)
                    {
                        if (b&1==1)
                        {
                            r*=a;
                        }
                        a*=a;
                        b>>=1;
                    }
                    cout<<r;
                    break;
                case 6:
                    x=a;
                    y=b;
                    r=a%b;
                    while (r!=0)
                    {
                        a=b;
                        b=r;
                        r=a%b;
                    }
                    cout<<b;
                    break;
                case 7:
                    x=a;
                    y=b;
                    r=a%b;
                    while (r!=0)
                    {
                        a=b;
                        b=r;
                        r=a%b;
                    }
                    cout<<x*y/b;
                    break;
                case 8:
                    goto here;
                    break;
                default:
                    cout<<"抱歉,目前还未具有此类运算哦!";
                    break; 
            }
            cout<<endl<<"_________________________________________"<<endl;
            cout<<"如有需要,请继续使用!"<<endl;
        }
        here:;
        cout<<"_________________________________________"<<endl;
        return 0;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀