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. int search(struct STUDENT students[],int id);
  27. struct STUDENT* b_search(struct STUDENT* head,int id,char* name,int select_num);
  28. int b_search_name(struct STUDENT students[],char* name,int head,int tail);
  29. int search_by_name(struct STUDENT students[],char* name);
  30.  
  31. void show(struct STUDENT students[],int id,int index);
  32. int add(struct STUDENT students[],int id,int index,int course);
  33. void save(FILE* fp,struct STUDENT students[]);
  34.  
  35. int main (void)
  36. {
  37. FILE *fp;
  38. struct STUDENT students[256];
  39. int id=-1;//search by id
  40. char name[80]={0};//search by name
  41. int index;
  42. int course;
  43. int result;
  44. int select_num;//save the number selected
  45. struct STUDENT* consequence;
  46.  
  47. //init(students);//對id,name做初始化
  48. fp=fopen(DATA_FILE,"r");
  49.  
  50. //load(fp,students);
  51. //fclose(fp);
  52.  
  53. printf("please select enter name or enter id:\n");
  54. printf("enter name 按1,enter id 按2\n");
  55. //scanf("%d",&select_num);
  56. select_num=1;
  57. if(select_num==1)
  58. {
  59. load(fp,select_num);
  60. printf("please enter name:");
  61. scanf("%s",name);
  62. consequence=b_search(head,-1,name,1);
  63.  
  64. if(consequence==NULL)
  65. {
  66. printf("name doesn`t exist,program ends\n");
  67. exit(1);
  68. }
  69. //id=name_to_id(students,name);
  70. printf("%d ",consequence->id);
  71. printf("%s",consequence->name);
  72. }
  73. else if(select_num==2)
  74. {
  75. load(fp,select_num);
  76. printf("please enter id:");
  77. scanf("%d",&id);
  78.  
  79. index=search(students,id);
  80.  
  81. if(index==-1)
  82. {
  83. printf("id doesn`t exist,program ends\n");
  84. exit(1);
  85. }
  86. }
  87.  
  88. while(1)
  89. {
  90. show(students,id,index);
  91. if(students[index].take_num==5)
  92. {
  93. printf("all courses are selected,program ends");
  94. break;
  95. }
  96. printf("請輸入課程代號,-1結束選課");
  97. scanf("%d",&course);
  98. if(course==-1)
  99. {
  100. printf("程式結束\n");
  101. break;
  102. }
  103.  
  104. result=add(students,id,index,course);
  105. if(result==1)
  106. {
  107. printf("選課成功\n");
  108. }
  109. else if(result==2)
  110. {
  111. printf("重複選課\n");
  112. }
  113. else if(result==3)
  114. {
  115. printf("無此課程\n");
  116. }
  117. }
  118.  
  119. fp=fopen(DATA_FILE,"w");
  120. save(fp,students);
  121. fclose(fp);
  122.  
  123.  
  124. return 0;
  125. }
  126.  
  127. /*void init(struct STUDENT students[])
  128. {
  129.   int i,j;
  130.   for(i=1;i<256;i++)
  131.   {
  132.   students[i].id=-1;
  133.   //students[i].name[80]={0};
  134.   memset(students[i].name,0,sizeof(students[i].name));
  135.   }
  136. }*/
  137.  
  138. void load(FILE* fp,int select_num)
  139. {
  140. int i,j,k;
  141. i=0;
  142. //if(select_num==2)/*enter id*/
  143. {
  144. while(!feof(fp))
  145. {
  146. i=i+1;
  147. struct STUDENT* student_ptr=(struct STUDENT*)malloc(sizeof(struct STUDENT));
  148. // printf("%d\n",i);
  149. fscanf(fp,"%d",&(student_ptr->id));
  150. fscanf(fp,"%s",student_ptr->name);
  151. fscanf(fp,"%d",&(student_ptr->take_num));
  152. // printf("%d ",student_ptr->id);
  153. // printf("%d\n",student_ptr->take_num);
  154. for(j=1;j<=(student_ptr->take_num);j++)
  155. {
  156. fscanf(fp,"%d",&(student_ptr->takes[j]));
  157.  
  158. }
  159.  
  160. student_ptr->left_next=NULL;
  161. student_ptr->right_next=NULL;
  162.  
  163. make_tree(student_ptr,&head,select_num);
  164. //printf("%d\n",i);
  165. }
  166. traverse_tree(&head);
  167. }
  168.  
  169. }
  170.  
  171. int search(struct STUDENT students[],int id)
  172. {
  173. int i,index;
  174. int j;
  175. struct STUDENT temp;//資料暫存處
  176. index=-1;
  177. /*for(i=1;i<256;i++)
  178.   {
  179.   if(students[i].id==id)
  180.   {
  181.   index=i;
  182.   break;
  183.   }
  184.   }*/
  185.  
  186. //insertion sort
  187. for(i=2;i<256;i++)
  188. {
  189. /*temp.id=students[i].id;//暫存資料
  190.   temp.name=students[i].name;
  191.   temp.take_num=students[i].take_num;
  192.   temp.takes=students[i].takes;*/
  193. //printf("%d %d\n",i,students[i].id);
  194. temp=students[i];
  195. for(j=i-1;j>=1;j--)
  196. {
  197. if(students[j].id<temp.id)
  198. break;
  199. else if(students[j].id>=temp.id)
  200. {
  201. students[j+1]=students[j];
  202. }
  203. }
  204.  
  205. if(j==i-1)
  206. continue;
  207. else
  208. students[j+1]=temp;
  209. }/*insertion sort*/
  210.  
  211. for(i=1;i<256;i++)
  212. {
  213. //printf("%d %d\n",i,students[i].id);
  214. }
  215. //system("pause");
  216. //binary search student
  217. //index=b_search(students,id,1,255);
  218. printf("%d",index);
  219. return index;
  220. }
  221.  
  222. int search_by_name(struct STUDENT students[],char* name)
  223. {
  224. int i,j,index=-1;
  225. int comp_result;
  226. struct STUDENT temp;
  227. /*for(i=1;i<256;i++)
  228.   {
  229.   if(strcmp(students[i].name,name)==0)
  230.   {
  231.   index=i;
  232.   break;
  233.   }
  234.   } */
  235.  
  236. /*sorting string by bubble sort*/
  237. for(j=255;j>=2;j--)
  238. {
  239. for(i=1;i<j;i++)
  240. {
  241. comp_result=strcmp(students[i].name,students[i+1].name);
  242. if(comp_result==1)
  243. {
  244. temp=students[i];
  245. students[i]=students[i+1];
  246. students[i+1]=temp;
  247. }
  248. else if(comp_result==0||comp_result==-1)
  249. continue;
  250.  
  251. }
  252. }
  253. for(i=1;i<256;i++)
  254. {
  255. //printf("%d %s\n",i,students[i].name);
  256. }
  257. index=b_search_name(students,name,1,255);
  258. //printf("%d",index);
  259. //system("pause");
  260. return index;
  261. }
  262.  
  263.  
  264. void show(struct STUDENT students[],int id,int index)
  265. {
  266. int i;
  267. printf("您已選以下課程:\n");
  268. for(i=1;i<=students[index].take_num;i++)
  269. {
  270. printf("%d",students[index].takes[i]);
  271. printf(" ");
  272. }
  273. printf("\n");
  274. }
  275.  
  276. int add(struct STUDENT students[],int id,int index,int course)
  277. {
  278. int i;
  279. if(course>=101&&course<=105)
  280. {
  281. for(i=1;i<=students[index].take_num;i++)
  282. {
  283. if(students[index].takes[i]==course)
  284. return 2;
  285. }
  286. students[index].takes[i]=course;
  287. students[index].take_num=students[index].take_num+1;
  288. return 1;
  289. }
  290. return 3;
  291. }
  292.  
  293. void save(FILE* fp,struct STUDENT students[])
  294. {
  295. int i,j;
  296. for(i=1;i<256;i++)
  297. {
  298. if(students[i].id!=-1)
  299. {
  300. fprintf(fp,"%d",students[i].id);
  301. fprintf(fp,"\t");
  302. fprintf(fp,"%s",students[i].name);
  303. fprintf(fp,"\t");
  304. fprintf(fp,"%d",students[i].take_num);
  305. fprintf(fp,"\t");
  306. for(j=1;j<=students[i].take_num;j++)
  307. {
  308. fprintf(fp,"%d",students[i].takes[j]);
  309. fprintf(fp,"\t");
  310. }
  311. fprintf(fp,"\n");
  312. }
  313. }
  314. }
  315.  
  316. struct STUDENT* b_search(struct STUDENT* head,int id,char* name,int select_num)
  317. {
  318. //printf("%d ",select_num);
  319. if(select_num==1)
  320. {
  321. if(head==NULL)
  322. {
  323. printf("%s",head->name);
  324. return NULL;
  325. }
  326. else if(head!=NULL)
  327. {
  328. if(strcmp((head)->name,name)>0)
  329. {
  330. b_search(head->left_next,-1,name,1);
  331. }
  332. else if(strcmp((head)->name,name)<0)
  333. {
  334. b_search(head->right_next,-1,name,1);
  335. }
  336. else if(strcmp((head)->name,name)==0)
  337. {
  338. printf("%s %d ",head->name,head->id);
  339. return head;
  340. }
  341. }
  342. }
  343.  
  344. }
  345.  
  346. int b_search_name(struct STUDENT students[],char* name,int head,int tail)
  347. {
  348. int comp_result;
  349. int index=-1;
  350. if(head==tail)
  351. {
  352. if(strcmp(name,students[head].name)==0)
  353. return head;
  354. else
  355. return index;
  356. }
  357.  
  358. comp_result=strcmp(name,students[(head+tail)/2].name);
  359.  
  360. if(comp_result<0)
  361. {
  362. index=b_search_name(students,name,head,(head+tail)/2);
  363. return index;
  364. }
  365. else if(comp_result==0)
  366. {
  367. return (head+tail)/2;
  368. }
  369. else if(comp_result>0)
  370. {
  371. index=b_search_name(students,name,((head+tail)/2)+1,tail);
  372. return index;
  373. }
  374. }
  375.  
  376. void make_tree(struct STUDENT* student_ptr,struct STUDENT** head,int select_num)
  377. {
  378. if(select_num==1)
  379. {
  380. if(*head==NULL)
  381. {
  382. *head=student_ptr;
  383. }
  384. else if(*head!=NULL)
  385. {
  386. if(strcmp((*head)->name,student_ptr->name)>0)
  387. {
  388. make_tree(student_ptr,&((*head)->left_next),select_num);
  389. }
  390. else if(strcmp((*head)->name,student_ptr->name)<=0)
  391. {
  392. make_tree(student_ptr,&((*head)->right_next),select_num);
  393. }
  394. }
  395. }
  396. else if(select_num==2)
  397. {
  398. if(*head==NULL)
  399. {
  400. *head=student_ptr;
  401. // printf("%d\n",(*head)->id);
  402. /*current=student_ptr;
  403.   previous=student_ptr;*/
  404. }
  405. else if(*head!=NULL)
  406. {
  407. if((*head)->id>=student_ptr->id)
  408. {
  409. /*previous=current;
  410.   current=head->left_next;*/
  411. if((*head)->left_next==NULL)
  412. {
  413. (*head)->left_next=student_ptr;
  414. }
  415. else if((*head)->left_next!=NULL)
  416. {
  417. make_tree(student_ptr,&((*head)->left_next),select_num);
  418. }
  419. }
  420. else if((*head)->id<student_ptr->id)
  421. {
  422. if((*head)->right_next==NULL)
  423. {
  424. (*head)->right_next=student_ptr;
  425. }
  426. else if((*head)->right_next!=NULL)
  427. {
  428. make_tree(student_ptr,&((*head)->right_next),select_num);
  429. }
  430. }
  431. }
  432. }
  433. //printf("%s %d\n",head->name,head->id);
  434. }
  435.  
  436. void traverse_tree(struct STUDENT** head)
  437. {
  438. if(*head==NULL)
  439. {
  440. return;
  441. }
  442. else
  443. {
  444. printf("%s %d\n",(*head)->name,(*head)->id);
  445. traverse_tree(&((*head)->left_next));
  446. traverse_tree(&((*head)->right_next));
  447. }
  448. }
  449.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty