國立屏東大學 資訊工程系 物件導向程式設計
turnin code cpp.2A.hw3-1(週五上午,二甲) , cpp.2B.hw3-1(週五下午,二乙)
#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);
以及下面這個main.cpp程式:
#include <iostream> using namespace std; #include <cstring> #include "name.h" #include "initial.h" int main() { Name *someone; char *name = new char[LenFN+LenLN+1]; cout << "Please input your name: "; 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 <string> #include "name.h" Name *setName(const char *name) { Name *somebody = new Name; ... return somebody; }
至於initial.cpp則是負責initial()函式的實作,用以接收一個Name的結構體,並傳回一個字串:
注意:你只須turnin 「name.cpp」與「initial.cpp」這兩個程式!我們會使用以下的方式,編譯你的程式:
g++ -c name.cpp g++ -c initial.cpp g++ main.cpp name.o initial.o
[02:16 junwu@ws hw1]$ ./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 hw1]$ ./a.out Please input your name: Wu, Jun Your first name is Jun. Your last name is Wu. The initial of your name is Wu, J. [02:16 junwu@ws hw1]$
本題所需之檔案,可至ws.csie2.nptu.edu.tw上取得,其所在目錄為/home/stu/public/cpp2022/hw3-1 。
turnin code cpp.2A.hw3-2(週五上午,二甲) , cpp.2B.hw3-2(週五下午,二乙)
g++ member.cpp
[02:16 junwu@ws hw]$ ./a.out Name:Jun Wu Phone:0999111111 Type(g/s):g Discount:0.95 Name:Yo Yo Ma Phone:0999222111 Type(g/s):s Free Minutes:100 Name:Nathan Ho Phone:0999222333 Type(g/s):g Discount:0.9 Name:Alice Liu Phone:0999444444 Type(g/s):s Discount:200 Name:quit Input a name:Nathan Ho Nathan Ho(0999222333) G/0.9 Input a name:Alice Liu Alice Liu(0999444444) S/200 Input a name:Thomas Lin Member not found! Input: quit Bye [02:16 junwu@ws hw3]$