车票系统,与你的第2题要求略有不同,你自己修改吧
#include<stdio.h>
#include<stdlib.h>
#include<dos.h>
#include<time.h>
#include<conio.h>
#include<string.h>
const int MAX=20;
const int N=50;
const int MAXFILENAME=100;
enum {FALSE,TRUE};
struct time
{
int hour;
int minutes;
};
struct ticket
{
int SerialNumber;
struct time SetOut;
char DeparturePoint[MAX];
char TerminalPoint[MAX];
float LastTime;
int FixNumber;
int FixedNumber;
}car[N];
int RecordNumber;
int FLAG=FALSE;
/**********************************************/
int ShowMessage(void); //载入原始数据
void mainmenu(void); //主菜单
void InsertMessage(void); //录入班次信息
void SearchMenu(void); //查询路线子菜单
void SearchByNumber(void); //按班次号查询
void SearchByAddress(void); //按终点站查询
void TicketManagement(void); //车票售出与退回
void TicketOrder(void); //售票
void TicketDelete(void); //退票
void exit(void); //退出该系统
/********************************************/
int main(void)
{
RecordNumber=ShowMessage();
if(1)
do{
mainmenu();
}while(FLAG==FALSE);
return 0;
}
void mainmenu(void)
{
system("color 1f");
system("mode con: cols=140 lines=130");
char functionNumber;
printf(" 车票管理系统\n\n");
printf(" 制作:hmm182007\n");
printf(" 日期:2007/11/25/19:57\n");
printf("============================================================================\n");
printf(" 1.录入班次信息\t\n");
printf(" 2.浏览班次信息\t\n");
printf(" 3.查询行车路线\t\n");
printf(" 4.售票与退票系统\t\n");
printf(" 5.退出该系统\t\n");
printf("============================================================================\n");
printf("请选择你所需要的功能:");
scanf("%c",&functionNumber);
switch(functionNumber)
{
case '1':
{
InsertMessage();
printf("\n按任意键回主菜单......\n");
getchar();
getchar();
}
break;
case '2':
{
ShowMessage();
printf("\n按任意键回主菜单......\n");
getchar();
getchar();
}
break;
case '3':
{
SearchMenu();
printf("\n按任意键回主菜单......\n");
getchar();
getchar();
}
break;
case '4':
{
TicketManagement();
getchar();
getchar();
}
break;
case '5':FLAG=TRUE;exit();break;
default:
{
printf("输入错误,请确保你的输入为1--5.\n");
printf("按任意键回主菜单......\n");
getchar();
getchar();
}
}//switch ends
FLAG=FALSE;
}
void InsertMessage(void)
{
FILE *fp;
char filename[MAXFILENAME];
int i;
printf("请输入文件名或者文件路径名:");
scanf("%s",filename);
if((fp=fopen(filename,"r"))==NULL)
{
printf("文件%s不可读,请确认文件%s存在或者文件路径正确.\n\n",filename,filename);
mainmenu();
}
for(i=RecordNumber;!feof(fp)&&!ferror(fp);i++)
{
fscanf(fp,"%d%d%d",&car[i].SerialNumber,&car[i].SetOut.hour,&car[i].SetOut.minutes);
fscanf(fp,"%s%s%f%d%d",&car[i].DeparturePoint,&car[i].TerminalPoint,&car[i].LastTime,&car[i].FixNumber,&car[i].FixedNumber);
}
int count=i;
printf("班次\t发车时间\t起点\t终点\t行车时间(小时)\t额定载量\t已定票人数\n");
for(i=0;i<count;i++)
{
printf("%d\t%d:%d\t\t",car[i].SerialNumber,car[i].SetOut.hour,car[i].SetOut.minutes);
printf("%s\t%s\t\t%.1f\t %d\t\t%d\n",car[i].DeparturePoint,car[i].TerminalPoint,car[i].LastTime,car[i].FixNumber,car[i].FixedNumber);
}
fclose(fp);
}
int ShowMessage(void)
{
int i;
FILE *fp;
if((fp=fopen("add.txt","r"))==NULL)
{
printf("读取数据失败,请检查文件add.txt是否存在.\n");
exit(-1);
}
for(i=0;!feof(fp)&&!ferror(fp);i++)
{
fscanf(fp,"%d%d%d",&car[i].SerialNumber,&car[i].SetOut.hour,&car[i].SetOut.minutes);
fscanf(fp,"%s%s%f%d%d",&car[i].DeparturePoint,&car[i].TerminalPoint,&car[i].LastTime,&car[i].FixNumber,&car[i].FixedNumber);
}
int count=i;
printf("班次\t发车时间\t起点\t终点\t行车时间(小时)\t额定载量\t已定票人数\n");
for(i=0;i<count;i++)
{
printf("%d\t%d:%d\t\t",car[i].SerialNumber,car[i].SetOut.hour,car[i].SetOut.minutes);
printf("%s\t%s\t\t%.1f\t %d\t\t%d\n",car[i].DeparturePoint,car[i].TerminalPoint,car[i].LastTime,car[i].FixNumber,car[i].FixedNumber);
}
return count;
}
void SearchMenu(void)
{
system("color 1f");
system("mode con: cols=140 lines=130");
char functionNumber;
printf(" 查询子菜单\n");
printf("============================================================================\n");
printf(" 1.按班次号查询\t\n");
printf(" 2.按终点站查询\t\n");
printf(" 3.返回主菜单\t\n");
printf("============================================================================\n");
printf("请选择你所需要的功能:");
scanf("%*c%c",&functionNumber);
switch(functionNumber)
{
case '1':SearchByNumber();break;
case '2':SearchByAddress();break;
case '3':
getchar();
mainmenu();
break;
default:
printf("输入错误,请确保你的输入为1--3.\n");
printf("按任意键回查询子菜单......\n");
getchar();
getchar();
SearchMenu();
}
}
void SearchByNumber(void)
{
int SearchNumber;
printf("请输入要查询的班次号:");
scanf("%d",&SearchNumber);
printf("班次\t发车时间\t起点\t终点\t行车时间(小时)\t额定载量\t已定票人数\n");
printf("%d\t%d:%d\t\t",car[SearchNumber-1].SerialNumber,car[SearchNumber-1].SetOut.hour,car[SearchNumber-1].SetOut.minutes);
printf("%s\t%s\t\t%.1f\t %d\t\t%d\n",car[SearchNumber-1].DeparturePoint,car[SearchNumber-1].TerminalPoint,car[SearchNumber-1].LastTime,car[SearchNumber-1].FixNumber,car[SearchNumber-1].FixedNumber);
}
void SearchByAddress(void)
{
int i;
char Address[MAX];
printf("请输入终点站的名称:");
scanf("%s",&Address);
printf("班次\t发车时间\t起点\t终点\t行车时间(小时)\t额定载量\t已定票人数\n");
for(i=0;i<RecordNumber;i++)
{
//对比终点站记录,找到并输出
if((strcmp(Address,car[i].TerminalPoint))==0)
{
printf("%d\t%d:%d\t\t",car[i].SerialNumber,car[i].SetOut.hour,car[i].SetOut.minutes);
printf("%s\t%s\t\t%.1f\t %d\t\t%d\n",car[i].DeparturePoint,car[i].TerminalPoint,car[i].LastTime,car[i].FixNumber,car[i].FixedNumber);
}
}
}
void TicketManagement(void)
{
char functionNumber;
system("color 1f");
system("mode con: cols=140 lines=130");
printf(" 订票与退票菜单\n");
printf("============================================================================\n");
printf(" 1.订票\t\n");
printf(" 2.退票\t\n");
printf(" 3.返回主菜单.\n");
printf("============================================================================\n");
printf("选择你需要的功能:");
scanf("%*c%c",&functionNumber);
switch(functionNumber)
{
case '1':
{
TicketOrder();
//getchar();
//getchar();
}
break;
case '2':TicketDelete();break;
case '3':
{
getchar();
mainmenu();
}
break;
default:
{
printf("输入错误,请确保你的输入为1--3.\n");
printf("按任意键回主菜单......\n");
getchar();
getchar();
mainmenu();
}
}
}
void TicketOrder(void)
{
int i;
printf("请输入要订购的车票的班次:");
scanf("%d",&i);
if(i<0||i>RecordNumber)
{
printf("对不起,今天没有这趟车,请明天再来,谢谢使用!\n");
printf("按任意键回主菜单......\n");
getchar();//getchar();
TicketManagement();
}
time_t tval;
struct tm *now;
tval = time(NULL);
now = localtime(&tval);
if((now->tm_hour==car[i].SetOut.hour&&now->tm_min<car[i].SetOut.minutes)||(now->tm_hour<car[i].SetOut.hour))
{
if(car[i].FixedNumber<car[i].FixNumber)
{
car[i].FixedNumber++;
printf("你的订票成功,请按时上车,谢谢使用!\n");
}
else printf("对不起,今天的这趟车的票已卖完,请明天再来,谢谢合作!\n");
}
else printf("对不起,今天的这趟车已发出,请明天再来,谢谢合作!\n");
printf("班次\t发车时间\t起点\t终点\t行车时间(小时)\t额定载量\t已定票人数\n");
printf("%d\t%d:%d\t\t",car[i-1].SerialNumber,car[i-1].SetOut.hour,car[i-1].SetOut.minutes);
printf("%s\t%s\t\t%.1f\t %d\t\t%d\n",car[i-1].DeparturePoint,car[i-1].TerminalPoint,car[i-1].LastTime,car[i-1].FixNumber,car[i-1].FixedNumber);
//printf("按任意键回主菜单......\n");
//getchar();getchar();
}
void TicketDelete(void)
{
int i;
printf("请输入要退购的车票的班次:");
scanf("%d",&i);
if(i<0||i>RecordNumber)
{
printf("对不起,今天没有这趟车,无法完成退票.谢谢使用!\n");
getchar();
//getchar();
TicketManagement();
getchar();
}
time_t tval;
struct tm *now;
tval = time(NULL);
now = localtime(&tval);
if((now->tm_hour==car[i].SetOut.hour&&now->tm_min<car[i].SetOut.minutes)||(now->tm_hour<car[i].SetOut.hour))
{
if(car[i].FixedNumber>=1)
{
car[i].FixedNumber--;
printf("退订车票成功,谢谢使用!\n");
}
else printf("对不起,今天的这趟车的票尚未卖出,无法完成退票!\n");
}
else printf("对不起,今天的这趟车已发出,无法完成退票!\n");
printf("班次\t发车时间\t起点\t终点\t行车时间(小时)\t额定载量\t已定票人数\n");
printf("%d\t%d:%d\t\t",car[i-1].SerialNumber,car[i-1].SetOut.hour,car[i-1].SetOut.minutes);
printf("%s\t%s\t\t%.1f\t %d\t\t%d\n",car[i-1].DeparturePoint,car[i-1].TerminalPoint,car[i-1].LastTime,car[i-1].FixNumber,car[i-1].FixedNumber);
getchar();//getchar();
}
void exit(void)
{
printf("\n\n*********************谢谢使用本系统,欢迎下次继续使用***********************\n");
exit(0);
}
温馨提示:内容为网友见解,仅供参考