國立暨南國際大學 100 學年度第二學期小考試卷                   (考試時間: 14:10-14:20)
科目名稱:資訊系統 與網路導論 開課系所:資訊工程 學系 任課教師
吳坤熹
系所別:
年級:
學號:
姓名:
考試日期
2012.3.21
  1. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // Operator Overloading
    #include <iostream>
    using std::cout;

    int gcd(int a, int b)
    {
    int r; // remainder
    if (a==0 && b==0)
    {
    cout << "The GCD of two zeros are undefined.\n";
    exit(1);
    }
    while (b!=0)
    {
    r = a % b;
    a = b;
    b = r;
    }
    return a;
    }

    class CRational
    {
    int numerator; // 分子
    int denominator; // 分母
    public:
    CRational(int n=0, int d=1): numerator(n), denominator(d) {}

    CRational Add(CRational a, CRational b) const
    {
    return CRational(
    a.numerator * b.denominator + a.denominator * b.numerator,
    a.denominator * b.denominator);
    }

    CRational operator+(CRational a)
    {
    return Add(*this, a);
    }

    void Reduce()
    {
    int n = gcd(numerator, denominator);
    numerator /= n;
    denominator /= n;
    }

    void Print()
    {
    cout << numerator << '/' << denominator << '\n';
    }
    };

    int main()
    {
    CRational a(1,2), b(1,3), c(1,4), d(1,5), e(1,6);
    CRational f, g, h, i, j;
    f = a + a;
    g = b + e;
    h = a + b + c;
    i = a + b + c + d;
    j = a + b + c + d + e;
    f.Print();
    g.Print();
    return 0;
    }


  2. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // Operator Overloading
    #include <iostream>
    using std::cout;

    class CRational
    {
    int numerator; // 分子
    int denominator; // 分母
    public:
    CRational(int n=0, int d=1): numerator(n), denominator(d) {}
    CRational operator=(CRational a)
    {
    numerator = a.numerator;
    denominator = a.denominator;
    return *this;
    }

    void Print()
    {
    cout << numerator << '/' << denominator << '\n';
    }
    };

    int main()
    {
    int a=1, b=2, c=3;
    CRational d(1,5), e(1,6), f(1,7);
    (a = b ) = c;
    (d = e ) = f;
    cout << a << '\n';
    d.Print();
    return 0;
    }