今天给各位分享基于c语言的订票系统的知识,其中也会对c语言购票系统进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
1、c语言车票订购系统2、c语言程序设计飞机订票系统3、订票系统 用C语言编写4、C语言 订票系统5、飞机订票系统设计 c语言6、C语言程序设计飞机订票系统
c语言车票订购系统
#include
#include
#include
#include
int shoudsave=0 ;
int count1=0,count2=0,mark=0,mark1=0 ;
/*定义存储火车信息的结构体*/
struct train
{
char num[10];/*列车号*/
char city[10];/*目的城市*/
char takeoffTime[10];/*发车时间*/
char receiveTime[10];/*到达时间*/
int price;/*票价*/
int bookNum ;/*票数*/
};
/*订票人的信息*/
struct man
{
char num[10];/*ID*/
char name[10];/*姓名*/
int bookNum ;/*需求的票数*/
};
/*定义火车信息链表的结点结构*/
typedef struct node
{
struct train data ;
struct node * next ;
}Node,*Link ;
/*定义订票人链表的结点结构*/
typedef struct people
{
struct man data ;
struct people*next ;
}bookMan,*bookManLink ;
/* 初始界面*/
void printInterface()
{
puts(“********************************************************”);
puts(“* Welcome to use the system of booking tickets *”);
puts(“********************************************************”);
puts(“* You can choose the operation: *”);
puts(“* 1:Insert a train information *”);
puts(“* 2:Inquire a train information *”);
puts(“* 3:Book a train ticket *”);
puts(“* 4:Update the train information *”);
puts(“* 5:Advice to you about the train *”);
puts(“* 6:save information to file *”);
puts(“* 7:quit the system *”);
puts(“********************************************************”);
}
/*添加一个火车信息*/
void InsertTraininfo(Link linkhead)
{
struct node *p,*r,*s ;
char num[10];
r = linkhead ;
s = linkhead-next ;
while(r-next!=NULL)
r=r-next ;
while(1)
{
printf(“please input the number of the train(0-return)”);
scanf(“%s”,num);
if(strcmp(num,”0″)==0)
break ;
/*判断是否已经存在*/
while(s)
{
if(strcmp(s-data.num,num)==0)
{
printf(“the train ‘%s’has been born!\n”,num);
return ;
}
s = s-next ;
}
p = (struct node*)malloc(sizeof(struct node));
strcpy(p-data.num,num);
printf(“Input the city where the train will reach:”);
scanf(“%s”,p-data.city);
printf(“Input the time which the train take off:”);
scanf(“%s”,p-data.takeoffTime);
printf(“Input the time which the train receive:”);
scanf(“%s”,p-data.receiveTime);
printf(“Input the price of ticket:”);
scanf(“%d”,p-data.price);
printf(“Input the number of booked tickets:”);
scanf(“%d”,p-data.bookNum);
p-next=NULL ;
r-next=p ;
r=p ;
shoudsave = 1 ;
}
}
/*打印火车票信息*/
void printTrainInfo(struct node*p)
{
puts(“\nThe following is the record you want:”);
printf(“number of train: %s\n”,p-data.num);
printf(“city the train will reach: %s\n”,p-data.city);
printf(“the time the train take off: %s\nthe time the train reach:
%s\n”,p-data.takeoffTime,p-data.receiveTime);
printf(“the price of the ticket: %d\n”,p-data.price);
printf(“the number of booked tickets:
%d\n”,p-data.bookNum);
}
struct node * Locate1(Link l,char findmess[],char numorcity[])
{
Node*r ;
if(strcmp(numorcity,”num”)==0)
{
r=l-next ;
while(r)
{
if(strcmp(r-data.num,findmess)==0)
return r ;
r=r-next ;
}
}
else if(strcmp(numorcity,”city”)==0)
{
r=l-next ;
while(r)
{
if(strcmp(r-data.city,findmess)==0)
return r ;
r=r-next ;
}
}
return 0 ;
}
/*查询火车信息*/
void QueryTrain(Link l)
{
Node *p ;
int sel ;
char str1[5],str2[10];
if(!l-next)
{
printf(“There is not any record !”);
return ;
}
printf(“Choose the way:\n1:according to the number of
train;\n2:according to the city:\n”);
scanf(“%d”,sel);
if(sel==1)
{
printf(“Input the the number of train:”);
scanf(“%s”,str1);
p=Locate1(l,str1,”num”);
if(p)
{
printTrainInfo(p);
}
else
{
mark1=1 ;
printf(“\nthe file can’t be found!”);
}
}
else if(sel==2)
{
printf(“Input the city:”);
scanf(“%s”,str2);
p=Locate1(l,str2,”city”);
if(p)
{
printTrainInfo(p);
}
else
{
mark1=1 ;
printf(“\nthe file can’t be found!”);
}
}
}
/*订票子模块*/
void BookTicket(Link l,bookManLink k)
{
Node*r[10],*p ;
char ch,dem ;
bookMan*v,*h ;
int i=0,t=0 ;
char str[10],str1[10],str2[10];
v=k ;
while(v-next!=NULL)
v=v-next ;
printf(“Input the city you want to go: “);
scanf(“%s”,str);
p=l-next ;
while(p!=NULL)
{
if(strcmp(p-data.city,str)==0)
{
r[i]=p ;
i++;
}
p=p-next ;
}
printf(“\n\nthe number of record have %d\n”,i);
for(t=0;t
printTrainInfo(r[t]);
if(i==0)
printf(“\n\t\t\tSorry!Can’t find the train for you!\n”);
else
{
printf(“\ndo you want to book it?1/0\n”);
scanf(“%d”,ch);
if(ch == 1)
{
h=(bookMan*)malloc(sizeof(bookMan));
printf(“Input your name: “);
scanf(“%s”,str1);
strcpy(h-data.name,str1);
printf(“Input your id: “);
scanf(“%s”,str2);
strcpy(h-data.num,str2);
printf(“Input your bookNum: “);
scanf(“%d”,dem);
h-data.bookNum=dem ;
h-next=NULL ;
v-next=h ;
v=h ;
printf(“\nLucky!you have booked a ticket!”);
getch();
shoudsave=1 ;
}
}
}
bookMan*Locate2(bookManLink k,char findmess[])
{
bookMan*r ;
r=k-next ;
while(r)
{
if(strcmp(r-data.num,findmess)==0)
{
mark=1 ;
return r ;
}
r=r-next ;
}
return 0 ;
}
/*修改火车信息*/
void UpdateInfo(Link l)
{
Node*p ;
char findmess[20],ch ;
if(!l-next)
{
printf(“\nthere isn’t record for you to modify!\n”);
return ;
}
else
{
QueryTrain(l);
if(mark1==0)
{
printf(“\nDo you want to modify it?\n”);
getchar();
scanf(“%c”,ch);
if(ch==’y’);
{
printf(“\nInput the number of the train:”);
scanf(“%s”,findmess);
p=Locate1(l,findmess,”num”);
if(p)
{
printf(“Input new number of train:”);
scanf(“%s”,p-data.num);
printf(“Input new city the train will reach:”);
scanf(“%s”,p-data.city);
printf(“Input new time the train take off”);
scanf(“%s”,p-data.takeoffTime);
printf(“Input new time the train reach:”);
scanf(“%s”,p-data.receiveTime);
printf(“Input new price of the ticket::”);
scanf(“%d”,p-data.price);
printf(“Input new number of people who have booked ticket:”);
scanf(“%d”,p-data.bookNum);
printf(“\nmodifying record is sucessful!\n”);
shoudsave=1 ;
}
else
printf(“\t\t\tcan’t find the record!”);
}
}
else
mark1=0 ;
}
}
/*系统给用户的提示信息*/
void AdvicedTrains(Link l)
{
Node*r ;
char str[10];
int mar=0 ;
r=l-next ;
printf(“Iuput the city you want to go: “);
scanf(“%s”,str);
while(r)
{
if(strcmp(r-data.city,str)==0r-data.bookNum200)
{
mar=1 ;
printf(“\nyou can select the following train!\n”);
printf(“\n\nplease select the fourth operation to book the ticket!\n”);
printTrainInfo(r);
}
r=r-next ;
}
if(mar==0)
printf(“\n\t\t\tyou can’t book any ticket now!\n”);
}
/*保存火车信息*/
void SaveTrainInfo(Link l)
{
FILE*fp ;
Node*p ;
int count=0,flag=1 ;
fp=fopen(“c:\\train.txt”,”wb”);
if(fp==NULL)
{
printf(“the file can’t be opened!”);
return ;
}
p=l-next ;
while(p)
{
if(fwrite(p,sizeof(Node),1,fp)==1)
{
p=p-next ;
count++;
}
else
{
flag=0 ;
break ;
}
}
if(flag)
{
printf(“the number of the record which have been saved is %d\n”,count);
shoudsave=0 ;
}
fclose(fp);
}
/*保存订票人的信息*/
void SaveBookmanInfo(bookManLink k)
{
FILE*fp ;
bookMan*p ;
int count=0,flag=1 ;
fp=fopen(“c:\\man.txt”,”wb”);
if(fp==NULL)
{
printf(“the file can’t be opened!”);
return ;
}
p=k-next ;
while(p)
{
if(fwrite(p,sizeof(bookMan),1,fp)==1)
{
p=p-next ;
count++;
}
else
{
flag=0 ;
break ;
}
}
if(flag)
{
printf(“the number of the record which have been saved is %d\n”,count);
shoudsave=0 ;
}
fclose(fp);
}
int main()
{
FILE*fp1,*fp2 ;
Node*p,*r ;
char ch1,ch2 ;
Link l ;
bookManLink k ;
bookMan*t,*h ;
int sel ;
l=(Node*)malloc(sizeof(Node));
l-next=NULL ;
r=l ;
k=(bookMan*)malloc(sizeof(bookMan));
k-next=NULL ;
h=k ;
fp1=fopen(“c:\\train.txt”,”ab+”);
if((fp1==NULL))
{
printf(“can’t open the file!”);
return 0 ;
}
while(!feof(fp1))
{
p=(Node*)malloc(sizeof(Node));
if(fread(p,sizeof(Node),1,fp1)==1)
{
p-next=NULL ;
r-next=p ;
r=p ;
count1++;
}
}
fclose(fp1);
fp2=fopen(“c:\\man.txt”,”ab+”);
if((fp2==NULL))
{
printf(“can’t open the file!”);
return 0 ;
}
while(!feof(fp2))
{
t=(bookMan*)malloc(sizeof(bookMan));
if(fread(t,sizeof(bookMan),1,fp2)==1)
{
t-next=NULL ;
h-next=t ;
h=t ;
count2++;
}
}
fclose(fp2);
while(1)
{
system(“cls”);
printInterface();
printf(“please choose the operation: “);
scanf(“%d”,sel);
system(“cls”);
if(sel==8)
{
if(shoudsave==1)
{
getchar();
printf(“\nthe file have been changed!do you want to save it(y/n)?\n”);
scanf(“%c”,ch1);
if(ch1==’y’||ch1==’Y’)
{
SaveBookmanInfo(k);
SaveTrainInfo(l);
}
}
printf(“\nThank you!!You are welcome too\n”);
break ;
}
switch(sel)
{
case 1 :
InsertTraininfo(l);break ;
case 2 :
QueryTrain(l);break ;
case 3 :
BookTicket(l,k);break ;
case 4 :
UpdateInfo(l);break ;
case 5 :
AdvicedTrains(l);break ;
case 6 :
SaveTrainInfo(l);SaveBookmanInfo(k);break ;
case 7 :
return 0;
}
printf(“\nplease press any key to continue…….”);
getch();
}
return 0;
}
c语言程序设计飞机订票系统
呵呵 这个也是我在网上搜的 挺不错 没有错误
#includestdio.h //标准输入、输出头文件
#includestring.h //包含字符串函数处理头文件
#includestdlib.h //包含动态存储与释放函数头文件
#define N 10000
struct air //定义结构体数组
{
int num;
char start[20];
char over[20];
char time[10];
int count;
}s[N];
int i;
int m=0;
#define PRINT “%-d%12s%12s%10s%12d\n”,s[i].num,s[i].start,s[i].over,s[i].time,s[i].count //定义输出格式
void input(); //输入航班信息
void print(); //输出航班信息
void save(); //保存航班信息
void read(); //读取航班信息
void search(); //查找航班信息
void shanchu(); //删除航班信息
void dingpiao(); //订票信息
void tuipiao(); //退票信息
void xiugai(); //修改信息
void main()
{
int j;
printf(” ★—您好,欢迎进入中国民航管理系统!—★\n”);
printf(“================================================================================\n”);
do
{
printf(” ——– ☆ 1.输入航班信息 ☆——– \n\n”
” ——– ☆ 2.浏览航班信息 ☆——– \n\n”
” ——– ☆ 3.修改航班信息 ☆——– \n\n”
” ——– ☆ 4.查找航班信息 ☆——– \n\n”
” ——– ☆ 5.删除航班信息 ☆——– \n\n”
” ——– ☆ 6.订票信息 ☆——– \n\n”
” ——– ☆ 7.退票信息 ☆——– \n\n”
” ——– ☆ 0.退出 ☆——– \n\n”);
printf(“================================================================================\n”);
printf(“请在0-7中选择以回车键结束:\n\n”);
scanf(“%d”,j);
switch(j)
{
case 1: input();//调用输入模块
break;
case 2:print();//调用打印模块
break;
case 3:xiugai();//调用修改模块
break;
case 4:search();//调用查找模块
break;
case 5:shanchu(); //调用删除模块
break;
case 6:dingpiao();//调用订票模块
break;
case 7:tuipiao();//调用退票模块
break;
case 0:;
break;
}
}while(j!=0); //判断结束
printf(“谢谢使用,再见!\n”);
}//主函数结束
void input()//打印模块程序
{
char f[]=”2008china”; //设置密码
int y;
printf(“请输入密码并以回车键结束:\n\n”);
scanf(“%s”,f); //读取密码
if(strcmp(f,”2008china”)==0)
{
printf(“请依次输入航班信息(机票数位0结束输入):\n\n”
“完成输入信息请键入w以回车键结束\n\n”); //打印提示信息
printf(“————————————————————————–\n”);
for(i=0;iN;i++)
{
printf(“请输入航班号:\n”);
scanf(“%d”,s[i].num); //读取航班号
printf(“请输入起始站:\n”);
scanf(“%s”,s[i].start);//读取起始站
printf(“请输入终点站:\n”);
scanf(“%s”,s[i].over);//读取终点站
printf(“请输入时间:\n”);
scanf(“%s”,s[i].time);//读取时间
printf(“请输入机票数(机票数为0结束输入):\n”,m);
scanf(“%d”,s[i].count);//读取机票数
m++;
printf(“第%d个信息已经输完是否继续?按任意键继续,按 0结束”,m);
scanf(“%d”,y);
if(y==0)
{
save();//将结构体信息存盘
print();//输出输入的航班信息
break;
}
}
}
else
printf(“输入密码错误!请检查您的密码是否正确!谢谢!再见!\n\n”);
}
void save()//保存模块程序
{
FILE *fp,*fp1;//定义文件指针
if((fp=fopen(“chen.dat”,”wb”))==NULL)//打开文件并判断是否出错
{
printf(“创建文件失败!\n\n”);//打印出错提示
getchar();
return;
}
if((fp1=fopen(“hao.dat”,”wb”))==NULL)//打开文件并判断是否出错
{
printf(“创建文件失败!\n\n”);//打印出错提示
getchar();
return;
}
for(i=0;im;i++)
if(fwrite(s[i],sizeof(struct air),1,fp)==0)//向文件写入数据,并判断是否出错
printf(“向文件输入数据失败!\n\n”);
fprintf(fp1,”%d”,m);
fclose(fp);//关闭文件
fclose(fp1);//关闭文件
}
void read()//从文件读取信息模块
{
FILE *fp,*fp1;//定义文件指针
if((fp=fopen(“chen.dat”,”rb”))==NULL)//打开文件,并判断是否出错
{
printf(“出错,请检查文件是否存在,按任意键返回住菜单”);//打印出错提示
getchar();
}
if((fp1=fopen(“hao.dat”,”rb”))==NULL)//打开文件并判断是否出错
{
printf(“创建文件失败!\n\n”);//打印出错提示
getchar();
return;
}
fscanf(fp1,”%d”,m);
fclose(fp1);//关闭文件
for(i=0;im;i++)
{
fread(s[i],sizeof(air),1,fp);//从文件中读取信息
}
fclose(fp);//关闭文件
}
void print()//打印模块
{
char w[10];
read();//调用读取文件函数
printf(“航班号 起始站 终点站 时间 机票数\n”);
for(i=0;im;i++)
{
printf(PRINT);//打印信息
}
printf(“请按任意键回车键结束返回上层菜单以:\n”);
scanf(“%s”,w);
}
void search()//查询模块
{
char name1[20];
char name2[20];
char ii[10];
int n,no;
do
{
printf(“请选择查找方式:\n\n”);//打印查询方式菜单
printf(“1.按航班号查找\n\n”
“2.按终点站查找\n\n”
“3.按航线查找\n\n”
“0.返回\n\n”);
printf(“请在0-3中选择:\n\n”
“按其他键以回车键结束返回主菜单:\n\n”);
scanf(“%d”,n);//读取查找方式
if(n==0)
break;
switch(n)
{
case 1:
printf(“请输入航班号:\n”);
scanf(“%d”,no);//航班号
break;
case 2:
printf(“请输入终点站名称:\n”);
scanf(“%s”,name2);//读取终点站
break;
case 3:
printf(“请输入起始站名称:\n”);
scanf(“%s”,name1);//读取起始站
printf(“请输入终点站名称:\n”);
scanf(“%s”,name2);//终点站
break;
}
read();//调用读取函数
for(i=0;im;i++)
{
if(strcmp(s[i].over,name1)==0||strcmp(s[i].over,name2)==0)//按终点站起始站判断输出条件
{
printf(“\n查找航班信息成功!\n”);
printf(“航班号 起始站 终点站 时间 机票数\n”);
printf(PRINT);//打印信息
break;
}
if(s[i].num==no)//按航班号判断输出条件
{
printf(“\n查找航班信息成功!\n”);
printf(“航班号 起始站 终点站 时间 机票数\n”);
printf(PRINT);//打印信息
break;
}
}
no=0;//将航班号赋值为0
printf(“没有您需要的信息或查找完毕:\n\n”
“是否继续查找?请键入yes或no以回车键结束\n”);
scanf(“%s”,ii);
}while(strcmp(ii,”yes”)==0);//判断结束
}
void shanchu()//删除模块
{
char name1[20];
char name2[20];
char ii[10];
char f[]=”2008china”;//设置密码
int no,n;
printf(“请输入密码并以回车键结束:\n\n”);
scanf(“%s”,f);//读取密码
if(strcmp(f,”2008china”)==0) //判断密码是否正确
{
do
{
printf(“请选择删除以方式回车键结束:\n\n”);//打印删除方式菜单
printf(“*1.按航班号删除\n\n”
“*2.按航线删除\n\n”
“*0.返回\n\n”);
printf(“请在0-2中选择以回车键结束:\n”);
scanf(“%d”,n);//读取删除方式
if(n==0)
break; //跳出循环
switch(n)
{
case 1:
printf(“请输入航班号:\n”);
scanf(“%d”,no);//读取航班号
read();//调用读取函数
break;//跳出循环
case 2:
printf(“请输入起始站 名称:\n”);
scanf(“%s”,name1);//读取起始站
printf(“请输入终点站名称:\n”);
scanf(“%s”,name2);//读取终点站
read();//调用读取函数
break;//跳出循环
}
for(i=0;im;i++)
{
if(s[i].num==no||strcmp(s[i].start,name1)==0strcmp(s[i].over,name2)==0)//判断输入信息是否存在
{
s[i]=s[m-1];
m–;
}
}
printf(“查找完毕或没有这个信息\n\n”);
printf(“是否继续删除\n”);
printf(“请键入yes或no以回车键结束\n”);
scanf(“%s”,ii); //读取是否继续信息
save(); //调用读取函数
if(!strcmp(ii,”yes”)) //判断是否继续删除
printf(“请按任意键以回车键结束返回上层菜单:\n”);
break;
}while(n!=1n!=2n!=3n!=4n!=0); //判断结束
}
else
printf(“对不起密码错误!您不是管理员,不能使用此项功能!谢谢!再见!\n\n”);
}
void dingpiao()//订票模块
{
int n;
char a[10];
do
{
search();//调用查询模块
printf(“请输入您要订的机票数以回车键结束:\n”);
scanf(“%d”,n);//读取所订机票数
if(n0)
{
printf(“请输入有效的机票数!\n”);//判断机票数是否出错
break;
}
if(s[i].count!=0s[i].count=n)//判断是否出错
{
s[i].count=s[i].count-n;
save();//调用保存函数
printf(“订票成功!\n\n”);
break;
}
if(s[i].countn)//判断是否出错
{
printf(“请输入有效的机票数:\n”);
break;
}
printf(“是否继续? 请输入yes或no以回车键结束:\n”);//判断是否继续订票
scanf(“%s”,a);
}while(!strcmp(a,”yes”));//判断结束
}
void tuipiao()//退票模块
{
int n;
char a[10];
do
{
search();//调用查询模块
printf(“请输入您要退的机票数目:\n”);
scanf(“%d”,n);//输入所退票数
if(n0) //判断票数是否有效
printf(“请输入有效的机票数!\n”);
s[i].count=s[i].count+n;
save(); //调用保存模块
printf(“退票成功!\n\n”);
printf(“是否继续? 请键入yes或no以回车键结束:\n\n”);//判断是否继续退票
scanf(“%s”,a);
}while(!strcmp(a,”yes”));//判断并跳出循环
getchar();
}
void xiugai() //修改模块
{
struct xiu //定义结构体
{
int no;
char name1[20];
char name2[20];
char time[20];
int count;
}x[1];
char j[10];
char f[]=”2008china”;//设置密码
int n;
printf(“请输入密码并以回车键结束:\n\n”);
scanf(“%s”,f);//读取密码
if(strcmp(f,”2008china”)==0)//判断是否出错
{
read();//调用读取模块
do
{
printf( “请选择修改方式:\n\n”
“*1,按航班号修改:\n\n”
“*2,按航线修改: \n\n”);
printf(“请在1—2中修改以回车键结束:\n\n”);
scanf(“%d”,n);//读取修改方式
switch(n)
{
case 1:printf(“请输入航班号:\n”);
scanf(“%d”,x[0].no);//读取航班号
break;
case 2:printf(“请输入起始站:\n”);
scanf(“%s”,x[0].name1);//读取起始站
printf(“请输入终点站:\n”);
scanf(“%s”,x[0].name2);//读取终点站
break;
}
for(i=0;im;i++)
{
if(strcmp(s[i].over,x[0].name1)==0strcmp(s[i].over,x[0].name2)==0)//判断输出条件
{
printf(“航班号 起始站 终点站 时间 机票数\n”);
printf(PRINT);
break;
}
if(s[i].num==x[0].no)//判断输出条件
{
printf(“航班号 起始站 终点站 时间 机票数\n”);
printf(PRINT);
break;
}
}
x[0].no=0; //将结构体中的号为零
printf(“请输入新航班号、起始站、终点站、时间(星期几)、机票数:\n”);
scanf(“%d%s%s%s%d”,x[0].no,x[0].name1,x[0].name2,x[0].time,x[0].count);//定义输入格式
s[i].num=x[0].no;//替换航班号
strcpy(s[i].start,x[0].name1);//替换其始站
strcpy(s[i].over,x[0].name2);//替换终点站
strcpy(s[i].time,x[0].time);//替换时间
s[i].count=x[0].count;//替换机票数
save();//调用保存模块
printf(“是否继续?请键入yes或no以回车键结束:\n\n”);
scanf(“%s”,j);
}while(strcmp(j,”yes”)==0); //判断结束
}
else
printf(“对不起密码错误!您不是管理员,不能使用此项功能!谢谢!再见!\n\n”);
}
订票系统 用C语言编写
这种问题狠狠提高悬赏分吧,否则连认真看完需求的人都不会有几个。
C语言 订票系统
可以对数据库操作不?还是将所有信息自己写入临时数据文件???
如果有数据库的话这个倒是很简单实现,用文件的话稍微麻烦点…还有就是在修改数据文件的时候运行可能比较慢,因为我的办法是删除现有文件然后重新写入…如果记录很多的话是比较慢的…直接更新表记录的话就很快了
飞机订票系统设计 c语言
(已修改,请用最新的代码)代码说明:
1级菜单:选择购买的航班号,并显示对应座位状态。
(我只做测试,所以初始化initFlight函数中我只初始了2个航班,需要自己按照我的代码添)
(注意:实际开发软件,链表数据是从数据库中读取的,需要实时同步,如果要多次调用initFlight函数,记得自己写一个释放内存的函数,把所有链表“SINFO和FLINFO”节点都释放掉,释放函数我没写,需要你自己写!!!)
2级菜单:选择购买对应座位号,完成购买,并实时显示购买结果。
位置编号、座位最大排数、舱室类型、折扣等参数均由常量参数空值,需要修改自行改常量。
注意:舱室类型(我默认3个类型头等舱、公务舱、经济舱)对应折扣参数:tDiscount二维数组。如要如要添加新的舱室类型,必须将参数常量TYPESIZE、typeName、types、tDiscount这4个同时修改,具体看代码备注!!
座位票价=基础票价*类型折扣*时段折扣。
因为飞机不让吸烟,所以我没做吸烟区(笑),如果你需要,可以作为类型自行添加!
#includestdio.h
#includestdlib.h
#includeconio.h
#includestring.h
#includemalloc.h
#includetime.h
//———————–相关参数,想改变,在这里修改!!!!!!!—————————–
const float timeDiscount=1;//时段折扣,影响所有航班最终价格,默认1
const char cID[5]=”ABCD”;//位置编号
const int maxRow=20;//位置最大排号
//注意:如果修改类型数量,types和tDiscount必须同时修改!!!
#define TYPESIZE 3//类型数量
const char typeName[TYPESIZE][10]={“头等舱”,”公务舱”,”经济舱”};
const int types[TYPESIZE][2]={{1,2},{3,4},{5,20}};//排号对应类型。1~2排头等舱,3~4排公务舱,5~20排经济舱
const float tDiscount[TYPESIZE]={1.5,1.3,1};//类型折扣。头等舱1.5倍,公务舱1.3倍,经济舱1倍
//——————————————————————————-
typedef struct seatInfo//座位信息,一条链表对应一个航班信息,链表顺序从第一排左边第一个开始往后A1~D1,A2~D2。。。
{
char cloID;//位置编号A、B、C、D
int row;//位置排号
int type;//座位所属类型:0:头等舱、1:公务舱、2:经济舱,不同类型对应不同的类型折扣tDiscount
int sell;//出售状态,0:未出售;1:已出售
struct seatInfo *next;
}SINFO;
typedef struct flightInfo//航班信息
{
char fid[10];//航班号
time_t tfTime;//起飞时间
time_t ldTime;//降落时间
char toCity[20];//抵达城市
float tPrice;//基础票价,不同位置具有不同折扣,座位票价=基础票价*类型折扣*时段折扣
struct flightInfo *next;
struct seatInfo *sHead;//对应座位链表的头节点
}FLINFO;
void meError(void *p);
SINFO *getSINFO();//获取座位链表
//addFLINFO:添加航班信息链表的节点flinfoHead:头节点(第一次传NULL会自动生成),flinfoTail:尾节点,fNew:要添加的结构信息(成员指针无需赋值)
FLINFO *addFLINFO(FLINFO **ffHead,FLINFO *flinfoTail,FLINFO fNew);//返回尾节点
time_t getTime_tfromStr(char *sTime);//将YYYY-MM-DD hh:mm:ss格式的时间字符串转换成time_t型数值
FLINFO *initFlight();//初始化航班信息,返回航班链表头节点,如果想手动输入,请在这里添加!!!正常软件开发,这一步应该是从数据库读取!
char *getTString(struct tm *tm0);//通过tm获取时间字符串
void showSinfo(FLINFO *flinfo);//显示航班对应座位信息
void printfFlinfo(FLINFO * flinfoHead);
FLINFO *selectFlinfo(FLINFO *flinfoHead,char *fid);//选择航班号,返回节点
void showSinfo(FLINFO *flinfo);//显示航班对应座位信息
SINFO *selectSinfo(FLINFO *flinfo,char *sid);//选择座位,返回节点
int main()
{
FLINFO *flinfoHead=initFlight(),*ffSelect=NULL;
SINFO *sfSelect=NULL;
char fid[10]={0},sid[10]={10};
while(1)
{
ffSelect=NULL;
sfSelect=NULL;
memset(fid,0,10);
memset(sid,0,10);
printfFlinfo(flinfoHead);
printf(“请输入要购买的航班号:”);
scanf(“%s”,fid);
ffSelect=selectFlinfo(flinfoHead,fid);
if(!ffSelect)
{
printf(“未找到对应航班,按任意键继续—–\n”);
getch();
system(“cls”);
continue;
}
system(“cls”);
printf(“航班号:%s 座位信息如下:\n”,ffSelect-fid);
showSinfo(ffSelect);
printf(“请输入要购买的座位编号(输入0返回主菜单):”);
scanf(“%s”,sid);
if(!strcmp(sid,”0″))
{
system(“cls”);
continue;
}
else
{
sfSelect=selectSinfo(ffSelect,sid);
if(!sfSelect||sfSelect-sell)
{
printf(“未找到对应座位或该座位已出售,请重新输入!按任意键继续—–\n”);
getch();
system(“cls”);
continue;
}
printf(“购买成功!按任意键继续—–“);
sfSelect-sell=1;
getch();
system(“cls”);
}
}
return 0;
}
SINFO *selectSinfo(FLINFO *flinfo,char *sid)//选择座位,返回节点
{
SINFO *sinfoHead=flinfo-sHead;
while(sinfoHead-next)
{
if(sinfoHead-next-cloID==sid[0] sinfoHead-next-row==atoi(sid+1))
return sinfoHead-next;
sinfoHead=sinfoHead-next;
}
return NULL;
}
void showSinfo(FLINFO *flinfo)//显示航班对应座位信息
{
SINFO *sinfoHead=flinfo-sHead,*sfp=NULL;
int i,j,k,row=maxRow,clo=strlen(cID);
char typeStr[10]={0};
for(i=0;irow;i++)
{
//———读取座位所属舱室————
memset(typeStr,0,10);
for(k=0;kTYPESIZE;k++)
if(i+1=types[k][0] i+1=types[k][1])
strcpy(typeStr,typeName[k]);
//————————————–
printf(“\n”);
for(j=0;jclo;j++)
printf(“————- “);
printf(“\n”);
sfp=sinfoHead;
for(j=0;jclo;j++)
{
printf(“| %c%02d | “,sfp-next-cloID,sfp-next-row);
sfp=sfp-next;
}
printf(“\n”);
sfp=sinfoHead;
for(j=0;jclo;j++)
{
printf(“| %c | “,sfp-next-sell?2:1);
sfp=sfp-next;
}
printf(“\n”);
sfp=sinfoHead;
for(j=0;jclo;j++)
{
printf(“|%6s:%4.0f| “,typeStr,flinfo-tPrice*tDiscount[sfp-next-type]*timeDiscount);
sfp=sfp-next;
}
printf(“\n”);
sinfoHead=sfp;
}
for(j=0;iclo;j++)
printf(“——- “);
printf(“\n”);
}
FLINFO *selectFlinfo(FLINFO *flinfoHead,char *fid)//选择航班号,返回节点
{
while(flinfoHead-next)
{
if(!strcmp(flinfoHead-next-fid,fid))
return flinfoHead-next;
flinfoHead=flinfoHead-next;
}
return NULL;
}
void printfFlinfo(FLINFO * flinfoHead)
{
while(flinfoHead-next)
{
printf(“目的地:%s,航班号:%s\n—-起飞时间:%s,抵达时间:%s\n\n”,flinfoHead-next-toCity,flinfoHead-next-fid,getTString(localtime(flinfoHead-next-tfTime)),getTString(localtime(flinfoHead-next-ldTime)));
flinfoHead=flinfoHead-next;
}
}
char *getTString(struct tm *tm0)//通过tm获取时间字符串
{
char *str=(char *)malloc(sizeof(char)*20),num[5]={0};
meError(str);
memset(str,0,20);
sprintf(num,”%4d”,tm0-tm_year+1900);
strcat(str,num);
strcat(str,”-“);
memset(num,0,5);
sprintf(num,”%02d”,tm0-tm_mon);
strcat(str,num);
strcat(str,”-“);
memset(num,0,5);
sprintf(num,”%02d”,tm0-tm_mday);
strcat(str,num);
strcat(str,” “);
memset(num,0,5);
sprintf(num,”%02d”,tm0-tm_hour);
strcat(str,num);
strcat(str,”:”);
memset(num,0,5);
sprintf(num,”%02d”,tm0-tm_min);
strcat(str,num);
strcat(str,”:”);
memset(num,0,5);
sprintf(num,”%02d”,tm0-tm_sec);
strcat(str,num);
return str;
}
time_t getTime_tfromStr(char *sTime)//将YYYY-MM-DD hh:mm:ss格式的时间字符串转换成time_t型数值
{
time_t rt;
struct tm *tm1=NULL;
rt=time(NULL);
tm1=localtime(rt);
sscanf(sTime,(“%4d-%2d-%2d %2d:%2d:%2d”),tm1-tm_year,tm1-tm_mon,tm1-tm_mday,tm1-tm_hour,tm1-tm_min,tm1-tm_sec);
tm1-tm_year-=1900;
tm1-tm_mon–;
rt=mktime(tm1);
return rt;
}
FLINFO *initFlight()//初始化航班信息,返回航班链表头节点,如果想手动输入,请在这里添加!!!正常软件开发,这一步应该是从数据库读取!
{
FLINFO *ffHead=NULL,*flinfoTail=NULL,fNew;
//——–添加一个航班信息—-需要增加按照我下面调用方式写——————————–
strcpy(fNew.fid,”CI502″);
fNew.tfTime=getTime_tfromStr(“2019-02-20 03:30:30”);
fNew.ldTime=getTime_tfromStr(“2019-02-20 05:20:30”);
strcpy(fNew.toCity,”台北”);
fNew.tPrice=1000;
fNew.next=NULL;
flinfoTail=addFLINFO(ffHead,flinfoTail,fNew);
//——————————————————————————————–
strcpy(fNew.fid,”9C8921″);
fNew.tfTime=getTime_tfromStr(“2019-02-20 14:30:30”);
fNew.ldTime=getTime_tfromStr(“2019-02-20 16:40:30”);
strcpy(fNew.toCity,”香港”);
fNew.tPrice=500;
fNew.next=NULL;
flinfoTail=addFLINFO(ffHead,flinfoTail,fNew);
return ffHead;
}
FLINFO *addFLINFO(FLINFO **ffHead,FLINFO *flinfoTail,FLINFO fNew)//返回尾节点
//添加航班信息链表的节点flinfoHead:头节点(第一次传NULL会自动生成),flinfoTail:尾节点,fNew:要添加的结构信息(成员指针无需赋值)
{
FLINFO *flinfoHead=*ffHead;
if(flinfoHead==NULL)
{
*ffHead=(FLINFO *)malloc(sizeof(FLINFO));
flinfoHead=*ffHead;
meError(flinfoHead);
flinfoHead-next=NULL;
}
FLINFO *flinfoNew=(FLINFO *)malloc(sizeof(FLINFO));
meError(flinfoNew);
flinfoNew-next=NULL;
flinfoNew-fid[0]=0;
strcpy(flinfoNew-fid,fNew.fid);
flinfoNew-ldTime=fNew.ldTime;
flinfoNew-tfTime=fNew.tfTime;
flinfoNew-toCity[0]=0;
strcpy(flinfoNew-toCity,fNew.toCity);
flinfoNew-tPrice=fNew.tPrice;
flinfoNew-sHead=getSINFO();
if(flinfoHead-next==NULL)
flinfoHead-next=flinfoNew;
else
flinfoTail-next=flinfoNew;
flinfoTail=flinfoNew;
return flinfoTail;
}
SINFO *getSINFO()//获取座位链表
{
int maxClo=strlen(cID),cnt=maxClo*maxRow,clo=0,row=1,i;
SINFO *sinfoHead=(SINFO *)malloc(sizeof(SINFO)),*sinfoTail=NULL;
meError(sinfoHead);
sinfoHead-next=NULL;
SINFO *sinfoNew=NULL;
while(cnt–)//按顺序生成对应数量的座位链表
{
if(clo==maxClo)
clo=0,row++;
if(row==maxRow+1)
row=1;
sinfoNew=(SINFO *)malloc(sizeof(SINFO));
meError(sinfoNew);
sinfoNew-cloID=cID[clo];
sinfoNew-row=row;
for(i=0;iTYPESIZE;i++)
if(row=types[i][0] row=types[i][1])
{
sinfoNew-type=i;
break;
}
sinfoNew-sell=0;
sinfoNew-next=NULL;
if(sinfoHead-next==NULL)
sinfoHead-next=sinfoNew;
else
sinfoTail-next=sinfoNew;
sinfoTail=sinfoNew;
clo++;
}
return sinfoHead;
}
void meError(void *p)//内存申请失败
{
if(p==NULL)
{
printf(“\n异常:内存申请失败!回车结束程序!\n”);
while(getch()!=’\r’);
exit(0);
}
}
C语言程序设计飞机订票系统
#include stdio.h
#include string.h
#include conio.h
#include ctype.h
#include stdlib.h
#include malloc.h
#include math.h//overflow
#define ok 1
typedef struct Yidingkehu
{//单链表
char name[15];//已订票的客户姓名
int dingpiaoshu;//已订票数量
struct Yidingkehu *next1;//
}Yidingkehu,*Link;
typedef struct Weidingkehu
{//单链队
char name[15];//预订票的客户姓名
int yudingpiao;// 要订票数量
struct Weidingkehu *next2;//下一个链队结点指针
}Weidingkehu,*Qptr;
typedef struct Hangxian
{//创建一个含有六个信息的结构体
char hangbanhao[15];//航班号-
char feijihao[15];//飞机号
int feixingriqi;//起飞时间
int chenkerenshu;//座位数
int yupiao;//余票
char zhongdianzhai[15];//降落城市
struct Hangxian *next;//指向下一个链结点的指针
struct Yidingkehu *yiding;//定义一个指向已订票客户的头结点指针
struct Weidingkehu *yudingqueue;
}Hangxian,*Linklist;
Linklist InitLinklist();//01
int InsertLinklist(Linklist head1);//02
void hbhchaxun();//通过航班号查询
void mddchaxun();//通过目的地查询
void lurugongneng();//初始化录入功能
void chaxungongnen();//查询功能
void dingpiaogongnen();//订票功能
void tuipiaogongnen();//退票功能
void main()
{
int n;
do{ //打印主界面
printf(“\t 欢迎使用航空客运订票系统\n”);
printf(“\t+++++++++++++++++++++++++++++\n”);
printf(“\t==1. 录入功能 ==\n”);
printf(“\t==2. 查询功能 ==\n”);
printf(“\t==3. 订票功能 ==\n”);
printf(“\t==4. 退票功能 ==\n”);
printf(“\t==5. 退出 ==\n”);
printf(“\t+++++++++++++++++++++++++++++\n”);
printf(“\t请选择:”);
scanf(“%d”,n);printf(“\n”);
switch(n)
{
case 1: lurugongneng();//录入功能
break;
case 2: chaxungongnen();//查询功能
break;
case 3: dingpiaogongnen();//订票功能
break;
case 4:tuipiaogongnen();//退票功能
break;
default :exit(0);//退出
}
}while(n==1||n==2||n==3||n==4);
}
void lurugongneng()//初始化的单链表*********************************************************录入功能
{
Linklist p;
//int m,n;
if(!p) exit(OVERFLOW);
printf(“\t请依次输入下面几项内容:\n\n”);//这里的输入采用一个个单独输入,避免了乱赋值的现象
printf(“航班号\n”);
gets(p-hangbanhao);//这里的二个gets主要是因为在回车键的输入,其中的第一个是来接收上次的回车
gets(p-hangbanhao);
printf(“飞机号\n”);
gets(p-feijihao);
printf(“终点站\n”);
gets(p-zhongdianzhai);
printf(“飞行日期\n”);
scanf(“%d”,p-feixingriqi);
printf(“乘客总数\n”);
scanf(“%d”,p-chenkerenshu);
printf(“余票数\n”);
scanf(“%d”,p-yupiao);
}
void chaxungongnen()//******************************************************************查询功能
{
int n;
printf(“\t 查 找 航 线 信 息 \n”);
printf(“\t+++++++++++++++++++++++++++++\n”);
printf(“\t==1. 通过目的地查询 ==\n”);
printf(“\t==2. 通过航班号查询 ==\n”);
printf(“\t+++++++++++++++++++++++++++++\n”);
printf(“\t请选择:”);
scanf(“%d”,n);
printf(“\n”);//格式化
switch(n)
{
case 1:mddchaxun();
break;
case 2:hbhchaxun();
break;
default :break;
}
}
void mddchaxun()//通过目的地查询
{
char c[15];
int m;
Linklist p=L;
printf(“\t请输入要查询的目的地:”);
gets(c);
gets(c);//原因同上
do{
p=p-next;
if(p)
{
m=strcmpi((*p).zhongdianzhai,c);//如果==的话则m=0;
if(m==0)
{
printf(“\t航班信息:\n”);
printf(“\t航班号:%s\n”,p-hangbanhao);
printf(“\t飞机号:%s\n”,p-feijihao);
printf(“\t飞行时间:周%d\n”,p-feixingriqi);
printf(“\t余票量:%d\n”,p-yupiao);
}
}
else
{//如果不匹配的话就做
printf(“\t对不起没有你要找的目的地:\n\n”); m=0;
}
}while(m!=0);
}
void hbhchaxun()//通过目的地查询
{
char c[15];
int m;
Linklist p=L;
printf(“\t请输入要查询的航班号:”);
gets(c); gets(c);printf(“\n”);
do{
p=p-next;
if(p)
{
m=strcmpi((*p).hangbanhao,c);//如果==的话则m=0;这里的(*p).与p-的作用是一样的
if(m==0)
{
printf(“\t航班信息:\n”);
printf(“\t航班号:%s\n”,p-hangbanhao);
printf(“\t飞机号:%s\n”,p-feijihao);
printf(“\t飞行时间:周%d\n”,p-feixingriqi);
printf(“\t余票量:%d\n\n”,p-yupiao);
}
}
else
{//如果不匹配的话就做
printf(“\t对不起没有你要找的航班号:\n”); m=0;
}
}while(m!=0);
}
void dingpiaogongnen()//***************************************************************订票功能
{
char c[15];
int m=1,piao,ydpiao=0,yd=0,n;//
gets(c);
printf(“请输入终点站名:”); gets(c); printf(“\n”);
p=L-next;
if(p) {
do{//查找一下,是否有这个航班
if(!p)
{
printf(“对不起,没有你要找的航班:\n\n”);
goto loop1;
}
m=strcmpi(p-zhongdianzhai,c);
if(m==0)
{
printf(“航班信息:\n”);
printf(“航班号:%s\n”,p-hangbanhao);
printf(“飞机号:%s\n”,p-feijihao);
printf(“飞行时间:周%d\n”,p-feixingriqi);
printf(“余票量:%d\n”,p-yupiao);}
else p=p-next;
}while(m!=0);
if(m==0)
{
do{
printf(“\n请输入你要订的票数:”); scanf(“%d”,piao);
if(piao=p-yupiao)
{
h=p-yiding;
if(h)
{
h1=h;
h=h-next1;
h=(struct Yidingkehu*)malloc(sizeof(Yidingkehu));
printf(“请输入你的名字:”);
gets(h-name);gets(h-name);
h-dingpiaoshu=piao;
h-next1=h1-next1;
h1-next1=h;
p-yupiao=p-yupiao-piao;
printf(“订票成功:\n”); m=2;
}
}
else
{
printf(“余票量:%d\n”,p-yupiao);
printf(“对不起,余票 %d 张不足,不能完成订票\n\n”,p-yupiao);
printf(” 是否要重新订票?\n”);
printf(“需要请输入1 否则请按2 预订请输入3 : “);
scanf(“%d”,m);
printf(“\n”);
if(m==3) goto loop3;
}
}while(m==1);
}
}
else if(!p)
{
loop3: struct Weidingkehu *q3;
printf(“对不起,该航班的票已售完\n”);
q.front=p-yudingqueue;
if(q.front==q.rear) printf(“没有人预订票,是否要预订?\n”);
else if(q.front!=q.rear) printf(“已有人预订票,是否要预订?\n”);
printf(“预订请输入1 否则输入2 : “);
scanf(“%d”,n);
printf(“\n”);
if(n==1)
{
printf(“请输入你的姓名”); gets(q3-name); gets(q3-name);//q3不能指向name???
printf(“请输入订票数”); scanf(“%d”,q3-yudingpiao);
q3-next2=NULL;
q.rear-next2=q3;
q.rear=q3;
printf(” 你已经预订了 !\n”);
}
}
loop1:;
}
void tuipiaogongnen()//***************************************************************退票功能
{
}
请采纳答案,支持我一下。
关于基于c语言的订票系统和c语言购票系统的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。