回复 19楼 书生牛犊
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <windows.h>
void system_time();
void load();
void add();
void search();
void buy_ticket();
void back_ticket();
void output();
void foutput();
void search1();
void search2();//申明函数
struct time
{
int hour;
int minute;
};
struct Ticket
{
char number[100];//班次
struct time ft;//列车出发时间
char origin_station[100];//起始站
char last_station[100];//终点站
float last_time;//行车时间
int max_number;//额定载量
int sold_ticket;//已订票人数
char condition[100];//状态
struct Ticket*next;
};
int i;
int syshour;
//记录系统时间小时
int sysminute;
//记录系统时间分钟
char filename[100];
//文件名数组
int record;
//记录条数记录
struct Ticket *head,*end,*p,*q;//全局变量,方便后面的使用
void main()
{
printf("当前时间:");
system_time();
int n;
n=1;
while(n!=0)
{
printf("*******************************************\n");
printf("
欢迎使用车票管理系统
\n");
printf("
1.录入车票信息
\n");
printf("
2.单个添加车次信息
\n");
printf("
3.查询路线
\n");
printf("
4.购票
\n");
printf("
5.退票
\n");
printf("
0.退出程序
\n");
printf("*******************************************\n");
printf("请输入您想做的操作:");
scanf("%d",&n);
switch(n)
{
case 1:load();break;
case 2:add();break;
case 3:search();break;
case 4:buy_ticket();break;
case 5:back_ticket();break;
case 6:back_ticket();break;
case 0:break;
default :printf("选择错误!!!");
}
printf("感谢您的使用!!!\n");
}
}
void system_time()
{
SYSTEMTIME sys;
GetLocalTime( &sys );
//调用系统时间
printf( "%4d /%02d/%02d %02d:%02d:%02d 星期%1d\n",
sys.wYear,
sys.wMonth,
sys.wDay,
sys.wHour,
sys.wMinute,
sys.wSecond,
sys.wDayOfWeek);
syshour=sys.wHour;
sysminute=sys.wMinute;
}
void output(struct Ticket *head1)
{
struct Ticket *q;//建立量表的函数
printf("班次\t时间\t起始站\t终点站\t行车时间\t额定载量\t已订票人数\t状态\n");
q=head1->next;
while(q!=NULL)
{
printf("%s\t%02d:%02d\t%s\t%s\t%f\t%d\t%d\t%s\t",q->number,q->ft.hour,q->ft.minute,q->origin_station,q->last_station,q->last_time,q->max_number,q->sold_ticket);
if((syshour>q->ft.hour)||((syshour==q->ft.hour) &&(sysminute>q->ft.minute)))
printf("已发出\n",q->condition);
else
printf(" \n",q->condition);
q=q->next;
}
}
void foutput(struct Ticket *head1)
{
FILE*fp1;
struct Ticket *q;
fp1=fopen("车票班次信息.txt","w");
if(fp1==NULL)
{
printf("无法打开文件!\n");
exit(0);
}
q=head1->next;
while(q!=NULL)
{
fprintf(fp1,"%s\t%02d:%02d\t%s\t%s\t%f\t%d\t%d\t ",q->number,q->ft.hour,q->ft.minute,q->origin_station,q->last_station,q->last_time,q->max_number,q->sold_ticket);
q=q->next;
}
fclose(fp1);
}
void load()
{
FILE*fp;
fp=fopen("车票班次信息.txt","r");
if(fp==NULL)
{
printf("无法打开文件");
exit(0);
}
head=(struct Ticket *)malloc(sizeof(struct Ticket));
end=head;
end->next=NULL;
while(!feof(fp))
{
p=(struct Ticket *)malloc(sizeof(struct Ticket));
fscanf(fp,"%s%02d:%02d%s%s%f%d%d\n",&p->number,&p->ft.hour,&p->ft.minute,&p->origin_station,&p->last_station,&p->last_time,&p->max_number,&p->sold_ticket);
p->next=NULL;
end->next=p;
end=p;
}
output(head);
fclose(fp);
}
void add()
{
load();
p=(struct Ticket *)malloc(sizeof(struct Ticket));
printf("请输入您要增加的班次:");
scanf("%s",&p->number) ;
printf("请输入您要增加的班次的出发时间(h):");
scanf("%02d:%02d",&p->ft.hour,&p->ft.minute);
printf("请输入你要增加的班次的起始站:");
scanf("%s",&p->origin_station);
printf("请输入您要增加的班次的终点站:");
scanf("%s",&p->last_station);
printf("请输入您要增加的班次的行车时间:");
scanf("%f",&p->last_time);
printf("请输入您要增加的班次的额定载量");
scanf("%d",&p->max_number);
printf("请输入您要增加的班次的已订票人数");
scanf("%d",&p->sold_ticket);
p->next=NULL;
end->next=p;
end=p;
foutput(head);
output(head);
}
void search()
{
int s;
printf("*****************查询菜单****************\n");
printf("
1.按班次号查询
\n");
printf("
2.按终点站查询
\n");
printf("
0.返回主菜单
\n");
printf("*****************************************\n");
printf("请输入您想要的操作:\n");
scanf("%d\n",&s);
switch(s)
{
case 1:search1();break;
case 2:search2();break;
case 0:output(head);
default:printf("选择错误!!!");
}
}
void search1()
{
struct Ticket *p;
char number[100];
char i;
printf("请输入您要查询车辆的班次号:\n");
scanf("%d",&i);
p=head->next;
do
{
if(strcmp(number,p->number)==0)
{
printf("%s\t%02d:%02d\t%s\t%s\t%f\t%d\t%d\n",p->number,p->ft.hour,p->ft.minute,p->origin_station,p->last_station,p->last_time,p->max_number,p->sold_ticket);
printf("\n\n");
}
}
while(p!=NULL);
printf("输入的班次号有误!");
foutput(head);
output(head);
}
void search2()
{
struct Ticket *p;
char last_station[100];
char c;
printf("请输入您要查询车次的终点站:\n");
scanf("%s",&c);
p=head->next;
do
{
if(strcmp(last_station,p->last_station)==0)
{
printf("%d\t\t%02d:%02d\t\t%s\t\t%s\t\t%f\t\t%d\t\t%d\n\n",p->number,p->ft.hour,p->ft.minute,p->origin_station,p->last_station,p->last_time,p->max_number,p->sold_ticket);
printf("\n\n");
}
}
while(p!=NULL);
foutput(head);
output(head);
}
void buy_ticket()
{
int m,n;
char number[100];
m=0;
load();
printf("请输入您要买的班次号:\n");
scanf("%s",number);
p=head->next;
while(p!=NULL)
{
if(strcmp(number,p->number)==0)
{
printf("请输入您要购买的票数:\n");
scanf("%d",&n);
if(p->sold_ticket=p->max_number)
printf("您需要的车票已售空\n");
else if (n>(p->max_number-p->sold_ticket))
printf("您需要的班次车票不足\n");
else if((syshour>p->ft.hour)||((syshour==p->ft.hour) &&(sysminute>p->ft.minute)))
printf("您需要的班次车辆已发出\n");
else
{
p->sold_ticket=p->sold_ticket+n;
printf("订票成功\n");
}
m++;
p=p->next;
}
else if(m==0)
printf("您输入的班次号有误!\n");
else foutput(head);
output(head);
}
}
void back_ticket()
{
int m,n;
char number[100];
m=0;
load();
printf("请输入您要退票的班次\n");
scanf("%s",number);
p=head->next;
while(p!=NULL)
{
if(strcmp(number,p->number)==0)
{
printf("请输入您要退还的票数\n");
scanf("%d",&n);
if((syshour>p->ft.hour)||((syshour==p->ft.hour) &&(sysminute>p->ft.minute)))
printf("您退还的班次已发出,退票失败!");
else
p->sold_ticket=p->sold_ticket+n;
m++;
}
if(m==0)
printf("对不起,您输入的班次号错误!\n");
else
foutput(head);
output(head);
}
}