科目名稱: 程式設計
期末考 開課系所:資工系 考試日期 2019.6.12
系所別:
年級:
學號:
姓名:
考試時間 08:30-10:30
Open book; turn off computer & mobile phone
1
2
3


4
5
6




7
8
9
10
11





12



  1. (10%) 請 問電影《美麗人生》中,女 主角 Dora 的職業是什麼?
    1. 護士
    2. 歌唱家
    3. 軍人
    4. 老師

  2. (10%) Given the following input,
    1/2
    3
    41/12
    -9
    +5/2
    6.2/2
    8+2
    3/a
    3/
    3/0

    +-9
    x/7
    If the output is
    true
    true
    true
    true
    true
    false
    false
    false
    false
    false
    false
    false
    What regular expression should you fill in (2)?
    // regex_match
    #include <iostream>
    #include <string>
    #include <regex>
    using std::cout;
    using std::string;
    using std::cin;
    using std::regex;

    int main()
    {
        string s;
        regex re = regex("     (2)     ");
        cout << std::boolalpha;
        while (cin >> s)
            cout << regex_match(s, re) << '\n';

        return 0;
    }

  3. (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).

    // regex_token_iterator
    #include <iostream>
    #include <string>
    #include <regex>
    using std::regex;
    using std::sregex_token_iterator;

    int main()
    {
        std::string s("0AA1BB2CC3");
        regex splitter( "[0-9]+" ); // regex to split a string
        sregex_token_iterator tokenIterator( s.begin(), s.end(), splitter, -1 );
        sregex_token_iterator end; // empty iterator

        unsigned nCount = 0;
        while (  tokenIterator != end ) // tokenIterator isn't empty
        {
            ++nCount;
            std::cout << nCount << *tokenIterator << '\n';
            ++tokenIterator;
        }
        std::cout << nCount << '\n';
    }

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

    // Containers vector and list
    #include <iostream>
    #include <vector>
    #include <list>

    int main()
    {
        int a[10] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 };
        std::vector<int> b(a, a+10);
        std::list<int> c;

        for (int i=0; i<10; ++i)
            if (b.at(i) % 5 < 3)
                c.push_back(b.at(i));
            else
                c.push_front(b.at(i));

        int n=1;
        int sum=0;
        std::list<int>::const_iterator i;
        for (i=c.begin(); i!=c.end(); ++i, n=-n)
            sum += *i * n;
        std::cout << sum << '\n';
        return 0;
    }

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

    // File Stream and String Stream
    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <string>
    using std::string;
    using std::ifstream;
    using std::ofstream;
    using std::istringstream;
    using std::cout;

    int main()
    {
        ofstream outfile("a.txt");
        outfile << "The game starts now." << '\n';
        outfile << "You have to score one thousand points.\n";
        outfile << "If you do that, ";
        outfile << "you take home a tank with a big gun.\n";
        outfile.close();

        ifstream infile("a.txt");
        string line;
        string word;
        int nLine = 0;
        int nWord = 0;
        while ( getline(infile, line) )
        {
            ++nLine;
            istringstream iss(line);
            while ( iss >> word )
                ++nWord;
        }
        cout << nLine << ' ' << nWord << '\n';

        return 0;
    }

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

    // When is static objects destroyed
    #include <iostream>
    #include <string>
    using std::cout;
    using std::string;

    class Object
    {
    public:
        Object(string s): name(s)
        { cout << name << "\tconstructed.\n"; }
        ~Object()
        { cout << name << "\tdestroyed.\n"; }
    private:
        string name;
    };

    void f(string s)
    { Object a(s);
     
    static Object b("Charlie");
    }

    int main()
    {
        Object a("Alpha");
        f("Bravo");
        f("Delta");
        return 0;
    }



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

    // Copy Constructor or Assignment Operator?
    #include <iostream>
    using std::cout;
    using std::endl;
    using std::ostream;

    class Rational {
    friend ostream& operator<<(ostream& output, const Rational& b) {
        output << b.numerator << '/' << b.denominator;
        return output;
    }

    public:
        Rational(int n=0, int d=1): numerator(n), denominator(d) {}
        Rational(const Rational&b) {
            numerator = b.numerator + 1;
            denominator = b.denominator;
        }
        Rational& operator=(const Rational& b) {
            numerator = b.numerator;
            denominator = b.denominator + 1;
            return *this;
        }
    private:
        int numerator;
        int denominator;
    };

    int main()
    {
        Rational a(1, 2);
        Rational b(a);  // This is a copy constructor
        Rational c;
        c = b;          // This is an assignment
        Rational d = b; // Is this an assignment or a copy constructor?
        cout << a << ' ' << b << ' ' << c << ' ' << d << endl;

        return 0;
    }

  8. (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).

    // vector of vector
    #include <iostream>
    #include <vector>
    using std::vector;
    using std::cout;
    using std::endl;

    int main()
    {
        typedef vector<int> Row;
        typedef vector<Row> Matrix;
        Row r(3, 3);
        Matrix A;
        A.push_back(r);
        A.push_back(r);
        A.push_back(r);
        for (int i=0; i<3; ++i)
            for (int j=0; j<3; ++j)
                A[i][j] += 10*i + j;
        for (int i=0; i<3; ++i)
            cout << A[i].at(2) << ' ';
        cout << endl;
        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).

    // Function Pointers (P.393)
    #include <iostream>

    int add(int a, int b)
    { return a + b; }

    int sub(int a, int b)
    { return a - b; }

    int mul(int a, int b)
    { return a * b; }

    int divide(int a, int b)
    { return a / b; }

    int main()
    {
    int (*f)[](int, int) = { add, sub, mul, divide };

    for (int i=0; i<4; i++)
    {
    std::cout << f[i](12, 6) << ' ';
    }

    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).
    // Polymorphism and Virtual Functions
    #include <iostream>
    using std::cout;

    class Polygon {
    protected:
    int width, height;
    public:
    void set_values (int a, int b)
    { width=a; height=b; }
    int area() { return width*height; }
    };

    class Rectangle: public Polygon {
    public:
    virtual int area()
    { return width*height; }
    };

    class Triangle: public Polygon {
    public:
    virtual int area()
    { return width*height/2; }
    };

    int main () {
    Rectangle rect;
    Triangle tri;
    Polygon* ppoly1 = &rect;
    Polygon* ppoly2 = &tri;
    ppoly1->set_values (4,5);
    ppoly2->set_values (4,5);
    cout << ppoly1->area() << '\n';
    cout << ppoly2->area() << '\n';
    return 0;
    }

  11. (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).
    // Recursion
    #include <iostream>
    #include <iomanip>
    using std::cout;
    using std::setw;

    int C(int n, int m) {
        if (n == m || m == 0)
            return 1;
        else
            return C(n-1, m) + C(n-1, m-1);
    }

    int main()
    {
        const int K=6;
        for (int i=0; i<=K; ++i)
        {
            for (int j=0; j<=i; ++j)
                cout << setw(3) << C(i, j);
            cout << '\n';
        }
        return 0;
    }

  12. (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).

    // Substitution Cipher
    #include <iostream>
    #include <string>
    using std::string;
    using std::cout;

    class Message {
    private:
        string message;
        string create_mapping(string);
    public:
        Message(string s): message(s) {}

        string encrypt(string s) {
            string key = create_mapping(s);
            string result;
            int k;
            for (int i=0; i<message.length(); ++i) {
                k = message[i] - 65;
                result += key[k];
            }
            return result;
        }

        string decrypt(string s) {
            string key = create_mapping(s);
            string result;
            int k;
            for (int i=0; i<message.length(); ++i) {
                k = key.find( message.at(i) );
                result += static_cast<char>(65 + k);
            }
            return result;
        }
    };

    int main()
    {
        Message msg2("CYREYLNEUOMYROC");
        cout << msg2.decrypt("UNIVERSITY") << '\n';
        return 0;
    }

    string Message::create_mapping(string s)
    {
        string key;
        char c;
        for (int i=0; i<s.length(); ++i)
            if (key.find( c = s.at(i) ) == string::npos)
                key += c;
        for (c='A'; c<='Z'; ++c)
            if (key.find( c ) == string::npos)
                key += c;
        return key;
    }