精華區beta NTUCH-HW 關於我們 聯絡資訊
第一題:(10分) 寫出下列C++語法中keyword的定義或用途: 1. overload 當建立function或是class,使用已定義重複function名稱,會依照輸入不 同類型的變數,而對應到不同的function . 2. exception 是一個數值、變數,或是一個物件用來記錄程式的錯誤資訊,當錯誤發生 時,會回傳錯誤發生的位置或是情形       . 3. friend  定義 class scope以外的範圍,可以使用class private 變數的function 或是operator等            . 4. private 定義只有在此class內部才可以被讀寫的變數使用權限   .                           . 5. file stream  是一個傳遞資料的通道,連接儲存資料的設備與所要運算處理的程式                    . 第二題:(10分)   下列程式執行後的輸出為何? #include <iostream> #include <cmath> using namespace std; void main() { int count; float my_func(float b); for(count=1; count<=10; count++) { if(count % 2 == 0) cout << my_func(count)*my_func(count) << endl; } } float my_func(float x) { float y; y = sqrt(x); return(y); } 2 4 6 8 10 ─────────────────────────────────────── ──────────────────── 答案欄:(空間不夠時請寫於背面) 第三題:(10分)   下列程式執行後的輸出為何? #include <iostream> #include <string> using namespace std; int main() { string str = "This cannot be"; cout << "The original string is: " << str << endl << " and has " << int(str.length()) << " characters." << endl; // insert characters str.insert(4," I know"); cout << "The string, after insertion, is: " << str << endl << " and has " << int(str.length()) << " characters." << endl; // replace characters str.replace(12, 6, "to"); cout << "The string, after replacement, is: " << str << endl << " and has " << int(str.length()) << " characters." << endl; // append characters str = str + " correct"; cout << "The string, after appending, is: " << str << endl << " and has " << int(str.length()) << " characters." << endl; return 0; } The original string is: This cannot be and has 14 characters. The string, after insertion, is: This I know cannot be and has 21 characters. The string, after replacement, is: This I know to be and has 17 characters. The string, after appending, is: This I know to be correct and has 25 characters. ─────────────────────────────────────── ──────────────────── 答案欄:(空間不夠時請寫於背面) 第四題:(10分)   下列程式執行後的輸出為何? #include <iostream> using std::cout; using std::endl; class M { public: M( int ); int mystery( int ); private: int data; int number; }; M::M( int q = 0 ) { data = q; number = 2; } int M::mystery( int q ) { data += q; return data; } int main() { M mObject( 2 ); M *mPtr = &mObject; cout << mObject.mystery( 20 ) << endl; cout << mPtr->mystery( 30 ) << endl; cout << mObject.mystery( 40 ) << endl; cout << mPtr->mystery( 50 ) << endl; return 0; } 22 52 92 142 ─────────────────────────────────────── ──────────────────── 答案欄:(空間不夠時請寫於背面) 第五題:(10分) 已知A為MxL(M橫列L直行)之矩陣,B為LxN之矩陣(M,L,N<11),試設計一程式: 一、計算矩陣A與矩陣B之乘積C(C=AxB,矩陣C必為MxN)。 二、計算矩陣D、E之和存入矩陣F中(F=D+E,D、E、F矩陣為NxN矩陣。) 程式設計要點如下: 1.矩陣相乘與相加部分請以函式function的方式撰寫。 2.由主程式呼叫函式完成矩陣的相加或相乘工作,並將結果輸出到螢幕及檔案。 3.矩陣內之數值可以由鍵盤輸入或由檔案中讀取。 4.有關所有矩陣的資料、運算、輸入與輸出功能必須以物件的觀念整合成為一個叫Matrix 的類別中。 5.您所寫的程式應至少要能計算三組矩陣A,B相加與相乘之結果。 #include <iostream> #include <iomanip> #include <cmath> #include <fstream> #include <string> using namespace std; class Matrix { private: int **Answer; //答案矩陣 int **A; //矩陣A int **B; //矩陣B int M; //矩陣A的列數 int L; //矩陣A的行數 int N; //矩陣B的列數 int P; //矩陣B的行數 char* FileName; //存輸入的檔名 ifstream input; ofstream output; bool InputYes; public: void MatrixInput(); void MatrixOutput(); void MatrixAdd(); void MatrixMul(); void MatrixInputFromFile(); void MatrixOutputToFile(); }; int main() { Matrix MM; char a; char u = 'a'; while(1) { cout<<"請選擇(1)鍵盤輸入 (2)檔案輸入"<<endl; cin>>a; if (a=='1') { MM.MatrixInput(); do { cout<<"請選擇(1)加法 (2)乘法"<<endl; cin>>a; if (a=='1') MM.MatrixAdd(); else if (a=='2') MM.MatrixMul(); else cout<<"輸入錯誤!!"<<endl; }while(a!='1'&& a!='2'); } else if (a=='2') { MM.MatrixInputFromFile(); do { cout<<"請選擇(1)加法 (2)乘法"<<endl; cin>>a; if (a=='1') MM.MatrixAdd(); else if (a=='2') MM.MatrixMul(); else cout<<"輸入錯誤!!"<<endl; }while(a!='1'&& a!='2'); } else { cout<<"輸入錯誤!!"<<endl; } cout<<"請選擇(1)螢幕輸出 (2)檔案輸出"<<endl; cin>>a; if (a=='1') MM.MatrixOutput(); else if (a=='2') MM.MatrixOutputToFile(); else cout<<"輸入錯誤!!"<<endl; cout<<"按任意鍵繼續,輸入N結束程式"<<endl; cin>>a; if (a=='N' || a=='n') break; } return 0; } void Matrix::MatrixInput() { cout<< "Row (A Matrix)"<<endl; //輸入A矩陣行數 cin>>M; cout<< "Column (A Matrix)"<<endl; //輸入A矩陣列數 cin>>L; cout<< "Row (B Matrix)"<<endl; //輸入B矩陣行數 cin>>N; cout<< "Column (B Matrix)"<<endl; //輸入B矩陣行數 cin>>P; A = new int* [M]; //建立A矩陣 for (int i=0 ; i<M ; i++) A[i] = new int [L]; B = new int* [N]; //建立B矩陣 for (int i=0 ; i<N ; i++) B[i] = new int [P]; for (int i=0 ; i<M ; i++) //輸入A矩陣 for (int j=0 ; j<L ; j++) { cout<<"A["<< i <<"]"<<"["<<j<<"] ="<<endl; cin>>A[i][j]; } for (int i=0 ; i<N ; i++) //輸入B矩陣 for (int j=0 ; j<P ; j++) { cout<<"B["<< i <<"]"<<"["<<j<<"] ="<<endl; cin>>B[i][j]; } } void Matrix::MatrixOutput() { if (InputYes == true) { cout<<"計算出來的答案是"<<endl; for (int i=0 ; i<M ; i++) //輸出答案 { for (int j=0 ; j<P ; j++) cout<<Answer[i][j]<<" "; cout<<endl; } } for (int i=0 ; i<M ; i++) //釋放記憶體空間 delete Answer[i]; delete Answer; } void Matrix::MatrixAdd() { if (M!=N || L!=P) //判斷是否可執行此運算 { cout<<"此二矩陣無法相加"<<endl; InputYes = false; } else { InputYes = true; Answer = new int*[M]; //建立Answer矩陣 for (int i=0 ; i<M ; i++) Answer[i] = new int [L]; for (int i=0 ; i<M ; i++) //計算相加結果 for (int j=0 ; j<L ; j++) Answer[i][j] = A[i][j]+B[i][j]; } } void Matrix::MatrixMul() { if (L!=N) //判斷是否可執行此運算 { cout<<"此二矩陣無法相乘"<<endl; InputYes = false; } else { InputYes = true; Answer = new int*[M]; //建立Answer矩陣 for (int i=0 ; i<M ; i++) Answer[i] = new int [P]; for (int i=0 ; i<M ; i++)//計算相加結果 { for (int j=0 ; j<P ; j++) { Answer[i][j] = 0; for (int k=0 ;k<L ; k++) { Answer[i][j] = Answer[i][j] + A[i][k]*B[k][j]; } } } } } void Matrix::MatrixInputFromFile() { cout<<"請輸入檔案名稱"<<endl; FileName = new char[50]; //輸入檔案名稱的暫存空間 cin>>FileName; input.open(FileName); //打開檔案 input>>M>>L; //讀取行列數 A = new int* [M]; //建立A矩陣 for (int i=0 ; i<M ; i++) A[i] = new int [L]; for (int i=0 ; i<M ; i++) //輸入A矩陣 for (int j=0 ; j<L ; j++) input>>A[i][j]; input>>N>>P; B = new int* [N]; //建立B矩陣 for (int i=0 ; i<N ; i++) B[i] = new int [P]; for (int i=0 ; i<N ; i++) //輸入B矩陣 for (int j=0 ; j<P ; j++) { input>>B[i][j]; } input.close(); //關閉檔案 } void Matrix::MatrixOutputToFile() { if (InputYes == true) { output.open("Answer.txt"); for (int i=0 ; i<M ; i++) //輸出答案 { for (int j=0 ; j<P ; j++) { output<<Answer[i][j]<<" "; } output<<endl; } output.close(); //關閉檔案 } for (int i=0 ; i<M ; i++) //釋放記憶體空間 delete Answer[i]; delete Answer; } 第六題:(10分) 請撰寫一個程式,由文字檔案story.txt中將一篇英文文章的每一個字元讀入程式中, 同時累計a,e,i,o,u五個母音的總數,最後將統計結果輸出到檔案vowel.txt中。 HINT: 你可能會用到下列之C++指令 ofstream outFile( “vowel.txt”, ios::out ); ─────────────────────────────────────── ──────────────────── 答案欄:(空間不夠時請寫於背面) #include <iostream> #include <fstream> using namespace std; int main() { ifstream input("story.txt",ios::in); //開啟檔案 ofstream output("vowel.txt",ios::out); char a; //存抓進來的字元 int A = 0; //記錄每一格字元有多少的計數器 int E = 0; int I = 0; int O = 0; int U = 0; while(input.good()) //偵測檔案內文字是否結束 { a = (char) input.get(); //依序抓入每一個字元 switch(a) { case ('a'): A++; break; case ('e'): E++; break; case ('i'): I++; break; case ('o'): O++; break; case ('u'): U++; break; } } output<<"a = "<<A<<" e = "<<E<<" i = "<<I<<" o = "<<O<<" u = "<<U<<endl; //將結果存入檔案 input.close(); //關閉檔案 output.close(); system("pause"); return 0; } 第七題:(10分) 請設計一個函式名稱為ReverseString,其功能為將一個字串中的字元之順序前後顛倒 ,例如將”Hello”轉成”olleH”。同時設計一個主程式呼叫此函數,達到使用者由鍵盤 輸入任何一個字串後,均將其前後顛倒輸出於螢幕上。而當使用者輸入一個空白字串時表 示程式結束。 ─────────────────────────────────────── ──────────────────── 答案欄:(空間不夠時請寫於背面) #include <iostream> #include <string> using namespace std; string ReverseString(string s); int main() { while(1) { string str; //建立一個字串儲存輸入的字串 getline(cin,str); //輸入字串 if (str.empty() == true) //若是空字串,則結束程式 break; else cout<< ReverseString(str)<<endl ; //輸出顛倒的字串 } return 0; } string ReverseString(string s) { int i = s.length(); //紀錄字串的長度 string reverse; //建立字串儲存顛倒的字串 for (i=i-1; i>=0 ; i--) //將字串倒過來存 reverse = reverse +s[i]; return reverse; } 第八題:(10分) 請定義一個AddressBook類別,其功能為用來記錄與管理同學的通訊錄,在此類別中需要 的資料成員包括了:個人姓名(Name)、電話號碼(Phone)、地址(Address)、生日 (Birthday)四項。其中除了個人姓名為公開(public)資料外,其他三項為內部(private) 資料。AddressBook的成員函式包括了:成員資料的輸入(SetName, SetPhone, SetAddress, SetBirthday)以及同學個人整筆資料的輸出(PrintRecord)五個函式。 請依據此AddressBook類別設計一個程式,程式功能為允許使用者輸入同班同學(同學人 數為N人)的通訊錄資料,然後將通訊錄輸出到螢幕上。 ─────────────────────────────────── 答案欄:(空間不夠時請寫於背面) #include "stdafx.h" #include "iostream" #include "string" using namespace std; class AddressBook{ private: string Phone; string Address; string Birthday; public: string Name; void SetName(){ cout << "Name:" ; getline(cin,Name); } void SetPhone(){ cout << "Phone:" ; getline(cin,Phone); } void SetAddress(){ cout << "Address:" ; getline(cin,Address); } void SetBirthday(){ cout << "Birthday:" ; getline(cin,Birthday); } void PrintRecord(){ cout << "============================" << endl; cout << "Name:" << Name << endl; cout << "Phone:" << Phone << endl; cout << "Address:" << Address << endl; cout << "Birthday:" << Birthday << endl; } }; int _tmain(int argc, _TCHAR* argv[]) { int N; string temp; cout << "How many people in your class? "; //================================================== //欲輸入N筆資料 //如用cin >> N;的方式會有小bug //不扣分 getline(cin,temp); N = atoi(temp.c_str()); //================================================== cout << endl; AddressBook *MyAddressBook = new AddressBook[N]; //動態產生N個class for (int i=0; i<N; i++){ cout << "Classmate No." << i+1 << endl; MyAddressBook[i].SetName(); MyAddressBook[i].SetPhone(); MyAddressBook[i].SetAddress(); MyAddressBook[i].SetBirthday(); cout << endl; } for (int i=0; i<N; i++){ MyAddressBook[i].PrintRecord(); } system("pause"); } 第九題:(10分) Create a base class named Rectangle containing length and width data members. From this class, derive a class named Box with another data member named depth. The member functions of the base Rectangle class should consist of a constructor and an area() function. The derived Box class should have a constructor, a volume() function, and an override function named area() that returns the surface area of the box. ─────────────────────────────────────── ──────────────────── 答案欄:(空間不夠時請寫於背面) #include "stdafx.h" #include <iostream> using namespace std; class rectangle { private: int height; int width; public: /*constructors*/ rectangle() { height = 0; width = 0; }; rectangle(int _height, int _width) { height = _height; width = _width; }; /*methods*/ void set_rec(int _height, int _width) { height = _height; width = _width; } int v_width() { return width; } int v_height() { return height; } int area() { return height*width; }; }; class box : public rectangle { private: int depth; rectangle rec; public: /*constructors*/ box() { depth = 0; } box(int _height, int _width, int _depth) { rec.set_rec(_height, _width); depth = _depth; }; /*methods*/ int area() { rectangle rec_w_h( rec.v_height(), rec.v_width() ); rectangle rec_w_d( rec.v_width(), depth); rectangle rec_h_d( rec.v_height(), depth); int surface_area = rec_w_h.area() + rec_w_d.area() + rec_h_d.area(); return 2 * surface_area; }; int volume() { int vol = rec.v_height() * rec.v_width() * depth; return vol; }; }; int _tmain(int argc, _TCHAR* argv[]) { box bb(2,3,4); cout<<bb.area()<<endl; cout<<bb.volume()<<endl; system("pause"); return 0; } 第十題:(10分) 請設計一個程式模擬醉漢走路的結果。假設醉漢的行走只有前、後、左、右、左前、右前 、左後、右後八個方向,而往八個方向行進的機率都一樣。令醉漢初始的位置座標為 (x,y)=(0,0),請利用程式模擬算出醉漢行走10000步以後,他所在的座標位置。 ─────────────────────────────────── 答案欄:(空間不夠時請寫於背面) #include "stdafx.h" #include "iostream" #include "string" #include "ctime" using namespace std; int _tmain(int argc, _TCHAR* argv[]) { srand((int)time(0)); int x = 0; //x座標 int y = 0; //y座標 int temp_x = 0; //x位移量 int temp_y = 0; //y位移量 for (int i=0; i<10000; i++){ temp_y = rand()%3-1 ; //亂數決定x位移量 temp_x = rand()%3-1 ; //亂數決定y位移量 while(temp_x == 0 && temp_y == 0){ //去除x,y位移量皆為的情況 temp_y = rand()%3-1 ; temp_x = rand()%3-1 ; } x += temp_x; //將x座標加上位移量 y += temp_y; //將x座標加上位移量 } cout << "The drunk's final position is at ( " << x << " , " << y << " )" << endl; system("pause"); } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.7.59