國立暨南國際大學 105 學年度第二學期 第二次期中考試卷

科目名稱:程式設計 開課系所:資訊工程 學系 考試日期 2017.5.31
系所別:
年級:
學號:
姓名:
考試時間 08:30-10:30
1
2
3  
4
5  
6  
7
8  

9  
10

11


  1. (10%) Determine whether the following code has syntax erros or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    // Class definition (P.279)
    #include <iostream>
    using std::cout;
    using std::endl;

    class CRational
    {
    public:
        int numerator;      // 分子
        int denominator;    // 分母

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

    int main()
    {
        CRational a, b, c;
        a.numerator = 1; a.denominator = 2; a.Print();
        b.numerator = 1; b.denominator = 3; b.Print();
        c.numerator = 1; c.denominator = 4; c.Print();
        return 0;
    }

  2. (10%) What will be the output of the following program?
    // Copy Constructor (P.317)
    #include <iostream>
    using std::cout;
    using std::endl;

    class CRational
    {
    public:
        int numerator;      // 分子
        int denominator;    // 分母

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

        CRational() {}      // default constructor
        CRational(const CRational &r)
        {
            numerator   = r.numerator;
            denominator = r.denominator + 1;
        }
    };

    int main()
    {
        CRational a; a.numerator = 1; a.denominator = 2;
        CRational b(a);
        CRational c = a;
        CRational d; d = a;

        a.Print();
        b.Print();
        c.Print();
        d.Print();
        return 0;
    }

  3. (10%) What will be the output of the following program?

    // Default values for constructor arguments (P.228)
    #include <iostream>
    using std::cout;
    using std::endl;

    class CRational
    {
    public:
        int numerator;      // 分子
        int denominator;    // 分母

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

        CRational(int p = 0, int q = 1)
        {
            numerator = p; denominator = q;
        }

        CRational(const CRational &r)
        {
            numerator   = r.numerator;
            denominator = r.denominator;
        }
    };

    int main()
    {
        CRational a(1, 2);
        CRational b(3);
        CRational c = a;
        CRational d(b);

        a.Print();
        b.Print();
        c.Print();
        d.Print();
        return 0;
    }


  4. (10%) What will be the output of the following program?

    // Overloading the Decrement Operator (P.348)
    #include <iostream>
    using std::cout;
    using std::endl;

    class CRational
    {
    public:
        int numerator;      // 分子
        int denominator;    // 分母

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

        CRational(int p = 0, int q = 1)
        {
            numerator = p; denominator = q;
        }

        CRational& operator--()
        {
            denominator--;
            return *this;
        }

        CRational operator--(int)
        {
            CRational r(*this);
            numerator--;
            return r;
        }
    };

    int main()
    {
        CRational a(5, 12);

        a.Print();
        (--a).Print();
        a--.Print();
        a.Print();
        return 0;
    }


  5. (10%) What will be the output of the following program?

    //
    Overloading the Addition Operator (P.344)
    #include <iostream>
    #include <iostream>
    using std::cout;
    using std::endl;

    class CRational
    {
    public:
        int numerator;      // 分子
        int denominator;    // 分母

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

        CRational(int p = 0, int q = 1)
        {
            numerator = p; denominator = q;
        }

        CRational operator+(CRational b)  const   // const member function (P.307)
        {
            return CRational( numerator*b.denominator + denominator*b.numerator,
                    denominator * b.denominator );
        }

        CRational& operator-(CRational b)   // Return a reference (P.341)
        {
            numerator = numerator*b.denominator - denominator*b.numerator;
            denominator = denominator * b.denominator;
            return *this;
        }
    };

    int main()
    {
        CRational a(1, 2);
        CRational b(1, 3);
        CRational c(1, 4);
        CRational d(1, 5);
        CRational e, f;

        e = a + b + c + d;
        f = a - b - c - d;
        e.Print();
        f.Print();
        a.Print();
        return 0;
    }


  6. (10%) Determine whether the following code has syntax erros or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    //
    Static data members (P.311)
    #include <iostream>
    using std::cout;
    using std::endl;

    class CNode
    {
    public:
        int value;
        CNode* previous;
        static CNode* pLastNode;

        CNode(int v) :
        value(v), previous(pLastNode)
        { pLastNode = this; }
    };

    CNode* CNode::pLastNode = NULL;

    int main()
    {
        CNode a(3), b(5), c(7);
        CNode* p;
        int n = 0;

        for (n=0, p=CNode::pLastNode; p!=NULL; p=p->previous)
            n++;
        cout << n << endl;
       
        return 0;
    }


  7. (10%) Determine whether the following code has syntax erros or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    // Using pointers with a class (P.274)
    #include <iostream>
    using std::cout;
    using std::endl;

    class CNode
    {
    public:
        int value;
        CNode* previous;
        static CNode* pLastNode;

        CNode(int v) :
        value(v), previous(pLastNode)
        { pLastNode = this; }
    };

    CNode* CNode::pLastNode = NULL;

    int main()
    {
        CNode a(3), b(5), c(7), d(9), e(11);
        CNode* p;
        int n = 0;

        for (n=0, p=&d; p!=NULL; p=p->previous)
            n++;
        cout << n << endl;
       
        return 0;
    }


  8. (10%) Determine whether the following code has syntax erros or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    // Sorting Rational Numbers (P.508)
    #include <iostream>
    #include <vector>
    #include <algorithm>
    using std::vector;
    using std::cout;
    using std::endl;

    class CRational
    {
    public:
        int numerator;      // 分子
        int denominator;    // 分母

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

        CRational(int p = 0, int q = 1)
        {
            numerator = p; denominator = q;
        }

        CRational operator+(CRational b)  const   // const member function (P.307)
        {
            return CRational( numerator*b.denominator + denominator*b.numerator,
                    denominator * b.denominator );
        }

        CRational& operator-(CRational b)   // Return a reference (P.341)
        {
            numerator = numerator*b.denominator - denominator*b.numerator;
            denominator = denominator * b.denominator;
            return *this;
        }
    };

    bool Less(CRational a, CRational b)
    {
        return a.numerator*b.denominator < b.numerator*a.denominator;
    }

    int main()
    {
        int b[] = { 1, 3, 5, 7, 2, 4, 6 };
        vector<CRational> a;
        for (size_t i=0; i<(sizeof b / sizeof b[0]); i++)
            a.push_back( CRational(1, b[i]) );

        sort(a.begin(), a.end(), Less);
        for (size_t i=0; i<a.size(); i++)
            a.at(i).Print();

        return 0;
    }

  9. (10%) Determine whether the following code has syntax erros or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // String Methods (P.405)
    #include <iostream>
    #include <iostream>
    #include <string>
    #include <vector>
    using std::vector;
    using std::string;
    using std::cout;
    using std::endl;

    class CDiagonal {
    public:
    int x1;
    int y1;
    int x2;
    int y2;

    CDiagonal(int a=0, int b=0, int c=0, int d=0):
    x1(a), y1(b), x2(c), y2(d) {}
    };

    string getDiagonal(vector<string> tabula, CDiagonal pos)
    {
    string s = string(1, tabula[pos.x1][pos.y1]) +
    string(1, tabula[pos.x2][pos.y2]);
    return s;
    }

    CDiagonal findDiagonal(vector<string> tabula, string s)
    { // s is a 2-character string
    char c1 = s[0];
    char c2 = s[1];
    int x1, y1, x2, y2;
    for (x1=0; x1<5; x1++)
    {
    y1 = tabula[x1].find(c1);
    if (y1 != string::npos) break;
    }
    for (x2=0; x2<5; x2++)
    {
    y2 = tabula[x2].find(c2);
    if (y2 != string::npos) break;
    }
    return CDiagonal(x1, y1, x2, y2);
    }

    string enc(string s, vector<string> tabula)
    {
    string ciphertext, c3c4;
    CDiagonal pos, pos2;
    for (size_t i=0; i<s.length()-1; i+=2)
    {
    pos = findDiagonal(tabula, s.substr(i,2));
    pos2.x1 = pos.x1;
    pos2.y1 = pos.y2;
    pos2.x2 = pos.x2;
    pos2.y2 = pos.y1;
    c3c4 = getDiagonal(tabula, pos2);
    ciphertext += c3c4;
    }
    return ciphertext;
    }

    int main()
    {
    string plaintext = "PXMMCTUBACSJNO";
    string ciphertext;
    string s("ABCDEFGHIJKLMNOPQRSTUVWXY");
    vector<string> t;
    for (size_t i=0; i<5; i++)
    t.push_back(s.substr(i*5, 5));
    ciphertext = enc(plaintext, t);
    cout << ciphertext << endl;
    return 0;
    }
  10. (10%) Determine whether the following code has syntax erros or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // Inheritance (P.433)
    #include <iostream>
    #include <vector>
    #include <string>
    using std::string;
    using std::vector;
    using std::cout;
    using std::endl;

    class MyString : public string {
    public:
    MyString(string s): string(s) {}

    vector<string> split(char sep = ' ') {
    vector<string> a;
    int anchor = 0;
    size_t pos;
    pos = this->find(sep, anchor);
    while (pos != string::npos) {
    a.push_back( this->substr(anchor, pos-anchor) );
    anchor = pos + 1;
    pos = this->find(sep, anchor);
    }
    a.push_back( this->substr(anchor) );
    return a;
    }
    };

    int main()
    {
    MyString s("1 2 3 4 5 6 5 4 3");
    vector<string> arr = s.split();
    for (size_t i=0; i<arr.size(); i++)
    cout << arr.at(i) << endl;
    return 0;
    }
  11. (10%) Determine whether the following code has syntax erros or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // Map (P.543)
    #include <iostream>
    #include <map>
    #include <vector>
    #include <string>
    using std::string;
    using std::vector;
    using std::map;
    using std::cout;
    using std::endl;

    class MyString : public string {
    public:
    MyString(string s): string(s) {}

    vector<MyString> split(char sep = ' ') {
    vector<MyString> a;
    int anchor = 0;
    size_t pos;
    pos = this->find(sep, anchor);
    while (pos != string::npos) {
    a.push_back( this->substr(anchor, pos-anchor) );
    anchor = pos + 1;
    pos = this->find(sep, anchor);
    }
    a.push_back( this->substr(anchor) );
    return a;
    }

    int unique() {
    vector<MyString> arr = this->split();
    map<MyString, int> Count;
    for (size_t i=0; i<arr.size(); i++)
    {
    MyString k = arr[i];
    map<MyString,int>::iterator iter = Count.find(k);
    if (iter == Count.end())
    Count[k] = 1;
    else
    Count[k]++;
    }
    return Count.size();
    }
    };

    int main()
    {
    MyString s("1 2 3 4 5 6 5 4 3 7");
    MyString t("0 4 9 2 9 1 0 9 6 0");
    cout << s.unique() << endl;
    cout << t.unique() << endl;
    return 0;
    }