我撰寫了這個執行數學運算的程式。嗯……我嘗試添加一個新的頭檔案“ZeroCheck.h”……,并創建(宣告和定義)一個例外類 ZeroCheck,它顯示訊息“分母為 0,除法無效”。通過 what() 成員函式。我想通過創建 Math3(6,0) 物件并使用 try 陳述句輸出數學物件來修改 main() 函式。ZeroCheck 例外應在 catch 塊中捕獲,如果除法無效應顯示錯誤訊息。但不幸的是,輸出與我期望的并不完全相同,我的意思是當我運行我的程式時,輸出是這樣的:
主檔案
#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);
}
數學.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.
Any idea what I can do to get the output I want? (Note: I have only tried to do what I could with the instructions provided, and by the way if you see something wrong with my code, I apologize in advance, I'm not familiar with some things yet, so...).
uj5u.com熱心網友回復:
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);
}
您可能已經想出了問題的答案,但我想無論如何我都會分享我的回答。可能有更好的方法來檢查狀況,但我認為它看起來不錯并且易于閱讀!祝你好運!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/446824.html
