#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct Shu{//存储站名序号//
char name[10];
int x;
struct Shu *next;
}*S,s;
typedef struct Zhan{
char name[10];
int sum;//存储到下一个站的距离//
int x;//标记序号//
struct Zhan *next;
}*Z,z;
typedef struct Lu{
char name[10];
struct Lu *next;
}*L,l;
bool meue3();
struct Lu *Read1();
struct Shu *Read2();
struct Zhan *Read3(char *);
void BC1(L *);
void BC2(S *);
void BC3(Z *,char *);
void Zhan_add(L *,S *);
void Zhan_xg(L *,S *);
void Zhan_shan(L *);
void put(L *,char *,Z *);
int search(S *,char *);
int main(){
while(meue3()){
}
return 0;
}
int search(S *H2,char *N){//给站点编号//
int n=0;
S h2=*H2;
n=h2->x;
while(h2!=NULL){
if(strcmp(N,h2->name)==0){
return h2->x;
}
h2=h2->next;
}
n++;
S h3=(S)malloc(sizeof(s));
h3->next=NULL;
strcpy(h3->name,N);
h3->x=n;
h3->next=(*H2);
(*H2)=h3;
return n;
}
void Zhan_add(L *H1,S *H2){
char n[10],N[10];
Z H3=NULL,h3;
printf("输入要操作的公交线路\n");
scanf("%s",N);
put(&(*H1),N,&H3);
if(H3!=NULL){
h3=H3;
while(h3->next->next!=NULL)
h3=h3->next;//h3指向公交车倒数第二个站点//
Z h2=(Z)malloc(sizeof(z));
h2->next=NULL;
printf("\n输入要插入的站名\n");
scanf("%s",&h2->name);
int i;
h2->x=search(&(*H2),h2->name);//给站点编号//
h2->sum=0;
printf("1.设置新的起点 2.设置新的终点 3.插入新站\n");
scanf("%d",&i);
switch(i){
case 1:
printf("输入%s到%s的距离\n",h2->name,H3->name);
scanf("%d",&h2->sum);
h2->next=H3;
H3=h2;
break;
case 2:
h3=h3->next;
printf("输入%s到%s的距离\n",h3->name,h2->name);
scanf("%d",&h3->sum);
h3->next=h2;
break;
case 3:
Z h4=H3;
printf("输入要插入的位置的前面一个站点\n");
scanf("%s",n);
while(h4!=NULL){
if(strcmp(h4->name,n)==0){
printf("输入%s站到%s站的距离\n",h4->name,h2->name);
scanf("%d",&h4->sum);
printf("输入%s站到%s站的距离\n",h2->name,h4->next->name);
scanf("%d",&h2->sum);
h2->next=h4->next;
h4->next=h2;
break;
}
h4=h4->next;
}
break;
}
BC3(&H3,N);
printf("成功添加站点\n");
}
}
void Zhan_xg(L *H1,S *H2){
char n[10],N[10];
Z H3=NULL,h3,h4;
printf("输入要操作的公交线路\n");
scanf("%s",N);
put(&(*H1),N,&H3);
if(H3!=NULL){
h3=H3;
while(h3->next->next!=NULL)
h3=h3->next;//h3指向公交车倒数第二个站点//
h4=H3;
Z h2=(Z)malloc(sizeof(z));
h2->next=NULL;
printf("\n输入要修改的站点\n");
scanf("%s",n);
printf("输入新的站点名\n");
scanf("%s",&h2->name);
h2->sum=0;
h2->x=search(&(*H2),h2->name);
if(strcmp(h4->name,n)==0){//修改节点为头节点//
printf("输入%s站到%s站的距离\n",h2->name,h4->next->name);
scanf("%d",&h2->sum);
h2->next=H3->next;
H3=h2;
}
else if(strcmp(h3->next->name,n)==0){//修改节点为尾节点//
printf("输入%s站到%s站的距离\n",h3->name,h2->name);
scanf("%d",&h3->sum);
h3->next=h2;
}
else{
while(h4!=NULL){
if(strcmp(h4->next->name,n)==0){
printf("输入%s站到%s站的距离\n",h4->name,h2->name);
scanf("%d",&h4->sum);
printf("输入%s站到%s站的距离\n",h2->name,h4->next->next->name);
scanf("%d",&h2->sum);
h2->next=h4->next->next;
h4->next=h2;
break;
}
h4=h4->next;
}
}
printf("成功修改站点\n");
BC3(&H3,N);
}
}
void Zhan_shan(L *H1){
char n[10],N[10];
Z H2=NULL,h3,h2;
printf("输入要操作的公交线路\n");
scanf("%s",N);
put(&(*H1),N,&H2);
if(H2!=NULL){
h3=H2;
while(h3->next->next!=NULL)
h3=h3->next;//h3指向公交车倒数第二个站点//
printf("\n输入要删除的站点:\n");
scanf("%s",n);
h2=H2;
if(strcmp(h2->name,n)==0){//删除站点为头节点//
H2=H2->next;
}
else if(strcmp(h3->next->name,n)==0){//删除站点为尾节点//
h3->next=NULL;
h3->sum=0;
}
else{
while(h2!=NULL){
if(strcmp(h2->next->name,n)==0){
printf("输入%s站到%s站的距离:\n",h2->name,h2->next->next->name);
scanf("%d",&h2->sum);
h2->next=h2->next->next;
break;
}
h2=h2->next;
}
}
BC3(&H2,N);
printf("成功删除站点\n");
}
}
bool meue3(){
bool N=true;
L H1;
H1=Read1();
H1=H1->next;
S H2;
H2=Read2();
H2=H2->next;
printf("1.站点添加 2.站点修改 3.站点删除 4.返回主页面\n");
int n;
scanf("%d",&n);
switch(n){
case 1:
Zhan_add(&H1,&H2);
BC2(&H2);
break;
case 2:
Zhan_xg(&H1,&H2);
BC2(&H2);
break;
case 3:
Zhan_shan(&H1);
break;
case 4:
N=false;
break;
}
BC1(&H1);
return N;
}
void put(L *H1,char *N,Z *H2){
L h1=*H1;
while(h1!=NULL){
if(strcmp(h1->name,N)==0)
break;
h1=h1->next;
}
if(h1==NULL)
printf("没有找到该公交线\n");
else{
printf("%s公交路线如下:\n",N);
(*H2)=Read3(N);
(*H2)=(*H2)->next;
Z h3=(*H2),h2=(*H2);
printf("%s %d %d",h2->name,h2->sum,h2->x);
h2=h2->next;
if(h2!=NULL){
while(h2!=NULL){//输出所有站//
printf("->%s %d %d",h2->name,h2->sum,h2->x);
h2=h2->next;
}
}
}
}
void BC3(Z *head,char *N){//保存公交路线的所有站点//
FILE *f;
Z p=(*head);
char F[200]="C:\\Users\\许和伟\\Desktop\\";
strcat(F,N);
char f1[10]=".txt";
strcat(F,f1);
f=fopen(F,"w+");
while(p!=NULL){
fwrite(p,sizeof(z),1,f);
p=p->next;
}
fclose(f);
}
struct Zhan *Read3(char *N){//读取公交路线的所有站点//
FILE *f;
char F[200]="C:\\Users\\许和伟\\Desktop\\";
strcat(F,N);
char f1[10]=".txt";
strcat(F,f1);
f=fopen(F,"r+");
Z p,head;
head=(Z)malloc(sizeof(z));
head->next=NULL;
p=head;
while(1){
Z q;
q=(Z)malloc(sizeof(z));
fread(q,sizeof(z),1,f);
if(feof(f)){
p->next=NULL;
break;
}
p->next=q;
p=q;
p->next=NULL;
}
fclose(f);
return head;
}
void BC2(S *head){//保存所有站点编号//
FILE *f;
S p=*head;
f=fopen("Shu.txt","w+");
while(p!=NULL){
fwrite(p,sizeof(s),1,f);
p=p->next;
}
fclose(f);
}
struct Shu *Read2(){//读取所有站点编号//
FILE *f;
S head,p;
head=(S)malloc(sizeof(s));
head->next=NULL;
f=fopen("Shu.txt","r+");
p=head;
while(1){
Shu* q;
q=(S)malloc(sizeof(s));
fread(q,sizeof(s),1,f);
if(feof(f)){
p->next=NULL;
free(q);
break;
}
q->next=NULL;
p->next=q;
p=q;
}
fclose(f);
return head;
}
void BC1(L *head){//保存所有公交名字//
FILE *f;
L p=(*head);
f=fopen("LU.txt","w+");
while(p!=NULL){
fwrite(p,sizeof(l),1,f);
p=p->next;
}
fclose(f);
}
struct Lu *Read1(){//读取所有公交名字//
FILE *f;
L head,p;
head=(L)malloc(sizeof(l));
head->next=NULL;
f=fopen("LU.txt","r+");
p=head;
while(!feof(f)){
L q;
q=(L)malloc(sizeof(l));
fread(q,sizeof(l),1,f);
if(feof(f)){
free(q);
break;
}
p->next=q;
p=q;
p->next=NULL;
}
fclose(f);
return head;
}