fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define DATA_FILE "data.txt"
  6.  
  7. struct STUDENT
  8. {
  9. int id;
  10. char name[80];
  11. int take_num;
  12. int takes[6];
  13. struct STUDENT* left_next;/*link list*/
  14. struct STUDENT* right_next;
  15. };
  16.  
  17. struct STUDENT* head;
  18. struct STUDENT* current;
  19. struct STUDENT* previous;/*link list*/
  20.  
  21. /*void init(struct STUDENT students[]);*/
  22. void load(FILE* fp,int select_num);
  23. void make_tree(struct STUDENT* student_ptr,struct STUDENT** head,int select_num);
  24. void traverse_tree(struct STUDENT** head);
  25.  
  26. struct STUDENT* b_search(struct STUDENT* head,int id,char* name,int select_num);
  27.  
  28. void show(struct STUDENT* student);
  29. int add(struct STUDENT* student,int select_course);
  30. void save(FILE* fp,struct STUDENT* student);
  31.  
  32. int main (void)
  33. {
  34. FILE *fp;
  35. struct STUDENT students[256];
  36. int id=-1;//search by id
  37. char name[80]={0};//search by name
  38. int index;
  39. int course;
  40. int result;
  41. int select_num;//save the number selected
  42. struct STUDENT* consequence;
  43.  
  44. //init(students);//對id,name做初始化
  45. fp=fopen(DATA_FILE,"r");
  46.  
  47. //load(fp,students);
  48. //fclose(fp);
  49.  
  50. printf("please select enter name or enter id:\n");
  51. printf("enter name 按1,enter id 按2\n");
  52. scanf("%d",&select_num);
  53. //select_num=1;
  54. if(select_num==1)
  55. {
  56. load(fp,select_num);
  57. fclose(fp);
  58. printf("please enter name:");
  59. scanf("%s",name);
  60. consequence=b_search(head,-1,name,1);
  61.  
  62. if(consequence==NULL)
  63. {
  64. printf("name doesn`t exist,program ends\n");
  65. exit(1);
  66. }
  67. //id=name_to_id(students,name);
  68. printf("%d ",consequence->id);
  69. printf("%s",consequence->name);
  70. }
  71. else if(select_num==2)
  72. {
  73. load(fp,select_num);
  74. fclose(fp);
  75. printf("please enter id:");
  76. scanf("%d",&id);
  77. consequence=b_search(head,id,0,2);
  78.  
  79.  
  80. if(consequence==NULL)
  81. {
  82. printf("id doesn`t exist,program ends\n");
  83. exit(1);
  84. }
  85. printf("%d ",consequence->id);
  86. printf("%s",consequence->name);
  87. }
  88.  
  89. while(1)
  90. {
  91. show(consequence);
  92. if(consequence->take_num==5)
  93. {
  94. printf("all courses are selected,program ends");
  95. break;
  96. }
  97. printf("請輸入課程代號,-1結束選課");
  98. scanf("%d",&course);
  99. if(course==-1)
  100. {
  101. printf("程式結束\n");
  102. break;
  103. }
  104.  
  105. result=add(consequence,course);
  106. if(result==1)
  107. {
  108. printf("選課成功\n");
  109. }
  110. else if(result==2)
  111. {
  112. printf("重複選課\n");
  113. }
  114. else if(result==3)
  115. {
  116. printf("無此課程\n");
  117. }
  118. }
  119.  
  120. fp=fopen(DATA_FILE,"w");
  121. save(fp,head);
  122. fprintf(fp,"\b");
  123. fclose(fp);
  124.  
  125. system("pause");
  126. return 0;
  127. }
  128.  
  129. /*void init(struct STUDENT students[])
  130. {
  131.   int i,j;
  132.   for(i=1;i<256;i++)
  133.   {
  134.   students[i].id=-1;
  135.   //students[i].name[80]={0};
  136.   memset(students[i].name,0,sizeof(students[i].name));
  137.   }
  138. }*/
  139.  
  140. void load(FILE* fp,int select_num)
  141. {
  142. int i,j,k;
  143. i=0;
  144. //if(select_num==2)/*enter id*/
  145. {
  146. while(!feof(fp))
  147. {
  148. i=i+1;
  149. struct STUDENT* student_ptr=(struct STUDENT*)malloc(sizeof(struct STUDENT));
  150. // printf("%d\n",i);
  151. fscanf(fp,"%d",&(student_ptr->id));
  152. fscanf(fp,"%s",student_ptr->name);
  153. fscanf(fp,"%d",&(student_ptr->take_num));
  154. // printf("%d ",student_ptr->id);
  155. // printf("%d\n",student_ptr->take_num);
  156. for(j=1;j<=(student_ptr->take_num);j++)
  157. {
  158. fscanf(fp,"%d",&(student_ptr->takes[j]));
  159.  
  160. }
  161.  
  162. student_ptr->left_next=NULL;
  163. student_ptr->right_next=NULL;
  164.  
  165. make_tree(student_ptr,&head,select_num);
  166. printf("%d\n",i);
  167. }
  168. traverse_tree(&head);
  169. }
  170.  
  171. }
  172.  
  173.  
  174. void show(struct STUDENT* student)
  175. {
  176. int i;
  177. printf("您已選以下課程:\n");
  178. for(i=1;i<=student->take_num;i++)
  179. {
  180. printf("%d",student->takes[i]);
  181. printf(" ");
  182. }
  183. printf("\n");
  184. }
  185.  
  186. int add(struct STUDENT* student,int course)
  187. {
  188. int i;
  189. if(course>=101&&course<=105)
  190. {
  191. for(i=1;i<=student->take_num;i++)
  192. {
  193. if(student->takes[i]==course)
  194. return 2;
  195. }
  196. student->takes[i]=course;
  197. student->take_num=(student->take_num)+1;
  198. return 1;
  199. }
  200. return 3;
  201. }
  202.  
  203. void save(FILE* fp,struct STUDENT* head)
  204. {
  205. int i;
  206. if(head==NULL)
  207. {
  208. return;
  209. }
  210. else
  211. {
  212. fprintf(fp,"%d ",head->id);
  213. fprintf(fp,"%s ",head->name);
  214. fprintf(fp,"%d ",head->take_num);
  215. for(i=1;i<=head->take_num;i++)
  216. {
  217. fprintf(fp,"%d",head->takes[i]);
  218. if(i<head->take_num)
  219. fprintf(fp," ");
  220.  
  221. }
  222. fprintf(fp,"\n");
  223. //printf("%d",head->id);
  224. save(fp,head->left_next);
  225. save(fp,head->right_next);
  226. }
  227. }
  228.  
  229. struct STUDENT* b_search(struct STUDENT* head,int id,char* name,int select_num)
  230. {
  231. //printf("%d ",select_num);
  232. struct STUDENT* result;
  233. if(select_num==1)
  234. {
  235. if(head==NULL)
  236. {
  237. result=NULL;
  238. return result;
  239. }
  240. else if(head!=NULL)
  241. {
  242. if(strcmp((head)->name,name)>0)
  243. {
  244. result=b_search(head->left_next,-1,name,1);
  245. return result;
  246. }
  247. else if(strcmp((head)->name,name)<0)
  248. {
  249. result=b_search(head->right_next,-1,name,1);
  250. return result;
  251. }
  252. else if(strcmp((head)->name,name)==0)
  253. {
  254.  
  255. return head;
  256. }
  257. }
  258. }
  259. else if(select_num==2)
  260. {
  261. if(head==NULL)
  262. {
  263. return NULL;
  264. }
  265. else
  266. {
  267. if(id==head->id)
  268. {
  269. result=head;
  270. return result;
  271. }
  272. else if(id<head->id)
  273. {
  274. result=b_search(head->left_next,id,0,2);
  275. return result;
  276. }
  277. else if(id>head->id)
  278. {
  279. result=b_search(head->right_next,id,0,2);
  280. return result;
  281. }
  282. }
  283. }
  284. }
  285.  
  286.  
  287. void make_tree(struct STUDENT* student_ptr,struct STUDENT** head,int select_num)
  288. {
  289. if(select_num==1)
  290. {
  291. if(*head==NULL)
  292. {
  293. *head=student_ptr;
  294. }
  295. else if(*head!=NULL)
  296. {
  297. if(strcmp((*head)->name,student_ptr->name)>0)
  298. {
  299. make_tree(student_ptr,&((*head)->left_next),select_num);
  300. }
  301. else if(strcmp((*head)->name,student_ptr->name)<=0)
  302. {
  303. make_tree(student_ptr,&((*head)->right_next),select_num);
  304. }
  305. }
  306. }
  307. else if(select_num==2)
  308. {
  309. if(*head==NULL)
  310. {
  311. *head=student_ptr;
  312. // printf("%d\n",(*head)->id);
  313. /*current=student_ptr;
  314.   previous=student_ptr;*/
  315. }
  316. else if(*head!=NULL)
  317. {
  318. if((*head)->id>=student_ptr->id)
  319. {
  320. /*previous=current;
  321.   current=head->left_next;*/
  322. if((*head)->left_next==NULL)
  323. {
  324. (*head)->left_next=student_ptr;
  325. }
  326. else if((*head)->left_next!=NULL)
  327. {
  328. make_tree(student_ptr,&((*head)->left_next),select_num);
  329. }
  330. }
  331. else if((*head)->id<student_ptr->id)
  332. {
  333. if((*head)->right_next==NULL)
  334. {
  335. (*head)->right_next=student_ptr;
  336. }
  337. else if((*head)->right_next!=NULL)
  338. {
  339. make_tree(student_ptr,&((*head)->right_next),select_num);
  340. }
  341. }
  342. }
  343. }
  344. //printf("%s %d\n",head->name,head->id);
  345. }
  346.  
  347. void traverse_tree(struct STUDENT** head)
  348. {
  349. if(*head==NULL)
  350. {
  351. return;
  352. }
  353. else
  354. {
  355. //printf("%s %d\n",(*head)->name,(*head)->id);
  356. traverse_tree(&((*head)->left_next));
  357. traverse_tree(&((*head)->right_next));
  358. }
  359. }
  360.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty