train.c
资源名称:C数据结构课程设计.rar [点击查看]
上传用户:janny_wxd
上传日期:2010-02-03
资源大小:261k
文件大小:5k
源码类别:
控制台编程
开发平台:
C/C++
- #include"train.h"
- train *init_train(){//初始化链表
- train *l;
- l=(train*)malloc(sizeof(train));
- if(l==NULL){
- exit(OVERFLOW);
- }
- l->next_town =NULL;
- l->next_train =NULL;
- return l;
- }
- town *init_town(){//初始化链表
- town *l;
- l=(town*)malloc(sizeof(town));
- if(l==NULL){
- exit(OVERFLOW);
- }
- l->next_town =NULL;
- return l;
- }
- status insert_train(train *l, char *train_num,char *start_place,char *end_place,int fare,int hour)
- {
- train *p;
- p=(train*)malloc(sizeof(train));
- strcpy(p->train_num ,train_num);
- strcpy(p->start_place ,start_place);
- strcpy(p->end_place ,end_place);
- p->fare =fare;
- p->hour =hour;
- p->next_town =NULL;
- p->next_train =l->next_train ;
- l->next_train =p;
- return OK;
- }
- status insert_town(train *l, char *name,time_train arrive,time_train leave)
- {
- town *p;
- p=(town*)malloc(sizeof(town));
- strcpy(p->name ,name);
- p->arrive .day =arrive.day ;
- p->arrive .hour =arrive.hour ;
- p->arrive .minute =arrive.minute ;
- p->leave .day =leave.day ;
- p->leave .hour =leave.hour ;
- p->leave .minute =leave.minute;
- p->next_town =NULL;
- p->next_town =l->next_town ;
- l->next_town =p;
- 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);
- return OK;
- }
- status find_train_num(train *l,char *train_num)
- {
- train *p;
- p=l->next_train;
- for(;p!=NULL;p=p->next_train )
- {
- if(strcmp(train_num,p->train_num )==0)
- {
- PR(" 列车号 始发站 终点站 车费 时间n");
- PR("%8s%8s%8s%8d元%8d小时n",p->train_num ,p->start_place ,p->end_place ,p->fare ,p->hour );
- return OK;
- }
- }
- PR("对不起,无 %s 列车!n",train_num);
- return ERROR;
- }
- status find_place(train *l,char *start_place,char *end_place,char choice)
- { //choice 0:最快,1:最省钱,2:全部
- train *p;
- train *q[SIZE];
- train *r;
- int i=0,j=0;
- p=l->next_train;
- for(;p!=NULL;p=p->next_train )
- {
- if(strcmp(start_place,p->start_place)==0&&strcmp(end_place,p->end_place)==0)
- {
- // PR("%s,%s,%s,%d,%dn",p->train_num ,p->start_place ,p->end_place ,p->fare ,p->hour );
- q[i]=p;
- i++;
- }
- }
- if(i>0)
- {
- if(choice=='0')
- {
- r=q[0];
- for(j=1;j<i;j++)
- {
- if(q[j]->hour <r->hour )
- {
- r=q[j];
- }
- }
- PR(" 列车号 始发站 终点站 车费 时间n");
- PR("%8s%8s%8s%8d元%8d小时n",r->train_num ,r->start_place ,r->end_place ,r->fare ,r->hour );
- return OK;
- }
- else if (choice=='1')
- {
- r=q[0];
- for(j=1;j<i;j++)
- {
- if(q[j]->fare < r->fare )
- {
- r=q[j];
- }
- }
- PR(" 列车号 始发站 终点站 车费 时间n");
- PR("%8s%8s%8s%8d元%8d小时n",r->train_num ,r->start_place ,r->end_place ,r->fare ,r->hour );
- return OK;
- }
- else if (choice=='2')
- {
- for(j=0;j<i;j++)
- {
- PR(" 列车号 始发站 终点站 车费 时间n");
- 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 );
- }
- }
- }
- else {//当没有一辆时.
- PR("无符合条件的列车!n");
- return ERROR;
- }
- return ERROR;
- }
- status creat_train(train *l)
- {
- int i,j,num,town_num;
- char train_num[9];
- char start_place[9];
- char end_place[9];
- int fare;
- int hour;
- time_train arrive;
- time_train leave;
- int day_a,day_l,hour_a,hour_l,minute_a,minute_l;
- char name[9];
- /* char *train_num[]={"T167","T168","1363","1364","37","38","K701","K702","T61","T62"};
- char *start_place[]={"北京","南昌","北京","成都","北京","武汉","北京","太原","北京","贵阳"};
- char *end_place[]={"南昌","北京","成都","北京","武汉","北京","太原","北京","贵阳","北京"};
- int fare[]={170,170,150,150,140,140,40,40,201,201};
- int hour[]={14,14,22,22,11,11,5,5,27,27};*/
- FILE *f;
- char filename[]="f:\train3.txt";
- if((f=fopen(filename,"r"))==NULL)
- {
- printf("can not open file to read(fscanf):%sn",filename);
- return ERROR;
- }
- fscanf(f,"%d",&num);
- PR("num:%dn",num);
- for(i=0;i<num;i++)
- {
- fscanf(f,"%d",&town_num);
- // PR("town_num: %dn",town_num);
- fscanf(f,"%s%s%s%d%d",train_num ,start_place ,end_place ,&fare ,&hour );
- PR("%s%s%s%d%dn",train_num ,start_place ,end_place ,fare ,hour );
- insert_train(l,train_num ,start_place ,end_place ,fare ,hour );
- for(j=0;j<town_num;j++)
- {
- fscanf(f,"%s%d%d%d%d%d%d",name,&day_a,&day_l,&hour_a,&hour_l,&minute_a,&minute_l);
- PR("%s,%s,%d,%d,%d,%d,%d,%dn",train_num,name,day_a,day_l,hour_a,hour_l,minute_a,minute_l);
- arrive.day =day_a;
- arrive.hour =hour_a;
- arrive.minute =minute_a;
- leave.day =day_l;
- leave.hour =hour_l;
- leave.minute =minute_l;
- if(l->next_train ==NULL)
- {
- insert_town(l,name,arrive,leave);
- }
- else insert_town(l->next_train,name,arrive,leave);
- }//end for j
- }
- fclose(f);
- return OK;
- }
- status print_train(train *l)
- {
- train *p=l;
- town *q;
- PR(" 列车号 始发站 终点站 车费 时间n");
- for(p=p->next_train ;p!=NULL;p=p->next_train )
- {
- PR("%8s%8s%8s%8d元%8d小时n",p->train_num ,p->start_place ,p->end_place ,p->fare ,p->hour );
- for (q=p->next_town;q!=NULL;q=q->next_town)
- {
- PR("车站 到达时间 开出时间n");
- // PR("%s",q->name );
- 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);
- }
- PR("n");
- }
- return OK;
- }