#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<dos.h>
#include<windows.h>
#define HEADER1 "--------------------火车订票系统--------------------------\n"
#define HEADER2 "| 编号 |出发城市|目的城市|发车时间|到达时间|价格|票数|\n"
#define HEADER3 "|----------|--------|--------|--------|--------|----|----|\n"
#define FORMAT "|%-10s|%-8s|%-8s|%-8s|%-8s|%4d|%4d|\n"
#define DATA p->data.num,p->data.startcity,p->data.reachcity,p->data.takeofftime,p->data.receivetime,p->data.price,p->data.ticketnum
//火车票
struct train{
char num[10]; //列车号
char startcity[10]; //出发城市
char reachcity[10]; //目的城市
char takeofftime[10]; //发车时间
char receivetime[10]; //到达时间
int price; //票价
int ticketnum; //票数
};
//订票人
struct man{
char num[18]; //身份证号
char name[10]; //姓名
int bookNum; //订的票数
};
//定义火车票信息链表的节点结构
typedef struct node{
struct train data;
struct node *next;
}Node, *Link;
//定义订票人链表的节点结构
typedef struct Man{
struct man data;
struct Man *next;
}book, *bookLink;
int saveflag = 1;
void menu();
void Traininfo(Link linkhead);
void searchtrain(Link l);
void printheader();
void printdata(Node *q);
void Bookticket(Link l, bookLink k);
void Modify(Link l);
void showtrain(Link l);
void SaveTrainInfo(Link l);
void SaveBookInfo(bookLink k);
int main()
{
FILE *fp1, *fp2;
Node *p, *r;
char ch1, ch2;
Link l;
bookLink k;
book *t, *h;
int sel;
l = (Node*)malloc(sizeof(Node));
l->next = NULL;
r = l;
k = (book*)malloc(sizeof(book));
k->next = NULL;
h = k;
fp1 = fopen("D:\\train.txt", "ab+");
if( (fp1 == NULL) ){
printf("打开文件失败!\n");
return 0;
}
while(!feof(fp1)){
p = (Node*)malloc(sizeof(Node));
if(fread(p, sizeof(Node), 1, fp1) == 1){
p->next = NULL;
r->next = p;
r = p;
}
}
fclose(fp1);
fp2 = fopen("D:\\man.txt", "ab+");
if( (fp2 == NULL) ){
printf("打开文件失败!\n");
return 0;
}
while(!feof(fp2)){
t = (book*)malloc(sizeof(book));
if(fread(t, sizeof(book), 1, fp2) == 1){
t->next = NULL;
h->next = t;
h = t;
}
}
fclose(fp2);
while(1){
system("CLS");
menu();
printf("\n\t\t\t\t请选择(0-6):");
scanf("%d", &sel);
system("CLS");
if(sel == 0){
if(saveflag == 1){ //当退出时判断信息是否保存
getchar();
printf("\n文件已经更改,是否保存(y/n)?\n");
scanf("%c", &ch1);
if(ch1 == 'y' || ch1 == 'Y'){
SaveBookInfo(k);
SaveTrainInfo(l);
}
}
printf("\n欢迎再次使用!\n");
break;
}
switch(sel){
case 1:
Traininfo(l);
break;
case 2:
searchtrain(l);
break;
case 3:
Bookticket(l, k);
break;
case 4:
Modify(l);
break;
case 5:
showtrain(l);
break;
case 6:
SaveTrainInfo(l);
SaveBookInfo(k);
break;
case 0:
return 0;
}
printf("\n按任意键继续......");
getch();
}
return 0;
}
void menu(){
puts("\n\n");
puts("\t\t\t\t|-------------------------------------------------------|");
puts("\t\t\t\t| 火车订票系统 |");
puts("\t\t\t\t|-------------------------------------------------------|");
puts("\t\t\t\t| 0.退出系统 |");
puts("\t\t\t\t| 1.添加火车票信息 |");
puts("\t\t\t\t| 2.查询火车票信息 |");
puts("\t\t\t\t| 3.订票模块 |");
puts("\t\t\t\t| 4.修改火车票信息 |");
puts("\t\t\t\t| 5.显示火车票信息 |");
puts("\t\t\t\t| 6.保存火车票信息和订票信息到磁盘文件 |");
puts("\t\t\t\t|-------------------------------------------------------|");
}
void Traininfo(Link linkhead){
struct node *p, *r, *s;
char num[10];
r = linkhead;
s = linkhead->next;
while(r->next != NULL){
r = r->next;
}
while(1){
printf("请输入火车编号(按0结束):");
scanf("%s", num);
if(strcmp(num, "0") == 0){
break;
}
while(s){
if(strcmp(s->data.num, num) == 0){
printf("火车'%s'已存在!\n", num);
return;
}
s = s->next;
}
p = (struct node*)malloc(sizeof(struct node));
strcpy(p->data.num, num);
printf("输入出发城市:");
scanf("%s", p->data.startcity);
printf("输入到站城市:");
scanf("%s", p->data.reachcity);
printf("输入出发时间:");
scanf("%s", p->data.takeofftime);
printf("输入到站时间:");
scanf("%s", p->data.receivetime);
printf("输入火车票价:");
scanf("%d", &p->data.price);
printf("输入预定票数:");
scanf("%d", &p->data.ticketnum);
p->next = NULL;
r->next = p;
r = p;
saveflag = 1;
}
}
void searchtrain(Link l){
Node *s[10], *r;
int sel, k, i = 0;
char str1[5], str2[10];
if(!l->next){
printf("没有车票信息!\n");
return;
}
printf("请选择查询方法:\n1:根据火车编号查询;\n2:根据城市查询:\n");
scanf("%d", &sel); //输入选择的序号
if(sel == 1){ //若输入的序号等于1,则根据车次查询
printf("请输入火车编号:");
scanf("%s", str1); //输入火车车次
r = l->next;
while(r != NULL){
if(strcmp(r->data.num, str1) == 0){ //检索是否有与输入的车次相匹配的火车
s[i] = r;
i++;
break;
}else{
r = r->next; //没有查到火车车次则指针r后移一位
}
}
}else if(sel == 2){ //选择2则根据城市查询
printf("请输入你想去的城市:");
scanf("%s", str2); //输入查询的城市
r = l->next;
while(r != NULL){
if(strcmp(r->data.reachcity, str2) == 0){
s[i] = r;
i++;
r = r->next;
}else{
r = r->next;
}
}
}
if(i == 0){
printf("抱歉,没有查询到!\n");
}else{
printheader(); //输出表头
for(k = 0; k < i; k++){
printdata(s[k]); //输出火车信息
}
}
}
void printheader(){
printf("\n");
printf(HEADER1);
printf(HEADER2);
printf(HEADER3);
}
void printdata(Node *q){
Node *p;
p = q;
printf(FORMAT, DATA);
}
void Bookticket(Link l, bookLink k){
Node *r[10], *p;
char ch[2], tnum[10], str[10], str1[10], str2[10];
book *q, *h;
int i = 0, t = 0, flag = 0, dnum;
q = k;
while(q->next != NULL){
q = q->next;
}
printf("请输入你要去的城市:");
scanf("%s", str); //输入要到达的城市
p = l->next;
while(p != NULL){
if(strcmp(p->data.reachcity, str) == 0){