#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ticket.h"
int main()
{
while (1)
{
system("cls");
show_menu();
}
return 0;
}
void input(int n)
{
struct ticket t;
fflush(stdin);
printf("请输入起点站: ");
gets(t.start);
printf("请输入终点站: ");
gets(t.end);
printf("请输入起飞日期: ");
gets(t.date);
printf("请输入张数: ");
scanf("%d", &t.num);
if (n == 1)
{
printf("请输入价格: ");
scanf("%d", &t.price);
}
update(&t, n);
}
void manage()
{
int n;
printf("\t*********************************\n");
printf("\t*\t添加机票--------1\t*\n");
printf("\t*\t删除机票--------2\t*\n");
printf("\t*\t返回上级--------0\t*\n");
printf("\t*********************************\n");
printf("\t请选择: ");
scanf("%d", &n);
if (n != 1 && n != 2)
return;
input(n);
}
void sell_tickets()
{
FILE *fp = fopen("tickets.dat", "r");
int n;
fclose(fp);
if (fp == NULL)
{
puts("暂无机票");
fflush(stdin);
_getch();
return;
}
printf("\t*********************************\n");
printf("\t*\t查看机票--------1\t*\n");
printf("\t*\t购买机票--------2\t*\n");
printf("\t*\t退订机票--------3\t*\n");
printf("\t*\t返回上级--------0\t*\n");
printf("\t*********************************\n");
printf("\t请选择: ");
scanf("%d", &n);
switch (n)
{
case 1:
view();
break;
case 2:
input(4);
break;
case 3:
input(3);
break;
}
}
void show_menu()
{
int n;
printf("\t*********************************\n");
printf("\t*\t欢迎使用飞机订票系统\t*\n");
printf("\t*********************************\n");
printf("\t*\t售票服务--------1\t*\n");
printf("\t*\t系统管理--------2\t*\n");
printf("\t*\t退出系统--------3\t*\n");
printf("\t*********************************\n");
printf("\t请选择: ");
scanf("%d", &n);
switch (n)
{
case 1:
sell_tickets();
break;
case 2:
manage();
break;
case 3:
exit(0);
}
}
void show_result(int n, int num, int price)
{
int money = abs(num) * price;
int real;
if (n == 3)
printf("退票金额: %d元\n", money);
else if (n == 4)
{
printf("应支付金额: %d元\n", money);
printf("实付: ");
scanf("%d", &real);
printf("找回: %d元\n", real - money);
}
}
void update(struct ticket *t, int n)
{
int num;
struct ticket r;
FILE *fp = fopen("tickets.dat", "r+");
if (fp != NULL)
{
// 文件存在
// 修改机票数量
while (fread(&r, sizeof(r), 1, fp))
{
if (strcmp(r.start, t->start) == 0 && strcmp(r.end, t->end) == 0 && strcmp(r.date, t->date) == 0)
{
fseek(fp, -2 * (int)sizeof(int), SEEK_CUR);
// 计算剩余票数
num = r.num;
if (n == 1 || n == 3)
num += t->num;
else
num -= t->num;
if (num < 0)
puts("操作失败, 余票不够");
else
{
fwrite(&num, sizeof(int), 1, fp); // 更新剩余票数
if (n == 1)
fwrite(&t->price, sizeof(int), 1, fp); // 更新机票价格
fclose(fp);
puts("操作成功");
show_result(n, t->num, r.price);
}
_getch();
return;
}
}
if (n == 2 || n == 4)
{
puts("操作失败, 无该种机票!");
_getch();
return;
}
// 添加机票 / 退订已卖光的机票
fwrite(t, sizeof(struct ticket), 1, fp);
fclose(fp);
}
else
{
// 文件不存在
if (n == 2 || n == 4)
{
// 没票时无法删除机票或购票
puts("操作失败, 无该种机票!");
return;
}
// 添加机票 / 退订已卖光的机票
fp = fopen("tickets.dat", "w");
fwrite(t, sizeof(struct ticket), 1, fp);
fclose(fp);
show_result(n, t->num, t->price);
}
puts("操作成功");
_getch();
}
void view()
{
FILE *fp = fopen("tickets.dat", "r");
struct ticket t;
printf("起点站\t终点站\t日期\t\t票数\t价格\n");
while (fread(&t, sizeof(struct ticket), 1, fp))
{
if (t.num > 0)
printf("%-8s%-8s%-16s%-8d%-8d\n", t.start, t.end, t.date, t.num, t.price);
}
_getch();
fclose(fp);
}