C语言编写一个简单的航空管理系统
需要分析:
A.车寻航线:
1.根据旅客提出的起点站,终点站名输出下列信息:航班号,票价,折扣,最多载客量,是否满载,起飞时间,降落时间和飞行时间;
2.根据订票乘客的姓名可以查询所订航班的航班号,座位号,飞行日期等信息;
3.根据航班号查询航班的起点站,中转站,终点站名,票价,折扣,最多载客量,是否满载,起飞时间,降落时间和飞行时间;
B.承办客户提出的要求(航班号、订票数额)查询该航班票额情况,若有余票,则为客户办理订票手续,输出座位号,需付款项信息;若已满员或余票额少于盯票额,则需重新询问客户要求。若需要,可登记排队候补;
C.根据客户提供的情况(日期、航班),为客户办理退票手续。(然后查询该航班是否有人排队候补,首先询问排第一的客户,若所退票额所能满足他的要求,则为他办理订票手续,否则依次询问其他排队候补客户);
E.内部人员对航班情况的控制:
可以录入航班信息,删除航班信息,修改航班信息,查看基本航班信息。
概要设计:
因为每个客户名单或查询名单都包括多个数据域,这样就需要有一个能存储多个数据域的数据类型来存储,因此采用单链表类型。由于航线的信息是固定的,可选用结构体数组,又因为订票与预约人数无法预计,可选用链表存储信息。
线性表的单链表存储结构:typedef struct LNode{
ElemType;
Struct Lnode*next;}LNode,*LinkList;
a.抽象数据类型顺序表的定义如下:
ADT SqList{
数据对象:D={ai|ai∈数据类型,i=1,2,3…,n}
数据关系:R1={ai-1,ai|ai-1,ai∈D,i=1,2,3…,n}
基本操作:
InitList_Sq(L)
操作结果:创建空的顺序表。
CreatList_Sq(L)
操作结果:建立顺序表。
}ADT SqList
b.抽象数据类型单链表的定义如下:
ADT LinkList{
数据对象:D={ai|ai∈结构类型,i=1,2,3…,n,n0}
数据关系:R1={ai-1,ai|ai-1,ai∈D,i=1,2,3…,n}
基本操作:
InitList_L(L)
操作结果:创建空的顺序表。
}ADT LinkList
在main()里调用各个函数
2.主程序
void main(){
初始化;
do{
接受命令;
处理命令;
}while(“命令”!=“退出”);
}
3.程序的模块调用:
三.详细设计:
1.所有数据类型:
struct plan /*航班数据*/
{
char num[5];/*航班号码*/
char city[10];/*到达城市*/
char up[8];/*航班起飞时间*/
char down[8];/*航班到达时间*/
int pric ;/*航班价格*/
int rshu ;/*人数*/
int zheg[4];/*价格折扣*/
}
;
struct man/*定票人数据*/
{
char num[10];/*身份证号码*/
char nam[10];/*姓名*/
int demand ;/*定票数量*/
}
;
typedef struct node/*航班数据结点*/
{
struct plan data ;
struct node*next ;
}
Node,*Link ;
typedef struct people/*乘客数据结点*/
{
struct man data ;
struct people*next ;
}
peo,*LIN ;
2.程序所用函数:
void print()/*界面输出*/
{
printf(“============================System of book ticket===============================\n”);
printf(“\n”);
printf(“\t***********************************************************\n”);
printf(“\t*\t1—Bookticket \t2—Dishonorbill *\n”);
printf(“\t*\t3—Adding flight\t4—Adding flight *\n”);
printf(“\t*\t5—Modify \t6—Advice *\n”);
printf(“\t*\t7—Save \t8—exit *\n”);
printf(“\t##########################################################\n”);
}
添加航班模块
void add(Link l)/*添加航班数据*/
{
Node*p,*r,*s ;
char num[10];
r=l ;
s=l-next ;
while(r-next!=NULL)
r=r-next ;
while(1)
{
printf(“please input the number of the plan(0-return)”);/*输入0就返回*/
scanf(“%s”,num);
if(strcmp(num,”0″)==0)
break ;
while(s)
{
if(strcmp(s-data.num,num)==0)
{
printf(“=====tip:the number’%s’has been born!\n”,num);
return ;
}
s=s-next ;
}
p=(Node*)malloc(sizeof(Node));/*航班数据输入*/
strcpy(p-data.num,num);
printf(“Input the city where the plan will reach:”);/*飞机到达地城市*/
scanf(“%s”,p-data.city);
getchar();
printf(“Input the time which the plan take off:”);/*起飞时间*/
scanf(“%s”,p-data.up);
getchar();
printf(“Input the time which the plan reach:”);/*降落时间*/
scanf(“%s”,p-data.down);
getchar();
printf(“Input the price of ticket:$”);/*机票价格*/
scanf(“%d”,p-data.pric);
getchar();
printf(“Input the number of people who have booked ticket:”);/*定票数量*/
scanf(“%d”,p-data.rshu);
getchar();
printf(“Input the agio of the ticket:”);
scanf(“%s”,p-data.zheg);
getchar();
p-next=NULL ;
r-next=p ;
r=p ;
shoudsave=1 ;
}
}
输出模块
void pri(Node*p)/*输出函数*/
{
printf(“\n\t\t\tThe following is the record you want:\n”);
printf(“\nnumber of plan: %s”,p-data.num);
printf(“\ncity the plan will reach: %s”,p-data.city);
printf(“\nthe time the plan take off: %s\nthe time the plan reach: %s”,p-data.up,p-data.down);
printf(“\nthe price of the ticket: %d”,p-data.pric);
printf(“\nthe number of people who have booked ticket: %d”,p-data.rshu);
printf(“\nthe agio of the ticket:%s”,p-data.zheg);
}
退出函数模块
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 qur(Link l)/*航班信息查询*/
{
Node*p ;
int sel ;
char str1[5],str2[10];
if(!l-next)
{
printf(“TIP:there are not any record to be inquired for you!”);
return ;
}
printf(“Choose the way:(1-according to the number of plan;2-according to the city):”);/*选择航班号查询和终点城市查询*/
scanf(“%d”,sel);
if(sel==1)
{
printf(“Input the the number of plan:”);
scanf(“%s”,str1);
p=Locate1(l,str1,”num”);
if(p)
{
printf(“the following is what you want:\n”);
pri(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)
{
printf(“the following is what you want:\n”);
pri(p);
}
else
{
mark1=1 ;
printf(“\nthe file can’t be found!”);
}
}
}
定票模块
void buy(Link l,LIN k)/*定票函数*/
{
Node*r[10],*p ;
int ch,dem ;
peo*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;ti;t++)
pri(r[t]);
if(i==0)
printf(“\n\tSorry!Can’t find the plan for you!\n”);
else
{
printf(“\ndo you want to book it?1/0\n”);
printf(“please choose: “);
scanf(“%d”,ch);
if(ch==1)
{
h=(peo*)malloc(sizeof(peo));/*重新分配空间*/
printf(“Input your name: “);
scanf(“%s”,str1);
strcpy(h-data.nam,str1);
printf(“Input your id: “);
scanf(“%s”,str2);
strcpy(h-data.num,str2);
printf(“Input your demand: “);
scanf(“%d”,dem);
h-data.demand=dem ;
h-next=NULL ;
v-next=h ;
v=h ;
printf(“\n\tLucky!Success in booking ticket!”);
getch();
shoudsave=1 ;
}
}
}
peo*Locate2(LIN k,char findmess[])
{
peo*r ;
r=k-next ;
while(r)
{
if(strcmp(r-data.num,findmess)==0)
{
mark=1 ;
return r ;
}
r=r-next ;
}
return 0 ;
}
退票模块
void tui(LIN k)/*退票函数*/
{
char str[10];
peo*p,*r ;
int ch2=0 ;
printf(“Input your id: “);/*输入身份证号*/
scanf(“%s”,str);
p=Locate2(k,str);
if(mark!=1)
printf(“can’t find the people!”);
else if(mark==1)
{
mark=0 ;
printf(“\t\t\tthe following is the record you want:\n”);
printf(“your id:%s\n”,p-data.num);
printf(“name:%s\n”,p-data.nam);
printf(“your denmand:%d”,p-data.demand);
printf(“\ndo you want to refund the ticket?1/0”);
scanf(“%d”,ch2);
if(ch2==1)
{
if(p)
{
r=k ;
while(r-next!=p)
r=r-next ;
r-next=p-next ;
free(p);
}
count2–;
printf(“\nyou have sucessed in refunding ticket!”);
shoudsave=1 ;
}
}
}
void Modify(Link l)/*修改航班信息*/
{
Node*p ;
char findmess[20],ch ;
if(!l-next)
{
printf(“\n=====tip:there isn’t record for you to modify!\n”);
return ;
}
else
{
qur(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 plan:”);
scanf(“%s”,findmess);
p=Locate1(l,findmess,”num”);
if(p)
{
printf(“Input another number of plan:”);
scanf(“%s”,p-data.num);
getchar();
printf(“Input another city the plan will reach:”);
scanf(“%s”,p-data.city);
getchar();
printf(“Input another time the plan take off”);
scanf(“%s”,p-data.up);
printf(“Input another time the plan reach:”);
scanf(“%s”,p-data.down);
printf(“Input another price of the ticket::”);
scanf(“%d”,p-data.pric);
printf(“Input another number of people who have booked ticket:”);
scanf(“%d”,p-data.rshu);
printf(“Input another agio of the ticket:”);
scanf(“%s”,p-data.zheg);
printf(“\n=====tip:modifying record is sucessful!\n”);
shoudsave=1 ;
}
else
printf(“\tcan’t find the flight!”);
}
}
else
mark1=0 ;
}
}
void advice(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.rshu200)
{
mar=1 ;
printf(“\nyou can select the following plan!\n”);
printf(“\n\nplease select the fourth operation to book the ticket!\n”);
pri(r);
}
r=r-next ;
}
if(mar==0)
printf(“\n\t\t\tyou can’t book any ticket now!\n”);
}
void save1(Link l)/*保存数据*/
{
FILE*fp ;
Node*p ;
int count=0,flag=1 ;
fp=fopen(“g:\\data1″,”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 save2(LIN k) /*保存数据*/
{
FILE*fp ;
peo*p ;
int count=0,flag=1 ;
fp=fopen(“g:\\data2″,”wb”);/*文件连接*/
if(fp==NULL)
{
printf(“the file can’t be opened!”);
return ;
}
p=k-next ;
while(p)
{
if(fwrite(p,sizeof(peo),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);
}
四.主函数模块:
main()
{
FILE*fp1,*fp2 ;
Node*p,*r ;
char ch1,ch2 ;
Link l ;
LIN k ;
peo*t,*h ;
int sel ;
l=(Node*)malloc(sizeof(Node));
l-next=NULL ;
r=l ;
k=(peo*)malloc(sizeof(peo));
k-next=NULL ;
h=k ;
fp1=fopen(“g:\\data1″,”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(“g:\\data2″,”ab+”);
if((fp2==NULL))
{
printf(“can’t open the file!”);
return 0 ;
}
while(!feof(fp2))
{
t=(peo*)malloc(sizeof(peo));
if(fread(t,sizeof(peo),1,fp2)==1)
{
t-next=NULL ;
h-next=t ;
h=t ;
count2++;
}
}
fclose(fp2);
while(1)
{
getch();
clrscr();
print();
printf(“please choose the operation(1-8): “);
scanf(“%d”,sel);
if(sel==8)
{
if(shoudsave==1)
{
getchar();
printf(“\n=====tip:the file have been changed!do you want to save it(y/n)?\n”);
scanf(“%c”,ch1);
if(ch1==’y’||ch1==’Y’)
{
save2(k);
save1(l);
}
}
printf(“\n\tyou have exited! Happy serve for you”);
break ;
}
switch(sel)
{
case 1 :
buy(l,k);
break ;
case 2 :
tui(k);
break ;
case 3 :
qur(l);
break ;
case 4 :
add(l);
break ;
case 5 :
Modify(l);
break ;
case 6 :
advice(l);
break ;
case 7 :
{
save1(l);
save2(k);
break ;
}
case 8 :
exit(0);
}
}
getch();
}
如何用C语言编译求解,请附上详细C语言代码
回溯用递归算法来实现比较简洁,我写了一个简单的递归函数来实现你的题目要求,仅供参考,具体内容见附图。
说明:
函数:OutputSet(int *ASet,int Len)
功能:输出集合ASet,Len为集合元素个数
函数:SetList(ASet,Len,Maxlen,MaxElm)
功能:用递归法生成并输出要求的所有集合
参数:ASet:代表集合的指针
Len:集合元素个数
MaxLen:集合允许的最大元素个数
MaxElm:允许的最大元素值。
注:题目中允许的最大元素值是100,
因此应将SetList调用中的99改为100。
另外SetList中的循环代码还可以进一步优化,把j=0改成j=i。
飞机的C语言程序代码
我有坦克的
#include stdio.h
#include stdlib.h
#include string.h
#define LENTH 7
#define WIDTH 155
#define SLEEP_TIME 30000
#define ENTER_NUM 20
char fun_ascii[LENTH][WIDTH] = {
” | “,
” _\\–__|_ “,
” II=======OOOOO[/ *02 ___| “,
” ____\\______|/—–.”,
” /____________________|”,
” \\@@@@@@@@@@@@@@@@@@@/ “,
” ~~~~~~~~~~~~~~~~~~~ “
};
int g_shoot_pos;
int g_shoot_pos_static;
void display_ascii(void);
void print_enter(void);
void move_ascii(void)
{
int i, j;
for (i = 0; i LENTH; i++) {
for (j = 0; j WIDTH; j++) {
if (fun_ascii[i][j] != ‘ ‘) {
fun_ascii[i][j – 1] = fun_ascii[i][j];
fun_ascii[i][j] = ‘ ‘;
}
}
}
}
void shoot_init(void)
{
int i, j;
for (j = 0; j WIDTH; j++) {
if (fun_ascii[2][j] != ‘ ‘)
break;
}
–j;
g_shoot_pos_static = g_shoot_pos = j;
}
void shoot_begin(void)
{
fun_ascii[2][g_shoot_pos] = ‘O’;
system(“clear”);
print_enter();
display_ascii();
fun_ascii[2][g_shoot_pos] = ‘ ‘;
}
void shoot_ascii(void)
{
int i;
i = WIDTH / 2 – 1;
while (i 0 g_shoot_pos 0) {
shoot_begin();
g_shoot_pos–;
usleep(SLEEP_TIME);
i–;
}
}
void do_shoot_ascii(void)
{
int i = 3;
shoot_init();
while (i 0) {
shoot_ascii();
g_shoot_pos = g_shoot_pos_static;
i–;
}
}
void display_ascii(void)
{
int i, j;
for (i = 0; i LENTH; i++) {
for (j = 0; j WIDTH; j++)
printf(“%c”, fun_ascii[i][j]);
printf(“\n”);
}
}
void print_enter(void)
{
int i;
for (i = 0; i ENTER_NUM; i++)
printf(“\n”);
}
int main(void)
{
int i = WIDTH;
while (i 0) {
if (i == (WIDTH / 2)) {
do_shoot_ascii();
i–;
continue;
}
system(“clear”);
print_enter();
move_ascii();
display_ascii();
i–;
usleep(SLEEP_TIME);
}
return 0;
}
飞机订票系统设计 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语言飞机的小游戏中,击中敌机这部分怎么理解y+2=ny这部分
= 是赋值运算符 因此:
( y+2=ny ) 是大家都容易范的错误(包括我在内)
== 是比较运算符
(y+2==ny)将输出 true 或 false
因此:可以理解为 if ( true ) isKilled = 1;
求C++飞机大战的代码(有注释的最好)
#includeiostream
#includewindows.h
#includeconio.h
#includetime.h
#includestring
using namespace std;
/*=============== all the structures ===============*/
typedef struct Frame
{
COORD position[2];
int flag;
}Frame;
/*=============== all the functions ===============*/
void SetPos(COORD a)// set cursor
{
HANDLE out=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(out, a);
}
void SetPos(int i, int j)// set cursor
{
COORD pos={i, j};
SetPos(pos);
}
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), cursor_info);
}
//把第y行,[x1, x2) 之间的坐标填充为 ch
void drawRow(int y, int x1, int x2, char ch)
{
SetPos(x1,y);
for(int i = 0; i = (x2-x1); i++)
coutch;
}
//在a, b 纵坐标相同的前提下,把坐标 [a, b] 之间填充为 ch
void drawRow(COORD a, COORD b, char ch)
{
if(a.Y == b.Y)
drawRow(a.Y, a.X, b.X, ch);
else
{
SetPos(0, 25);
cout”error code 01:无法填充行,因为两个坐标的纵坐标(x)不相等”;
system(“pause”);
}
}
//把第x列,[y1, y2] 之间的坐标填充为 ch
void drawCol(int x, int y1, int y2, char ch)
{
int y=y1;
while(y!=y2+1)
{
SetPos(x, y);
coutch;
y++;
}
}
//在a, b 横坐标相同的前提下,把坐标 [a, b] 之间填充为 ch
void drawCol(COORD a, COORD b, char ch)
{
if(a.X == b.X)
drawCol(a.X, a.Y, b.Y, ch);
else
{
SetPos(0, 25);
cout”error code 02:无法填充列,因为两个坐标的横坐标(y)不相等”;
system(“pause”);
}
}
//左上角坐标、右下角坐标、用row填充行、用col填充列
void drawFrame(COORD a, COORD b, char row, char col)
{
drawRow(a.Y, a.X+1, b.X-1, row);
drawRow(b.Y, a.X+1, b.X-1, row);
drawCol(a.X, a.Y+1, b.Y-1, col);
drawCol(b.X, a.Y+1, b.Y-1, col);
}
void drawFrame(int x1, int y1, int x2, int y2, char row, char col)
{
COORD a={x1, y1};
COORD b={x2, y2};
drawFrame(a, b, row, col);
}
void drawFrame(Frame frame, char row, char col)
{
COORD a = frame.position[0];
COORD b = frame.position[1];
drawFrame(a, b, row, col);
}
void drawPlaying()
{
drawFrame(0, 0, 48, 24, ‘=’, ‘|’);//draw map frame;
drawFrame(49, 0, 79, 4, ‘-‘, ‘|’);//draw output frame
drawFrame(49, 4, 79, 9, ‘-‘, ‘|’);//draw score frame
drawFrame(49, 9, 79, 20, ‘-‘, ‘|’);//draw operate frame
drawFrame(49, 20, 79, 24, ‘-‘, ‘|’);//draw other message frame
SetPos(52, 6);
cout”得分:”;
SetPos(52, 7);
cout”称号:”;
SetPos(52,10);
cout”操作方式:”;
SetPos(52,12);
cout” a,s,d,w 控制战机移动。”;
SetPos(52,14);
cout” p 暂停游戏。”;
SetPos(52,16);
cout” e 退出游戏。”;
}
//在[a, b)之间产生一个随机整数
int random(int a, int b)
{
int c=(rand() % (a-b))+ a;
return c;
}
//在两个坐标包括的矩形框内随机产生一个坐标
COORD random(COORD a, COORD b)
{
int x=random(a.X, b.X);
int y=random(a.Y, b.Y);
COORD c={x, y};
return c;
}
bool judgeCoordInFrame(Frame frame, COORD spot)
{
if(spot.X=frame.position[0].X)
if(spot.X=frame.position[1].X)
if(spot.Y=frame.position[0].Y)
if(spot.Y=frame.position[0].Y)
return true;
return false;
}
void printCoord(COORD a)
{
cout”( “a.X” , “a.Y” )”;
}
void printFrameCoord(Frame a)
{
printCoord(a.position[0]);
cout” – “;
printCoord(a.position[1]);
}
int drawMenu()
{
SetPos(30, 1);
cout”P l a n e W a r”;
drawRow(3, 0, 79, ‘-‘);
drawRow(5, 0, 79, ‘-‘);
SetPos(28, 4);
cout”w 和 s 选择, k 确定”;
SetPos(15, 11);
cout”1. 简单的敌人”;
SetPos(15, 13);
cout”2. 冷酷的敌人”;
drawRow(20, 0, 79, ‘-‘);
drawRow(22, 0, 79, ‘-‘);
SetPos(47, 11);
cout”简单的敌人:”;
SetPos(51, 13);
cout”简单敌人有着较慢的移动速度。”;
SetPos(24, 21);
int j=11;
SetPos(12, j);
cout””;
//drawFrame(45, 9, 79, 17, ‘=’, ‘|’);
while(1)
{if( _kbhit() )
{
char x=_getch();
switch (x)
{
case ‘w’ :
{
if( j == 13)
{
SetPos(12, j);
cout” ”;
j = 11;
SetPos(12, j);
cout””;
SetPos(51, 13);
cout””;
SetPos(47, 11);
cout”简单的敌人:”;
SetPos(51, 13);
cout”简单敌人有着较慢的移动速度。”;
}
break;
}
case ‘s’ :
{
if( j == 11 )
{
SetPos(12, j);
cout” ”;
j = 13;
SetPos(12, j);
cout””;
SetPos(51, 13);
cout””;
SetPos(47, 11);
cout”冷酷的敌人:”;
SetPos(51, 13);
cout”冷酷的敌人移动速度较快。”;
}
break;
}
case ‘k’ :
{
if (j == 8)return 1;
else return 2;
}
}
}
}
}
/*
DWORD WINAPI MusicFun(LPVOID lpParamte)
{
//DWORD OBJ;
sndPlaySound(TEXT(“bgm.wav”), SND_FILENAME|SND_ASYNC);
return 0;
}
*/
/*================== the Game Class ==================*/
class Game
{
public:
COORD position[10];
COORD bullet[10];
Frame enemy[8];
int score;
int rank;
int rankf;
string title;
int flag_rank;
Game ();
//初始化所有
void initPlane();
void initBullet();
void initEnemy();
//初始化其中一个
//void initThisBullet( COORD );
//void initThisEnemy( Frame );
void planeMove(char);
void bulletMove();
void enemyMove();
//填充所有
void drawPlane();
void drawPlaneToNull();
void drawBullet();
void drawBulletToNull();
void drawEnemy();
void drawEnemyToNull();
//填充其中一个
void drawThisBulletToNull( COORD );
void drawThisEnemyToNull( Frame );
void Pause();
void Playing();
void judgePlane();
void judgeEnemy();
void Shoot();
void GameOver();
void printScore();
};
Game::Game()
{
initPlane();
initBullet();
initEnemy();
score = 0;
rank = 25;
rankf = 0;
flag_rank = 0;
}
void Game::initPlane()
{
COORD centren={39, 22};
position[0].X=position[5].X=position[7].X=position[9].X=centren.X;
position[1].X=centren.X-2;
position[2].X=position[6].X=centren.X-1;
position[3].X=position[8].X=centren.X+1;
position[4].X=centren.X+2;
for(int i=0; i=4; i++)
position[i].Y=centren.Y;
for(int i=6; i=8; i++)
position[i].Y=centren.Y+1;
position[5].Y=centren.Y-1;
position[9].Y=centren.Y-2;
}
void Game::drawPlane()
{
for(int i=0; i9; i++)
{
SetPos(position[i]);
if(i!=5)
cout”O”;
else if(i==5)
cout”|”;
}
}
void Game::drawPlaneToNull()
{
for(int i=0; i9; i++)
{
SetPos(position[i]);
cout” “;
}
}
void Game::initBullet()
{
for(int i=0; i10; i++)
bullet[i].Y = 30;
}
void Game::drawBullet()
{
for(int i=0; i10; i++)
{
if( bullet[i].Y != 30)
{
SetPos(bullet[i]);
cout”^”;
}
}
}
void Game::drawBulletToNull()
{
for(int i=0; i10; i++)
if( bullet[i].Y != 30 )
{
COORD pos={bullet[i].X, bullet[i].Y+1};
SetPos(pos);
cout” “;
}
}
void Game::initEnemy()
{
COORD a={1, 1};
COORD b={45, 15};
for(int i=0; i8; i++)
{
enemy[i].position[0] = random(a, b);
enemy[i].position[1].X = enemy[i].position[0].X + 3;
enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
}
}
void Game::drawEnemy()
{
for(int i=0; i8; i++)
drawFrame(enemy[i].position[0], enemy[i].position[1], ‘-‘, ‘|’);
}
void Game::drawEnemyToNull()
{
for(int i=0; i8; i++)
{
drawFrame(enemy[i].position[0], enemy[i].position[1], ‘ ‘, ‘ ‘);
}
}
void Game::Pause()
{
SetPos(61,2);
cout” “;
SetPos(61,2);
cout”暂停中…”;
char c=_getch();
while(c!=’p’)
c=_getch();
SetPos(61,2);
cout” “;
}
void Game::planeMove(char x)
{
if(x == ‘a’)
if(position[1].X != 1)
for(int i=0; i=9; i++)
position[i].X -= 2;
if(x == ‘s’)
if(position[7].Y != 23)
for(int i=0; i=9; i++)
position[i].Y += 1;
if(x == ‘d’)
if(position[4].X != 47)
for(int i=0; i=9; i++)
position[i].X += 2;
if(x == ‘w’)
if(position[5].Y != 3)
for(int i=0; i=9; i++)
position[i].Y -= 1;
}
void Game::bulletMove()
{
for(int i=0; i10; i++)
{
if( bullet[i].Y != 30)
{
bullet[i].Y -= 1;
if( bullet[i].Y == 1 )
{
COORD pos={bullet[i].X, bullet[i].Y+1};
drawThisBulletToNull( pos );
bullet[i].Y=30;
}
}
}
}
void Game::enemyMove()
{
for(int i=0; i8; i++)
{
for(int j=0; j2; j++)
enemy[i].position[j].Y++;
if(24 == enemy[i].position[1].Y)
{
COORD a={1, 1};
COORD b={45, 3};
enemy[i].position[0] = random(a, b);
enemy[i].position[1].X = enemy[i].position[0].X + 3;
enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
}
}
}
void Game::judgePlane()
{
for(int i = 0; i 8; i++)
for(int j=0; j9; j++)
if(judgeCoordInFrame(enemy[i], position[j]))
{
SetPos(62, 1);
cout”坠毁”;
drawFrame(enemy[i], ‘+’, ‘+’);
Sleep(1000);
GameOver();
break;
}
}
void Game::drawThisBulletToNull( COORD c)
{
SetPos(c);
cout” “;
}
void Game::drawThisEnemyToNull( Frame f )
{
drawFrame(f, ‘ ‘, ‘ ‘);
}
void Game::judgeEnemy()
{
for(int i = 0; i 8; i++)
for(int j = 0; j 10; j++)
if( judgeCoordInFrame(enemy[i], bullet[j]) )
{
score += 5;
drawThisEnemyToNull( enemy[i] );
COORD a={1, 1};
COORD b={45, 3};
enemy[i].position[0] = random(a, b);
enemy[i].position[1].X = enemy[i].position[0].X + 3;
enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
drawThisBulletToNull( bullet[j] );
bullet[j].Y = 30;
}
}
void Game::Shoot()
{
for(int i=0; i10; i++)
if(bullet[i].Y == 30)
{
bullet[i].X = position[5].X;
bullet[i].Y = position[5].Y-1;
break;
}
}
void Game::printScore()
{
if(score == 120 flag_rank == 0)
{
rank -= 3;
flag_rank = 1;
}
else if( score == 360 flag_rank == 1)
{
rank -= 5;
flag_rank = 2;
}
else if( score == 480 flag_rank == 2)
{
rank -= 5;
flag_rank = 3;
}
int x=rank/5;
SetPos(60, 6);
coutscore;
if( rank!=rankf )
{
SetPos(60, 7);
if( x == 5)
title=”初级飞行员”;
else if( x == 4)
title=”中级飞行员”;
else if( x == 3)
title=”高级飞行员”;
else if( x == 2 )
title=”王牌飞行员”;
couttitle;
}
rankf = rank;
}
void Game::Playing()
{
//HANDLE MFUN;
//MFUN= CreateThread(NULL, 0, MusicFun, NULL, 0, NULL);
drawEnemy();
drawPlane();
int flag_bullet = 0;
int flag_enemy = 0;
while(1)
{
Sleep(8);
if(_kbhit())
{
char x = _getch();
if (‘a’ == x || ‘s’ == x || ‘d’ == x || ‘w’ == x)
{
drawPlaneToNull();
planeMove(x);
drawPlane();
judgePlane();
}
else if (‘p’ == x)
Pause();
else if( ‘k’ == x)
Shoot();
else if( ‘e’ == x)
{
//CloseHandle(MFUN);
GameOver();
break;
}
}
/* 处理子弹 */
if( 0 == flag_bullet )
{
bulletMove();
drawBulletToNull();
drawBullet();
judgeEnemy();
}
flag_bullet++;
if( 5 == flag_bullet )
flag_bullet = 0;
/* 处理敌人 */
if( 0 == flag_enemy )
{
drawEnemyToNull();
enemyMove();
drawEnemy();
judgePlane();
}
flag_enemy++;
if( flag_enemy = rank )
flag_enemy = 0;
/* 输出得分 */
printScore();
}
}
void Game::GameOver()
{
system(“cls”);
COORD p1={28,9};
COORD p2={53,15};
drawFrame(p1, p2, ‘=’, ‘|’);
SetPos(36,12);
string str=”Game Over!”;
for(int i=0; istr.size(); i++)
{
Sleep(80);
coutstr[i];
}
Sleep(1000);
system(“cls”);
drawFrame(p1, p2, ‘=’, ‘|’);
SetPos(31, 11);
cout”击落敌机:”score/5″ 架”;
SetPos(31, 12);
cout”得分:”score;
SetPos(31, 13);
cout”获得称号:”title;
SetPos(30, 16);
Sleep(1000);
cout”继续? 是(y)| 否(n)”;
as:
char x=_getch();
if (x == ‘n’)
exit(0);
else if (x == ‘y’)
{
system(“cls”);
Game game;
int a = drawMenu();
if(a == 2)
game.rank = 20;
system(“cls”);
drawPlaying();
game.Playing();
}
else goto as;
}
/*================== the main function ==================*/
int main()
{
//游戏准备
srand((int)time(0));//随机种子
HideCursor();//隐藏光标
Game game;
int a = drawMenu();
if(a == 2)
game.rank = 20;
system(“cls”);
drawPlaying();
game.Playing();
}
绝对可以运行,望采纳