#include<stdio.h>
#include<time.h>
#include<string.h>
#include<malloc.h>
#include<stdlib.h>
#define NULL 0
#define LEN sizeof(struct bus)
struct bus
{
int busnum;
char starttime[10];
char startstation[20];
char arrivestation[20];
int timelong;
int total;
int booksum;
struct bus *next;
};
int n;
struct bus *head;
/* 建立链表 录入数据 */
struct bus *record(void)
{
struct bus *p1,*p2;
n=1;
p1=p2=(struct bus *) malloc(LEN);
printf("\nplease input the bus record :\n");
printf("\n \t\tinput number---- 1 ------bus record :\n");
printf(" input bus number : ");
scanf("%d",&p1->busnum);
printf("\n");
printf(" input start-time : ");
scanf("%s",p1->starttime);
printf("\n");
printf(" input start-station : ");
scanf("%s",p1->startstation);
printf("\n");
printf("input arrive-station :");
scanf("%s",p1->arrivestation);
printf("\n");
printf(" input time-long : ");
scanf("%d",&p1->timelong);
printf("\n");
printf("input total-sum : ");
scanf("%d",&p1->total);
printf("\n");
printf("input book-seat-sum : ");
scanf("%d",&p1->booksum);
head=NULL;
while(p1->busnum!=0)
{
n=n+1;
if(n==2) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct bus *)malloc(LEN);
printf("\n \t\tinput number---- %d ------bus record :\n",n);
printf(" input bus number : ");
scanf("%d",&p1->busnum);
printf("\n");
printf(" input start-time : ");
scanf("%s",p1->starttime);
printf("\n");
printf(" input start-station : ");
scanf("%s",p1->startstation);
printf("\n");
printf(" input arrive-station :");
scanf("%s",p1->arrivestation);
printf("\n");
printf(" input time-long : ");
scanf("%d",&p1->timelong);
printf("\n");
printf(" input total-sum : ");
scanf("%d",&p1->total);
printf("\n");
printf(" input book-seat-sum : ");
scanf("%d",&p1->booksum);
}
p2->next=NULL;
printf("\n");
return(head);
}
/* 浏览功能*/
void print(struct bus *head)
{
char *t;
struct bus *p;
time_t aclock;
time(&aclock);
t=ctime(&aclock);
t=t+11;
printf("\n");
printf("the bus record are:\n\n");
printf("--------------------------------------------------------------------------\n");
printf("num starttime startstation arrivestation timelong total booksum\n");
p=head;
if(head!=NULL)
do
{
printf("--------------------------------------------------------------------------\n");
printf(" %d %s %s %s %d %d %d\n",p->busnum,p->starttime,p->startstation,p->arrivestation,p->timelong,p->total,p->booksum);
if(strcmp(p->starttime,t)<=0)
printf(" This bus has pulled out !\n");
p=p->next;
}while(p!=NULL);
printf("--------------------------------------------------------------------------\n");
printf("\n\n");
}
/* 将数据写入文件*/
void xie(struct bus *head)
{
FILE *fp;
struct bus *p;
char filename[10];
printf("write the records to a file.\n\n\ninput the filename:\n");
scanf("%s",filename);
if((fp=fopen(filename,"wb"))==NULL)
{printf("can't open the file.\n");
}
for(p=head;p!=NULL;p=p->next)
fwrite(p,LEN,1,fp);
fclose(fp);
printf("success!\n");
}
/* 从文件中读出数据*/
struct bus *du()
{
struct bus *bus1,*bus2,*bus3;
FILE *fp;
char filename[10];
bus1=bus2=bus3=(struct bus *)malloc(LEN);
printf("input the file name:");
scanf("%s",filename);
if((fp=fopen(filename,"rb"))==NULL)
{
printf("can't open the file\n");
}
while(!feof(fp))
{
fread(bus1,LEN,1,fp);
if(bus2->next!=NULL)
{bus1=(struct bus *)malloc(LEN);
bus2->next=bus1;
bus2=bus1;
}}
fclose(fp);
printf("success!\n\n");
return(bus3);
}
/* 插入记录*/
struct bus *insert(struct bus *head)
{ struct bus *p0,*p1,*p2,*p;
p=(struct bus *)malloc(LEN);
printf("\n\n\please input the bus record :\n");
printf("num time start arrive long total booksum\n");
scanf("%d %s %s %s %d %d %d",&p->busnum,p->starttime,p->startstation,p->arrivestation,&p->timelong,&p->total,&p->booksum);
p1=head;
p0=p;
if(head==NULL)
{ head=p0;
p0->next=NULL;}
else
{ while((p0->busnum>p1->busnum)&&(p1->next!=NULL))
{ p2=p1;
p1=p1->next;}
if(p0->busnum<=p1->busnum)
{ if(head==p1) head=p0;
else p2->next=p0;
p0->next=p1;}
else
{ p1->next=p0;p0->next=NULL;}
}
n=n+1;
return(head);
}
/*查询功能*/
struct bus *all_search(struct bus *head)
{
int c;
int num;
struct bus *p;
char arrive_station[20];
printf("1. according to the bus number.\n");
printf("2. according to the arrive_station.\n");
printf("please input number 1 or 2 :\n");
scanf("%d",&c);
switch(c)
{
case 1: printf("1 input the bus number that you want to search :\n");
scanf("%d",&num);
p=head;
while(p!=NULL)
{
if(p->busnum==num)
{printf("%d %s %s %s %d %d %d\n",p->busnum,p->starttime,p->startstation,p->arrivestation,p->timelong,p->total,p->booksum);break;}
else p=p->next;}
break;
case 2: printf("2 input the arrive-station :\n");
scanf("%s",arrive_station);
p=head;
while(p!=NULL)
{
if(strcmp(p->arrivestation,arrive_station)==0)
{ printf("%d %s %s %s %d %d %d\n",p->busnum,p->starttime,p->startstation,p->arrivestation,p->timelong,p->total,p->booksum);break;}
else printf("*-----------------------------*\n");
p=p->next;}
break;
default : printf("error\n");
}
return(head);
}
/* 售票功能*/
struct bus *sell(struct bus *head)
{
struct bus *p;
int num,sum;
int c;
char *t;
time_t aclock;
time(&aclock);
t=ctime(&aclock);
t=t+11;
p=head;
printf("input the bus number:\n");
scanf("%d",&num);
while(p!=NULL)
{
if(p->busnum==num && p->booksum<p->total && strcmp(t,p->starttime)<0)
{printf("allow to sell tickets.\nplease input the sum of the tikets that you need :\n");
scanf("%d",&sum);
p->booksum=p->booksum+sum;
printf("%d %s %s %s %d %d %d\n",p->busnum,p->starttime,p->startstation,p->arrivestation,p->timelong,p->total,p->booksum);
break;}
else printf("*------------------------------*\n");
p=p->next;}
return(head);
}
/*退票功能*/
struct bus *refund(struct bus *head)
{
struct bus *p;
int num,sum;
int c;
char *t;
time_t aclock;
time(&aclock);
t=ctime(&aclock);
t=t+11;
printf("please input the bus number you want to refund :\n");
scanf("%d",&num);
for(p=head;p!=NULL;p=p->next)
{
if(p->busnum==num && strcmp(t,p->starttime)<0)
{
printf("you can refund the tickets.\n");
printf("please input the sum of the tickets that you will refund:\n");
scanf("%d",&sum);
p->booksum=p->booksum-sum;
printf("%d %s %s %s %d %d %d\n",p->busnum,p->starttime,p->startstation,p->arrivestation,p->timelong,p->total,p->booksum);
break;
}
else printf("*---------------tickets-----------------*\n");
}
return(head);
}
void main()
{
int c;
printf("\n\n");
printf("\t\t\twelcome!\n\n");
printf("\t ----------------------------------------\n");
printf("\t| 1.input the bus records. |\n");
printf("\t| 2.scan the train records. |\n");
printf("\t| 3.insert one bus record. |\n");
printf("\t| 4.search the route of the bus. |\n");
printf("\t| 5.sell the tickets. |\n");
printf("\t| 6.refound the tickets. |\n");
printf("\t| 7.clear the screen. |\n");
printf("\t| 8.write the records to text. |\n");
printf("\t| 9.read the records from text. |\n");
printf("\t| 0.exit. |\n");
printf("\t ----------------------------------------\n");
loop: printf("please choose the function '0--8' :\n");
scanf("%d",&c);
switch(c)
{
case 1 : head=record();break;
case 2 : print(head);break;
case 3 : head=insert(head);break;
case 4 : head=all_search(head);break;
case 5 : head=sell(head);break;
case 6 : head=refund(head);break;
case 7 : system("cls");
printf("\t ----------------------