train_graph.c
资源名称:C数据结构课程设计.rar [点击查看]
上传用户:janny_wxd
上传日期:2010-02-03
资源大小:261k
文件大小:6k
源码类别:
控制台编程
开发平台:
C/C++
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #define OK 1
- #define TRUE 1
- #define ERROR 0
- #define OVERFLOW -2
- #define PR printf
- #define SIZE 10
- typedef int status;
- typedef struct time{
- int day;
- int hour;
- int minute;
- }time;
- typedef struct town{
- char name[9];
- time arrive;
- time leave;
- struct town *next_town;
- }town;
- typedef struct train{
- char train_num[8];
- char start_place[9];
- char end_place[9];
- int fare;
- int hour;
- struct train *next_train;
- struct town *next_town;
- }train;
- 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;
- }
- 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 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=0;
- 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};
- for(i=0;i<10;i++)
- {
- insert_train(l,train_num[i],start_place[i],end_place[i],fare[i],hour[i]);
- }
- return OK;
- }
- status print_train(train *l)
- {
- train *p=l;
- 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 );
- }
- return OK;
- }
- void main()
- {
- char ch,ch2,choice,start_place[9],end_place[9],train_num[8];
- int t=1,tt=1;
- train *tr=init_train();
- creat_train(tr);
- while(t==1)
- {
- PR("*---------------------------------------*n");
- PR("*--旅游交通查询系统选择菜单-------------*n");
- PR("* 火车信息查询---------0 *n");
- PR("* 城市路径查询---------1 *n");
- PR("* 查看所有信息---------2 *n");
- PR("* 退出-----------------3 *n");
- PR("*---------------------------------------*n");
- PR("请选择: ");
- ch = getch();
- PR("%cn",ch);
- if(ch=='0')
- {
- while(tt==1)
- {
- PR("*--火车信息查询选择菜单-----------------*n");
- PR("* 按车次查询--------------0 *n");
- PR("* 按出发地与目的地查询----1 *n");
- PR("* 退出--------------------2 *n");
- PR("*---------------------------------------*n");
- PR("请选择: ");
- ch2 = getch();
- PR("%cn",ch2);
- if(ch2=='0')
- {
- PR("请输入列车号: ");
- scanf("%s",train_num);
- find_train_num(tr,train_num);
- }
- else if(ch2=='1')
- {
- PR("请输入始发站: ");
- scanf("%s",start_place);
- PR("请输入终点站: ");
- scanf("%s",end_place);
- PR("*----------------------------*n");
- PR("*--按车次查询选择菜单------*n");
- PR("* 最快-------0 *n");
- PR("* 最省钱-----1 *n");
- PR("* 全部-------2 *n");
- PR("* 退出-------3 *n");
- PR("*----------------------------*n");
- PR("请选择: ");
- choice = getch();
- PR("%cn",choice);
- find_place(tr,start_place,end_place,choice);
- }
- else if(ch2=='2'){
- tt=0;
- }
- else {
- PR("你的输入有误n");
- }
- }//end while
- }//end ch=0:火车信息查询
- else if(ch=='1')
- {
- PR("有待完善!n");
- }
- else if(ch=='2')
- {
- print_train(tr);
- }
- else if(ch=='3')
- {
- t=0;
- }
- else {
- PR("你的输入有误n");
- }
- }//end while
- }