c語言火車售票系統
Ⅰ 用c語言編寫一個車票管理系統
#include<stdio.h> ///宏定義函數
#include<string.h> //串操作及內存操作函數
#include<stdlib.h> /////調用系統函數
#include<time.h> ///時間庫函數
#include<conio.h> ///輸入輸出函數
#define N 1000 //宏定義多條記錄
typedef struct TICKET ////定義新結構體TICKET
{
char num[10]; //車次編號
char hour[3]; //時間,,小時
char min[3]; //分鍾
char from[10]; //起始站
char to[10]; //終點站
float hours; //車程實際時間
int max; //最大車載容量
int now; //已售票數
}CLASS;
int class_num=0; ///初始班次總數為0
CLASS records[N]; // 多條記錄
int system_time(); //系統時間
void NewMessage(); //新紀錄*錄入函數******
void ShowTable1(); //線框調用1,,,不用沒次輸
void ShowTable2(int i); //線框調用2,,,不用沒次輸
void Display(); //顯示所有函數
void add(); //增加記錄
void save(); // 保存函數
void load(); // 按班次查詢函數
void search(); //查詢函數
void change(); //修改函數**
void quit(); ////////// //退出函數******
void Ticketorder(); // //*售票函數***
void Ticketdelete(); //刪除函數*
int menu_select(); //菜單函數*
int whether(int); ////判斷時間是否超出函數***
void find(char s1[],char s2[]); //查詢函數★//s1為班次//s2為終點站
void deletemessage(); //刪除函數
int findnum(char s1[]); //班次查詢函數
void get(int,int); //修改及增加班次*
char *menu[]={"*****************歡迎使用車票查詢系統*****************",
"\n*******************MENU功能菜單***********************",
"\n◤ ★ 1. 錄入班次 ◥",
"\n◆ ★ 2. 顯示所有班次 ◆",
"\n◆ ★ 3. 查詢班次 ◆",
"\n◆ ★ 4. 增加班次 ◆",
"\n◆ ★ 5. 售票 ◆",
"\n◆ ★ 6. 退票 ◆",
"\n◆ ★ 7. 修改班次 ◆",
"\n◆ ★ 8. 刪除班次 ◆",
"\n◣ ★ 9. 退出 ◢"};
/**************************主函數***************************/
void main()
{
system("cls"); //刷屏
while(1) //菜單函數循環
{
switch(menu_select()) //菜單函數*
{
case 1: NewMessage();break; ////新紀錄*錄入函數******
case 2: Display();break; // //顯示
case 3: search();break; //查找函數**
case 4: add();break; // //增加記錄
case 5: Ticketorder();break; ////*售票函數***
case 6: Ticketdelete();break; //刪除函數*
case 7: change();break; //修改函數**
case 8: deletemessage();break; //////選擇調用刪除函數
case 9: quit();break; //////退出函數******
}
}
}
/***********************菜單函數**************************/
int menu_select() /////////菜單函數*
{
char s[5]; ///定義要輸入的數字功能
int c,i;
system("cls"); //刷屏
system("color 09");//改變顏色
for(i=0;i<11;i++)
{
printf("%s",menu[i]); //輸出菜單各項)
}
i=0;
while(c<0||c>9) //判斷是否是1到9的命令
{
printf("\n"); ///輸出換行
printf("******************************************************");
printf("\n請選擇(1-9):▁▂▃▄▆▇");
scanf("%s",s);
c=atoi(s); //atoi將字元串轉換成一個整數值
} ///輸出菜單面
return c; ////返回請求
}
/*******************************錄入函數************************************/
void NewMessage() //*錄入函數******
{
int i=0,j=5,h; ////
char s[5];
FILE *fp; ////定義文件型指針變數
system("cls"); //刷屏
if((fp=fopen("d:車票管理系統0.dat","rb"))!=NULL) //fopen打開文件
{
printf("車票信息已經存在請選擇增加功能!\n");
printf("任意輸入則返回菜單\n");
scanf("%s",s);
i=1; //通過是1
}
if(i==0)
{
system("cls"); //刷屏
printf("請輸入要錄入班次總數:\n");
scanf("%d",&class_num); //指向班次指針
system("cls"); //刷屏
for(i=0;i<class_num;i++)
{
system("cls"); //刷屏
printf("請輸入第%d個班次信息:\n",i+1);
h=-1;
for(;h!=i;) ///循環輸出
{
printf("請輸入班次:\n");
scanf("%s",records[i].num);
for(h=0;h<i;h++)
if(strcmp(records[h].num,records[i].num)==0)
//判斷字元串比較是否相等,, 待比較的字元串
{
printf("輸入錯誤!該班次已存在!\n");
break; ///返回
}
}
get(i,j); ////修改及增加班次*
j=5;
}
save(); //調用保存函數
}
}
/*******************************顯示所有函數********************************/
void Display() //顯示所有函數
{
int i,j;
system("cls"); //刷屏
load(); // 調用按班次查詢函數
ShowTable1(); //線框調用1,,,不用沒次輸
for(i=0,j=0;i<class_num;i++,j+=2) /////按班次順序輸出
{
printf("\n|----------|----------|----------|----------|--------|--------|--------|\n");
if(whether(i)) //判斷時間是否超出函數***
printf("|%10s|%5s:%-4s|%10s|%10s|%8.1f|%8d|%8d|",records[i].num,records[i].hour,
records[i].min, records[i].from,records[i].to,records[i].hours,
records[i].max,records[i].now);
else
printf("|%10s| 已發車 |%10s|%10s|%8.1f|%8d|%8d|",records[i].num,
records[i].from,records[i].to,records[i].hours,records[i].max,records[i].now);
}
ShowTable2(j); //線框調用2
printf("\n按任意鍵繼續....\n");
getch(); /////從控制台讀取一個字元,但不顯示在屏幕上
}
/**********************************列印表頭***********************************************/
void ShowTable1() //線框調用1,,,不用沒次輸
{
int i=2;
system("cls"); //刷屏
printf("**************************MESSAGE車程信息**********************************\n");
printf("\n|----------|----------|----------|----------|--------|--------|--------|\n");
printf("\n| 班次 | 發車時間 | 起點站 | 終點站 |行車時間|額定載量| 已售票 |\n");
}
/***************************************列印表尾************************************************/
void ShowTable2(int i) //線框調用2。不用沒次輸。
{
printf("\n|----------|----------|----------|----------|--------|--------|--------|\n");
}
/**************************查找函數11**************************/
void search() ////查詢調用
{
int i;
char s1[10]={'\0'},s2[10]={'\0'};
system("cls"); //刷屏
printf("1. 按班次查詢\n");
printf("2. 按終點站查詢\n");
printf("3. 退出\n");
printf("請選擇(1-3):\n");
scanf("%d",&i);
load(); // 調用按班次查詢函數
switch(i) //多分枝選擇語句
{
case 1: printf("請輸入要查詢的班次:\n"); //////s1為班次
scanf("%s",s1);
find(s1,s2); /////調用查找函數222 2
break;
case 2: printf("請輸入要查詢終點站:\n"); /////s2為終點站
scanf("%s",s2);
find(s1,s2); /////調用查找函數222 2
break;
case 3: break;
default : printf("輸入錯誤!\n");
break;
}
printf("按任意鍵繼續....\n");
getch(); ///從控制台讀取一個字元,但不顯示在屏幕上
}
/********************************查詢函數22*********************************/
void find(char s1[],char s2[])
{
int i,h=0,m;
ShowTable1(); ////調用線框1
if(s2[0]=='\0')
m=1; ///1為無條件執行
else m=0;
for(i=0;i<class_num;i++)
if(strcmp(s1,records[i].num)==0||strcmp(s2,records[i].to)==0)
////判斷字元串比較是否相等,, 待比較的字元串
{
printf("\n|----------|----------|----------|----------|--------|--------|--------|\n");
printf("|%10s|%5s:%-4s|%10s|%10s|%8.1f|%8d|%8d|",records[i].num,records[i].hour,records[i].min,
records[i].from,records[i].to,records[i].hours,records[i].max,records[i].now);
h+=2;
if(m==1)
break;
}
ShowTable2(h); ////調用線框2
if(h==0)
printf("要查找的班次不存在!\n");
}
/***********************增加函數****************************/
void add() // //增加記錄
{
int i,j=5;
load(); // 調用按班次查詢函數
system("cls"); //刷屏
printf("1. 增加班次\n");
printf("2. 返回\n");
printf("請選擇(1-2)\n");
scanf("%d",&i);
if(i==1)
{
system("cls"); //刷屏
printf("1. 請輸入要增加的班次:\n");
scanf("%s",records[class_num].num);
for(i=0;i<class_num;i++)
if(strcmp(records[class_num].num,records[i].num)==0)
//判斷字元串比較是否相等,, 待比較的字元串 判斷車次沒重復
{
printf("輸入錯誤!\n");
getch(); /////從控制台讀取一個字元,但不顯示在屏幕上
break;
}
if(i==class_num)
{
get(i,j); ////修改及增加班次*
class_num++; ///使班次數加1
save(); //調用保存函數
}
}
}
/********************************售票函數*****************************/
void Ticketorder() //*售票函數***
{
int i;
char num[10];
system("cls"); //刷屏
printf("1. 售票\n");
printf("2. 返回\n");
printf("請選擇(1-2):\n");
scanf("%d",&i);
if(i==1)
{
load(); // 調用按班次查詢函數
search(); //查找函數**
printf("請輸入要訂票的班次(若無請輸入0):\n");
scanf("%s",num);
for(i=0;i<class_num;i++)
if(strcmp(num,records[i].num)==0)//判斷字元串比較是否相等,, 待比較的字元串
if(records[i].max>records[i].now&&whether(i)==1)
//判斷時間是否超出函數***並且***沒超出最大客量
{
records[i].now++; ///使已售的暑假1
printf("通向%s班次為%s的票訂票成功!\n",records[i].to,records[i].num);
save(); //調用保存函數
getch(); /////從控制台讀取一個字元,但不顯示在屏幕上
break;
}
else
{
printf("該班次已滿或已發出!\n");
getch(); /////從控制台讀取一個字元,但不顯示在屏幕上
}
}
}
/****************************退票刪除函數***************************************/
void Ticketdelete() //刪除函數*
{
int i;
char num[10];
system("cls"); //刷屏
printf("1. 退票\n");
printf("2. 返回\n");
printf("請選擇(1-2)\n:");
scanf("%d",&i);
if(i==1)
{
system("cls"); //刷屏
load(); // 調用按班次查詢函數
printf("請輸入要退票的班次:\n");
scanf("%s",num);
i=findnum(num); //調用班次查詢函數
if(strcmp(num,records[i].num)==0) //判斷字元串比較是否相等 待比較的字元串
if(whether(i)) //判斷時間是否超出函數***
{
printf("確定(Y/N)?");
scanf("%s",num);
if(num[0]=='y'||num[0]=='Y')
{
records[i].now--; //使已售票加1
printf("退票成功!\n");
save(); //調用保存函數
getch(); //從控制台讀取一個字元,但不顯示在屏幕上
}
}
else
{
printf("該班車已發出,無法退票!\n");
getch(); /////從控制台讀取一個字元,但不顯示在屏幕上
}
if(i==class_num)
{ printf("輸入錯誤!\n");
getch(); /////從控制台讀取一個字元,但不顯示在屏幕上
}
}
}
/********************************修改函數*********************************/
void change() //修改函數**
{
char num[10],s[10];
int h=0,j=13,i;
load(); // 調用按班次查詢函數
system("cls"); //刷屏
printf("請輸入要修改的班次:\n");
scanf("%s",num);
i=findnum(num); //調用班次查詢函數
if(i==class_num)
{
printf("輸入錯誤,無此班次!\n");
getch(); ///從控制台讀取一個字元,但不顯示在屏幕上
}
else
{
printf("確定修改(Y/N)?\n");
scanf("%s",s);
if(s[0]=='y'||s[0]=='Y')
{
get(i,j); ////修改及增加班次*
save(); //調用保存函數
}
}
}
/*******************************刪除函數**********************************/
void deletemessage() //刪除班次信息
{
int i,h=0;
char num[10];
system("cls"); //刷屏
printf("1. 刪除班次\n");
printf("2. 返回\n");
printf("請選擇(1-2):\n");
scanf("%d",&i);
if(i==1)
{
system("cls"); //刷屏
printf("請輸入要刪除的班次:\n");
scanf("%s",num);
i=findnum(num); //調用班次查詢函數
if(i==class_num)
{
printf("輸入錯誤,無此班次!\n");
getch(); ///從控制台讀取一個字元,但不顯示在屏幕上
}
else
{
printf("確定?(y/n)\n");
scanf("%s",num);
if(num[0]=='y'||num[0]=='Y')
{
for(;i<class_num-1;i++)
records[i]=records[i+1];
class_num--; ////班次總數減1
save(); //調用保存函數
printf("刪除成功!\n");
getch();
}
}
}
}
/****************************按班次查詢函數****************************/
int findnum(char s1[]) //查找班次
{
int i,h=0;
ShowTable1(); //線框調用1,,,不用沒次輸
for(i=0;i<class_num;i++)
{
if(strcmp(s1,records[i].num)==0) //判斷字元串比較是否相等,如果符合則輸出車次等信息
{
printf("|----------|----------|----------|----------|--------|--------|--------|\n");
printf("|%10s|%5s:%-4s|%10s|%10s|%8.1f|%8d|%8d|",records[i].num,records[i].hour,
records[i].min,records[i].from,records[i].to,records[i].hours,records[i].max,records[i].now);
h+=2; ///加2使輸出的框架合適
break;
}
}
ShowTable2(h); ///調用框架2
return i;
}
/**************************************保存函數*******************************/
void save() //////保存函數
{
FILE *fp1,*fp2; //文件行指針
if((fp1=fopen("d:車票管理系統.dat","wb"))==NULL) //打開文件、為輸出打開一個二進制文件
{
printf("文件打開錯誤!\n");
exit(0);
}
if((fp2=fopen("d:車票管理系統0.dat","wb"))==NULL) //打開文件、為輸出打開一個二進制文件
{
printf("文件打開錯誤!\n");
exit(0);
}
fwrite(&class_num,sizeof(int),1,fp2); //寫入文件信息
fwrite(records,sizeof(CLASS),class_num,fp1);
fclose(fp1);fclose(fp2); ///關閉文件
}
/*******************************按班次查詢函數*******************************/
void load() //按班次查詢函數
{
FILE *fp1,*fp2;
if((fp1=fopen("d:車票管理系統.dat","rb"))==NULL)
{
system("cls"); //刷屏
printf("文件打開錯誤!\n");
getch(); ///從控制台讀取一個字元,但不顯示在屏幕上
exit(0);
}
if((fp2=fopen("d:車票管理系統0.dat","rb"))==NULL)
{
system("cls"); //刷屏
printf("文件打開錯誤!\n");
getch(); ///從控制台讀取一個字元,但不顯示在屏幕上
exit(0); //退出
}
fread(&class_num,sizeof(int),1,fp2); ///讀入信息
fread(records,sizeof(CLASS),class_num,fp1);/////讀入信息
fclose(fp1);fclose(fp2); ///文件關閉
}
/******************************退出函數***************************/
void quit() //退出函數******
{
char s[5];
printf("確認退出?(Y/N)\n");
scanf("%s",s);
if(s[0]=='y'||s[0]=='Y')
exit(0); //程序中止執行,返回調用過程..state 0-正常中止非0-非正常
}
/*************************修改及增加班次*******************************/
void get(int i,int j) // 修改及增加班次*
{
for(;;)
{
printf("請輸入發車時間(xx xx)");scanf("%s%s",records[i].hour,records[i].min);
if((atoi(records[i].hour)<24&&atoi(records[i].hour)>=0)&&(atoi(records[i].min)<60&&atoi(records[i].min)>=0))
break;
else
{
printf("輸入錯誤!\n");
getch(); /////從控制台讀取一個字元,但不顯示在屏幕上
}
}
printf("請輸入起點站:\n");
scanf("%s",records[i].from);
printf("請輸入終點站:\n");
scanf("%s",records[i].to);
printf("請輸入行車時間:\n");
scanf("%f",&records[i].hours);
printf("請輸入額定載量:\n");
scanf("%d",&records[i].max);
for(;;)
{
printf("請輸入已售票數:\n");
scanf("%d",&records[i].now);
if(records[i].now<=records[i].max)
break;
else
{
printf("輸入錯誤!\n");
getch(); /////從控制台讀取一個字元,但不顯示在屏幕上
}
}
}
/********************************判斷時間是否超出函數**********************************/
int whether(int i) //判斷時間是否超出函數***
{
struct tm *local; //時間結構體
time_t t; //把當前時間給t
t=time(NULL); //NULL在stdio.h中定義為0
local=localtime(&t); /////獲取當前系統時間
if(local->tm_hour<atoi(records[i].hour)||local->tm_hour==atoi(records[i].hour)&&local->tm_min<atoi(records[i].min))
//atoi將字元串轉換成一個整數值
return 1;
else
return 0;
}
我也是拷貝別人的 我用過了這個程序可行
Ⅱ 用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;
}
Ⅲ 如何用C++編寫一個航空售票系統
#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
using namespace std;
class Tair //通過定義一個類來定義數據錄入的函數編寫一個航空售票系統。
Ⅳ c語言如何編寫飛機訂票系統
java">#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
#include<malloc.h>
#include<math.h>//overflow
#defineok1
typedefstructYidingkehu
{//單鏈表
charname[15];//已訂票的客戶姓名
intdingpiaoshu;//已訂票數量
structYidingkehu*next1;//
}Yidingkehu,*Link;
typedefstructWeidingkehu
{//單鏈隊
charname[15];//預訂票的客戶姓名
intyudingpiao;//要訂票數量
structWeidingkehu*next2;//下一個鏈隊結點指針
}Weidingkehu,*Qptr;
typedefstructHangxian
{//創建一個含有六個信息的結構體
charhangbanhao[15];//航班號-
charfeijihao[15];//飛機號
intfeixingriqi;//起飛時間
intchenkerenshu;//座位數
intyupiao;//余票
charzhongdianzhai[15];//降落城市
structHangxian*next;//指向下一個鏈結點的指針
structYidingkehu*yiding;//定義一個指向已訂票客戶的頭結點指針
structWeidingkehu*yudingqueue;
}Hangxian,*Linklist;
LinklistInitLinklist();//01
intInsertLinklist(Linklist&head1);//02
voidhbhchaxun();//通過航班號查詢
voidmddchaxun();//通過目的地查詢
voidlurugongneng();//初始化錄入功能
voidchaxungongnen();//查詢功能
voiddingpiaogongnen();//訂票功能
voidtuipiaogongnen();//退票功能
voidmain()
{
intn;
do{//列印主界面
printf(" 歡迎使用航空客運訂票系統 ");
printf(" +++++++++++++++++++++++++++++ ");
printf(" ==>1.錄入功能== ");
printf(" ==>2.查詢功能== ");
printf(" ==>3.訂票功能== ");
printf(" ==>4.退票功能== ");
printf(" ==>5.退出== ");
printf(" +++++++++++++++++++++++++++++ ");
printf(" 請選擇:");
scanf("%d",&n);printf(" ");
switch(n)
{
case1:lurugongneng();//錄入功能
break;
case2:chaxungongnen();//查詢功能
break;
case3:dingpiaogongnen();//訂票功能
break;
case4:tuipiaogongnen();//退票功能
break;
default:exit(0);//退出
}
}while(n==1||n==2||n==3||n==4);
}
voidlurugongneng()//初始化的單鏈表*********************************************************錄入功能
{
Linklistp;
//intm,n;
if(!p)exit(OVERFLOW);
printf(" 請依次輸入下面幾項內容: ");//這里的輸入採用一個個單獨輸入,避免了亂賦值的現象
printf("航班號 ");
gets(p->hangbanhao);//這里的二個gets主要是因為在回車鍵的輸入,其中的第一個是來接收上次的回車
gets(p->hangbanhao);
printf("飛機號 ");
gets(p->feijihao);
printf("終點站 ");
gets(p->zhongdianzhai);
printf("飛行日期 ");
scanf("%d",&p->feixingriqi);
printf("乘客總數 ");
scanf("%d",&p->chenkerenshu);
printf("余票數 ");
scanf("%d",&p->yupiao);
}
voidchaxungongnen()//******************************************************************查詢功能
{
intn;
printf(" 查找航線信息 ");
printf(" +++++++++++++++++++++++++++++ ");
printf(" ==>1.通過目的地查詢== ");
printf(" ==>2.通過航班號查詢== ");
printf(" +++++++++++++++++++++++++++++ ");
printf(" 請選擇:");
scanf("%d",&n);
printf(" ");//格式化
switch(n)
{
case1:mddchaxun();
break;
case2:hbhchaxun();
break;
default:break;
}
}
voidmddchaxun()//通過目的地查詢
{
charc[15];
intm;
Linklistp=L;
printf(" 請輸入要查詢的目的地:");
gets(c);
gets(c);//原因同上
do{
p=p->next;
if(p)
{
m=strcmpi((*p).zhongdianzhai,c);//如果==的話則m=0;
if(m==0)
{
printf(" 航班信息: ");
printf(" 航班號:%s ",p->hangbanhao);
printf(" 飛機號:%s ",p->feijihao);
printf(" 飛行時間:周%d ",p->feixingriqi);
printf(" 余票量:%d ",p->yupiao);
}
}
else
{//如果不匹配的話就做
printf(" 對不起沒有你要找的目的地: ");m=0;
}
}while(m!=0);
}
voidhbhchaxun()//通過目的地查詢
{
charc[15];
intm;
Linklistp=L;
printf(" 請輸入要查詢的航班號:");
gets(c);gets(c);printf(" ");
do{
p=p->next;
if(p)
{
m=strcmpi((*p).hangbanhao,c);//如果==的話則m=0;這里的(*p).與p->的作用是一樣的
if(m==0)
{
printf(" 航班信息: ");
printf(" 航班號:%s ",p->hangbanhao);
printf(" 飛機號:%s ",p->feijihao);
printf(" 飛行時間:周%d ",p->feixingriqi);
printf(" 余票量:%d ",p->yupiao);
}
}
else
{//如果不匹配的話就做
printf(" 對不起沒有你要找的航班號: ");m=0;
}
}while(m!=0);
}
voiddingpiaogongnen()//***************************************************************訂票功能
{
charc[15];
intm=1,piao,ydpiao=0,yd=0,n;//
gets(c);
printf("請輸入終點站名:");gets(c);printf(" ");
p=L->next;
if(p){
do{//查找一下,是否有這個航班
if(!p)
{
printf("對不起,沒有你要找的航班: ");
gotoloop1;
}
m=strcmpi(p->zhongdianzhai,c);
if(m==0)
{
printf("航班信息: ");
printf("航班號:%s ",p->hangbanhao);
printf("飛機號:%s ",p->feijihao);
printf("飛行時間:周%d ",p->feixingriqi);
printf("余票量:%d ",p->yupiao);}
elsep=p->next;
}while(m!=0);
if(m==0)
{
do{
printf(" 請輸入你要訂的票數:");scanf("%d",&piao);
if(piao<=p->yupiao)
{
h=p->yiding;
if(h)
{
h1=h;
h=h->next1;
h=(structYidingkehu*)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("訂票成功: ");m=2;
}
}
else
{
printf("余票量:%d ",p->yupiao);
printf("對不起,余票%d張不足,不能完成訂票 ",p->yupiao);
printf("是否要重新訂票? ");
printf("需要請輸入1否則請按2預訂請輸入3:");
scanf("%d",&m);
printf(" ");
if(m==3)gotoloop3;
}
}while(m==1);
}
}
elseif(!p)
{
loop3:structWeidingkehu*q3;
printf("對不起,該航班的票已售完 ");
q.front=p->yudingqueue;
if(q.front==q.rear)printf("沒有人預訂票,是否要預訂? ");
elseif(q.front!=q.rear)printf("已有人預訂票,是否要預訂? ");
printf("預訂請輸入1否則輸入2:");
scanf("%d",&n);
printf(" ");
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("你已經預訂了! ");
}
}
loop1:;
}
voidtuipiaogongnen()//***************************************************************退票功能
{
}
///////////以下是人家的要求//////////
1、本系統採用一個包含N個數據的結構體數組,每個數據的結構應當包括:起飛地、目的地航班號、座次號碼、座次訂出與否標記、訂座者的姓名和訂座者的身份證號碼。
2、本系統顯示這樣的菜單:
(1)輸入航班信息
(2)輸出航班信息
(3)查找航班信息
a.顯示空座的數量
b.顯示空座的信息
c.顯示已訂座的信息
d.起飛時間
(4)訂票預約
(5)刪除定票預約
(6)退出系統
1、本系統成功執行菜單的每個信息,選項4)和5)將要求額外的輸入,並且它們都允許用戶收回其輸入。
2、查找航班信息既可按線路查,也可按目的地查。
4、在兩次運行之間,數據被保存於一個文件里,當本程序重新開
始的時候,它首先從該文件讀入數據
Ⅳ C語言進階編程求助「火車票銷售系統」
可以用資料庫系統來做
核心數據結構應該是票,應該包括: 車次 出發日期 出發站 到達站 車廂 座位 是否已出售 購票人 證件號 等欄位屬性
然後所有的操作都可以轉化為 添加、修改票數據
比如買了 現有一張票 0001 {T90、2012-1-1 出發、A 站出發、到達B站、未出售...}
那麼 買票操作就是 票0001的是「否已出售」 「購票人」 "證件號" 做響應修改
如果 只買AB站的一個區間C、D那麼還要修改出發站和到達站 同時添加另外2張新票數據 A到C的哈D到B的票 哈哈有點饒人
添加車次就按照列車的實際情況 一次型的向系統資料庫里添加 若干張票的shu