#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);
}