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

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

8  
9  
10

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

    // Range of Values (P.45)
    //
    Overflow (P.125)
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
        signed short n = 32767;
        n *= 3;
        cout << n << endl;

        unsigned char c = 'Z';
        c = c * 4;
        cout << c << endl;
        return 0;
    }


  2. (10%) What will be the output of the following program?
    // continue (P.116) vs. break (P.115)
    #include <iostream>

    int main()
    {
        const int K = 6;
        int count = 0;
        int i, j;
        for (i=1; i<K; i++)
        {
            if (i==5) continue;
            ++count;
        }

        for (j=1; j<K; j++)
        {
            if (4 == j) break;
            ++count;
        }
        std::cout << count << '\t' << i << '\t' << j << std::endl;
        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).

    // Pass by Reference (P.196)
    #include <iostream>
    using std::cout;

    void swap1(int a, int b)
    {
      int temp;
      temp = a; a = b; b = temp;
    }

    void swap2(int* a, int* b)
    {
      int temp;
      temp = *a; *a = *b; *b = temp;
    }

    void swap3(int& a, int b)
    {
      int temp;
      temp = a; a = b; b = temp;
    }

    void print(int a, int b)
    {
        cout << a << '\t' << b << '\n';
    }

    int main()
    {
        int x = 6;
        int y = 20;
        swap1( x,  y); print(x, y);
        swap2(&x, &y); print(x, y);
        swap3(x, y); print(x, y);
        return 0;
    }

  4. (10%) What will be the output of the following program?
    // Transposition Cipher - Scytale
    // The input key specifies the diameter of the scytale

    #include <iostream>
    #include <cstring>
    #include <cmath>
    using std::cout;
    using std::endl;

    #define MAX_WIDTH 9
    #define MAX_MESSAGE_LENGTH 200

    void decrypt(char* ciphertext, char* plaintext, int key);

    int main()
    {
    char matrix[MAX_WIDTH * MAX_WIDTH + 1];
    char plaintext[ MAX_MESSAGE_LENGTH];

    char ciphertext[ MAX_MESSAGE_LENGTH ] =
    "H crfaoptsrcwr eok oylmettof roeu hs";
    char *p = plaintext;
    unsigned key = 6;

    decrypt(ciphertext, plaintext, key);
    std::cout << plaintext << '\n';
    return 0;
    }

    void decrypt(char* ciphertext, char* plaintext, int height)
    {
    char* pc = plaintext;
    int i, j, offset = 0;
    int length = strlen(ciphertext);
    int width = ceil(1.0 * length / height);

    for (j=0; j<width; j++)
    for (i=0; i<height; i++)
    {
    plaintext[i * width + j] = ciphertext[i + height * j] ;
    }
    plaintext[ width * height ] = '\0';
    }
  5. (10%) What will be the output of the following program?

    // Recursive Function (P.214)
    #include <iostream>
    using std::cout;
    using std::endl;

    void f(int n)
    {
        if (n >= 16)
            f(n / 16);

        n %= 16;
        if (n < 10)
            cout << n;
        else {
            char c = 'A' + (n - 10);
            cout << c;
        }
    }

    int main()
    {
        f(620);
        f(65534);
        cout << endl;
        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).

    // Accessing Class Members (P.299)
    #include <iostream>
    using std::cout;
    using std::endl;

    int gcd(int a, int b)
    {
        if (b == 0)
            return a;
        else
            return gcd(b, a % b);
    }

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

        void Print()
        {
            this->Reduce();
            cout << numerator << '/' << denominator << endl;
        }

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

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

    int main()
    {
        CRational a(96, 72); a.Print();
        CRational b(49, 25); b.Print();
        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).

    // Destructors and Dynamic Memory Allocation (P.326)
    #include <iostream>
    class CData
    {
        public:
            int* pdata;

        CData(int n=0)
        {
           pdata = new(int);
           *pdata = n;
           std::cout << "Constructor called with initial value "
                << n << std::endl;
        }

        ~CData()
        {
           std::cout << "Destructor called to release the memory storing "
                    << *pdata << std::endl;
           delete pdata;
        }
    };


    int main()
    {
        CData* py = new CData(2017);
        CData  m(6);
        CData* pd = new CData(20);
        delete py;
        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).

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

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

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

    CNode* CNode::previous = NULL;

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

    for (n=0, p=d.pLastNode; p!=NULL; p=p->previous)
    n++;
    cout << n << endl;

    return 0;
    }

  9. (10%) If we replace the OnDraw() function in P.669 as below, what result will be shown on the screen after you run the program?
    // CView::OnDraw()  (P.669)
    void CSketcherView::OnDraw(CDC* pDC)
    {
    CSketcherDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
    return;

    const float R = 200.0f;
    const int x0 = R, y0 = R;
    int degree;
    float theta;
    const CPoint origin = CPoint(x0, y0);
    CPoint points[12];
    for (int i=0; i<12; i++) {
    degree = 30 * i;
    theta = degree * M_PI / 180;
    points[i] = CPoint(R*cos(theta), R*sin(theta));
    }
    pDC->MoveTo( points[0] + origin );
    for (int i=1; i<=12; i++) {
    pDC->LineTo( points[5*i % 12] + origin );
    }
    }
  10. (10%) We want to
        develop an MFC program which allows the user to input a message and
        then print out the encrypted Caesar cipher.
    [Caesar Cipher]

    However, the following code cannot be compiled successfully.  Please point out which line of the code must be revised, and how should it be revised.
    // GetWindowText()
    CString encrypt(CString s, int key)
    {
    CString result = _T("");
    char c;
    for (int i=0; i<s.GetLength(); i++)
    {
    c = s.GetAt(i) + key;
    result = result + c;
    }
    return result;
    }

    void CF10View::OnBnClickedButton1()
    {
    CString message, cipher;
    message = GetDlgItem(IDC_TXT_MESSAGE)->GetWindowText( );
    cipher = encrypt(message, 3);
    GetDlgItem(IDC_TXT_CIPHER)->SetWindowText( cipher );
    }