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