(已修改,请用最新的代码)代码说明:
1级菜单:选择购买的航班号,并显示对应座位状态。
(我只做测试,所以初始化initFlight函数中我只初始了2个航班,需要自己按照我的代码添)
(注意:实际开发软件,链表数据是从数据库中读取的,需要实时同步,如果要多次调用initFlight函数,记得自己写一个释放内存的函数,把所有链表“SINFO和FLINFO”节点都释放掉,释放函数我没写,需要你自己写!!!)
2级菜单:选择购买对应座位号,完成购买,并实时显示购买结果。
位置编号、座位最大排数、舱室类型、折扣等参数均由常量参数空值,需要修改自行改常量。
注意:舱室类型(我默认3个类型头等舱、公务舱、经济舱)对应折扣参数:tDiscount二维数组。如要如要添加新的舱室类型,必须将参数常量TYPESIZE、typeName、types、tDiscount这4个同时修改,具体看代码备注!!
座位票价=基础票价*类型折扣*时段折扣。
因为飞机不让吸烟,所以我没做吸烟区(笑),如果你需要,可以作为类型自行添加!
#include<stdio.h>#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<malloc.h>
#include<time.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=0i<rowi++)
{
//---------读取座位所属舱室------------
memset(typeStr,0,10)
for(k=0k<TYPESIZEk++)
if(i+1>=types[k][0] && i+1<=types[k][1])
strcpy(typeStr,typeName[k])
//--------------------------------------
printf("\n")
for(j=0j<cloj++)
printf("------------- ")
printf("\n")
sfp=sinfoHead
for(j=0j<cloj++)
{
printf("| %c%02d | ",sfp->next->cloID,sfp->next->row)
sfp=sfp->next
}
printf("\n")
sfp=sinfoHead
for(j=0j<cloj++)
{
printf("| %c | ",sfp->next->sell?2:1)
sfp=sfp->next
}
printf("\n")
sfp=sinfoHead
for(j=0j<cloj++)
{
printf("|%6s:%4.0f| ",typeStr,flinfo->tPrice*tDiscount[sfp->next->type]*timeDiscount)
sfp=sfp->next
}
printf("\n")
sinfoHead=sfp
}
for(j=0i<cloj++)
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=0i<TYPESIZEi++)
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)
}
}
这是以前的课程设计题目,希望你再接再厉 好好学习#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define m 4//3架飞机
#define n 5 //每架飞机5张票
struct node
{
char name[21]
char id[21]
int seat,plane,date
node *next,*pre
}
struct wait
{
char name[21]
char id[21]
char phone[8]
int seat,plane,date,count
wait *next,*pre
}
struct piao
{
int seat[n+1]
}
void makenull()
void makenull_piao()
void makenull_information()
void list_menu()
void list_piao()
void makenull_wait()
void list_information()
void plane_information(node *head)
void book()
void add_information(node *head,int x,int y)
void add_wait(int x,int y)
void search_delete(int x)
void write_to_file()
void show_wait()
bool comp(node *x,node*y)
node *head1,*head2,*head3,*q
wait *wait_head,*wait_end
char c
piao a[m]
void main()
{
makenull()
do
{ list_menu()
cout<<endl<<"choose an operation: "
cin>>c
if (c!='6')
switch(c)
{
case '0' : show_wait()break
case '1' : {list_piao()book()}break
case '2' : search_delete(1)break
case '3' : list_piao()break
case '4' : list_information()break
case '5' : search_delete(0)break
default : break
}
}while(c!='6')
cout<<"Exit System "
}
void makenull()
{
makenull_piao()
makenull_information()
makenull_wait()
}
void list_menu()
{ cout<<endl<<""
cout<<endl<<" 菜单"
cout<<endl<<" ************************"
cout<<endl<<" *0 . 查看排队情况 *"
cout<<endl<<" *1 . 订票 *"
cout<<endl<<" *2 . 退票 *"
cout<<endl<<" *3 . 查看剩余票*"
cout<<endl<<" *4 . 查看飞机信息 *"
cout<<endl<<" *5 . 查看乘客信息 *"
cout<<endl<<" *6 . 退出 *"
cout<<endl<<" ************************"
cout<<endl<<""
}
void makenull_piao()
{
FILE *fp
int i
if((fp=fopen("piao.dat","r")) == NULL )
{
fp=fopen("piao.dat","w")
for (i=1i<=m-1i++)
fwrite(&a[i],sizeof(piao),1,fp)
fclose(fp)
fp=fopen("piao.dat","r")
}
for(i=1i<=m-1i++)
fread(&a[i],sizeof(piao),1,fp)
fclose(fp)
}
void makenull_information()
{
node *r
FILE *fp
int i,j,sum
sum=a[1].seat[0]+a[2].seat[0]+a[3].seat[0]
fp=fopen("information.dat","r")
head1=new node
head2=new node
head3=new node
head1->pre=NULL
head1->next=NULL
head2->pre=NULL
head2->next=NULL
head3->pre=NULL
head3->next=NULL
q=head1
for(i=1i<=sumi++)
{
j=0
r=new node
fread(r,sizeof(node),1,fp)
q->next=r
r->pre=q
r->next=NULL
q=q->next
fclose(fp)
if(i==a[1].seat[0]+1) {
head2->next=q
q->pre->next=NULL
q->pre=head2
}
if(i==a[1].seat[0]+a[2].seat[0]+1) {
head3->next=q
q->pre->next=NULL
q->pre=head3
}
}
}
void makenull_wait()
{
wait *tempw
FILE *fp
tempw=new wait
int i
if((fp=fopen("wait.txt","r")) ==NULL )
{
fp=fopen("wait.txt","w")
fclose(fp)
}
wait_end=new wait
wait_head=new wait
wait_end->next=NULL
wait_end->pre=NULL
wait_head=wait_end
wait_head->count=0
fp=fopen("wait.txt","r")
fread(wait_head,sizeof(wait),1,fp)
for(i=1i<=wait_head->counti++)
{
fread(tempw,sizeof(wait),1,fp)
wait_end->next=tempw
tempw->pre=wait_end
tempw->next=NULL
wait_end=tempw
}
}
void list_piao()
{
int i,j
for(i=1i<=m-1i++)
{
if(a[i].seat[0]!=n)
{
cout<<endl<<"第 "<<i<<" 架飞机剩余的票 :"<<endl
for(j=1j<=nj++)
if (a[i].seat[j]==0) cout<<" "<<j
cout<<endl
}
else cout<<endl<<"The "<<i<<" plane is full !"<<endl<<endl
}
}
void list_information()
{
int x
do {cout<<endl<<"显示哪架飞机的信息 ? "cin>>xcout<<endl}while(x<1 || x>=m)
cout<<endl<<"第 "<<x<<" 架飞机的信息如下 "<<endl
if(x==1) plane_information(head1)
if(x==2) plane_information(head2)
if(x==3) plane_information(head3)
}
void plane_information(node *head)
{
node *q
char ch
int x=0
if(head!=NULL &&head->next!=NULL)
q=head->next
else {
q=NULL
cout<<"飞机空,无预订票 !"<<endl
}
while(q!=NULL)
{
cout<<endl<<"*******************"<<endl
q->date=q->plane
cout<<"日期 :"<<q->date<<endl
cout<<"座位号 : "<<q->seat<<endl
cout<<"姓名 : "<<q->name
cout<<endl<<"ID 号 : "<<q->id
q=q->nextx++
if (x % 3 ==0) ch=getch()
}
cout<<endl
}
void book()
{
int i,j,p
cout<<endl<<"请选择地点:(1、2、3) "
do {
cin>>i
if (i<1 || i>=m) {
cout<<endl<<"**** 超出范围!****"<<endl<<"请重新输入:"
}
else
{cout<<endl<<"你要订的是到"<<i<<"地的飞机"<<endl
cout<<endl<<"第 "<<i<<" 架飞机剩余的票 :"<<endl
for(p=1p<=np++)
if (a[i].seat[p]==0) cout<<" "<<p
cout<<endl
break}
}while(1)
cout<<endl<<"请选择座位号 : "
do {
cin>>j
if (j<1 || j>n) {
cout<<endl<<"**** 超出范围!****"<<endl<<"请重新输入:"
}
else
{
q->date=i
cout<<endl<<"您的订票日期 : "<<q->date<<endl
break
}
}while(1)
if (a[i].seat[j]==0) {
a[i].seat[j]=1
cout<<endl
a[i].seat[0]++
if(i==1) add_information(head1,1,j)
if(i==2) add_information(head2,2,j)
if(i==3) add_information(head3,3,j)
}
else
{
cout<<endl<<"**** 对不起,该座位已被预订,您被安排到订票等候队列 ****"<<endl
add_wait(i,j)
}
}
void add_wait(int x,int y)
{
wait *tempw
tempw=new wait
tempw->next=NULL
cout<<"请输入个人信息"<<endl
cout<<endl<<"*************"<<endl
cout<<"姓名 : "cin>>tempw->name
cout<<"ID号 : "cin>>tempw->id
cout<<"电话 :"cin>>tempw->phone
tempw->seat=y
tempw->plane=x
wait_end->next=tempw
tempw->pre=wait_end
wait_end=wait_end->next
cout<<endl<<"**** 正在排队等候 ****"<<endl
wait_head->count++
write_to_file()
}
void show_wait()
{
wait *tempw
tempw=wait_head->next
if (tempw==NULL) cout<<endl<<"排队中没有人!"<<endl
while(tempw!=NULL)
{
cout<<tempw->name<<" - "
tempw=tempw->next
}
}
void add_information(node *head,int x,int y)
{
node *temp
temp=new node
temp->pre=NULL
temp->next=NULL
cout<<"请输入个人信息"<<endl
cout<<endl<<"*************"<<endl
cout<<"姓名 : "cin>>temp->name
cout<<"ID号 : "cin>>temp->id
temp->seat=y
temp->plane=x
temp->next=head->next
temp->pre=head
if (head->next!=NULL) head->next->pre=temp
head->next=temp
write_to_file()
cout<<endl<<"**** 订票成功 ****"<<endl
}
void search_delete(int x)
{
node *p,*q,*r
wait *tempw,*tempw2,*tempw3
int step=1,t1,t2,i
char ch
p=new node
tempw=new wait
tempw2=new wait
tempw3=new wait
q=head1
cout<<endl<<"请输入个人信息"<<endl
cout<<"*************"<<endl
cout<<endl<<"姓名 : "cin>>p->name
do{
q=q->next
if ( (q!=NULL) &&
(comp(q,p)) )
{
cout<<endl
q->date=q->plane
cout<<"Located!"<<endl
cout<<"****************"
cout<<endl<<"姓名 : "<<q->name
cout<<endl<<"ID号 : "<<q->id
cout<<endl<<"座位号 : "<<q->seat
cout<<endl<<"班机号 : "<<q->plane
cout<<endl<<"日期 : "<<q->date<<endl
if (x==1) {
cout<<"删除该纪录 ? [Y/N] "
cin>>ch
if (ch=='Y' || ch=='y') {
t1=q->plane
t2=q->seat
a[t1].seat[t2]=0
a[t1].seat[0]--
r=qq=q->pre
r->pre->next=r->next
if(r->next!=NULL) r->next->pre=r->pre
delete(r)
cout<<"**** 记录删除成功 ! ****"
write_to_file()
tempw=wait_head
for(i=0i<wait_head->counti++)
{
tempw=tempw->next
if(tempw==NULL) break
if((tempw->plane==t1) &&(tempw->seat==t2))
{
strcpy(tempw3->name,tempw->name)
strcpy(tempw3->phone,tempw->phone)
cout<<endl<<"等候的人中有可以订票的了:"<<endl
cout<<endl<<"姓名 : "<<tempw->name
cout<<endl<<"ID号 : "<<tempw->id<<endl
a[t1].seat[0]++
a[t1].seat[t2]=1
if(tempw->plane==1) add_information(head1,1,tempw->seat)
if(tempw->plane==2) add_information(head2,2,tempw->seat)
if(tempw->plane==3) add_information(head3,3,tempw->seat)
tempw2=tempw->pre
tempw2->next=tempw->next
if(tempw->next==NULL) wait_end=tempw2
else tempw->next->pre=tempw2
delete(tempw)
wait_head->count--
write_to_file()
cout<<endl<<"等候的"<<tempw3->name<<"已经成功订票,已经由电话"<<tempw3->phone<<"通知了"<<endl
break
}
}
}
}continue
}
else
{
if (q==NULL)
{
step++
if(step==2) q=head2
if(step==3) q=head3
if(step==4) {cout<<endl<<"**** 信息检索完毕 ****"break}
}
}
}while(1)
}
bool comp(node *x,node *y)
{
node *p,*q
int i,j,k
p=x
q=y
i=j=0
do
{
while ( (p->name[i] != q->name[j]) &&(p->name[i] != '\0') ) i++
if (p->name[i] == '\0') {return(false)break}
else
{
k=i
while ( (p->name[k] == q->name[j]) &&(q->name[j]!='\0') ) {k++j++}
if (q->name[j]=='\0') return(true)
else
{
j=0
i++
}
}
}while( (q->name[j]!='\0') &&(p->name[i] != '\0') )
return(false)
}
void write_to_file()
{
FILE *fp
int i,j
int x[m]
node *p
wait *tempw
tempw=new wait
tempw=wait_head
fp=fopen("piao.dat","w")
for (i=1i<=m-1i++)
{
fwrite(&a[i],sizeof(piao),1,fp)
}
fclose(fp)
fp=fopen("information.dat","w")
x[0]=0x[1]=a[1].seat[0]
for(i=0,j=1j<=m-1j++) {i=i+a[j].seat[0]x[j]=a[j].seat[0]+x[j-1]}
j=1p=head1->next
for(j=1j<=ij++)
{
if(j==x[1]+1) p=head2->next
if(j==x[2]+1) p=head3->next
if(p==NULL)break
fwrite(p,sizeof(node),1,fp)
p=p->next
}
fclose(fp)
fp=fopen("wait.txt","w")
for(j=0j<=wait_head->countj++)
{
if(tempw==NULL)break
fwrite(tempw,sizeof(wait),1,fp)
tempw=tempw->next
}
fclose(fp)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)