-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.c
1253 lines (1136 loc) · 29.8 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_VERTEX_NUM 200
#define HEADER1 "--------------------------------广州地铁线路----------------------------------\n"
#define HEADER2 "|NO| 线路名称 | 起点站 | 终点站 |站点数|起运时间|收班时间| 运行时长 |票价|\n"
#define HEADER3 "|--|----------|----------|----------|------|--------|--------|----------|----|\n"
#define HEADER4 "------------------------------------------------------------------------------\n"
#define HEADER5 "--------------------------------------------\n"
#define HEADER6 "| 站点名称 | 是否可换乘 | 所属线路编号 |\n"
#define FORMAT "|%-2d|%-10s|%-10s|%-10s|%-6d|%-8s|%-8s|%-10s|%-4d|\n"
#define DATA p->data.NO,p->data.name,p->data.start,p->data.destination,p->data.number,p->data.starttime,p->data.endtime,p->data.totaltime,p->data.price
int MAX_LINE_NUM=8;
int istrans[MAX_VERTEX_NUM]={0};
typedef struct line
{
int NO; //线路编号//
char name[15]; //线路名称//
char start[15]; //起点站//
char destination[15]; //终点站//
int number; //站点数//
char starttime[10]; //起运时间//
char endtime[10]; //收班时间//
char totaltime[10]; //运行时长//
int price; //票价//
}LINE;
typedef struct node //line结构体接口//
{
LINE data;
struct node *next;
}NODE,*NODELINE;
typedef struct enode_type
{
int adjvex; //邻接点的位置//
int weight; //权值(即线路编号)//
struct enode_type *nextarc; //指向下一个邻接点//
}enode;
typedef struct vnode_type
{
int id; //顶点的编号//
char name[20]; //顶点的名字,即站的名字//
enode *fadj; //指向第一个点//
}vnode;
typedef struct Graph_type
{
vnode vertices[MAX_VERTEX_NUM]; //顶点集合(vertex)//
int vexnum; //图的顶点数目//
int arcnum; //图的边数目//
}Graph;
typedef struct pathnode_type
{
int top; //路径的数目//
int path[MAX_VERTEX_NUM]; //路径信息//
int trans[MAX_VERTEX_NUM]; //换乘点信息,0为非换乘点,1为换乘点//
int counter; //换乘的次数//
struct pathnode_type *next; //指向下一个可抵达终点的路径信息节点//
}pathnode;
typedef struct LINK_type
{
pathnode *head; //路径接口//
}LINK;
int initialize();
//菜单//
void menu();
//功能一:显示线路信息//
void Initline();
//功能一:读取线路信息//
void readfromfile_line(NODELINE L);
//输出表头//
void printheader();
//打印数据//
void printdata(NODE *q);
//功能一:显示线路信息接口//
void showline(NODELINE L);
//获取站点的编号,从1-130//
int locatevex(Graph *G,char *a);
//获取当前线路号//
int getline(Graph *G,int i,int j);
//获取换乘点//
void gettrans(Graph *G,pathnode *all);
//建立带无向网络//
void create_graph(Graph *G);
//DFS深度优先搜索出所有可能的路线走法//
void DFS(Graph *G,int stack[],int visited[],int v,int destination,int top,LINK *L);
//输出最少换乘路径信息//
void print_lesstransfer(Graph *G,LINK *L);
//最少换乘的函数接口,参数1:起始站,参数2:终点站//
void lesstransfer(char start[],char destination[]);
//创建一个路径链表//
void create_path(LINK *L);
//插入一个路径信息//
void addpath(LINK *L,pathnode *p);
//计费,(按每个站点之间1公里简易计算,与实际有很大差别)//
int caculate_price(int station);
//乘车时间
int caculate_time(int station,int count);
//站点查询线路,可实现换乘站点自动判断
int station_search_line(char station[],int parameter);
void deletedata();
void inputdata();
void save_line_information(NODELINE L);
void xiugaidata();
int main()
{
int num;
int label;
char start[20],destination[20];
if(initialize()==0) exit(0);
while(1)
{
menu();
printf("请输入<0-7>:");
scanf("%d",&label);
if(label==0) {printf("感谢您的使用!\n"); exit(0);}
switch(label)
{
case 1:
Initline();
break;
case 2:
inputdata();
break;
case 3:
xiugaidata();
break;
case 4:
printf("4\n");
deletedata();
break;
case 5:
printf("请输入起始站:");
scanf("%s",start);
printf("请输入终点站:");
scanf("%s",destination);
if(strcmp(start,destination)==0)
printf("您输入的站点名相同!");
else
lesstransfer(start,destination);//最小换乘接口//
break;
case 6:
//可实现换乘站点的自动判断
printf("请输入站点名:");
scanf("%s",start);
if(station_search_line(start,1)==-1)
printf("未查询到%s站的信息!\n",start);
break;
default:
fflush(stdin);
printf("您输入的指令有误!\n");
}
printf("\n按任意键继续...\n");
getch();
}
return 0;
}
void menu()
{
printf("\t|-----------------------------------------------|\n");
printf("\t| 广州地铁查询系统 |\n");
printf("\t|-----------------------------------------------|\n");
printf("\t| 0:退出系统 |\n");
printf("\t| 1:显示地铁线路 |\n");
printf("\t| 2:录入地铁线路 |\n");
printf("\t| 3:修改地铁线路 |\n");
printf("\t| 4:删除地铁线路 |\n");
printf("\t| 5:换乘费用查询 |\n");
printf("\t| 6:站点名查线路 |\n");
printf("\t|-----------------------------------------------|\n");
}
int initialize()
{
int flag=1;
FILE *fp;
fp=fopen("stationinfo.txt","r");
if(fp==NULL) {printf("缺失stationinfo.txt\n");flag=0;}
fclose(fp);
fp=fopen("station.txt","r");
if(fp==NULL) {printf("缺失station.txt\n");flag=0;}
fclose(fp);
fp=fopen("stationdata.txt","r");
if(fp==NULL) {printf("缺失stationdata.txt\n");flag=0;}
fclose(fp);
fp=fopen("line.txt","r");
if(fp==NULL) {printf("缺失line.txt\n");flag=0;}
fclose(fp);
fp=fopen("line.txt","r");
fscanf(fp,"%d%d",&MAX_LINE_NUM);
fclose(fp);
if(flag==0) exit(0);
else return flag;
}
void Initline()
{
int i,j,n,temp,k;
int a[MAX_VERTEX_NUM]={0};
int b[MAX_VERTEX_NUM]={0};
int c[MAX_VERTEX_NUM]={0};
char ch;
NODELINE L,p;
Graph G;
FILE *fp;
L=(NODE *)malloc(sizeof(NODE));
L->next=NULL;
readfromfile_line(L);
showline(L);
printf("是否显示所有地铁站点?<Y or N>");
getchar();
scanf("%c",&ch);
if(ch=='Y' || ch=='y')
{
create_graph(&G);
fp=fopen("stationdata.txt","r");
for(i=1;i<=MAX_VERTEX_NUM;i++)
{
fscanf(fp,"%d %d %d",&a[i],&b[i],&c[i]);
}
temp=1;
L=L->next;
for(i=1;i<=MAX_LINE_NUM;i++)
{
printf("-----------------------------------地铁%d号线-----------------------------------\n",L->data.NO);
for(j=temp;c[j]==i;j++)
{
printf("%s->",G.vertices[a[j]].name);
}
printf("%s\n",G.vertices[b[--j]].name);
temp=++j;
L=L->next;
}
fclose(fp);
}
else
return;
}
void readfromfile_line(NODELINE L)
{
int NO; /*线路编号*/
char name[15]; /*线路名称*/
char start[15]; /*起点站*/
char destination[15]; /*终点站*/
int number; /*站点数*/
char starttime[10]; /*起运时间*/
char endtime[10]; /*收班时间*/
char totaltime[10]; /*运行时长*/
int price; /*票价*/
int i,j;
int counter;
FILE *fp;
NODELINE r,p;
//NODESTATION S;
r=L;
fp=fopen("line.txt","r");
fscanf(fp,"%d\n",&counter);
for(i=1;i<=counter;i++)
{
p=(NODE *)malloc(sizeof(NODE));
fscanf(fp,"%d\n",&NO);
fscanf(fp,"%s\n",name);
fscanf(fp,"%s\n",start);
fscanf(fp,"%s\n",destination);
fscanf(fp,"%d\n",&number);
fscanf(fp,"%s\n",starttime);
fscanf(fp,"%s\n",endtime);
fscanf(fp,"%s\n",totaltime);
fscanf(fp,"%d\n",&price);
p->data.NO=NO;
strcpy(p->data.name,name);
strcpy(p->data.start,start);
strcpy(p->data.destination,destination);
p->data.number=number;
strcpy(p->data.starttime,starttime);
strcpy(p->data.endtime,endtime);
strcpy(p->data.totaltime,totaltime);
p->data.price=price;
p->next=NULL;
r->next=p;
r=p;
}
fclose(fp);
}
void printheader()
{
printf(HEADER1);
printf(HEADER2);
printf(HEADER3);
}
void printdata(NODE *q)
{
NODE *p;
p=q;
printf(FORMAT,DATA);
//printf("%s",p->data.destination);
}
void showline(NODELINE L)
{
NODELINE r;
r=L;
printheader();
if(r->next==NULL) printf("无地铁线路!\n");
while(r->next!=NULL)
{
r=r->next;
printdata(r);
}
printf(HEADER4);
}
//最少换乘的函数接口,参数1:起始站,参数2:终点站//
void lesstransfer(char start[],char destination[])
{
Graph G;
LINK L;
int v1,v2;
int stack[MAX_VERTEX_NUM]={0}; //初始化
int visited[MAX_VERTEX_NUM]={0};
create_graph(&G); //创建一个无向网络//
create_path(&L); //创建保存路径的链表//
v1=locatevex(&G,start);
v2=locatevex(&G,destination);
if(v1==-1 || v2==-1)
{
printf("您输入的站名有误!\n");
return;
}
DFS(&G,stack,visited,v1,v2,0,&L);
print_lesstransfer(&G,&L);
}
//获取站点的编号,从1-130//
int locatevex(Graph *G,char a[])
{
int i=1;
while(strcmp(G->vertices[i].name,a)&&i<=G->vexnum)
i++;
if(i<=G->vexnum)
return i;
else
return -1;
}
//创建一个无向网络
void create_graph(Graph *G)
{
enode * p;
int i,j,k,weight;
FILE *fp;
fp=fopen("stationinfo.txt","r");
fscanf(fp,"%d%d",&G->vexnum,&G->arcnum);
fclose(fp);
fp=fopen("station.txt","r");
for(i=1;i<=G->vexnum;i++)
{
fscanf(fp,"%s",G->vertices[i].name);
G->vertices[i].id=i;
G->vertices[i].fadj=NULL;
//利用头插法插入结点
}
fclose(fp);
fp=fopen("stationdata.txt","r");
for (k=1;k<=G->arcnum;k++)
{
fscanf(fp,"%d %d %d",&i,&j,&weight);
p=(enode *)malloc(sizeof(enode));
p->adjvex=j;
p->weight=weight;
p->nextarc=G->vertices[i].fadj;
G->vertices[i].fadj=p;
//无向网络,双向邻接
p=(enode *)malloc(sizeof(enode));
p->adjvex=i;
p->weight=weight;
p->nextarc=G->vertices[j].fadj;
G->vertices[j].fadj=p;
}
fclose(fp);
}
/*利用深度搜索的递归找出所有的路径
参数一:图 参数2:临时存放路径顺序数组 参数3:是否访问 参数4:当前位置 参数5:到达的位置 参数6:栈顶 参数7:所有路径的链表*/
void DFS(Graph *G,int stack[],int visited[],int v,int destination,int top,LINK *L)
{
int j,i;
enode *p;
pathnode *all;
stack[top]=v;
if(v==destination)
{
all=(pathnode *)malloc(sizeof(pathnode));
all->counter=0;
all->top=top;
for(i=0;i<=top;i++)
{
all->trans[i]=0;
all->path[i]=stack[i];
}
gettrans(G,all);
addpath(L,all);
return ;//递归边界条件
}
else
visited[v]=1;
p=G->vertices[v].fadj;
while(p)
{
if(!visited[p->adjvex])
DFS(G,stack,visited,p->adjvex,destination,top+1,L);
p=p->nextarc;
}
stack[top]=0;
visited[v]=0;
}
//输出路径//
void print_lesstransfer(Graph *G,LINK *L)
{
int maximum=999;
int line,k;
int count=1;
int i=0,j;
pathnode *p,*r;
p=L->head;
while(p->next!=NULL)
{
p=p->next;
if(p->counter<=maximum)
maximum=p->counter;
}
r=L->head;
while(r->next!=NULL)
{
r=r->next;
if(r->counter==maximum)
{
i=0;
printf("\n最少换乘线路:\n");
printf("------------------------->-------------------------->------------------------->\n");
for(k=0;k<(r->top);k++)
{
if(r->trans[k])
{
printf("%s(在此换乘%d号线)---->",G->vertices[r->path[k]].name,G->vertices[r->path[k]].fadj->weight);
i++;
}
else
{
line=G->vertices[r->path[k]].fadj->weight;
printf("%s(%d|%d号线)-->",G->vertices[r->path[k]].name,G->vertices[r->path[k]].id,line);
i++;
}
}
printf("%s(终点)\n",G->vertices[r->path[(r->top)]].name);
printf("-------------------------------------------------------------------------------\n");
if(maximum==0)
printf("不需要换乘\n");
else
printf("需要换乘:%d次\n",maximum);
printf("经过站点:%d个\n",i);
printf("乘车时间:%d分钟\n",caculate_time(i,maximum));
printf("票价为:%d元\n",caculate_price(i));
break;//为了避免重复,直接跳出
}
}
}
void create_path(LINK *L)
{
//新建链表操作//
L->head=(pathnode *)malloc(sizeof(pathnode));
L->head->next=NULL;
}
void addpath(LINK *L,pathnode *p)
{
//链表的插入操作//
p->next=L->head->next;
L->head->next=p;
}
//求出同一条边上的两点间的线路号//
int getline(Graph *G,int i,int j)
{
enode *p=G->vertices[i].fadj;
while(p)
{
if(p->adjvex==j)
return p->weight;
p=p->nextarc;
}
return 0;
}
//求出每条路线上的转乘次数//
void gettrans(Graph *G,pathnode *all)
{
int i;
for(i=0;i<all->top-1;i++)
if(getline(G,all->path[i],all->path[i+1])!=getline(G,all->path[i+1],all->path[i+2]))
{
all->trans[i+1]=1;
all->counter++;
}
}
//简易价格计算//
int caculate_price(int station)
{
if(station<=4)
return 2;
else if (station>4 && station<=12)
return (2+(station%4-1));
else if (station>12 && station<=24)
return (4+(station%4-1));
else
return 7;
}
//乘车时间计算//
int caculate_time(int station,int count)
{
return (3*station+count*6);
}
//站点名查找线路信息 自动判断换乘点 //
int station_search_line(char station[],int parameter)
{
Graph G;
enode *p;
FILE *fp;
int line[2]={0};
int num,i,count,clear,l=0;
int a[MAX_VERTEX_NUM]={0};
int b[MAX_VERTEX_NUM]={0};
int c[MAX_VERTEX_NUM]={0};
create_graph(&G);
fp=fopen("stationinfo.txt","r");
fscanf(fp,"%d%d",&clear,&count);
fclose(fp);
num=locatevex(&G,station);
fp=fopen("stationdata.txt","r");
for(i=1;i<=count;i++)
fscanf(fp,"%d %d %d",&a[i],&b[i],&c[i]);
fclose(fp);
for(i=1;i<=count;i++)
{
if(num==b[i])
{
line[l++]=c[i];
}
}
// p=G.vertices[num].fadj;
if(line[1]==0 && line[0]==0)
return -1;
if(parameter==1)
{
if(line[1]==0)
{
printf(HEADER5);
printf(HEADER6);
printf(HEADER5);
printf("|%12s|%12s|%12d号线|\n",station,"否",line[0]);
printf(HEADER5);
}
//printf("%s站位于地铁%d号线\n",station,line[0]);
else
{
printf(HEADER5);
printf(HEADER6);
printf(HEADER5);
printf("|%-12s|%-12s| %d号线和%d号线 |\n",station,"是",line[0],line[1]);
printf(HEADER5);
}
//printf("%s站是换乘站,位于地铁%d号线和地铁%d线\n",station,line[0],line[1]);
}
else if(parameter==0)
{
if(line[1]==0)
return 0;
else
return 1;
}
else
printf("参数传递错误!\n");
}
//添加数据
void inputdata()
{
Graph G,G1;
FILE *fp;
NODELINE L,p,r,s;
int vexnum,arcnum;
char station[20];
int istrans;
int i,j,k,n;
char ch;
int stationnum;
int newroute[100];
char newroutename[100][20];
int NO; //线路编号//
char name[15]; //线路名称//
char start[15]; //起点站//
char destination[15]; //终点站//
int number; //站点数//
char starttime[10]; //起运时间//
char endtime[10]; //收班时间//
char totaltime[10]; //运行时长//
int price; //票价//
char str[20];
int ret;
fp=fopen("stationinfo.txt","r");
fscanf(fp,"%d%d",&vexnum,&arcnum);
fclose(fp);
fp=fopen("line.txt","r");
fscanf(fp,"%d",&MAX_LINE_NUM);
fclose(fp);
vexnum++;//新的站点的序号
L=(NODE *)malloc(sizeof(NODE));
L->next=NULL;
readfromfile_line(L);
create_graph(&G);
G1=G;
/* *********************************以下为录入线路信息***************************** */
r=L;
while(r->next!=NULL)
r=r->next;
label: printf("请输入新的线路编号(0-返回):",MAX_LINE_NUM+1);//考虑不为负数
ret=scanf("%d",&NO);
if(NO==0) return;
while(ret!=1)
{
lab:fflush(stdin);
printf("请正确输入线路编号:");
ret=scanf("%d",&NO);
}
if(NO!=MAX_LINE_NUM+1)
{
printf("输入的线路编号应为现有线路编号的下一位!(%d号)\n",MAX_LINE_NUM+1); goto lab;
}
s=L;
while(s)
{
if(s->data.NO==NO)
{
printf("该地铁线路编号已存在!\n");
getchar();
goto label;
}
s=s->next;
}
p=(NODE *)malloc(sizeof(NODE));
p->data.NO=NO;
printf("请输入地铁线路名称:");
scanf("%s",p->data.name);
printf("请输入起始站:");
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
if(isdigit(str[i]))
{
fflush(stdin);
printf("站点名不能带有数字!\n请重新输入起始站:");
scanf("%s",str);
break;
}
strcpy(p->data.start,str);
//scanf("%s",p->data.start);
memset(str,0,sizeof(str));
printf("请输入终点站:");
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
{
if(isdigit(str[i]))//判断是否数字
{
fflush(stdin);//清空文件缓冲
printf("站点名不能带有数字!\n请重新输入终点站:");
scanf("%s",str);
break;
}
}
strcpy(p->data.destination,str);
//scanf("%s",p->data.destination);
memset(str,0,sizeof(str));
printf("请输入站点数:");
ret=scanf("%d",&p->data.number);
while(ret!=1)
{
label1:fflush(stdin);
printf("请正确输入站点数:");
ret=scanf("%d",&p->data.number);
}
if(p->data.number<0) { printf("请输入一个大于0的数字!\n"); goto label1; }
printf("请输入起运时间:");
scanf("%s",p->data.starttime);
printf("请输入收班时间:");
scanf("%s",p->data.endtime);
printf("请输入运行时长:");
scanf("%s",p->data.totaltime);
printf("请输入票价:");
ret=scanf("%d",&p->data.price);
while(ret!=1)
{
label2:fflush(stdin);
printf("请正确输入票价:");
ret=scanf("%d",&p->data.price);
}
if((p->data.price<0)||(p->data.price>30)) { printf("请输入一个大于0且小于30的数字!\n"); goto label2; }
p->next=NULL;
r->next=p;
r=p;
MAX_LINE_NUM++;
//arcnum+=number;
/* *********************************以上为录入线路信息***************************** */
/* *********************************以下为录入站点信息***************************** */
printf("请输入地铁%d号线的站点信息:\n",r->data.NO);
stationnum=r->data.number;
for(i=1;i<=stationnum;i++)
{
printf("请输入第%d个站点的名称:",i);
scanf("%s",station);
for(j=1;j<=MAX_VERTEX_NUM;j++)
{
if(strcmp(G.vertices[j].name,station)==0)
{
printf("该站点是否为换乘点<0-1>?");//站点名重复,要么是换乘点,要么是站点名重复输错
scanf("%d",&istrans);
if(istrans)
{
newroute[i]=locatevex(&G,station);//寻找第一次出现的地方
strcpy(newroutename[i],"0");//如果有把它置为0
vexnum--;
break;
}
else
{
while(strcmp(G.vertices[j].name,station)==0)
{
printf("站点名重复!请重新输入第%d个站点名称:",i);
scanf("%s",station);
}
newroute[i]=vexnum;
strcpy(newroutename[i],station);
}
}
else
{
newroute[i]=vexnum;
strcpy(newroutename[i],station);
}
}
vexnum++;
}
/*for(i=1;i<=stationnum;i++)
{
if(i+1<=stationnum)
printf("%d %d %d\n",newroute[i],newroute[i+1],r->data.NO);
printf("%s\n",newroutename[i]);
}*/
fp=fopen("stationdata.txt","a+");//添加新增站点信息
for(i=1;i<=stationnum;i++)
{
if(i+1<=stationnum)//另接表要+1
{fprintf(fp,"%d %d %d\n",newroute[i],newroute[i+1],r->data.NO);arcnum++;}
}
fclose(fp);
fp=fopen("station.txt","a+");//换乘点不重复则添加
for(i=1;i<=stationnum;i++)
{
if(strcmp(newroutename[i],"0")!=0)
fprintf(fp,"%s\n",newroutename[i]);
}
fclose(fp);
fp=fopen("stationinfo.txt","w");
fprintf(fp,"%d\n%d",vexnum-1,arcnum);
fclose(fp);
fp=fopen("line.txt","a+");
fprintf(fp,"%d\n",r->data.NO);
fprintf(fp,"%s\n",r->data.name);
fprintf(fp,"%s\n",r->data.start);
fprintf(fp,"%s\n",r->data.destination);
fprintf(fp,"%d\n",r->data.number);
fprintf(fp,"%s\n",r->data.starttime);
fprintf(fp,"%s\n",r->data.endtime);
fprintf(fp,"%s\n",r->data.totaltime);
fprintf(fp,"%d\n",r->data.price);
fclose(fp);
/* *********************************以上为录入站点信息***************************** */
save_line_information(L);
printf("成功更新一条地铁线路信息,总线路为%d条!\n",MAX_LINE_NUM);
}
void deletedata()
{
Graph G,G1;
FILE *fp;
NODELINE L,p,r,s;
int vexnum,arcnum;
char c[10];
char station[20];
int istrans[MAX_VERTEX_NUM];
int i,j,k,n;
char ch;
int stationnum;
int logic=0; //判断有无站点
int NO; //线路编号//
char name[15]; //线路名称//
char start[15]; //起点站//
char destination[15]; //终点站//
int number; //站点数//
char starttime[10]; //起运时间//
char endtime[10]; //收班时间//
char totaltime[10]; //运行时长//
int price; //票价//
int a1[MAX_VERTEX_NUM],b1[MAX_VERTEX_NUM],c1[MAX_VERTEX_NUM];
char name1[MAX_VERTEX_NUM][20];//记录删除站点的名字
int del=0,del1=0,del2=0;//记录删除站点的数目
int templabel=0;
char tempname[MAX_VERTEX_NUM][20];
fp=fopen("stationinfo.txt","r");
fscanf(fp,"%d%d",&vexnum,&arcnum);
fclose(fp);
fp=fopen("line.txt","r");
fscanf(fp,"%d",&MAX_LINE_NUM);
fclose(fp);
L=(NODE *)malloc(sizeof(NODE));
L->next=NULL;
readfromfile_line(L);
create_graph(&G);
G1=G;
/* *********************************以下为录入线路信息***************************** */
r=L;
printf("请问您是否要删除最后一条线路?(A-是,else-否):");
getchar();
scanf("%s",&c);
if((strcmp("a",c)==0)||(strcmp("A",c)==0))
{
NO=MAX_LINE_NUM;
/*printf("请输入要删除的的线路编号(0-返回):");
if( scanf("%d",&NO) !=1)
{
fflush(stdin);
printf("请正确线路编号:");
scanf("%d",&NO);
}*/
s=L;
while(s->next!=NULL)
{
s=s->next;
if(s->data.NO==NO)
{logic=1; break;}
}//找到S节点
if(logic==0) {printf("该线路不存在!\n");return ;}
//del1=s->data.number;
stationnum=s->data.number;
while(r->next!=s)
r=r->next;//找到S节点前驱
r->next=s->next;
free(s);
MAX_LINE_NUM--;
save_line_information(L);
fp=fopen("stationdata.txt","r");
//删除该线路的所有站点邻接信息
for(i=1;i<=arcnum;i++)
fscanf(fp,"%d %d %d",&a1[i],&b1[i],&c1[i]);//a[],b[]站点,c[]线路编号
fclose(fp);
fp=fopen("stationdata.txt","w");
for(i=1;c1[i]!=NO;i++)
{fprintf(fp,"%d %d %d\n",a1[i],b1[i],c1[i]);del++;}
fclose(fp);
//读入邻接表数据,
fp=fopen("stationdata.txt","r");
for(i=1;i<=arcnum;i++)
fscanf(fp,"%d %d %d",&a1[i],&b1[i],&c1[i]);
fclose(fp);
//从邻接表数据中获取需要修改站点的第一个序号
for(i=1;i<=arcnum;i++)
{
if(c1[i]==NO)
{templabel=a1[i];break;}
}
fp=fopen("station.txt","r");
for(i=templabel-stationnum;i<=templabel-1;i++)
{
fscanf(fp,"%s",tempname[i]);
if( station_search_line(tempname[i],0)==0 )//找所有站点,看是否出现过
{istrans[i]=0;del1++;}//del1++;} //非换乘点置为0,换乘点置为1
else
{istrans[i]=1;del2++;}
//printf("%d",istrans[i]);
}
fclose(fp);
//更新顶点数和边数信息
fp=fopen("stationinfo.txt","w");
fprintf(fp,"%d\n%d",vexnum-del1-del2,del);
fclose(fp);
//删除该线路所有站点名
fp=fopen("station.txt","r");
for(i=1;i<=vexnum;i++)
fscanf(fp,"%s",name1[i]);
fclose(fp);
fp=fopen("station.txt","w");
for(i=1;i<=vexnum-del1-del2;i++)
fprintf(fp,"%s\n",name1[i]);
fclose(fp);
printf("成功更新一条地铁线路信息,总线路为%d条!\n",MAX_LINE_NUM);
}
else return;
}
void xiugaidata()
{
Graph G,G1;
FILE *fp;
NODELINE L,p,r,s;
int vexnum,arcnum;
char station[20];
char ch;
int i,j,k,n,ret;
int stationnum;
int newroute[100];
char newroutename[100][20];
int logic;
int NO; //线路编号//