train.c
上传用户:janny_wxd
上传日期:2010-02-03
资源大小:261k
文件大小:5k
源码类别:

控制台编程

开发平台:

C/C++

  1. #include"train.h"
  2. train *init_train(){//初始化链表
  3. train *l;
  4. l=(train*)malloc(sizeof(train));
  5. if(l==NULL){
  6. exit(OVERFLOW);
  7. }
  8. l->next_town =NULL;
  9. l->next_train =NULL;
  10. return l;
  11. }
  12. town *init_town(){//初始化链表
  13. town *l;
  14. l=(town*)malloc(sizeof(town));
  15. if(l==NULL){
  16. exit(OVERFLOW);
  17. }
  18. l->next_town =NULL;
  19. return l;
  20. }
  21. status insert_train(train *l, char *train_num,char *start_place,char *end_place,int fare,int hour)
  22. {
  23. train *p;
  24. p=(train*)malloc(sizeof(train));
  25. strcpy(p->train_num ,train_num);
  26. strcpy(p->start_place ,start_place);
  27. strcpy(p->end_place ,end_place);
  28. p->fare =fare;
  29. p->hour =hour;
  30. p->next_town =NULL;
  31. p->next_train =l->next_train ;
  32. l->next_train =p;
  33. return OK;
  34. }
  35. status insert_town(train *l, char *name,time_train arrive,time_train leave)
  36. {
  37. town *p;
  38. p=(town*)malloc(sizeof(town));
  39. strcpy(p->name ,name);
  40. p->arrive .day =arrive.day ;
  41. p->arrive .hour =arrive.hour ;
  42. p->arrive .minute =arrive.minute ;
  43. p->leave .day =leave.day ;
  44. p->leave .hour =leave.hour ;
  45. p->leave .minute =leave.minute;
  46. p->next_town =NULL;
  47. p->next_town =l->next_town ;
  48. l->next_town =p;
  49. PR("%s,%d,%d,%d,%d,%d,%dn",p->name,p->arrive .day ,p->arrive .hour ,p->arrive.minute ,p->leave .day ,p->leave .hour ,p->leave .minute);
  50. return OK;
  51. }
  52. status find_train_num(train *l,char *train_num)
  53. {
  54. train *p;
  55. p=l->next_train;
  56. for(;p!=NULL;p=p->next_train )
  57. {
  58. if(strcmp(train_num,p->train_num )==0)
  59. {
  60. PR("   列车号  始发站  终点站    车费        时间n");
  61. PR("%8s%8s%8s%8d元%8d小时n",p->train_num ,p->start_place ,p->end_place ,p->fare ,p->hour );
  62. return OK;
  63. }
  64. }
  65. PR("对不起,无 %s 列车!n",train_num);
  66. return ERROR;
  67. }
  68. status find_place(train *l,char *start_place,char *end_place,char choice)
  69. {                            //choice 0:最快,1:最省钱,2:全部
  70. train *p;
  71. train *q[SIZE];
  72. train *r;
  73. int i=0,j=0;
  74. p=l->next_train;
  75. for(;p!=NULL;p=p->next_train  )
  76. {
  77. if(strcmp(start_place,p->start_place)==0&&strcmp(end_place,p->end_place)==0)
  78. {
  79. // PR("%s,%s,%s,%d,%dn",p->train_num ,p->start_place ,p->end_place ,p->fare ,p->hour );
  80. q[i]=p;
  81. i++;
  82. }
  83. }
  84. if(i>0)
  85. {
  86. if(choice=='0')
  87. {
  88. r=q[0];
  89. for(j=1;j<i;j++)
  90. {
  91. if(q[j]->hour <r->hour )
  92. {
  93. r=q[j];
  94. }
  95. }
  96. PR("   列车号  始发站  终点站    车费        时间n");
  97. PR("%8s%8s%8s%8d元%8d小时n",r->train_num ,r->start_place ,r->end_place ,r->fare ,r->hour );
  98. return OK;
  99. }
  100. else if (choice=='1')
  101. {
  102. r=q[0];
  103. for(j=1;j<i;j++)
  104. {
  105. if(q[j]->fare < r->fare )
  106. {
  107. r=q[j];
  108. }
  109. }
  110. PR("   列车号  始发站  终点站    车费        时间n");
  111. PR("%8s%8s%8s%8d元%8d小时n",r->train_num ,r->start_place ,r->end_place ,r->fare ,r->hour );
  112. return OK;
  113. }
  114. else if (choice=='2')
  115. {
  116. for(j=0;j<i;j++)
  117. {
  118. PR("   列车号  始发站  终点站    车费        时间n");
  119. PR("%8s%8s%8s%8d元%8d小时n",q[j]->train_num ,q[j]->start_place ,q[j]->end_place ,q[j]->fare ,q[j]->hour );
  120. }
  121. }
  122. }
  123. else {//当没有一辆时.
  124. PR("无符合条件的列车!n");
  125. return ERROR;
  126. }
  127. return ERROR;
  128. }
  129. status creat_train(train *l)
  130. {
  131. int i,j,num,town_num;
  132. char train_num[9];
  133. char start_place[9];
  134. char end_place[9];
  135. int fare;
  136. int hour;
  137. time_train arrive;
  138. time_train leave;
  139. int day_a,day_l,hour_a,hour_l,minute_a,minute_l;
  140. char name[9];
  141. /* char *train_num[]={"T167","T168","1363","1364","37","38","K701","K702","T61","T62"};
  142. char *start_place[]={"北京","南昌","北京","成都","北京","武汉","北京","太原","北京","贵阳"};
  143. char *end_place[]={"南昌","北京","成都","北京","武汉","北京","太原","北京","贵阳","北京"};
  144. int fare[]={170,170,150,150,140,140,40,40,201,201};
  145. int hour[]={14,14,22,22,11,11,5,5,27,27};*/
  146. FILE *f;
  147. char filename[]="f:\train3.txt";
  148. if((f=fopen(filename,"r"))==NULL)
  149. {
  150. printf("can not open file to read(fscanf):%sn",filename);
  151. return ERROR;
  152. }
  153. fscanf(f,"%d",&num);
  154. PR("num:%dn",num);
  155. for(i=0;i<num;i++)
  156. {
  157. fscanf(f,"%d",&town_num);
  158. // PR("town_num: %dn",town_num);
  159. fscanf(f,"%s%s%s%d%d",train_num ,start_place ,end_place ,&fare ,&hour );
  160. PR("%s%s%s%d%dn",train_num ,start_place ,end_place ,fare ,hour );
  161. insert_train(l,train_num ,start_place ,end_place ,fare ,hour );
  162. for(j=0;j<town_num;j++)
  163. {
  164. fscanf(f,"%s%d%d%d%d%d%d",name,&day_a,&day_l,&hour_a,&hour_l,&minute_a,&minute_l);
  165. PR("%s,%s,%d,%d,%d,%d,%d,%dn",train_num,name,day_a,day_l,hour_a,hour_l,minute_a,minute_l);
  166. arrive.day =day_a;
  167. arrive.hour =hour_a;
  168. arrive.minute =minute_a;
  169. leave.day =day_l;
  170. leave.hour =hour_l;
  171. leave.minute =minute_l;
  172. if(l->next_train ==NULL)
  173. {
  174. insert_town(l,name,arrive,leave);
  175. }
  176. else insert_town(l->next_train,name,arrive,leave);
  177. }//end for j
  178. }
  179. fclose(f);
  180. return OK;
  181. }
  182. status print_train(train *l)
  183. {
  184. train *p=l;
  185. town *q;
  186. PR("   列车号  始发站  终点站    车费        时间n");     
  187. for(p=p->next_train ;p!=NULL;p=p->next_train )
  188. {
  189. PR("%8s%8s%8s%8d元%8d小时n",p->train_num ,p->start_place ,p->end_place ,p->fare ,p->hour );
  190. for (q=p->next_town;q!=NULL;q=q->next_town)
  191. {
  192. PR("车站 到达时间 开出时间n");
  193. // PR("%s",q->name );
  194. PR( "%s%d(天):%d:%d%d(天):%d:%d",q->name,q->arrive .day ,q->arrive .hour ,q->arrive.minute ,q->leave .day ,q->leave .hour ,q->leave .minute);
  195. }
  196. PR("n");
  197. }
  198. return OK;
  199. }