2014-09-10 62 views
-2

有人可以解釋什麼是在這個代碼?你不必一條一條地走,但總體總結會很有幫助。它與其他小類C++代碼類似,因此您不必完全引用此代碼。C++解釋中的分數類

#include<iostream> 

using namespace std; 

class Fraction 

{ 

    private: 

    int num, den; 

    public: 

    Fraction(int n = 1, int d = 2) : num(n), den(d) { } 

    void show() { cout«num«」/」«den; } 

    Fraction operator+(Fraction f) const 

    { return Fraction(num+f.num, den+f.den); } 

    Fraction operator-(Fraction f) const 

    { return Fraction(num-f.num, den-f.den); } 

    Fraction operator*(Fraction f) const 

    { return Fraction(num*f.num, den*f.den); } 

    Fraction operator/(Fraction f) const 

    { 

    int rNum = (f.den*num)/den; 

    return (rNum, f.num); 

    } 

    friend ostream& operator«(ostream& out, Fraction& f) 

    { cout«f.num«」/」«f.den; return out; } 

    friend istream& operator»(istream& in, Fraction& f) 

    { cout«」\nEnter Numerator: 「; cin»f.num; cout«」Enter Denominator: 「; 

    cin»f.den; cout«f.num«」/」«f.den; return in; } 

    int ndMax() 

    { return (num<=den) ? num:den; } 

    void reduce() 

    { 

    for(int i = 2; i<=ndMax(); i++) 

    if(num%i == 0 && den%i == 0) 

    { num = num/i; den = den/i; i = 1; } 

    } 

}; // 

int main() 

{ 

    Fraction f1(5, 6); 

    Fraction f2(80, 1001); 

    cout«f1«」 and 「«f2«endl; 

    Fraction f3 = f1/f2; 

    f3.reduce(); cout«f3; 

    cout«endl; 

    return 0; 

} 
+1

類的定義,它支持一些重載運營商和不基於單獨的分子和分母(因爲它會在紙上的數學課做)..什麼部分需要額外的解釋數學? – user2864740 2014-09-10 22:44:53

+0

這個班似乎設計不好。 operator +()和operator-()的實現不會執行分數的正常加法和減法,而是執行一些加法/減法分子和分母的無意義操作。 – mattnewport 2014-09-11 00:12:11

+0

是亞光。我只是改變了它的邏輯,現在它似乎運作良好。仍然困惑我的主要部分是這條線。分數(int n = 1,int d = 2):num(n),den(d){}。這裏發生了什麼?另外,私有變量的重點是什麼。 – pr0grammingIsFun 2014-09-11 00:47:30

回答

1
// This declares a dependency on the system module, iostream. Specifically, this code uses the 
// system provided objects, cout, and cin, for output and input. 
#include <iostream> 

// This says if the compiler cannot find a symbol, it should try looking in the namespace, std. 
using namespace std; 

// This defines a type called, Fraction. 
class Fraction { 
private: 
    // This declares two private member variables, num, and den, presumably representing the 
    // numerator and denominator of a fraction. Each object of type Fraction will have its own 
    // num and den. Because they are private they cannot be accidentally modified outside the 
    // definition of Fraction. They are the module's secret. 
    int num, den; 

public: 
    // This declares a constructor. It uses the default argument notation to actually define 
    // three constructor syntaxes. Two arguments may be given, one, or zero. Fraction() = 1/2, 
    // Fraction(x) = x/2, and Fraction(x, y) = x/y. A default denominator of 1 would be more 
    // normal, especially since this constructor can be used to implicitly convert an int into 
    // a Fraction. 
    Fraction(int n = 1, int d = 2) : num(n), den(d) { } 

    // This function outputs a representation of a Fraction to cout. It repeats the definition 
    // of operator<< below, which violates the DRY principle so it should probably be written 
    // as: cout << *this; 
    void show() { 
    cout << num << "/" << den; } 

    // This is an operator overload. It allows you to write, Fraction + Fraction. Note that it 
    // does not add fractions in the expected way, instead it does a vector addition. 
    Fraction operator+(Fraction f) const { 
    return Fraction(num + f.num, den + f.den); } 

    // Similarly broken. 
    Fraction operator-(Fraction f) const { 
    return Fraction(num - f.num, den - f.den); } 

    // Correct multiplication, although normalizing the fraction is kind of expected. 
    Fraction operator*(Fraction f) const { 
    return Fraction(num * f.num, den * f.den); } 

    // Cannot be bothered to decide if this is correct. Multiplying by the inverse would be the 
    // more obvious thing to do, like: return *this * Fraction(f.den, f.num); 
    Fraction operator/(Fraction f) const { 
    int rNum = (f.den * num)/den; 
    return Fraction(rNum, f.num); } 

    // These functions support reading and writing a Fraction using streams. Note that they should 
    // make use of the out and in parameters, then they would work with all stream types. 
    friend ostream& operator<<(ostream& out, Fraction& f) { 
    cout << f.num << "/" << f.den; 
    return out; } 
    // The prompts here are poor design. 
    friend istream& operator>>(istream& in, Fraction& f) { 
    cout << "\nEnter Numerator: "; 
    cin >> f.num; 
    cout << "Enter Denominator: "; 
    cin >> f.den; 
    cout << f.num << "/" << f.den; 
    return in; } 

    // This function does not do what you would expect based on the name. It returns min(num, den). 
    // It should be declared const, like operator+ is since it is thread-safe. 
    int ndMax() { 
    return (num <= den) ? num : den; } 

    // This is weird and evil. The intent is to normalize the fraction. Euclid's algorithm would 
    // be the usual way. 
    void reduce() { 
    for (int i = 2; i <= ndMax(); i++) 
     if (num % i == 0 && den % i == 0) { 
     num = num/i; 
     den = den/i; 
     i = 1; // Evil!!! Do not modify the loop variable in the body of the loop. 
     } } 

    // There are a number of other functions, not explicit here, that are implicitly generated 
    // by the compiler. For example a copy constructor and assignment operator. 
}; 

// This is a simple test driver for the Fraction class. 
int main() { 
    Fraction f1(5, 6); 
    Fraction f2(80, 1001); 
    cout << f1 << " and " << f2 << endl; 
    Fraction f3 = f1/f2; 
    f3.reduce(); 
    cout << f3; 
    cout << endl; 
    return 0; } 
+0

非常感謝!比我預期的更多的信息。我仍然對公衆以下的線路感到困惑。我的C++語法知識非常糟糕。爲什麼有一個「:」爲什麼沒有「;」最後。還有必要聲明這樣的構造函數嗎?我的老師也在另一個程序中聲明瞭一個沒有參數的構造函數,而他用不同的方法做了這個。像MyString(); MyString(string a);這可以在這裏完成嗎? – pr0grammingIsFun 2014-09-11 02:32:27

+0

函數定義不以分號結尾。它不僅僅是這個構造函數,也沒有一個函數定義以分號結尾。 冒號標誌着構造函數初始化列表的開始。 是的,你可以聲明三個獨立的構造函數,而不是使用默認參數。 – 2014-09-11 03:11:30