1. 編寫c語言:飛機訂票系統
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <conio.h>
#define MAXSIZE 3
typedef struct wat_ros
{
char name[10];
int req_amt;
struct wat_ros *next;
}qnode,*qptr;
typedef struct pqueue
{
qptr front;
qptr rear;
}linkqueue;
typedef struct ord_ros
{
char name[10];
int ord_amt;
int grade;
struct ord_ros *next;
}linklist;
struct airline
{
char ter_name[10]; /* 站點名 */
char air_num[10]; /* 航班號 */
char plane_num[10]; /* 飛機號 */
char date[7]; /* 飛行日期 */
int tkt_amt; /* 乘員定額 */
int tkt_sur; /* 余票量 */
linklist *order;
linkqueue wait;
}lineinfo;
struct airline *start;
struct airline air[MAXSIZE]={
{"beijing", "1", "B8571", "SUN", 3, 1},
{"shanghai","2","S1002","MON",2,5},
{"london","3","L1003","FRI",1,3}
};;
void display(struct airline *info)
{
printf("%8s\t%3s\t%s\t%4s\t\%d\t\%d\t\n",info->ter_name,info->air_num,info->plane_num,info->date,info->tkt_amt,info->tkt_sur);
}
/* 瀏覽航線信息函數 */
void list()
{
struct airline *info;
int i = 0;
info = start;
printf("終點站名\t航班號\t飛機號\t飛行日期" "乘員定額" "余票量\n");
while(i < MAXSIZE){
display(info);
info++;
i++;
}
printf("\n\n");
}
void search()
{
struct airline *info,*find();
char name[10];
int i=0;
info=start;
printf("請輸入終褲激埋點站名:");
scanf("%s",name);
while(i<MAXSIZE)
{
if(!strcmp(name,info->ter_name)) break;
info++;
i++;
}
if(i>=MAXSIZE)
printf("對不起,該航線未找到!\n");
else
{
printf("終點站名\t航班胡螞號\t飛機號\t飛行日期\t乘員定額\t余票鉛臘量\n");
display(info);
}
}
struct airline *find()
{
struct airline *info;
char number[10];
int i=0;
info=start;
printf("請輸入航班號:");
scanf("%s",number);
while(i<MAXSIZE)
{
if(!strcmp(number,info->air_num)) return info;
info++;
i++;
}
printf("對不起,該航線末找到!\n");
return NULL;
}
/* 瀏覽已訂票客戶信息 */
void prtlink()
{
linklist *p;
struct airline *info;
info=find();
p=info->order;
if(p!=NULL){
printf("客戶姓名 訂票數額 艙位等級\n");
while(p){
printf("%s\t\t%d\t%d\n",p->name,p->ord_amt,p->grade);
p=p->next;
}
}
else
printf("該航線沒有客戶信息!!\n");
}
linklist *insertlink(linklist *head,int amount,char name[],int grade)
{ linklist *p1,*new1;
p1=head;
new1=(linklist *)malloc(sizeof(linklist));
if(!new1) {printf("\nOut of memory!!\n");return NULL;}
strcpy(new1->name,name);
new1->ord_amt=amount;
new1->grade=grade;
new1->next=NULL;
if(head==NULL)
{head=new1;new1->next=NULL;}
else
head=new1;
new1->next=p1;
return head;
}
linkqueue appendqueue(linkqueue q,char name[],int amount)
{ qptr new1;
new1=(qptr)malloc(sizeof(qnode));
strcpy(new1->name,name);
new1->req_amt=amount;
new1->next=NULL;
if(q.front==NULL)
q.front=new1;
else
q.rear->next=new1;
q.rear=new1;
return q;
}
void order()
{ struct airline *info;
int amount,grade;
char name[10];
info=start;
if(!(info=find())) return;
printf("請輸入你訂票所需要的數量:");
scanf("%d",&amount);
if(amount>info->tkt_amt)
{ printf("\n對不起,您輸入的票的數量已經超過乘員定額!");
return;
}
if(amount<=info->tkt_sur)
{
int i;
printf("請輸入您的姓名(訂票客戶):");
scanf("%s",name);
printf("請輸入%s票的艙位等級:",name);
scanf("%d",&grade);
info->order=insertlink(info->order,amount,name,grade);
for(i=0;i<amount;i++)
printf("%s的座位號是:%d\n",name,info->tkt_amt-info->tkt_sur+i+1);
info->tkt_sur-=amount;
printf("\n祝您乘坐愉快!\n");
}
else
{ char r;
printf("\n已經沒有更多的票,您需要排隊等候嗎?(Y/N)");
//r=getch();
//printf("%c",r);
scanf ( "回車繼續等待%c", &r );
if(r=='Y'||r=='y')
{ printf("\n請輸入您的姓名(排隊訂票客戶):");
scanf("%s",name);
info->wait=appendqueue(info->wait,name,amount);
printf("\n注冊成功!\n");
}
else printf("\n歡迎您下次再次訂購!\n");
}
}
void return_tkt()
{ struct airline *info;
qnode *t,*back,*f,*r;
int grade;
linklist *p1,*p2,*head;
char cusname[10];
if(!(info=find())) return;
head=info->order;
p1=head;
printf("請輸入你的姓名(退票客戶):");
scanf("%s",cusname);
while(p1!=NULL) {
if(!strcmp(cusname,p1->name)) break;
p2=p1;p1=p1->next;
}
if(p1==NULL){ printf("對不起,你沒有訂過票!\n");return;}
else
{
if(p1==head) head=p1->next;
else p2->next=p1->next;
info->tkt_sur+=p1->ord_amt;
grade=p1->grade;
printf("%s成功退票!\n",p1->name);
free(p1);
}
info->order=head;
f=(info->wait).front;
r=(info->wait).rear;
t=f;
while(t)
{
if((info->tkt_sur = info->wait.front->req_amt))
{
int i;
info->wait.front=t->next;
printf("%s訂票成功!\n",t->name);
for(i=0;i<t->req_amt;i++)
printf("%s的座位號是:%d\n",t->name,(info->tkt_sur)-i);
info->tkt_sur-=t->req_amt;
info->order=insertlink(info->order,t->req_amt,t->name,grade);
free(t);
break;
}
back=t;t=t->next;
if((info->tkt_sur)>=(t->req_amt)&&t!=NULL)
{ int i;
back->next=t->next;
printf("%s訂票成功!\n",t->name);
for(i=0;i<t->req_amt;i++)
printf("<%s>'s seat number is:%d\n",t->name,(info->tkt_sur)-i);
info->tkt_sur-=t->req_amt;
info->order=insertlink(info->order,t->req_amt,t->name,grade);
free(t);break;
}
if(f==r) break;
}
}
void sort_tkt()
{
int j;
struct airline t,*info,*p,*q;
p=info=air;
for(p=info;(p+1)->tkt_sur;p++)
for(q=p+1;q->tkt_sur;q++)
{
if(p->tkt_sur<q->tkt_sur)
{
t=*p;
*p=*q;
*q=t;
}
}
printf("終點站名\t航班號\t飛機號\t飛行日期\t乘員定額\t余票量\n");
for(j=0;j<MAXSIZE;j++)
{
display(info);
info++;
}
}
int menu_select()
{
int c;
char s[20];
printf("\n\t\t 航空客運訂票系統(by zjhxmjl)\n");
printf(" ******************************************\n");
printf(" ** 1.瀏覽航線信息 **\n");
printf(" ** 2.瀏覽已訂票客戶信息 **\n");
printf(" ** 3.查詢航線 **\n");
printf(" ** 4.辦理訂票業務 **\n");
printf(" ** 5.辦理退票業務 **\n");
printf(" ** 6.查看剩餘票數並排序 **\n");
printf(" ** 0.退出系統 **\n");
printf(" *******************************************\n");
do
{
printf("請選擇:");
scanf("%s",s);
c = atoi(s); /* atoi將字元串轉換為整型 */
}while(c < 0||c > 7);
return c;
}
int main()
{
start = air;
for(;;)
{
switch(menu_select())
{
case 1:list();break;
case 2:prtlink();break;
case 3:search();break;
case 4:order();break;
case 5:return_tkt();break;
case 6:sort_tkt();break;
case 0:printf("\n歡迎使用本系統,再見!\n");exit(0);
}
printf("\nPress any key to continue!\n");
getchar();
}
return 0;
}
2. C語言程序設計 編寫程序計算飛機票款.輸入艙位代碼和購票數量,輸出總票款
#include<stdio.h>
intmain(void)
{
intn;
floatf,c,y,i;
charg;
printf("請輸入F艙、C艙和Y艙的公布價; ");
scanf("%f%f%f",&f,&c,&y);
getchar();
printf("請輸入倉位代碼和購票數量; ");
scanf("%c%d",&g,&n);
switch(g)
{
case'B':i=y*0.9*n;break;
case'H':i=y*0.85*n;break;
case'K':i=y*0.8*n;break;
case'L':i=y*0.75*n;break;
case'M':i=y*0.7*n;break;
case'N':i=y*0.65*n;break;
case'Q':i=y*0.6*n;break;
case'T':i=y*0.55*n;break;
case'X':i=y*0.5*n;break;
}
printf("%.2f ",i);
}
3. C語言 火車購票
#include<stdio.h>
intmain()
{
inti,j,k=0,k1,n,need[100]={0},a[100]={0},flag=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&need[i]);
for(k=0;k<n;)
{
i=0;k1=0;
for(i=0;i<100;i+=5)
{
for(j=0;j<5;j++)
if(a[i+j]==0)flag++;
if(flag>=need[k])
{
for(flag=need[k];flag>0;)
{
for(j=0;j<5;j++)
汪團{
if(a[i+j]==0)
{
握陸a[i+j]=1;
flag--;
printf("%d",i+j+1);
if(flag==0)
{
k++;
k1=1;
break;
}
}
}
}
printf(" "困皮橘);
}
elseflag=0;
if(k1)break;
}
}
return0;
}
4. 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()//***************************************************************退票功能
{
}
請採納答案,支持我一下。
5. C語言高手請進!急求火車訂票程序!!!分可追加!!!
3個月前回答的問題,跟你這個比較類似。
一機場每天有n個航班,每個班次都有一班次號(1、2、3…n),固定的發車時間,固定的路線(起始站、終點站),大致的行車時間,固定的額定載客量。如
班次 發車時間 起點站 終點站 行車時間 額定載量 已定票人數
1 8:00 郫縣 廣漢凱運 2 45 30
2 6:30 郫縣 成都 0.5 40 40
3 7:00 郫縣 成都 0.5 40 20
4 10:00 郫縣 成都 0.5 40 2
功能要求:用c設計一系統
(1)錄入班次信息(信息用文件保存),可不定時地增加班次數據
(2)瀏覽班次信息,可顯示出所有班次當前狀況(如果當前系統時間超過了某班次的發車時間,則顯示「此班已發出」的提示信息)。
(3)查詢路線:可按班次號查詢 ,可按終點站查詢
(4)售票和退票功能
A:當查詢出已定票人數小於額定載量且當前系統時間小於發車時間時才能售票,自動更新已售票人數
B:退票時,輸入退票的班次,當本班車未發出時才能退票,自動更新已售票人數
提問者: 尉遲憂葉 - 見習魔法師 二級
最佳答案
我今天晚上給你寫好.
寫好了,花了好長時間寫和調試,費了不少力氣啊,呵呵.說實話,100分有點少哦,不然到現在肯定有會的人給你解答了.
程序如下,我運行過了,各個功能保證可以實現,你只要先用1功能錄入一寫信息資料,以1 8:00 郫縣 廣漢 2 45 30 這樣的格式錄入.然後再瀏覽查詢,買票等等.因為我的編譯器不支持漢字,所以調試的時候很多提示語我用英文寫的,不過都是些簡單的,相信你看的懂.如果有問題問我叢散q23824056.
#include<stdio.h>
#include<滲孫氏time.h>
#include<string.h>
typedef struct information
{
int numb;
char starttime[10];
char begin[21];
char end[21];
float lasttime;
int canload;
int alreadyload;
}INFORMATION;
void AddInformation(void)
{
FILE *fp;
INFORMATION info;
printf("please input the information you want to insert,input 0 to return back:\n");
scanf("%d", &info.numb);
if(info.numb == 0) return;
scanf("%s%s%s%f%d%d", info.starttime, info.begin, info.end, &info.lasttime, &info.canload, &info.alreadyload);
if((fp = fopen("c:\\information.txt", "a+")) == 0)
{
printf("open file error!");
exit(0);
}
fprintf(fp, "%d %s %s %s %f %d %d\n", info.numb, info.starttime, info.begin, info.end, info.lasttime, info.canload, info.alreadyload);
fclose(fp);
}
int GetHour(char *s)
{
char *p, *q;
p = (char *)malloc(10);
strcpy(p, s);
q = p;
while(*q != ':')
{
q++;
}
*q = '\0';
return atoi(p);
}
int GetMinute(char *s)
{
char *p;
p = (char *)malloc(10);
strcpy(p, s);
while(*p != ':')
{
p++;
}
p++;
return atoi(p);
}
void ShowInformation(void)
{
FILE *fp;
INFORMATION info;
time_t t;
struct tm *timeinfo;
if((fp = fopen("c:\\information.txt", "r+")) == 0)
{
printf("open file error!");
exit(0);
}
time(&t);
timeinfo = localtime(&t);
while(!feof(fp))
{
fscanf(fp, "%d%s%s%s%f%d%d\n", &info.numb, info.starttime, info.begin, info.end, &info.lasttime, &info.canload, &info.alreadyload);
printf("%d %s %s %s %f %d %d ", info.numb, info.starttime, info.begin, info.end, info.lasttime, info.canload, info.alreadyload);
if(GetHour(info.starttime) < timeinfo->tm_hour) printf("此班已發出!\n");
else if(GetHour(info.starttime) == timeinfo->tm_hour)
{
if(GetMinute(info.starttime)<=timeinfo->tm_min) printf("此班已發出!\n");
else printf("\n");
}
else printf("\n");
}
fclose(fp);
}
void SearchbyNum(void)
{
FILE *fp;
INFORMATION info;
int bc;
printf("please input the number,input 0 to return back:\n");
scanf("%d", &bc);
if(0 == bc) return;
if((fp = fopen("c:\\information.txt", "r+")) == 0)
{
printf("open file error!");
exit(0);
}
while(!feof(fp))
{
fscanf(fp, "%d%s%s%s%f%d%d\n", &info.numb, info.starttime, info.begin, info.end, &info.lasttime, &info.canload, &info.alreadyload);
if(info.numb == bc)
{
printf("%d %s %s %s %f %d %d\n", info.numb, info.starttime, info.begin, info.end, info.lasttime, info.canload, info.alreadyload);
}
}
fclose(fp);
}
void SearchbyEnd(void)
{
FILE *fp;
INFORMATION info;
char zhongdian[100];
printf("please input the place,input 0 to return back:\n");
scanf("%s", zhongdian);
if(strcmp(zhongdian, "0") == 0) return;
if((fp = fopen("c:\\information.txt", "r+")) == 0)
{
printf("open file error!");
exit(0);
}
while(!feof(fp))
{
fscanf(fp, "%d%s%s%s%f%d%d\n", &info.numb, info.starttime, info.begin, info.end, &info.lasttime, &info.canload, &info.alreadyload);
if(strcmp(zhongdian, info.end) == 0)
{
printf("%d %s %s %s %f %d %d\n", info.numb, info.starttime, info.begin, info.end, info.lasttime, info.canload, info.alreadyload);
}
}
fclose(fp);
}
void SearchWay(void)
{
int i;
printf("1. search by number\n");
printf("2. search by end\n");
printf("0. return back\n");
printf("which way do you want?\n");
scanf("%d", &i);
switch(i)
{
case 1: SearchbyNum(); break;
case 2: SearchbyEnd(); break;
case 0: return;
default : break;
}
}
void Buy(void)
{
FILE *fp;
INFORMATION info;
time_t t;
struct tm *timeinfo;
int bc;
long pos, pos1;
printf("please input the banci you want to buy,input 0 to return back:\n");
scanf("%d", &bc);
if(0 == bc) return;
if((fp = fopen("c:\\information.txt", "r+")) == 0)
{
printf("open file error!");
exit(0);
}
time(&t);
timeinfo = localtime(&t);
while(1)
{
pos1 = ftell(fp);
fscanf(fp, "%d%s%s%s%f%d%d\n", &info.numb, info.starttime, info.begin, info.end, &info.lasttime, &info.canload, &info.alreadyload);
pos = ftell(fp) - pos1;
if(info.numb == bc)
{
if(info.alreadyload < info.canload)
{
if(GetHour(info.starttime) < timeinfo->tm_hour)
{
printf("sorry, the plane has taken off!");
}
else if(GetHour(info.starttime) == timeinfo->tm_hour)
{
if(GetMinute(info.starttime) <= timeinfo->tm_min) printf("sorry, the plane has taken off!");
else
{
printf("take your ticket, bye!");
info.alreadyload ++;
fseek(fp, -pos, SEEK_CUR);
fprintf(fp, "%d %s %s %s %f %d %d\n", info.numb, info.starttime, info.begin, info.end, info.lasttime, info.canload, info.alreadyload);
}
}
else
{
printf("take your ticket, bye!");
info.alreadyload ++;
fseek(fp, -pos, SEEK_CUR);
fprintf(fp, "%d %s %s %s %f %d %d\n", info.numb, info.starttime, info.begin, info.end, info.lasttime, info.canload, info.alreadyload);
}
}
else printf("sorry, the tickets are all sold out!");
break;
}
}
fclose(fp);
}
void Cancel(void)
{
FILE *fp;
INFORMATION info;
time_t t;
struct tm *timeinfo;
int bc;
long pos, pos1;
printf("please input the banci you want to cancel,input 0 to return back:\n");
scanf("%d", &bc);
if(0 == bc) return;
if((fp = fopen("c:\\information.txt", "r+")) == 0)
{
printf("open file error!");
exit(0);
}
time(&t);
timeinfo = localtime(&t);
while(1)
{
pos1 = ftell(fp);
fscanf(fp, "%d%s%s%s%f%d%d\n", &info.numb, info.starttime, info.begin, info.end, &info.lasttime, &info.canload, &info.alreadyload);
pos = ftell(fp) - pos1;
if(info.numb == bc)
{
if(GetHour(info.starttime) < timeinfo->tm_hour)
{
printf("sorry, the plane has taken off!");
}
else if(GetHour(info.starttime) == timeinfo->tm_hour)
{
if(GetMinute(info.starttime) <= timeinfo->tm_min) printf("sorry, the plane has taken off!");
else
{
printf("your ticket is canceled, bye!");
info.alreadyload --;
fseek(fp, -pos, SEEK_CUR);
fprintf(fp, "%d %s %s %s %f %d %d\n", info.numb, info.starttime, info.begin, info.end, info.lasttime, info.canload, info.alreadyload);
}
}
else
{
printf("your ticket is canceled, bye!");
info.alreadyload --;
fseek(fp, -pos, SEEK_CUR);
fprintf(fp, "%d %s %s %s %f %d %d\n", info.numb, info.starttime, info.begin, info.end, info.lasttime, info.canload, info.alreadyload);
}
break;
}
}
fclose(fp);
}
void ForTicket(void)
{
int i;
printf("1. I want to buy a ticket\n");
printf("2. I want to return a ticket\n");
printf("0. return back\n");
printf("which do you want?");
scanf("%d", &i);
switch(i)
{
case 1: Buy(); break;
case 2: Cancel(); break;
case 0: return;
default : break;
}
}
int main()
{
int i;
while(1)
{
printf("\n1. 錄入班次信息\n");
printf("2. 瀏覽班次信息\n");
printf("3. 查詢路線\n");
printf("4. 售票退票\n");
printf("\nPlease input a number from 1 to 4 like above ,use 0 to exit!\n");
scanf("%d",&i);
switch(i)
{
case 0: return 0;
case 1: AddInformation(); break;
case 2: ShowInformation(); break;
case 3: SearchWay(); break;
case 4: ForTicket(); break;
default :
printf("Wrong number!\n"); break;
}
}
}
回答者:sunshine66 - 高級經理 六級 6-8 08:58
6. 飛機訂票系統設計 c語言
(已修改,請用最新的代碼)代碼說明:
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>
//-----------------------相關參數,想改變,在這里修改!!!!!!!-----------------------------
constfloattimeDiscount=1;//時段折扣,影響所有航班最終價格,默認1
constcharcID[5]="ABCD";//位置編號
constintmaxRow=20;//位置最大排號
//注意:如果修改類型數量,types和tDiscount必須同時修改!!!
#defineTYPESIZE3//類型數量
constchartypeName[TYPESIZE][10]={"頭等艙","公務艙","經濟艙"};
constinttypes[TYPESIZE][2]={{1,2},{3,4},{5,20}};//排號對應類型。1~2排頭等艙,3~4排公務艙,5~20排經濟艙
constfloattDiscount[TYPESIZE]={1.5,1.3,1};//類型折扣。頭等艙1.5倍,公務艙1.3倍,經濟艙1倍
//-------------------------------------------------------------------------------
typedefstructseatInfo//座位信息,一條鏈表對應一個航班信息,鏈表順序從第一排左邊第一個開始往後A1~D1,A2~D2。。。
{
charcloID;//位置編號A、B、C、D
introw;//位置排號
inttype;//座位所屬類型:0:頭等艙、1:公務艙、2:經濟艙,不同類型對應不同的類型折扣tDiscount
intsell;//出售狀態,0:未出售;1:已出售
structseatInfo*next;
}SINFO;
typedefstructflightInfo//航班信息
{
charfid[10];//航班號
time_ttfTime;//起飛時間
time_tldTime;//降落時間
chartoCity[20];//抵達城市
floattPrice;//基礎票價,不同位置具有不同折扣,座位票價=基礎票價*類型折扣*時段折扣
structflightInfo*next;
structseatInfo*sHead;//對應座位鏈表的頭節點
}FLINFO;
voidmeError(void*p);
SINFO*getSINFO();//獲取座位鏈表
//addFLINFO:添加航班信息鏈表的節點flinfoHead:頭節點(第一次傳NULL會自動生成),flinfoTail:尾節點,fNew:要添加的結構信息(成員指針無需賦值)
FLINFO*addFLINFO(FLINFO**ffHead,FLINFO*flinfoTail,FLINFOfNew);//返回尾節點
time_tgetTime_tfromStr(char*sTime);//將YYYY-MM-DDhh:mm:ss格式的時間字元串轉換成time_t型數值
FLINFO*initFlight();//初始化航班信息,返回航班鏈表頭節點,如果想手動輸入,請在這里添加!!!正常軟體開發,這一步應該是從資料庫讀取!
char*getTString(structtm*tm0);//通過tm獲取時間字元串
voidshowSinfo(FLINFO*flinfo);//顯示航班對應座位信息
voidprintfFlinfo(FLINFO*flinfoHead);
FLINFO*selectFlinfo(FLINFO*flinfoHead,char*fid);//選擇航班號,返回節點
voidshowSinfo(FLINFO*flinfo);//顯示航班對應座位信息
SINFO*selectSinfo(FLINFO*flinfo,char*sid);//選擇座位,返回節點
intmain()
{
FLINFO*flinfoHead=initFlight(),*ffSelect=NULL;
SINFO*sfSelect=NULL;
charfid[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("未找到對應航班,按任意鍵繼續-----
");
getch();
system("cls");
continue;
}
system("cls");
printf("航班號:%s座位信息如下:
",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("未找到對應座位或該座位已出售,請重新輸入!按任意鍵繼續-----
");
getch();
system("cls");
continue;
}
printf("購買成功!按任意鍵繼續-----");
sfSelect->sell=1;
getch();
system("cls");
}
}
return0;
}
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))
returnsinfoHead->next;
sinfoHead=sinfoHead->next;
}
returnNULL;
}
voidshowSinfo(FLINFO*flinfo)//顯示航班對應座位信息
{
SINFO*sinfoHead=flinfo->sHead,*sfp=NULL;
inti,j,k,row=maxRow,clo=strlen(cID);
chartypeStr[10]={0};
for(i=0;i<row;i++)
{
//---------讀取座位所屬艙室------------
memset(typeStr,0,10);
for(k=0;k<TYPESIZE;k++)
if(i+1>=types[k][0]&&i+1<=types[k][1])
strcpy(typeStr,typeName[k]);
//--------------------------------------
printf("
");
for(j=0;j<clo;j++)
printf("-------------");
printf("
");
sfp=sinfoHead;
for(j=0;j<clo;j++)
{
printf("|%c%02d|",sfp->next->cloID,sfp->next->row);
sfp=sfp->next;
}
printf("
");
sfp=sinfoHead;
for(j=0;j<clo;j++)
{
printf("|%c|",sfp->next->sell?2:1);
sfp=sfp->next;
}
printf("
");
sfp=sinfoHead;
for(j=0;j<clo;j++)
{
printf("|%6s:%4.0f|",typeStr,flinfo->tPrice*tDiscount[sfp->next->type]*timeDiscount);
sfp=sfp->next;
}
printf("
");
sinfoHead=sfp;
}
for(j=0;i<clo;j++)
printf("-------");
printf("
");
}
FLINFO*selectFlinfo(FLINFO*flinfoHead,char*fid)//選擇航班號,返回節點
{
while(flinfoHead->next)
{
if(!strcmp(flinfoHead->next->fid,fid))
returnflinfoHead->next;
flinfoHead=flinfoHead->next;
}
returnNULL;
}
voidprintfFlinfo(FLINFO*flinfoHead)
{
while(flinfoHead->next)
{
printf("目的地:%s,航班號:%s
----起飛時間:%s,抵達時間:%s
",flinfoHead->next->toCity,flinfoHead->next->fid,getTString(localtime(&flinfoHead->next->tfTime)),getTString(localtime(&flinfoHead->next->ldTime)));
flinfoHead=flinfoHead->next;
}
}
char*getTString(structtm*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);
returnstr;
}
time_tgetTime_tfromStr(char*sTime)//將YYYY-MM-DDhh:mm:ss格式的時間字元串轉換成time_t型數值
{
time_trt;
structtm*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);
returnrt;
}
FLINFO*initFlight()//初始化航班信息,返回航班鏈表頭節點,如果想手動輸入,請在這里添加!!!正常軟體開發,這一步應該是從資料庫讀取!
{
FLINFO*ffHead=NULL,*flinfoTail=NULL,fNew;
//--------添加一個航班信息----需要增加按照我下面調用方式寫--------------------------------
strcpy(fNew.fid,"CI502");
fNew.tfTime=getTime_tfromStr("2019-02-2003:30:30");
fNew.ldTime=getTime_tfromStr("2019-02-2005: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-2014:30:30");
fNew.ldTime=getTime_tfromStr("2019-02-2016:40:30");
strcpy(fNew.toCity,"香港");
fNew.tPrice=500;
fNew.next=NULL;
flinfoTail=addFLINFO(&ffHead,flinfoTail,fNew);
returnffHead;
}
FLINFO*addFLINFO(FLINFO**ffHead,FLINFO*flinfoTail,FLINFOfNew)//返回尾節點
//添加航班信息鏈表的節點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;
returnflinfoTail;
}
SINFO*getSINFO()//獲取座位鏈表
{
intmaxClo=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;i<TYPESIZE;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++;
}
returnsinfoHead;
}
voidmeError(void*p)//內存申請失敗
{
if(p==NULL)
{
printf("
異常:內存申請失敗!回車結束程序!
");
while(getch()!='
');
exit(0);
}
}
7. 用C語言寫火車訂票系統
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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:\n>>1:according to the number of train;\n>>2: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<i;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)==0&&r->data.bookNum<200)
{
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;
}
8. 用C語言編寫火車售票系統
如果誰能用C語言能把這么題目就這樣順利的編出來的話,他已經是編程高手了,這太高級了
9. 求c語言,電影院售票管理系統的設計與實現的代碼
#include<stdio.h>
#include<stdlib.h>
structcell
{
char*name;
inttime;
intseat;
intsaled;
}a,b,c,d;
intsel=0;
voidtitle();
voidsaleproc();
intsaleThread(structcell*ce,intdd);
voidtiupiaoproc();
inttiupiaoThread(structcell*ce);
voidcount();
intinit(structcell*ce,char*name,inttime,intseat)
{
ce->name=name;
ce->time=time;
ce->seat=seat;
ce->saled=0;
return1;
}
intsaleThread(structcell*ce,intdd)
{
if((ce->seat-ce->saled)>=dd)
{
ce->saled+=dd;
printf("售票成功!按ESC鍵返回主菜單 ");
fflush(stdin);
intcc=_getche();
return1;
}
else
{
printf("售票失敗---余票不足!按ESC鍵返回主菜單 ");
fflush(stdin);
intcc=_getche();
return0;
}
}
voidsaleproc()
{
intf=1;
while(f)
{
system("cls");
printf("*********************************** ");
printf("** ");
printf("*廳影片時間余票* ");
printf("** ");
printf("*A%s%d:%d%3d* ",a.name,a.time/60,a.time%60,a.seat-a.saled);
printf("** ");
printf("*B%s%d:%d%3d* ",b.name,b.time/60,b.time%60,b.seat-b.saled);
printf("** ");
printf("*C%s%d:%d%3d* ",c.name,c.time/60,c.time%60,c.seat-c.saled);
printf("** ");
printf("*D%s%d:%d%3d* ",d.name,d.time/60,d.time%60,d.seat-d.saled);
printf("** ");
printf("*********************************** ");
printf("請選擇放映廳(a,b,c,d)和要購的票數(按ESC鍵返回主菜單)(格式:a5): ");
fflush(stdin);
charcc;
intdd;
dd=_getche();
if(dd==27)
{
f=0;
break;
}
else
{
cc=(char)dd;
}
scanf("%d",&dd);
switch(cc)
{
case'a':
saleThread(&a,dd);
f=0;
break;
case'b':
saleThread(&b,dd);
f=0;
break;
case'c':
saleThread(&c,dd);
f=0;
break;
case'd':
saleThread(&d,dd);
f=0;
break;
default:
break;
}
}
title();
}
inttiupiaoThread(structcell*ce)
{
if(ce->saled==0)
{
printf("撞尼瑪鬼了老子一張票都沒賣,哪來的退票??? ");
fflush(stdin);
intcc=_getche();
return0;
}
ce->saled--;
printf("退票成功!按任意鍵返回主菜單 ");
fflush(stdin);
intcc=_getche();
return1;
}
voidtiupiaoproc()
{
intf=1;
while(f)
{
system("cls");
printf("*********************************** ");
printf("** ");
printf("*退票* ");
printf("** ");
printf("*A%-20s* ",a.name);
printf("** ");
printf("*B%-20s* ",b.name);
printf("** ");
printf("*C%-20s* ",c.name);
printf("** ");
printf("*D%-20s* ",d.name);
printf("** ");
printf("*********************************** ");
printf("請輸入要退的廳(a,b,c,d): ");
charcc;
fflush(stdin);
scanf("%c",&cc);
if(cc=='')
{
f=0;
break;
}
switch(cc)
{
case'a':
tiupiaoThread(&a);
f=0;
break;
case'b':
tiupiaoThread(&b);
f=0;
break;
case'c':
tiupiaoThread(&c);
f=0;
break;
case'd':
tiupiaoThread(&d);
f=0;
break;
default:
break;
}
}
title();
}
voidcountproc()
{
intf=1;
while(f)
{
system("cls");
printf("*********************************** ");
printf("** ");
printf("*統計* ");
printf("** ");
printf("*A廳:余票%3d上座率%5.2f%%* ",a.seat-a.saled,(((float)a.saled/(float)a.seat)*100));
printf("** ");
printf("*B廳:余票%3d上座率%5.2f%%* ",b.seat-b.saled,(((float)b.saled/(float)b.seat)*100));
printf("** ");
printf("*C廳:余票%3d上座率%5.2f%%* ",c.seat-c.saled,(((float)c.saled/(float)c.seat)*100));
printf("** ");
printf("*D廳:余票%3d上座率%5.2f%%* ",d.seat-d.saled,(((float)d.saled/(float)d.seat)*100));
printf("** ");
printf("*********************************** ");
printf("按ESC鍵返回主菜單! ");
fflush(stdin);
intcc=_getche();
if(cc==27)
{
f=0;
break;
}
}
title();
}
voidtitle()
{
intf=1;
while(f)
{
system("cls");
printf("*********************************** ");
printf("** ");
printf("*歡迎光臨牛B電影院* ");
printf("** ");
printf("*1.售票* ");
printf("** ");
printf("*2.退票* ");
printf("** ");
printf("*3.統計* ");
printf("** ");
printf("*4.退出* ");
printf("** ");
printf("*********************************** ");
printf("請選擇:(1,2,3,4) ");
fflush(stdin);
scanf("%d",&sel);
getchar();
switch(sel)
{
case1:
saleproc();
f=0;
break;
case2:
tiupiaoproc();
f=0;
break;
case3:
countproc();
f=0;
break;
case4:
system("exit");
f=0;
break;
default:
break;
}
}
}
intmain(intargc,char*argv[])
{
init(&a,"鋼鐵俠3",18*60+30,150);
init(&b,"致青春",19*60+30,150);
init(&c,"姜戈",20*60+30,150);
init(&d,"生化危機4",20*60+20,100);
title();
return0;
}
10. 請高手幫忙啊!C語言編程~飛機訂票系統
昨天有個人在網上羨搭也問了《飛機訂票系統》。不過他是要把程序改的更完善些。程序編繹通過!兄裂拿運行良好。
我幫他修改了一點。他剛剛結貼~
你可以源枝到這里去看一下。希望能幫到你!
http://..com/question/42468468.html