如果分母为 0,则抛出 ZeroCheck 异常对象

Throw a ZeroCheck exception object if the denominator is 0

我编写了这个执行数学运算的程序。好吧......,我尝试添加一个新的头文件“ZeroCheck.h”......,并创建(声明和定义)一个异常class ZeroCheck,它显示消息“Denominator is 0, invalid division. “通过 what() 成员函数。我想通过创建 Math3(6,0) 对象并使用 try 语句输出 Mathematics 对象来修改 main() 函数。 ZeroCheck 异常应在 catch 块中捕获,如果除法无效,则应显示错误消息。 但不幸的是,输出与我期望的不完全一样,我的意思是当我 运行 我的程序时,输出是这样的:

main.cpp

#include <iostream>
#include <cstdlib>

#include "Mathematics.h"
#include "ZeroCheck.h"

// using std::cout, std::cerr, std::ostream;
using namespace std;


int
main()
{
    try {
        Mathematics<int> Math1(10, 5);
        Mathematics<double> Math2(5.5, 3.4);
        Mathematics<int> Math3(6, 0);

        cout << "Math 1:" << '\n';
        cout << Math1 << '\n' << '\n';

        cout << "Math 2:" << '\n';
        cout << Math2 << '\n';

        cout << "Math 3" << '\n';
        cout << Math3 << '\n';

    } catch (ZeroCheck e) {
        cerr << e.what() << '\n';
    }

    return (EXIT_SUCCESS);
}


template <class T>
Mathematics<T>::Mathematics(T v1, T v2) : val1(v1), val2(v2) { }


template <class T>
T Mathematics<T>::addition()
{
    return (val1 + val2);
}


template <class T>
T Mathematics<T>::subtraction()
{
    return (val1 - val2);
}


template <class T>
T Mathematics<T>::multiplication()
{
    return (val1 * val2);
}


template <class T>
T Mathematics<T>::division()
{
    if (val2 == 0) {
        throw ZeroCheck("Denominator is 0, invalid division.");
    }

    return (val1 / val2);
}


template<class T>
ostream& operator<<(ostream& os, Mathematics<T>& obj)
{
    os << "The result of calculation for: " << obj.val1 << " and " << obj.val2 << '\n'
       << "Sum is: " << obj.addition() << '\n'
       << "Difference is: " << obj.subtraction() << '\n'
       << "Product is: " << obj.multiplication() << '\n'
       << "Quotient is: " << obj.division() << '\n';

    return (os);
}

Mathematics.h

#ifndef MATHEMATICS_H_INCLUDED
#define MATHEMATICS_H_INCLUDED

#include <iostream>

using std::ostream;


template <class T>
class Mathematics {

    public:
        Mathematics(T, T);

        T addition();
        T subtraction();
        T multiplication();
        T division();

        template <class U>friend ostream& operator<<(ostream&, Mathematics<U>&);

    private:
        T val1;
        T val2;

};

#endif  /* MATHEMATICS_H_INCLUDED */

ZeroCheck.h

#ifndef ZEROCHECK_H_INCLUDED
#define ZEROCHECK_H_INCLUDED

#include <string>

using std::string;


class ZeroCheck {

    public:
        ZeroCheck()
        {
            this->msg = "";
        }


        ZeroCheck(string _msg)
        {
            this->msg = _msg;
        }

        string what()
        {
            return (msg);
        }

    private:
        string msg;
};

#endif  /* ZEROCHECK_H_INCLUDED */

但不幸的是,输出与我期望的不完全一样,我的意思是当我 运行 我的程序时,输出是这样的:

Math 1:                                                                                                                                         
The result of calculation for: 10 and 5   
Sum is: 15      
Difference is: 5  
Product is: 50                                  
Quotient is: 2                                                                                                                                  

      
Math 2:                               
The result of calculation for: 5.5 and 3.4
Sum is: 8.9     
Difference is: 2.1
Product is: 18.7                                
Quotient is: 1.61765                                                                                                                            

Math 3
The result of calculation for: 6 and 0
Sum is: 6
Difference is: 6
Product is: 0
// As you can see here the output is: 'Quotient is: Denominator is 0, invalid division.' Instead of just 'Denominator is 0, invalid division.'
Quotient is: Denominator is 0, invalid division.

这是我期望的输出:

Math 1:                                                                                                                                                                              
The result of calculation for: 10 and 5
Sum is: 15                                       
Difference is: 5
Product is: 50                                             
Quotient is: 2                            
                
                  
Math 2:            
The result of calculation for: 5.5 and 3.4
Sum is: 8.9       
Difference is: 2.1                               
Product is: 18.7
Quotient is: 1.61765


Math 3:            
The result of calculation for: 6 and 0
Sum is: 6
Difference is: 6                               
Product is: 0
Denominator is 0, invalid division.

知道我可以做些什么来获得我想要的输出吗? (注意:我只是尽力按照提供的说明做我能做的,顺便说一句,如果你发现我的代码有问题,我提前道歉,我还不熟悉一些东西,所以......) .

template<class T>
ostream& operator<<(ostream& os, Mathematics<T>& obj)
{
    os << "The result of calculation for: " << obj.val1 << " and " << obj.val2 << '\n'
    << "Sum is: " << obj.addition() << '\n'
    << "Difference is: " << obj.subtraction() << '\n'
    << "Product is: " << obj.multiplication() << '\n';
    if (obj.val1 == 0 || obj.val2 == 0)
    {
        os << obj.division() << '\n';
    }
    else
    {
        os << "Quotient is: " << obj.division() << '\n';
    }
    return (os);
}

您可能已经找到问题的答案,但我想我还是会分享我的回答。可能有更好的方法来检查条件,但我认为它看起来不错并且易于阅读!祝你好运!!