精華區beta NTU-Exam 關於我們 聯絡資訊
課程名稱︰計算機程式 課程性質︰學習C++程式設計 課程教師︰魏宏宇 開課系所︰物理學系 考試時間︰94/11/08 試題 : Fall 2005 Computer Programming Midterm Exam Please write your C++ with “proper styles”. I. Briefly explain what are structured programming language and object-oriented programming language. Give examples for these two types of programming languages (6%) II. Briefly explain what are compiler and linker. When you write your C++ program hw.cpp with standard input and output commands, explain how executable application hw.exe is created. (9%) III. Write C++ statement(s) to do the following tasks: (Assume we already have using statement) (15%) A. Declare variables x, y, and z. They are integers. B. Compare x and y and display the smaller variable. C. Compute z = x^y IV. Describe the memory operations of variables a,b,c, and d. After executing each line of the code, fill in the values of each member space. (Fill in “–“ if the memory space is not existed. Fill in “?” if the value of the memory space is unknown) (15% 錯一格扣一分,扣完為止)   a b c d Ln1 Ln1 int a,b; Ln2 Ln2 int c=2,d=7; Ln3 Ln3 a=c++ + --d; Ln4 Ln4 b=d%c; Ln5 Ln5 c*=a+1; V. Write a program to display odd numbers from 1 to 31 (1, 3, 5, 7, ……29, 31) with the following techniques. (15%) A. while B. do… while C. for VI. The following codes are a part of a program. This program has 2 variables (1) an integer input number x (2) total number of iterations run_time. First of all, the program will prompt users to input run_time value. In each iteration, the program will prompt the user to input an integer number x. If x=0, the program will display INPUT IS ZERO. If x<0, the program will display INPUT IS NEGATIVE. If x is an even number greater than 0, the program will display \\. If x is an odd number greater than 0, the program will display ///. (15%) …(some source codes)…… int run_time; //number of execution int x; //variable x [Please fill in your source codes here] …………. VII. Create a class named “rectangular”(長方形). There are 2 data members (1) width , and (2) length and 2 member functions (1) Compute perimeter(週長) and (2) Compute the area size(面積) of the rectangular. When a rectangular object is created, set its width and length with constructor. (25%) A. Define the interface of the class in rectangular.h B. Implement the class in rectangular.cpp C. Write a main program (main.cpp) that prompts users to input width and length of a rectangular. Then program will compute and display the area size and perimeter of the rectangular. -----------------------------------解答----------------------------------- I. Structured programming: use function/procedure and avoid goto. (e.g. C) OOP: use the concept of object/class to model software components as real-world objects. (e.g. C++) II. * Complier translates high-level language (e.g. C++ source codes) into machine language (object code) * Linker combines object codes and creates executable application. ┌─────────┐ │ hw.cpp │ └─────────┘ ↓ ┌─────────┐ │ compiler │ └─────────┘ ↓ ┌──────────┐ ┌─────────┐ │C++ standard library│ │ object code │ │ object code │ │ from hw.cpp │ └────┬─────┘ └────┬────┘ │ │ └──────┐ ┌───────┘ ↓ ↓ ┌─────────┐ │ linker │ └─────────┘ ↓ ┌─────────┐ │ executable │ └─────────┘ III. A. int x,y,z; B. if (x<y) z=x; else z=y; cout << “smaller number is ” << z; C. #include <cmath> …. z=pow(x,y); IV.   a b c d Ln1 ? ? - - Ln1 int a,b; Ln2 ? ? 2 7 Ln2 int c=2,d=7; Ln3 8 ? 3 6 Ln3 a=c++ + --d; Ln4 8 0 3 6 Ln4 b=d%c; Ln5 8 0 27 6 Ln5 c*=a+1; V. A. int x; int max_value=31; //while x=1; while (x<=max_value) { cout << x << " "; x+=2; } cout << endl; B. x=1; do { cout << x << " "; x+=2; }while (x<=max_value); cout << endl; C. for (x=1; x<=31;x+=2) { cout << x << " "; } VI. int run_time; //number of execution int x; //variable x int i; cout << "Please enter number of iteration "; cin >> run_time; for (i=1; i<=run_time;i++) { cout << "Please enter x "; cin >> x; if (x<0) { cout << "INPUT IS NEGATIVE"<<endl; } else if (x==0) { cout << "INPUT IS ZERO" <<endl; } else { if (x%2) { cout << "///" <<endl; } else { cout << "\\\\" <<endl; } } } VII. A. //rectangular.h class Rectangular { public: Rectangular(double, double); double computeArea(); double computePerimeter(); private: double width; double length; }; B. // rectangular.cpp #include "rectangular.h" Rectangular::Rectangular(double w, double l) { width=w; length=l; } double Rectangular::computeArea() { double area_size; area_size=width*length; return area_size; } double Rectangular::computePerimeter() { double perimeter; perimeter=2*(width+length); return perimeter; } C. #include <iostream> using std::cin; using std::cout; using std::endl; #include "rectangular.h" int main() { double input_length, input_width; cout << "Enter width and length of a rectangular" <<endl; cin >> input_width >>input_length; Rectangular my_rect(input_width,input_length); cout << my_rect.computeArea() << endl; cout << my_rect.computePerimeter() << endl; return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.239.232