当前位置:首页 » 编程语言 » c语言火车售票系统

c语言火车售票系统

发布时间: 2025-07-13 15:06:55

Ⅰ 用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

热点内容
linux部署windows 发布:2025-07-13 22:53:37 浏览:261
c语言printf函数用法 发布:2025-07-13 22:53:30 浏览:293
压缩萝卜干 发布:2025-07-13 22:52:01 浏览:672
为什么dns一直配置错误 发布:2025-07-13 22:43:06 浏览:258
fortran如何编译 发布:2025-07-13 22:31:05 浏览:480
sql语句查询字段 发布:2025-07-13 22:24:20 浏览:632
python目录遍历 发布:2025-07-13 22:16:24 浏览:96
卖房说解压 发布:2025-07-13 22:06:49 浏览:216
C加密传输 发布:2025-07-13 22:06:08 浏览:162
配置不合理怎么解决 发布:2025-07-13 22:01:07 浏览:735