國立屏東商業技術學院 資訊工程系 物件導向程式設計
turnin code cpp.hw2
due date: 23:59, March 27
考慮以下兩個C++標頭檔:
#define LenFN 20 #define LenLN 10 #ifndef STRUCT_NAME #define STRUCT_NAME struct Name { char firstname[LenFN+1]; char lastname[LenLN+1]; }; Name *setName(const char *name); #endif
#include "name.h" char *initial(Name *n);
以及下面這個main1.cpp程式:
using namespace std; #include <iostream> #include <cstring> #include "name.h" #include "initial.h" int main() { Name *someone = new Name; char *name = new char[LenFN+LenLN+1]; cout << "Please input your name: "; for(int i=0;i<(LenFN+LenLN);i++) cout << "_"; for(int i=0;i<(LenFN+LenLN);i++) cout << "\b"; cin.getline(name, LenFN+LenLN); someone = setName(name); cout << "Your first name is " << someone->firstname << "." << endl; cout << "Your last name is " << someone->lastname << "." << endl; cout << "The initial of your name is " << initial(someone) << endl; return 0; }
using namespace std; #include <iostream> #include <cstring> #include "name.h" Name *setName(const char *name) { Name *somebody = new Name; ... return somebody; }
using namespace std; #include <iostream> #include <cstring> #include "name.h" char *initial(Name *n) { char *name = new char[LenFN+LenLN+1]; .... return name; }
注意:你只須turnin 「name.cpp」與「initial.cpp」這兩個程式!我們會使用以下的方式,編譯你的程式:
g++ -c name.cpp g++ -c initial.cpp g++ main1.cpp name.o initial.o
[02:16 junwu@ws hw2]$ ./a.out Please input your name: Yo-Yo Ma______________________ Your first name is Yo-Yo. Your last name is Ma. The initial of your name is Ma, Y.-Y. [02:16 junwu@ws hw2]$
本題所需之檔案,可至ws.csie.npic.edu.tw上取得,其所在目錄為/home/stu/public/cpp/2014SCPPHW2 。
g++ -c name.cpp g++ member.cpp name.o
[02:16 junwu@ws hw2]$ ./a.out < members.dat Input: Wu Jun (0912345678) G/0.8 Input: Lin Member not found! Input: Liu Ming-Chung (0988123456) S/100 Input: Kuo Cho-Ming (0999999999) S/80 Ping (0955333222) G/0.9 Input: Quit Bye [02:16 junwu@ws hw2]$
本題所需之檔案,可至ws.csie.npic.edu.tw上取得,其所在目錄為/home/stu/public/cpp/2014SCPPHW2 。