#include<stdio.h>
#include<malloc.h>
#define M 80
#define N 20
#define bool int
#define true 1
#define false 0
typedef struct TagSeat
{
int seatNum; //座位号
bool isSell; //是否卖出
}Seat;
typedef struct TagInterval
{
int intervalNum;
Seat seat[4]; //间隔中的4个座位对象
struct TagInterval *next;
}Interval;
Interval* CreateList(); //创建座位链表
void ShowList(Interval *pHead);
bool GetRequest(int *p); //得到每次购票的数量信息
void BuySeat(int i); //买票
void OutputResult(int * SeatNum, int Count); //将售票信息输入到文件中
void main()
{
int i = 0;
Interval *pHead = CreateList(); //得到火车座位的初始链表
//ShowList(pHead); //显示链表
bool bool1 = GetRequest(&i);
if(bool1 == false)
{
printf("无人购票或票已售空\n");
}
else
{
BuySeat(pHead,i);
}
}
Interval* CreateList()
{
int i = 0;
int minNum;
Interval* p = NULL;
Interval* pHead = (Interval*)malloc(sizeof(Interval));
pHead->intervalNum = 1;
minNum = 1;
pHead->seat[0].seatNum = minNum;
pHead->seat[0].isSell = false;
pHead->seat[1].seatNum = minNum + 1;
pHead->seat[1].isSell = false;
pHead->seat[2].seatNum = minNum + 4;
pHead->seat[2].isSell = false;
pHead->seat[3].seatNum = minNum + 5;
pHead->seat[3].isSell = false;
pHead->next = NULL; //头指针的初始化
p = pHead;
for(i = 2 ;i < N + 1; i++)
{
Interval *s = (Interval*)malloc(sizeof(Interval));
s->intervalNum = i;
if(i % 2 != 0) //间隔号为单号
{
minNum = (i - 1) * 4 + 1;
s->seat[0].seatNum = minNum;
s->seat[0].isSell = false;
s->seat[1].seatNum = minNum + 1;
s->seat[1].isSell = false;
s->seat[2].seatNum = minNum + 4;
s->seat[2].isSell = false;
s->seat[3].seatNum = minNum + 5;
s->seat[3].isSell = false;
s->next = NULL;
p->next = s;
p = p->next;
}
else //间隔号为双号
{
minNum = (i * 4) - 5;
s->seat[0].seatNum = minNum;
s->seat[0].isSell = false;
s->seat[1].seatNum = minNum + 1;
s->seat[1].isSell = false;
s->seat[2].seatNum = minNum + 4;
s->seat[2].isSell = false;
s->seat[3].seatNum = minNum + 5;
s->seat[3].isSell = false;
s->next = NULL;
p->next = s;
p = p->next;
}
}
return pHead;
}
void ShowList(Interval *pHead)
{
int i;
Interval *p = NULL;
p = pHead;
while(p->next != NULL)
{
printf("第 %d 个间隔的座位号为:\n",p->intervalNum);
for(i = 0;i < 4; i++)
{
printf("%d",p->seat[i].seatNum);
printf(" ");
}
printf("\n");
p = p->next;
}
printf("第 %d 个间隔的座位号为:\n",p->intervalNum);
for(i = 0;i < 4; i++)
{
printf("%d",p->seat[i].seatNum);
printf(" ");
}
printf("\n");
}
void BuySeat(Interval* pHead,int i)
{
static int seatNum[4] = {0}; //存储座位号的数组
int count = 0; //计数
Interval* p = pHead;
if(i == 1) //买一张票
{
while(p != NULL)
{
for(j = 0; j < 4; j++)
{
if(p->seat[j].isSell == false)
{
p->seat[j].isSell = true; //购票
seatNum[0] = p->seat[j].seatNum;
OutputResult(seatNum,i);
break;
}
}
p = p->next
}
}
if(i == 2)
{
while(p != NULL)
{
for(j = 0;j < 4;j++)
{
if(p->seat[j].isSell == false)
{
if(j % 2 == 0)
{
if(p->seat[j+1].isSell == false)
{
p->seat[j].isSell = true;
p->seat[j+1].isSell = true;
seatNum[0] = p->seat[j].seatNum;
seatNum[1] = p->seat[j+1].seatNum;
OutputResult(seatNum,i);
count++;
break;
}
}
else
{
if(p->seat[j-1].isSell == false)
{
p->seat[j].isSell = true;
p->seat[j+1].isSell = true;
seatNum[0] = p->seat[j].seatNum;
seatNum[1] = p->seat[j-1].seatNum;
OutputResult(seatNum,i);
count++;
break;
}
} //优先售相邻的座
}
}
}
p = pHead;
if(count == 0)//没有相邻座票
{
int count01 = 0;//记录第几次买票
while(p != NULL)
{
for(j = 0;j < 4;j++)
{
if(p->seat[j].isSell == false)
{
p->seat[j].isSell = true; //按顺序售票
count01++;
if(count01 == 2)
{
break;
}
}
}
}
}
}//一次购买两张票
if(i == 3)
{
while(p != NULL)
{
for(j = 0;j < 4;j++)
{
if(p->seat[j].isSell == false)
{
if(j = 0)
{
if(p->seat[j+1].isSell == false && p->seat[j+4] == false)
{
p->seat[j].isSell == true;
p->seat[j+1].isSell == true;
p->seat[j+4].isSell == true;
count++;
break;
}
if(p->seat[j+1].isSell == false && p->seat[j+5].isSell == false)
{
p->seat[j].isSell == true;
p->seat[j+1].isSell == true;
p->seat[j+5].isSell == true;
count++;
break;
}
if(p->seat[j+4].isSell == false && p->seat[j+5].isSell == false)
{
p->seat[j].isSell == true;
p->seat[j+4].isSell == true;
p->seat[j+5].isSell == true;
count++;
break;
}
}
if(j == 1)
{
if(p->seat[j+4].isSell == false && p->seat[j+5].isSell == false)
{
p->seat[j].isSell == true;
p->seat[j+4].isSell == true;
p->seat[j+5].isSell == true;
count++;
break;
}
}
}
}
}
p = pHead;
if(count == 0)//没有同一间隔座票
{
int count01 = 0;//记录第几次买票
while(p != NULL)
{
for(j = 0;j < 4;j++)
{
if(p->seat[j].isSell == false)
{
p->seat[j].isSell = true; //按顺序售票
count01++;
if(count01 == 3)
{
break;
}
}
}
}
}
} //一次购买3张票
if(i == 4)
{
while(p != NULL)
{
if(p->seat[0].isSell == false)
{
if(p->seat[1].isSell == false && p->seat[2].isSell == false && p->seat[3].isSell ==false)
{
p->seat[0].isSell = true;
p->seat[1].isSell = true;
p->seat[2].isSell = true;
p->seat[3].isSell = true;
count++;
break;
}//同一间隔的票
}
p = p->next;
}
p = pHead;
if(count == 0)
{
int count01 = 0;//记录第几次买票
while(p != NULL)
{
for(j = 0;j < 4;j++)
{
if(p->seat[j].isSell == false)
{
p->seat[j].isSell = true; //按顺序售票
count01++;
if(count01 == 4)
{
break;
}
}
}
}
}
}//一次性购买4张票
}
bool GetRequest(int *p)
{
static FILE* fp = fopen("d:\\request.txt","r");
static int nIndex = 0;
static int inputData[M] = {0};
static bool isfirst = true;
int i;
if(isfirst){
for(i = 0; i < M; i++){
if(fscanf(fp,"%d,",&inputData[i]) == EOF) break;
}
isfirst = false;
fclose(fp);
}
if(inputData[nIndex] == 0 || nIndex == M) return false;
*p = inputData[nIndex++];
return true;
}
void OutputResult(int * SeatNum, int Count)
{
FILE* fp = fopen("d:\\result.txt","a");
int i;
fprintf(fp,"====================================\n");
for(i = 0; i < Count; i++){
fprintf(fp,"%d,",SeatNum[i]);
}
fprintf(fp,"\n");
fclose(fp);
}
欢迎使用CSDN-markdown编辑器
最新推荐文章于 2024-09-29 15:51:42 发布