首页 > 代码库 > 用c写的练习簿

用c写的练习簿

  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <string>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <Windows.h>
  7. using namespace std;
  8. typedef struct data
  9. {
  10. struct data *next;
  11. struct data *prior;
  12. string name;
  13. long long num;
  14. string sex;
  15. data()
  16. {
  17. next = NULL;
  18. prior = NULL;
  19. name = "NULL";
  20. num = -1;
  21. sex = "NULL";
  22. }
  23. }list;
  24. #define Len sizeof(list)
  25. list* search(list *tp);
  26. list* creatList()
  27. {
  28. list* tp = new list;
  29. tp->prior = tp;
  30. tp->next = tp;
  31. return tp;
  32. }
  33. void clearList(list *tp)
  34. {
  35. while(tp->name != "NULL" && tp->num != -1 && tp->sex != "NULL")
  36. tp = tp->next;
  37. list* head = tp;
  38. tp = tp->next;
  39. while(head->next->name != "NULL" && head->next->num != -1 && head->next->sex != "NULL")
  40. {
  41. head->next = tp->next;
  42. free(tp);
  43. tp = head->next;
  44. }
  45. head->prior = head;
  46. cout << "Contract Empty" << endl;
  47. }
  48. void destoryList(list *tp)
  49. {
  50. clearList(tp);
  51. free(tp);
  52. }
  53. void insert(list *tp, list *tep)
  54. {
  55. tep->next = tp->next;
  56. tep->prior = tp;
  57. tp->next->prior = tep;
  58. tp->next = tep;
  59. }
  60. list* makeNode()
  61. {
  62. list* tp = new list;
  63. cout << "please input name, sex, num : ";
  64. cin >> tp->name >> tp->sex >> tp->num;
  65. return tp;
  66. }
  67. void delNode(list *tp)
  68. {
  69. cout << "Delete element : ";
  70. if(!search(tp)) cout << "The element which you want to delete is not exist" << endl;
  71. else
  72. {
  73. tp->prior->next = tp->next;
  74. tp->next->prior = tp->prior;
  75. free(tp);
  76. }
  77. }
  78. list* search(list *tp)
  79. {
  80. string str;
  81. cout << "please input the key style (include name,num) : ";
  82. cin >> str;
  83. list* head = tp;
  84. if(str == "name")
  85. {
  86. cout << "please input which name you want to search : ";
  87. cin >> str;
  88. tp = tp->next;
  89. while(tp->name != str && tp != head)
  90. tp = tp->next;
  91. if(tp->name != str)
  92. return NULL;
  93. else
  94. return tp;
  95. }
  96. else if(str == "num")
  97. {
  98. long long num;
  99. cout << "please input which number you want to search : ";
  100. cin >> num;
  101. tp = tp->next;
  102. while(tp->num != num && tp != head)
  103. tp = tp->next;
  104. if(tp->num != num)
  105. return NULL;
  106. else return tp;
  107. }
  108. else
  109. return NULL;
  110. }
  111. void modify(list *head)
  112. {
  113. cout << "Modify elemtion : ";
  114. list *tp = search(head);
  115. if(!tp) cout << "the key can‘t find in the list" << endl;
  116. else
  117. {
  118. cout << "the elemtion which you want modify is " << tp->name << ‘ ‘ << tp->sex << ‘ ‘ << tp->num << endl;
  119. cout << "please input key what you want to modify :" << endl << "1. name" << endl << "2. sex" << endl << "3. num" << endl;
  120. int key;
  121. L:
  122. cin >> key;
  123. if(key == 1)
  124. {
  125. cout << "please input name : ";
  126. cin >> tp->name;
  127. }
  128. else if(key ==2)
  129. {
  130. cout << "please input sex : ";
  131. cin >> tp->sex;
  132. }
  133. else if(key == 3)
  134. {
  135. cout << "please input num : ";
  136. cin >> tp->num;
  137. }
  138. else
  139. {
  140. cout << "Input error , please input again :";
  141. goto L;
  142. }
  143. }
  144. cout << "the elemtion which you modify is " << tp->name << ‘ ‘ << tp->sex << ‘ ‘ << tp->num << endl;
  145. }
  146. void print(list *tp)
  147. {
  148. list* head = tp;
  149. tp = tp->next;
  150. while(tp!= head)
  151. {
  152. if(tp->name != "NULL" && tp->sex != "NULL" && tp->num != -1)
  153. cout << tp->name << ‘ ‘ << tp->sex << ‘ ‘ << tp->num << endl;
  154. tp = tp->next;
  155. }
  156. if(head->name != "NULL" && head->sex != "NULL" && head->num != -1)
  157. cout << head->name << ‘ ‘ << head->sex << ‘ ‘ << head->num << endl;
  158. }
  159. int main()
  160. {
  161. printf("\t\t\t\tThe Contract List\n\n");
  162. system("color a");
  163. list* head = creatList();
  164. cout << "please select which operate you want to do :" << endl;
  165. cout << "1. Insert" << endl << "2. Search " << endl << "3. Delete" << endl << "4. Modify" << endl
  166. << "5. Print all elemtion" << endl << "6. Empty contract"<< endl <<"0. Exit" << endl;
  167. cout << "_____________________________________________________________________________" << endl;
  168. //system("color b");
  169. int key;
  170. while(cin >> key && key != 0)
  171. {
  172. if(key == 1)
  173. {
  174. list*a =makeNode();
  175. insert(head, a);
  176. }
  177. else if(key == 2)
  178. {
  179. A:
  180. list *b = search(head);
  181. if(!b)
  182. {
  183. cout << "the key you input is not exist in element , please input again" << endl;
  184. goto A;
  185. }
  186. else
  187. cout << "The element is :" << b->name << ‘ ‘ << b->sex << ‘ ‘ << b->num << endl;
  188. }
  189. else if(key == 3)
  190. {
  191. delNode(head);
  192. }
  193. else if(key == 4)
  194. {
  195. modify(head);
  196. }
  197. else if(key == 5)
  198. {
  199. print(head);
  200. }
  201. else if(key == 6)
  202. clearList(head);
  203. else
  204. cout << "Input Error" << endl;
  205. }
  206. destoryList(head);
  207. return 0;
  208. }



来自为知笔记(Wiz)


附件列表

     

    用c写的练习簿