test
1
/**/
/* *
2 * 0318作業
3 * 學號:CAW4417
4 * 姓名:黃獻葦
5 *
6 * 第1本第2本不可指定加入位置,第3本後可指定加入哪本書籍編號之後
7 *
8 */
9 #include < iostream >
10 #include < string >
11 #include < sstream >
12
13 using namespace std;
14
15 struct Book {
16 char m_szName[ 20 ]; // 書名
17 char m_szAuthor[ 20 ]; // 作者
18 int m_iISBN; // ISBN
19 int m_iPrice; // 價格
20 Book * m_pNextBook;
21 } ;
22
23 enum {
24 ADD_NEW_BOOK_ACTION = 1 ,
25 DELETE_BOOK_ACTION = 2 ,
26 SEARCH_BOOK_ACTION = 3 ,
27 LIST_BOOK_ACTION = 4 ,
28 EXIT = 5
29 } ;
30
31 enum {
32 NAME_CONDITION = 1 ,
33 AUTHOR_CONDITION = 2 ,
34 ISBN_CONDITION = 3 ,
35 PRICE_CONDITION = 4
36 } ;
37
38 class LinkList {
39 public :
40 Book * m_pHeader; // 記錄第一本書的位置
41 int m_iAmount; // 加入此LinkList的書籍數量
42 void addBook();
43 void deleteBook();
44 void searchBook();
45 void listAllBook();
46 void searchBookCondition( int condition, string str);
47 void searchBookCondition( int condition, int number);
48 } ;
49
50 void getUserInputInt( int * p); // 取得user輸入的數字選項
51 void getUserInputIntBookInfo(Book * book); // 取得user輸入的書籍資訊
52 void showTitle();
53 string int2str( int i);
54
55 /**/ /*
56 *加入書籍
57 *第1本第2本不可指定加入位置,第3本後可指定加入哪本書籍編號之後
58 */
59 void LinkList::addBook() {
60
61 int l_iInput = 0 ;
62 if ( m_iAmount >= 2 ) { // 加入第3本書以後才問
63 cout << " 請問要加在第幾本書後面: " ;
64 getUserInputInt( & l_iInput);
65 }
66
67
68 if ( m_iAmount < 2 ) { // 加入第1本or第2本書
69 Book * l_pNewBook = new Book();
70 getUserInputIntBookInfo(l_pNewBook);
71
72 l_pNewBook -> m_pNextBook = NULL;
73 if ( 0 == m_iAmount ) {
74 m_pHeader = l_pNewBook;
75 }
76 else if ( 1 == m_iAmount ) {
77 m_pHeader -> m_pNextBook = l_pNewBook;
78 }
79
80 }
81 else if ( (m_iAmount >= 2 ) && (l_iInput <= m_iAmount ) ) { // 選擇加入在目前書籍中哪一本之後
82 Book * l_pNewBook = new Book();
83 getUserInputIntBookInfo(l_pNewBook);
84
85 Book * l_pAddAfter = m_pHeader;
86 int l_i = 0 ;
87 l_iInput -- ;
88 while (l_iInput > l_i) {
89 l_pAddAfter = l_pAddAfter -> m_pNextBook;
90 l_i ++ ;
91 }
92 Book * l_pNextBook = l_pAddAfter -> m_pNextBook;
93 l_pAddAfter -> m_pNextBook = l_pNewBook;
94 l_pNewBook -> m_pNextBook = l_pNextBook;
95
96 }
97 else if (l_iInput > m_iAmount) { // 超過目前書籍量
98 cout << " 目前只有 " << m_iAmount << " 本書 " << endl;
99 return ;
100 }
101 m_iAmount ++ ;
102 }
103
104 /**/ /*
105 *刪除指定編號之書籍
106 */
107 void LinkList::deleteBook() {
108 if ( 0 == m_iAmount ) {
109 cout << " 裡面沒有書可以刪除 " << endl;
110 return ;
111 }
112
113 cout << " 請輸入要刪除第幾本書 " ;
114 int l_iInput = 0 ;
115 getUserInputInt( & l_iInput);
116
117 if ( l_iInput > m_iAmount ) {
118 cout << " 錯誤!目前無此編號之書籍!! " << endl;
119 return ;
120 }
121 l_iInput -- ;
122
123 Book * l_mDeleteBook = m_pHeader;
124
125 if ( 0 == l_iInput ) { // 刪除第一本書
126 m_pHeader = m_pHeader -> m_pNextBook;
127 delete l_mDeleteBook;
128 }
129 else {
130 Book * l_pPreBook;
131 int l_i = 0 ;
132 while (l_iInput > l_i) {
133 l_pPreBook = l_mDeleteBook;
134 l_mDeleteBook = l_mDeleteBook -> m_pNextBook;
135 l_i ++ ;
136 }
137 l_pPreBook -> m_pNextBook = l_mDeleteBook -> m_pNextBook; // 前一本書指向下一本
138 delete l_mDeleteBook;
139 }
140
141 m_iAmount -- ;
142 }
143
144 /**/ /*
145 *尋找指定之書籍
146 */
147 void LinkList::searchBook() {
148
149 if (m_iAmount < 1 ) {
150 cout << " 裡面沒有書可以查詢!! " << endl;
151 return ;
152 }
153
154 cout << " 請輸入要查詢的條件 1-書名,2-作者,3-ISBN,4-價格 : " ;
155
156 int l_iSearchCondition = 0 ;
157 getUserInputInt( & l_iSearchCondition);
158
159
160 switch (l_iSearchCondition) {
161 case NAME_CONDITION:
162 case AUTHOR_CONDITION: {
163 string str;
164 cout << " 請輸入欲查詢的條件: " ;
165 cin >> str;
166 searchBookCondition(l_iSearchCondition,str);
167 break ;
168 }
169 case ISBN_CONDITION:
170 case PRICE_CONDITION: {
171 int l_iUserInput = 0 ;
172 cout << " 請輸入欲查詢的條件: " ;
173 getUserInputInt( & l_iUserInput);
174 searchBookCondition(l_iSearchCondition,l_iUserInput);
175 break ;
176 }
177 default :
178 cout << " 請輸入正確選項 " << endl;
179 }
180
181 }
182
183
184 /**/ /*
185 *用書名或作者查
186 */
187 void LinkList::searchBookCondition( int condition, string str) {
188 cout << " 用書名或作者查 " << endl;
189 Book * l_pCurrentBook = m_pHeader;
190 string l_szMessage ;
191
192 if ( condition == NAME_CONDITION ) { // 查書名
193 for ( int i = 0 ; i < m_iAmount ; i ++ ) {
194 if ( str == l_pCurrentBook -> m_szName ) {
195 if ( 0 == l_szMessage.size() ) {
196 l_szMessage += " 搜尋結果如下:\n " ;
197 }
198 l_szMessage += " 第 " ;
199 l_szMessage += int2str(i + 1 );
200 l_szMessage += " 本書==>書名: " ;
201 l_szMessage += l_pCurrentBook -> m_szName;
202 l_szMessage += " 作者: " ;
203 l_szMessage += l_pCurrentBook -> m_szAuthor;
204 l_szMessage += " ISBN: " ;
205 l_szMessage += int2str(l_pCurrentBook -> m_iISBN);
206 l_szMessage += " 價格: " ;
207 l_szMessage += int2str(l_pCurrentBook -> m_iPrice);
208 l_szMessage += " \n " ;
209 }
210 l_pCurrentBook = l_pCurrentBook -> m_pNextBook;
211 } // end of for
212 }
213 else if ( condition == AUTHOR_CONDITION ) { // 查作者
214 for ( int i = 0 ; i < m_iAmount ; i ++ ) {
215 if ( str == l_pCurrentBook -> m_szAuthor ) {
216 if ( 0 == l_szMessage.size() ) {
217 l_szMessage += " 搜尋結果如下:\n " ;
218 }
219 l_szMessage += " 第 " ;
220 l_szMessage += int2str(i + 1 );
221 l_szMessage += " 本書==>書名: " ;
222 l_szMessage += l_pCurrentBook -> m_szName;
223 l_szMessage += " 作者: " ;
224 l_szMessage += l_pCurrentBook -> m_szAuthor;
225 l_szMessage += " ISBN: " ;
226 l_szMessage += int2str(l_pCurrentBook -> m_iISBN);
227 l_szMessage += " 價格: " ;
228 l_szMessage += int2str(l_pCurrentBook -> m_iPrice);
229 l_szMessage += " \n " ;
230 }
231 l_pCurrentBook = l_pCurrentBook -> m_pNextBook;
232 } // end of for
233 }
234
235 if ( 0 == l_szMessage.size() ) {
236 l_szMessage += " 你的搜尋無結果!!!!! " ;
237 }
238 cout << l_szMessage << endl;
239 }
240
241 /**/ /*
242 *用ISBN或價格查
243 */
244 void LinkList::searchBookCondition( int condition, int number) {
245 Book * l_pCurrentBook = m_pHeader;
246 string l_szMessage ;
247
248 if ( condition == ISBN_CONDITION ) { // 查ISBN
249 for ( int i = 0 ; i < m_iAmount ; i ++ ) {
250 if ( number == l_pCurrentBook -> m_iISBN ) {
251 if ( 0 == l_szMessage.size() ) {
252 l_szMessage += " 搜尋結果如下:\n " ;
253 }
254 l_szMessage += " 第 " ;
255 l_szMessage += int2str(i + 1 );
256 l_szMessage += " 本書==>書名: " ;
257 l_szMessage += l_pCurrentBook -> m_szName;
258 l_szMessage += " 作者: " ;
259 l_szMessage += l_pCurrentBook -> m_szAuthor;
260 l_szMessage += " ISBN: " ;
261 l_szMessage += int2str(l_pCurrentBook -> m_iISBN);
262 l_szMessage += " 價格: " ;
263 l_szMessage += int2str(l_pCurrentBook -> m_iPrice);
264 l_szMessage += " \n " ;
265 }
266 l_pCurrentBook = l_pCurrentBook -> m_pNextBook;
267 } // end of for
268 }
269 else if ( condition == PRICE_CONDITION ) { // 查售價
270 for ( int i = 0 ; i < m_iAmount ; i ++ ) {
271 if ( number == l_pCurrentBook -> m_iPrice ) {
272 if ( 0 == l_szMessage.size() ) {
273 l_szMessage += " 搜尋結果如下:\n " ;
274 }
275 l_szMessage += " 第 " ;
276 l_szMessage += int2str(i + 1 );
277 l_szMessage += " 本書==>書名: " ;
278 l_szMessage += l_pCurrentBook -> m_szName;
279 l_szMessage += " 作者: " ;
280 l_szMessage += l_pCurrentBook -> m_szAuthor;
281 l_szMessage += " ISBN: " ;
282 l_szMessage += int2str(l_pCurrentBook -> m_iISBN);
283 l_szMessage += " 價格: " ;
284 l_szMessage += int2str(l_pCurrentBook -> m_iPrice);
285 l_szMessage += " \n " ;
286 }
287 l_pCurrentBook = l_pCurrentBook -> m_pNextBook;
288 } // end of for
289 }
290
291 if ( 0 == l_szMessage.size() ) {
292 l_szMessage += " 你的搜尋無結果!!!!! " ;
293 }
294 cout << l_szMessage << endl;
295
296 }
297
298
299
300 /**/ /*
301 *列出所有書籍
302 */
303 void LinkList::listAllBook() {
304 if ( NULL == m_pHeader ) {
305 cout << " 沒有書!! " << endl;
306 return ;
307 }
308
309 cout << " 目前共有: " << m_iAmount << " 本書,所有書籍清單如下: " << endl;
310 Book * l_pTempBook = m_pHeader;
311 while ( NULL != l_pTempBook ) {
312 cout << " 書名: " << l_pTempBook -> m_szName;
313 cout << " 作者: " << l_pTempBook -> m_szAuthor;
314 cout << " ISBN: " << l_pTempBook -> m_iISBN;
315 cout << " 價格: " << l_pTempBook -> m_iPrice << endl;
316 cout << " ******************************* " << endl;
317 l_pTempBook = l_pTempBook -> m_pNextBook;
318 }
319
320 }
321
322 /**/ /*
323 *取得user輸入
324 */
325 bool getUserInputInt( int * p) {
326 cin >> * p;
327 if ( ! cin ) {
328 cerr << " 錯誤!! " << endl;
329 return false ;
330 }
331 }
332
333
334 /**/ /*
335 *取得user輸入的書籍資訊
336 */
337 void getUserInputIntBookInfo(Book * book) {
338 cout << " 請輸入書名: " ;
339 cin >> book -> m_szName;
340 cout << " 請輸入作者: " ;
341 cin >> book -> m_szAuthor;
342 cout << " 請輸入ISBN: " ;
343 cin >> book -> m_iISBN;
344 cout << " 請輸入價格: " ;
345 cin >> book -> m_iPrice;
346 }
347
348
349 /**/ /*
350 *show出工作項目
351 */
352 void showTitle() {
353 cout << " ################## " << endl;
354 cout << " 1)add a new book # " << endl;
355 cout << " 2)delete a book # " << endl;
356 cout << " 3)search a book # " << endl;
357 cout << " 4)list all book # " << endl;
358 cout << " 5)exit # " << endl;
359 cout << " ################## " << endl;
360 cout << " 請輸入: " ;
361 }
362
363 /**/ /*
364 *轉換數字為字串
365 */
366 string int2str( int i) {
367 string s;
368 stringstream ss(s);
369 ss << i;
370 return ss.str();
371 }
372
373 int main() {
374
375 LinkList * l_firstLinkList = new LinkList();
376
377 while ( true ) {
378 showTitle();
379 int input = 0 ; // user input
380 getUserInputInt( & input );
381
382 /**/ /*
383 if( ADD_NEW_BOOK_ACTION != input &&
384 DELETE_BOOK_ACTION != input &&
385 SEARCH_BOOK_ACTION != input &&
386 LIST_BOOK_ACTION != input ){
387 break;
388 }
389 */
390
391 if ( EXIT == input ) {
392 break ;
393 }
394
395 switch (input) {
396 case ADD_NEW_BOOK_ACTION: {
397 l_firstLinkList -> addBook();
398 break ;
399 }
400 case DELETE_BOOK_ACTION: {
401 l_firstLinkList -> deleteBook();
402 break ;
403 }
404 case SEARCH_BOOK_ACTION: {
405 l_firstLinkList -> searchBook();
406 break ;
407 }
408 case LIST_BOOK_ACTION: {
409 l_firstLinkList -> listAllBook();
410 break ;
411 }
412 default :
413 cout << " 請輸入正確選項!! " << endl;
414 } // end switch
415
416
417
418 } // end while
419
420 system( " pause " );
421 return 0 ;
422 }
423
2 * 0318作業
3 * 學號:CAW4417
4 * 姓名:黃獻葦
5 *
6 * 第1本第2本不可指定加入位置,第3本後可指定加入哪本書籍編號之後
7 *
8 */
9 #include < iostream >
10 #include < string >
11 #include < sstream >
12
13 using namespace std;
14
15 struct Book {
16 char m_szName[ 20 ]; // 書名
17 char m_szAuthor[ 20 ]; // 作者
18 int m_iISBN; // ISBN
19 int m_iPrice; // 價格
20 Book * m_pNextBook;
21 } ;
22
23 enum {
24 ADD_NEW_BOOK_ACTION = 1 ,
25 DELETE_BOOK_ACTION = 2 ,
26 SEARCH_BOOK_ACTION = 3 ,
27 LIST_BOOK_ACTION = 4 ,
28 EXIT = 5
29 } ;
30
31 enum {
32 NAME_CONDITION = 1 ,
33 AUTHOR_CONDITION = 2 ,
34 ISBN_CONDITION = 3 ,
35 PRICE_CONDITION = 4
36 } ;
37
38 class LinkList {
39 public :
40 Book * m_pHeader; // 記錄第一本書的位置
41 int m_iAmount; // 加入此LinkList的書籍數量
42 void addBook();
43 void deleteBook();
44 void searchBook();
45 void listAllBook();
46 void searchBookCondition( int condition, string str);
47 void searchBookCondition( int condition, int number);
48 } ;
49
50 void getUserInputInt( int * p); // 取得user輸入的數字選項
51 void getUserInputIntBookInfo(Book * book); // 取得user輸入的書籍資訊
52 void showTitle();
53 string int2str( int i);
54
55 /**/ /*
56 *加入書籍
57 *第1本第2本不可指定加入位置,第3本後可指定加入哪本書籍編號之後
58 */
59 void LinkList::addBook() {
60
61 int l_iInput = 0 ;
62 if ( m_iAmount >= 2 ) { // 加入第3本書以後才問
63 cout << " 請問要加在第幾本書後面: " ;
64 getUserInputInt( & l_iInput);
65 }
66
67
68 if ( m_iAmount < 2 ) { // 加入第1本or第2本書
69 Book * l_pNewBook = new Book();
70 getUserInputIntBookInfo(l_pNewBook);
71
72 l_pNewBook -> m_pNextBook = NULL;
73 if ( 0 == m_iAmount ) {
74 m_pHeader = l_pNewBook;
75 }
76 else if ( 1 == m_iAmount ) {
77 m_pHeader -> m_pNextBook = l_pNewBook;
78 }
79
80 }
81 else if ( (m_iAmount >= 2 ) && (l_iInput <= m_iAmount ) ) { // 選擇加入在目前書籍中哪一本之後
82 Book * l_pNewBook = new Book();
83 getUserInputIntBookInfo(l_pNewBook);
84
85 Book * l_pAddAfter = m_pHeader;
86 int l_i = 0 ;
87 l_iInput -- ;
88 while (l_iInput > l_i) {
89 l_pAddAfter = l_pAddAfter -> m_pNextBook;
90 l_i ++ ;
91 }
92 Book * l_pNextBook = l_pAddAfter -> m_pNextBook;
93 l_pAddAfter -> m_pNextBook = l_pNewBook;
94 l_pNewBook -> m_pNextBook = l_pNextBook;
95
96 }
97 else if (l_iInput > m_iAmount) { // 超過目前書籍量
98 cout << " 目前只有 " << m_iAmount << " 本書 " << endl;
99 return ;
100 }
101 m_iAmount ++ ;
102 }
103
104 /**/ /*
105 *刪除指定編號之書籍
106 */
107 void LinkList::deleteBook() {
108 if ( 0 == m_iAmount ) {
109 cout << " 裡面沒有書可以刪除 " << endl;
110 return ;
111 }
112
113 cout << " 請輸入要刪除第幾本書 " ;
114 int l_iInput = 0 ;
115 getUserInputInt( & l_iInput);
116
117 if ( l_iInput > m_iAmount ) {
118 cout << " 錯誤!目前無此編號之書籍!! " << endl;
119 return ;
120 }
121 l_iInput -- ;
122
123 Book * l_mDeleteBook = m_pHeader;
124
125 if ( 0 == l_iInput ) { // 刪除第一本書
126 m_pHeader = m_pHeader -> m_pNextBook;
127 delete l_mDeleteBook;
128 }
129 else {
130 Book * l_pPreBook;
131 int l_i = 0 ;
132 while (l_iInput > l_i) {
133 l_pPreBook = l_mDeleteBook;
134 l_mDeleteBook = l_mDeleteBook -> m_pNextBook;
135 l_i ++ ;
136 }
137 l_pPreBook -> m_pNextBook = l_mDeleteBook -> m_pNextBook; // 前一本書指向下一本
138 delete l_mDeleteBook;
139 }
140
141 m_iAmount -- ;
142 }
143
144 /**/ /*
145 *尋找指定之書籍
146 */
147 void LinkList::searchBook() {
148
149 if (m_iAmount < 1 ) {
150 cout << " 裡面沒有書可以查詢!! " << endl;
151 return ;
152 }
153
154 cout << " 請輸入要查詢的條件 1-書名,2-作者,3-ISBN,4-價格 : " ;
155
156 int l_iSearchCondition = 0 ;
157 getUserInputInt( & l_iSearchCondition);
158
159
160 switch (l_iSearchCondition) {
161 case NAME_CONDITION:
162 case AUTHOR_CONDITION: {
163 string str;
164 cout << " 請輸入欲查詢的條件: " ;
165 cin >> str;
166 searchBookCondition(l_iSearchCondition,str);
167 break ;
168 }
169 case ISBN_CONDITION:
170 case PRICE_CONDITION: {
171 int l_iUserInput = 0 ;
172 cout << " 請輸入欲查詢的條件: " ;
173 getUserInputInt( & l_iUserInput);
174 searchBookCondition(l_iSearchCondition,l_iUserInput);
175 break ;
176 }
177 default :
178 cout << " 請輸入正確選項 " << endl;
179 }
180
181 }
182
183
184 /**/ /*
185 *用書名或作者查
186 */
187 void LinkList::searchBookCondition( int condition, string str) {
188 cout << " 用書名或作者查 " << endl;
189 Book * l_pCurrentBook = m_pHeader;
190 string l_szMessage ;
191
192 if ( condition == NAME_CONDITION ) { // 查書名
193 for ( int i = 0 ; i < m_iAmount ; i ++ ) {
194 if ( str == l_pCurrentBook -> m_szName ) {
195 if ( 0 == l_szMessage.size() ) {
196 l_szMessage += " 搜尋結果如下:\n " ;
197 }
198 l_szMessage += " 第 " ;
199 l_szMessage += int2str(i + 1 );
200 l_szMessage += " 本書==>書名: " ;
201 l_szMessage += l_pCurrentBook -> m_szName;
202 l_szMessage += " 作者: " ;
203 l_szMessage += l_pCurrentBook -> m_szAuthor;
204 l_szMessage += " ISBN: " ;
205 l_szMessage += int2str(l_pCurrentBook -> m_iISBN);
206 l_szMessage += " 價格: " ;
207 l_szMessage += int2str(l_pCurrentBook -> m_iPrice);
208 l_szMessage += " \n " ;
209 }
210 l_pCurrentBook = l_pCurrentBook -> m_pNextBook;
211 } // end of for
212 }
213 else if ( condition == AUTHOR_CONDITION ) { // 查作者
214 for ( int i = 0 ; i < m_iAmount ; i ++ ) {
215 if ( str == l_pCurrentBook -> m_szAuthor ) {
216 if ( 0 == l_szMessage.size() ) {
217 l_szMessage += " 搜尋結果如下:\n " ;
218 }
219 l_szMessage += " 第 " ;
220 l_szMessage += int2str(i + 1 );
221 l_szMessage += " 本書==>書名: " ;
222 l_szMessage += l_pCurrentBook -> m_szName;
223 l_szMessage += " 作者: " ;
224 l_szMessage += l_pCurrentBook -> m_szAuthor;
225 l_szMessage += " ISBN: " ;
226 l_szMessage += int2str(l_pCurrentBook -> m_iISBN);
227 l_szMessage += " 價格: " ;
228 l_szMessage += int2str(l_pCurrentBook -> m_iPrice);
229 l_szMessage += " \n " ;
230 }
231 l_pCurrentBook = l_pCurrentBook -> m_pNextBook;
232 } // end of for
233 }
234
235 if ( 0 == l_szMessage.size() ) {
236 l_szMessage += " 你的搜尋無結果!!!!! " ;
237 }
238 cout << l_szMessage << endl;
239 }
240
241 /**/ /*
242 *用ISBN或價格查
243 */
244 void LinkList::searchBookCondition( int condition, int number) {
245 Book * l_pCurrentBook = m_pHeader;
246 string l_szMessage ;
247
248 if ( condition == ISBN_CONDITION ) { // 查ISBN
249 for ( int i = 0 ; i < m_iAmount ; i ++ ) {
250 if ( number == l_pCurrentBook -> m_iISBN ) {
251 if ( 0 == l_szMessage.size() ) {
252 l_szMessage += " 搜尋結果如下:\n " ;
253 }
254 l_szMessage += " 第 " ;
255 l_szMessage += int2str(i + 1 );
256 l_szMessage += " 本書==>書名: " ;
257 l_szMessage += l_pCurrentBook -> m_szName;
258 l_szMessage += " 作者: " ;
259 l_szMessage += l_pCurrentBook -> m_szAuthor;
260 l_szMessage += " ISBN: " ;
261 l_szMessage += int2str(l_pCurrentBook -> m_iISBN);
262 l_szMessage += " 價格: " ;
263 l_szMessage += int2str(l_pCurrentBook -> m_iPrice);
264 l_szMessage += " \n " ;
265 }
266 l_pCurrentBook = l_pCurrentBook -> m_pNextBook;
267 } // end of for
268 }
269 else if ( condition == PRICE_CONDITION ) { // 查售價
270 for ( int i = 0 ; i < m_iAmount ; i ++ ) {
271 if ( number == l_pCurrentBook -> m_iPrice ) {
272 if ( 0 == l_szMessage.size() ) {
273 l_szMessage += " 搜尋結果如下:\n " ;
274 }
275 l_szMessage += " 第 " ;
276 l_szMessage += int2str(i + 1 );
277 l_szMessage += " 本書==>書名: " ;
278 l_szMessage += l_pCurrentBook -> m_szName;
279 l_szMessage += " 作者: " ;
280 l_szMessage += l_pCurrentBook -> m_szAuthor;
281 l_szMessage += " ISBN: " ;
282 l_szMessage += int2str(l_pCurrentBook -> m_iISBN);
283 l_szMessage += " 價格: " ;
284 l_szMessage += int2str(l_pCurrentBook -> m_iPrice);
285 l_szMessage += " \n " ;
286 }
287 l_pCurrentBook = l_pCurrentBook -> m_pNextBook;
288 } // end of for
289 }
290
291 if ( 0 == l_szMessage.size() ) {
292 l_szMessage += " 你的搜尋無結果!!!!! " ;
293 }
294 cout << l_szMessage << endl;
295
296 }
297
298
299
300 /**/ /*
301 *列出所有書籍
302 */
303 void LinkList::listAllBook() {
304 if ( NULL == m_pHeader ) {
305 cout << " 沒有書!! " << endl;
306 return ;
307 }
308
309 cout << " 目前共有: " << m_iAmount << " 本書,所有書籍清單如下: " << endl;
310 Book * l_pTempBook = m_pHeader;
311 while ( NULL != l_pTempBook ) {
312 cout << " 書名: " << l_pTempBook -> m_szName;
313 cout << " 作者: " << l_pTempBook -> m_szAuthor;
314 cout << " ISBN: " << l_pTempBook -> m_iISBN;
315 cout << " 價格: " << l_pTempBook -> m_iPrice << endl;
316 cout << " ******************************* " << endl;
317 l_pTempBook = l_pTempBook -> m_pNextBook;
318 }
319
320 }
321
322 /**/ /*
323 *取得user輸入
324 */
325 bool getUserInputInt( int * p) {
326 cin >> * p;
327 if ( ! cin ) {
328 cerr << " 錯誤!! " << endl;
329 return false ;
330 }
331 }
332
333
334 /**/ /*
335 *取得user輸入的書籍資訊
336 */
337 void getUserInputIntBookInfo(Book * book) {
338 cout << " 請輸入書名: " ;
339 cin >> book -> m_szName;
340 cout << " 請輸入作者: " ;
341 cin >> book -> m_szAuthor;
342 cout << " 請輸入ISBN: " ;
343 cin >> book -> m_iISBN;
344 cout << " 請輸入價格: " ;
345 cin >> book -> m_iPrice;
346 }
347
348
349 /**/ /*
350 *show出工作項目
351 */
352 void showTitle() {
353 cout << " ################## " << endl;
354 cout << " 1)add a new book # " << endl;
355 cout << " 2)delete a book # " << endl;
356 cout << " 3)search a book # " << endl;
357 cout << " 4)list all book # " << endl;
358 cout << " 5)exit # " << endl;
359 cout << " ################## " << endl;
360 cout << " 請輸入: " ;
361 }
362
363 /**/ /*
364 *轉換數字為字串
365 */
366 string int2str( int i) {
367 string s;
368 stringstream ss(s);
369 ss << i;
370 return ss.str();
371 }
372
373 int main() {
374
375 LinkList * l_firstLinkList = new LinkList();
376
377 while ( true ) {
378 showTitle();
379 int input = 0 ; // user input
380 getUserInputInt( & input );
381
382 /**/ /*
383 if( ADD_NEW_BOOK_ACTION != input &&
384 DELETE_BOOK_ACTION != input &&
385 SEARCH_BOOK_ACTION != input &&
386 LIST_BOOK_ACTION != input ){
387 break;
388 }
389 */
390
391 if ( EXIT == input ) {
392 break ;
393 }
394
395 switch (input) {
396 case ADD_NEW_BOOK_ACTION: {
397 l_firstLinkList -> addBook();
398 break ;
399 }
400 case DELETE_BOOK_ACTION: {
401 l_firstLinkList -> deleteBook();
402 break ;
403 }
404 case SEARCH_BOOK_ACTION: {
405 l_firstLinkList -> searchBook();
406 break ;
407 }
408 case LIST_BOOK_ACTION: {
409 l_firstLinkList -> listAllBook();
410 break ;
411 }
412 default :
413 cout << " 請輸入正確選項!! " << endl;
414 } // end switch
415
416
417
418 } // end while
419
420 system( " pause " );
421 return 0 ;
422 }
423