• PTA 7-42 《图书目录管理系统》(初级修订版)


    请设计一个简单的图书目录管理系统(初级版)。

    图书目录信息包括:

    统一书号(不超过13字符)
    书名(不超过30字符)
    作者(不超过20字符)
    出版社(不超过30字符)
    出版日期(包括:年、月、日)
    价格(精确到分)
    注意:为了保证运行窗口能正确地显示,请右击运行窗口,修改“属性”:

    在“选项”选项卡中,选中“使用旧版本控制台”
    在“字体”选项卡中,选择“8×16”“点阵字体”
    在“布局”选项卡中,将屏幕窗口宽度均设为“120”。
    具体需求如下:

    显示如下的主菜单:
    Append Find Remove Modify Show Quit > _
    如果用户输入 A 或 a、F 或 f、R 或 r、M 或 m、S 或 s,则可完成相应的操作。如果用户输入其它字符,则显示错误信息。
    程序将反复显示主菜单,让用户持续工作。如果用户输入 Q 或 q,则程序结束。

    Append Find Remove Modify Show Quit > Q
    Thank you! Goodbye!
    若用户输入其它字符,则显示错误信息。
    Append Find Remove Modify Show Quit > B
    Incorrect choice!
    Append Find Remove Modify Show Quit > 9
    Incorrect choice!
    Append Find Remove Modify Show Quit > +
    Incorrect choice!
    若用户输入 A 或 a,则可以输入新书的信息,将其添加到图书目录中。
    Append Find Remove Modify Show Quit > a
         ISBN: 9780439227148
        Title: The Call of the Wild
       Author: Jack London
    Publisher: Scholastic Press
     Pub date: 2001/1/1
        Price: 39.4
    Append Find Remove Modify Show Quit > A
         ISBN: 9781772262902
        Title: Oliver Twist
       Author: Charles Dickens
    Publisher: Engage Books
     Pub date: 2016/9/15
        Price: 648
    Append Find Remove Modify Show Quit > a
         ISBN: 9787515911076
        Title: The Call of the Wild
       Author: Jack London
    Publisher: Aerospace Publishing House
     Pub date: 2016/5/1
        Price: 29.8
    若用户输入的日期信息不正确,则显示错误信息,并要求用户重新输入。
    Append Find Remove Modify Show Quit > A
         ISBN: 9787501592401
        Title: The Old Man and the Sea
       Author: Ernest Hemingway
    Publisher: Knowledge Press
     Pub date: 2016/2/30
    Incorrect date! Please reenter: 2016/6/31
    Incorrect date! Please reenter: 2016/8/1
        Price: 25.8
    若用户输入 S 或 s,则按书号升序排序,然后列表显示全部图书。
    Append Find Remove Modify Show Quit > S
    ISBN--------- Title------------------------- Author-------------- Publisher--------------------- Pub-date-- Price---
    9780439227148 The Call of the Wild           Jack London          Scholastic Press               2001/01/01    39.40
    9781772262902 Oliver Twist                   Charles Dickens      Engage Books                   2016/09/15   648.00
    9787501592401 The Old Man and the Sea        Ernest Hemingway     Knowledge Press                2016/08/01    25.80
    9787515911076 The Call of the Wild           Jack London          Aerospace Publishing House     2016/05/01    29.80
    若用户输入 F 或 f,则输入书名,然后显示该书名的图书。如果没有对应的图书,则显示错误信息。
    Append Find Remove Modify Show Quit > F
    Title: The Call of the Wild
    ISBN--------- Title------------------------- Author-------------- Publisher--------------------- Pub-date-- Price---
    9780439227148 The Call of the Wild           Jack London          Scholastic Press               2001/01/01    39.40
    9787515911076 The Call of the Wild           Jack London          Aerospace Publishing House     2016/05/01    29.80
    Append Find Remove Modify Show Quit > f
    Title: Gulliver's Travels
    Not found!
    说明:输出查找结果时,不作排序操作。

    若用户输入 R 或 r,则输入书号,然后将删除该书号的图书。如果没有对应的图书,则显示错误信息。
    Append Find Remove Modify Show Quit > r
    ISBN: 9781772262902
    Remove(y/n)? n
    Append Find Remove Modify Show Quit > S
    ISBN--------- Title------------------------- Author-------------- Publisher--------------------- Pub-date-- Price---
    9780439227148 The Call of the Wild           Jack London          Scholastic Press               2001/01/01    39.40
    9781772262902 Oliver Twist                   Charles Dickens      Engage Books                   2016/09/15   648.00
    9787501592401 The Old Man and the Sea        Ernest Hemingway     Knowledge Press                2016/08/01    25.80
    9787515911076 The Call of the Wild           Jack London          Aerospace Publishing House     2016/05/01    29.80
    Append Find Remove Modify Show Quit > R
    ISBN: 9780439227148
    Remove(y/n)? Y
    Append Find Remove Modify Show Quit > s
    ISBN--------- Title------------------------- Author-------------- Publisher--------------------- Pub-date-- Price---
    9781772262902 Oliver Twist                   Charles Dickens      Engage Books                   2016/09/15   648.00
    9787501592401 The Old Man and the Sea        Ernest Hemingway     Knowledge Press                2016/08/01    25.80
    9787515911076 The Call of the Wild           Jack London          Aerospace Publishing House     2016/05/01    29.80
    Append Find Remove Modify Show Quit > r
    ISBN: 9787515914145
    Not found!
    Append Find Remove Modify Show Quit > s
    ISBN--------- Title------------------------- Author-------------- Publisher--------------------- Pub-date-- Price---
    9781772262902 Oliver Twist                   Charles Dickens      Engage Books                   2016/09/15   648.00
    9787501592401 The Old Man and the Sea        Ernest Hemingway     Knowledge Press                2016/08/01    25.80
    9787515911076 The Call of the Wild           Jack London          Aerospace Publishing House     2016/05/01    29.80
    要求:用户回答是否删除时,必须回答 Y 或 N (大小写均可)。如果是其它字符,则显示错误信息,要求用户重新回答。

    Append Find Remove Modify Show Quit > R
    ISBN: 9781772262902
    Remove(y/n)? k
    Incorrect answer!
    Remove(y/n)? $
    Incorrect answer!
    Remove(y/n)? N
    若用户输入 M 或 m,则可以修改图书信息。首先按书号查找,然后重新输入该图书的信息。
    Append Find Remove Modify Show Quit > m
    ISBN: 9787515911076
    Modify(y/n)? y
         ISBN: 9787544724968
        Title: The House on Mango Street
       Author: Sandra Heathneros
    Publisher: Yilin Press
     Pub date: 2012/1/1
        Price: 30
    Append Find Remove Modify Show Quit > M
    ISBN: 9787501592401
    Modify(y/n)? n
    Append Find Remove Modify Show Quit > m
    ISBN: 9787515914145
    Not found!
    Append Find Remove Modify Show Quit > S
    ISBN--------- Title------------------------- Author-------------- Publisher--------------------- Pub-date-- Price---
    9781772262902 Oliver Twist                   Charles Dickens      Engage Books                   2016/09/15   648.00
    9787501592401 The Old Man and the Sea        Ernest Hemingway     Knowledge Press                2016/08/01    25.80
    9787544724968 The House on Mango Street      Sandra Heathneros    Yilin Press                    2012/01/01    30.00
    要求:用户回答是否修改时,必须回答 Y 或 N (大小写均可)。如果是其它字符,则显示错误信息,要求用户重新回答。

    Append Find Remove Modify Show Quit > M
    ISBN: 9787544724968
    Modify(y/n)? K
    Incorrect answer!
    Modify(y/n)? *
    Incorrect answer!
    Modify(y/n)? n
    相关习题:图书目录管理系统(高级版)。

    Append Find Remove Modify Show Quit > Thank you! Goodbye!
    q

    代码 

    注:代码还有很多bug,请各路大神指点一二

    1. #define _CRT_SECURE_NO_WARNINGS
    2. #include
    3. #include
    4. #include
    5. struct bookInfo //创建结构体存储书籍数据
    6. {
    7. char ISBN[13];
    8. char Title[30];
    9. char Author[20];
    10. char Publisher[30];
    11. char Pub_date[10];
    12. double Price;
    13. };
    14. struct Node
    15. {
    16. struct bookInfo data;
    17. struct Node* next;
    18. };
    19. struct Node* list = NULL;//初始化
    20. struct Node* createHead()//建立表头
    21. {
    22. //动态分配内存
    23. struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
    24. //变量初始化
    25. headNode->next = NULL;
    26. return headNode;
    27. }
    28. struct Node* createNode(struct bookInfo data)
    29. {
    30. struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    31. newNode->data = data;
    32. newNode->next = NULL;
    33. return newNode;
    34. }
    35. //表头插入法
    36. void Append(struct Node* headNode,struct bookInfo data)
    37. {
    38. struct Node* newNode = createNode(data);
    39. newNode->next = headNode->next;
    40. headNode->next = newNode;
    41. }
    42. //查找
    43. struct Node* Find(struct Node* headNode,char* Title)
    44. {
    45. struct Node* posNode = headNode->next;
    46. while (posNode != NULL && strcmp(posNode->data.Title, Title))
    47. {
    48. posNode = posNode->next;
    49. }
    50. return posNode;
    51. }
    52. void Remove(struct Node* headNode,char* ISBN)
    53. {
    54. struct Node* posLeftNode = headNode;
    55. struct Node* posNode = headNode->next;
    56. while (posNode != NULL && strcmp(posNode->data.ISBN, ISBN))
    57. {
    58. posLeftNode = posNode;
    59. posNode = posLeftNode->next;
    60. }
    61. if (posNode == NULL)
    62. {
    63. return;
    64. }
    65. else
    66. {
    67. posLeftNode->next = posNode->next;
    68. free(posNode);
    69. posNode = NULL;
    70. }
    71. }
    72. /*若用户输入 M 或 m,则可以修改图书信息。首先按书号查找,然后重新输入该图书的信息。*/
    73. void Modify(struct Node* headNode,char* ISBN )
    74. {
    75. struct Node* posNode = headNode->next;
    76. while (posNode != NULL && strcmp(posNode->data.ISBN, ISBN))
    77. {
    78. posNode = posNode->next;
    79. }
    80. char userKey;
    81. printf("Modify(y/n)? ");
    82. userKey = getchar();
    83. if (posNode != NULL && (userKey == 'y'|| userKey == 'Y'))
    84. {
    85. scanf("%s%s%s%s%s%.2f\n", &posNode->data.ISBN, posNode->data.Title, posNode->data.Author, posNode->data.Publisher, posNode->data.Pub_date, &posNode->data.Price);
    86. //scanf("%s", posNode->data.ISBN);
    87. //scanf("%s", posNode->data.Title);
    88. //scanf("%s", posNode->data.Author);
    89. //scanf("%s", posNode->data.Publisher);
    90. //scanf("%s", posNode->data.Pub_date);
    91. //scanf("%.2f", &posNode->data.Price);
    92. fflush(stdin);
    93. }
    94. else
    95. {
    96. return;
    97. }
    98. }
    99. void bubluSortList(struct Node* headNode)
    100. {
    101. for (struct Node* p = headNode->next; p != NULL; p = p->next)
    102. {
    103. for (struct Node* q = p; q != NULL; q = q->next)
    104. {
    105. if (q->data.ISBN > q->next->data.ISBN)
    106. {
    107. struct bookInfo tempData = q->data;
    108. q->data = q->next->data;
    109. q->next->data = tempData;
    110. }
    111. }
    112. }
    113. }
    114. void Show(struct Node* headNode)
    115. {
    116. bubluSortList(headNode);
    117. struct Node* pMove = headNode->next;
    118. printf("ISBN--------- Title------------------------- Author-------------- Publisher--------------------- Pub-date-- Price---\n");
    119. while (pMove != NULL)
    120. {
    121. printf("%s\t%s\t%s\t%s\t%s\t%.2f\n", pMove->data.ISBN, pMove->data.Title, pMove->data.Author, pMove->data.Publisher, pMove->data.Pub_date, pMove->data.Price);
    122. //printf("%s\t", pMove->data.ISBN);
    123. //printf("%s\t", pMove->data.Title);
    124. //printf("%s\t", pMove->data.Author);
    125. //printf("%s\t", pMove->data.Publisher);
    126. //printf("%s\t", pMove->data.Pub_date);
    127. //printf("%.2f\n", pMove->data.Price);
    128. fflush(stdin);
    129. pMove = pMove->next;
    130. }
    131. }
    132. void saveInfoToFile(const char* fileName, struct Node* headNode)
    133. {
    134. FILE* fp = fopen(fileName, "w");
    135. struct Node* pMove = headNode->next;
    136. while (pMove != NULL)
    137. {
    138. fprintf("%s\t%s\t%\ts%\ts%\ts%.2f\n", pMove->data.ISBN, pMove->data.Title, pMove->data.Author, pMove->data.Publisher, pMove->data.Pub_date, pMove->data.Price);
    139. //fprintf("%s\t", pMove->data.ISBN);
    140. //fprintf("%s\t", pMove->data.Title);
    141. //fprintf("%s\t", pMove->data.Author);
    142. //fprintf("%s\t", pMove->data.Publisher);
    143. //fprintf("%s\t", pMove->data.Pub_date);
    144. //fprintf("%.2f\n", &pMove->data.Price);
    145. pMove = pMove->next;
    146. }
    147. fclose(fp);
    148. }
    149. void readInfoToFile(const char* fileName, struct Node* headNode)
    150. {
    151. FILE* fp = fopen(fileName, "r");
    152. if (fp == NULL)
    153. {
    154. fp = fopen(fileName, "w+");
    155. }
    156. struct bookInfo tempData ;
    157. while (fscanf("%s %s %s %s %s %.2f\n", tempData.ISBN, tempData.Title, tempData.Author, tempData.Publisher, tempData.Pub_date, &tempData.Price) != EOF)
    158. {
    159. Append(list,tempData);
    160. }
    161. fclose(fp);
    162. }
    163. void menu()
    164. {
    165. char ret=0;
    166. struct bookInfo tempData ={0,0,0,0,0,0};
    167. struct Node* result = NULL;
    168. ret = getchar();
    169. switch (ret) {
    170. case 'A':
    171. case 'a':
    172. //scanf("%s", tempData.ISBN);
    173. //scanf("%s", tempData.Title);
    174. //scanf("%s", tempData.Author);
    175. //scanf("%s", tempData.Publisher);
    176. //scanf("%s", tempData.Pub_date);
    177. //scanf("%.2f", &tempData.Price);
    178. scanf_s("%s%s%s%s%s%.2f\n", tempData.ISBN, tempData.Title, tempData.Author, tempData.Publisher, tempData.Pub_date, &tempData.Price);
    179. Append(list,tempData);
    180. saveInfoToFile("bookinfo.txt", list);
    181. fflush(stdin);
    182. break;
    183. case 'F':
    184. case 'f':
    185. scanf("%s", tempData.ISBN);
    186. result=Find(list,tempData.ISBN);
    187. if (result == NULL)
    188. {
    189. printf("Not founf!\n");
    190. }
    191. else
    192. {
    193. printf("ISBN--------- Title------------------------- Author-------------- Publisher--------------------- Pub-date-- Price---\n");
    194. printf("%s\t%s\t%\ts%\ts%\ts%.2f\n", result->data.ISBN, result->data.Title, result->data.Author, result->data.Publisher, result->data.Pub_date, result->data.Price);
    195. //printf("%s\t", result->data.ISBN);
    196. //printf("%s\t", result->data.Title);
    197. //printf("%s\t", result->data.Author);
    198. //printf("%s\t", result->data.Publisher);
    199. //printf("%s\t", result->data.Pub_date);
    200. //printf("%.2f\n", result->data.Price);
    201. fflush(stdin);
    202. }
    203. break;
    204. case 'R':
    205. case 'r':
    206. printf("ISBN :");
    207. scanf("%s", tempData.ISBN);
    208. Remove(list,tempData.ISBN);
    209. saveInfoToFile("bookinfo.txt", list);
    210. fflush(stdin);
    211. break;
    212. case 'M':
    213. case 'm':
    214. printf("ISBN :");
    215. scanf("%s", tempData.ISBN);
    216. Modify(list, tempData.ISBN);
    217. saveInfoToFile("bookinfo.txt", list);
    218. fflush(stdin);
    219. break;
    220. case 'S':
    221. case 's':
    222. Show(list);
    223. break;
    224. case 'Q':
    225. case 'q':
    226. printf("Thank you! Goodbye!");
    227. system("pause");
    228. exit(0);
    229. //Quit();
    230. break;
    231. default:
    232. printf("Incorrect choice!\n");
    233. fflush(stdin);
    234. break;
    235. }
    236. }
    237. int main()
    238. {
    239. list = createHead();
    240. readInfoToFile("bookinfo.txt",list);
    241. while (1)
    242. {
    243. printf("Append Find Remove Modify Show Quit > \n");
    244. menu();
    245. system("pause");
    246. system("cls");
    247. }
    248. system("pause");
    249. return 0;
    250. }

     

  • 相关阅读:
    EffectiveC++-条款38:通过复合塑模出 has-a 或 根据某物实现出
    第六季完美童模 代言人段沫如 全球赛个人风采展示
    概率论与数据统计学习:随机变量(一)——知识总结与C语言案例实现
    electron 基础
    前端js jsencrypt加密,后端解密
    SpringBoot中使用@Retryable注解进行重试
    C# 根据前台传入实体名称,动态查询数据
    js实现将文本生成二维码(腾讯云cos)
    生产环境下调试的技巧
    编译器一日一练(DIY系列之中间代码生成)
  • 原文地址:https://blog.csdn.net/peng_papa/article/details/126799311