(1)和(2)
俺不会用文件录信息😭
代码如下:如有帮助,请采纳一下,谢谢。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAXBC 100
struct Time
{
int hour;
int mint;
};
struct Ticketsinfo
{
int id; //班次
Time tt; //时间
char start[20]; //起点站
char dst[20]; //终点站
float time; //行车时间
int maxnmb; //额定载量
int outnmb; //已订票人数
};
//获取当前时间
Time GetCurrentTime()
{
Time tt;
time_t timep;
struct tm *p;
time(&timep);
p = gmtime(&timep);
tt.hour = p->tm_hour;
tt.mint = p->tm_min;
return tt;
}
//判断是否已经发车,1表示已发车
int isOut(Time tt)
{
Time cur = GetCurrentTime();
if (cur.hour > tt.hour)
{
return 1;
}else if (cur.hour < tt.hour)
{
return 0;
}else
{
if(cur.mint > tt.mint)
return 1;
else
return 0;
}
}
//显示所有班次
void ShowAllinfo(Ticketsinfo all[],int size)
{
int i;
for (i = 0; i < size;i++)
{
if(isOut(all[i].tt))
printf("%d\t%d:%d\t%s\t%s\t%.1f\t%d\t%d\t此班已发出\n",all[i].id,all[i].tt.hour,all[i].tt.mint,all[i].start,all[i].dst,all[i].time,all[i].maxnmb,all[i].outnmb);
else
printf("%d\t%d:%d\t%s\t%s\t%.1f\t%d\t%d\n",all[i].id,all[i].tt.hour,all[i].tt.mint,all[i].start,all[i].dst,all[i].time,all[i].maxnmb,all[i].outnmb);
}
}
//查询路线
void CheckLine(Ticketsinfo all[],int size)
{
int i;
char buf[40] = {0};
int id;
printf("请输入班次或者终点站:");
scanf("%s",buf);
id = atoi(buf);
for (i = 0; i < size; i++)
{
if (all[i].id == id || strcmp(buf,all[i].dst) == 0)
{
printf("%d\t%d:%d\t%s\t%s\t%.1f\t%d\t%d\n",all[i].id,all[i].tt.hour,all[i].tt.mint,all[i].start,all[i].dst,all[i].time,all[i].maxnmb,all[i].outnmb);
break;
}
}
}
//售票
void SaleTickets(Ticketsinfo all[],int size)
{
char buf[20]={0};
int i,id;
printf("请输入班次或者目的地:");
scanf("%s",buf);
id = atoi(buf);
for (i = 0; i< size; i++)
{
if (all[i].id == id || strcmp(buf,all[i].dst) == 0) //查找班次
{
//判断是否还有票
if (all[i].outnmb < all[i].maxnmb)
{
printf("当前剩余票数:%d,出票成功\n",all[i].maxnmb - all[i].outnmb);
all[i].outnmb += 1;
break;
}
}
}
}
//退票
void BackTickets(Ticketsinfo all[],int size)
{
char buf[20]={0};
int i,id;
printf("请输入需要退票的班次:");
scanf("%d",buf);
id = atoi(buf);
for (i = 0; i< size; i++)
{
if (all[i].id == id ) //查找班次
{
if (isOut(all[i].tt))
{
printf("该班次已经发车,无法退票\n");
break;
}else
{
all[i].outnmb -= 1;
printf("退票成功\n");
break;
}
}
}
}
int main()
{
Ticketsinfo all[MAXBC];
int nmb = 0; //文件中的班次数
int opt,i = 0;
int loops = 1;
FILE* fp;
if(!(fp = fopen("banci.txt","r"))) //假设信息在banci.txt中
{
printf("文件打开失败\n");
return 0;
}
while(!feof(fp))
{
fscanf(fp,"%d\t%d:%d\t%s\t%s\t%f\t%d\t%d",&all[nmb].id,&all[nmb].tt.hour,&all[nmb].tt.mint,all[nmb].start,all[nmb].dst,&all[nmb].time,&all[nmb].maxnmb,&all[nmb].outnmb);
nmb++;
}
fclose(fp);
printf("--------------车票管理系统------------\n");
printf("| 1.浏览全部班次 |\n");
printf("| 2.查询路线 |\n");
printf("| 3.售票 |\n");
printf("| 4.退票 |\n");
printf("| 9.退出系统 |\n");
printf("--------------------------------------\n");
while(loops)
{
scanf("%d",&opt);
switch(opt)
{
case 1:
ShowAllinfo(all,nmb);
break;
case 2:
CheckLine(all,nmb);
break;
case 3:
SaleTickets(all,nmb);
break;
case 4:
BackTickets(all,nmb);
break;
case 9:
loops = 0;
break;
default:
break;
}
}
return 0;
}