阿摩線上測驗 登入

申論題資訊

試卷:103年 - 103年專門職業及技術人員高等建築師、技師、第二次食品技師暨普通不動產經紀人、記帳士考高等_資訊技師#29113
科目:程式設計
年份:103年
排序:0

題組內容

三、試以 C++來定義有理數(意即分數)類別 Rational 如下,其包含有分子 a 和分母 b,其
中分母 b 不能為 0。
class Rational {
private:
int a; // 分子
int b; // 分母
……
public:
……
};
假設 main( )的主程式,和其所執行輸出的結果如下:

申論題內容

⑵試完成所需的建構子(包含預設建構子),多載運算子(overloaded operators):加、
減、乘、除等,以及成員函式 prt( ),和相關的程式碼。注意:有理數輸出必須為
最簡分數,並藉機將有理數化簡成最簡分數。(20 分)

詳解 (共 1 筆)

詳解 提供者:hchungw

為了完成 Rational 類別的定義,我們將實作所需的建構子、多載運算子,以及 prt() 成員函數。在這個實作中,所有運算結果都將自動化簡為最簡分數。
以下是完整的 Rational 類別定義:

#include <iostream>
#include <numeric>  // For std::gcd
class Rational {
private:
    int a;  // 分子
    int b;  // 分母
    // Helper function to reduce the fraction to its simplest form
    void reduce() {
        int gcd = std::gcd(a, b);  // Calculate the greatest common divisor of a and b
        a /= gcd;  // Divide both a and b by gcd to reduce the fraction
        b /= gcd;
        if (b < 0) {  // Handle negative denominators
            a = -a;
            b = -b;
        }
    }
public:
    // Default constructor that initializes the rational number as 0/1
    Rational() : a(0), b(1) {}
    // Constructor that takes two integers as numerator and denominator
    Rational(int numerator, int denominator) : a(numerator), b(denominator) {
        if (b == 0) {
            throw std::invalid_argument("Denominator cannot be zero");
        }
        reduce();  // Reduce the fraction immediately upon construction
    }
    // Method to print the rational number
    void prt() const {
        std::cout << a << "/" << b;
    }
    // Overload the + operator to add two Rational numbers
    Rational operator+(const Rational& rhs) const {
        return Rational(this->a * rhs.b + rhs.a * this->b, this->b * rhs.b);
    }
    // Overload the - operator to subtract two Rational numbers
    Rational operator-(const Rational& rhs) const {
        return Rational(this->a * rhs.b - rhs.a * this->b, this->b * rhs.b);
    }
    // Overload the * operator to multiply two Rational numbers
    Rational operator*(const Rational& rhs) const {
        return Rational(this->a * rhs.a, this->b * rhs.b);
    }
    // Overload the / operator to divide two Rational numbers
    Rational operator/(const Rational& rhs) const {
        if (rhs.a == 0) {
            throw std::invalid_argument("Cannot divide by Rational number with numerator 0");
        }
        return Rational(this->a * rhs.b, this->b * rhs.a);
    }
};
int main() {
    // Assume the input is provided in the command line arguments
    Rational r0;
    std::cout << "r0 = "; r0.prt(); std::cout << std::endl;
    Rational r1(2, 4);
    std::cout << "r1 = "; r1.prt(); std::cout << std::endl;
    Rational r2(3, 6);
    std::cout << "r2 = "; r2.prt(); std::cout << std::endl;
    Rational r3 = r1 + r2;
    std::cout << "r1 + r2 = "; r3.prt(); std::cout << std::endl;
    Rational r4 = r1 - r2;
    std::cout << "r1 - r2 = "; r4.prt(); std::cout << std::endl;
    Rational r5 = r1 * r2;
    std::cout << "r1 * r2 = "; r5.prt(); std::cout << std::endl;
    Rational r6 = r1 / r2;
    std::cout << "r1 / r2 = "; r6.prt(); std::cout << std::endl;
    return 0;
}
這段代碼中:
reduce() 是一個幫助函數,用於將分數化簡成最簡分數。
建構子會立即調用 reduce() 函數來確保對象始終保持在最簡分數形式。
prt() 函數用於輸出有理數的最簡分數形式。
運算子 +, -, *, / 被重載以實現有理數之間的算數