#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define MAXSIZE 100
typedef struct
{
char name[20];
char sex;
}Person;
typedef struct
{
Person* base;//Person类型的数组存储男女信息
int front;
int rear;
}Queue;
void QueueInit(Queue* ps)
{
ps->base = (Person*)malloc(sizeof(Person) * MAXSIZE);
if (ps->base == NULL)
{
perror("error");
exit(1);
}
ps->front = ps->rear = 0;
}
void EnQueue(Queue* ps, Person p)
{
if ((ps->rear+1)%MAXSIZE == ps->front)
{
printf("队满,无法插入\n");
exit(1);
}
ps->base[ps->rear] = p;
ps->rear = (ps->rear + 1) % MAXSIZE;
}
bool QueueEmpty(Queue* ps)
{
return ps->front == ps->rear;
}
void QueuePop(Queue* ps,Person *e)
{
if (ps->front == ps->rear)
{
printf("队空,无内容可以出队\n");
exit(1);
}
*e = ps->base[ps->front];
ps->front = (ps->front + 1) % MAXSIZE;
}
Person Gethead(Queue* ps)
{
if (ps->front != ps->rear)
{
return ps->base[ps->front];
}
}
void DancePartner(Person dancer[], int num)
{
Queue Fsex, Msex;//定义两个Queue类型
QueueInit(&Fsex);
QueueInit(&Msex);
int i;
Person p;
for (i = 0; i < num; i++)
{
p = dancer[i];
if (p.sex == 'F')
{
//F插入女生队列
EnQueue(&Fsex, p);
}
else
{
EnQueue(&Msex, p);
}
}
while (!QueueEmpty(&Fsex) && !QueueEmpty(&Msex))
{
//判断都不为空的话,就男女各自出一人
QueuePop(&Fsex, &p);
printf("%s ", p.name);
QueuePop(&Msex, &p);
printf("%s\n", p.name);
}
if (!QueueEmpty(&Fsex))
{
//万一是奇数的跳舞者,那么就输出这个名字
p = Gethead(&Fsex);
printf("多出的跳舞这名字为%s", p.name);
}
else if (!QueueEmpty(&Msex))
{
p = Gethead(&Msex);
printf("多出的跳舞这名字为%s", p.name);
}
}
int main()
{
int i, num;
Person dancer[MAXSIZE];
printf("请输入跳舞人数\n");
scanf("%d", &num);
printf("请输入跳舞人的名字和性别,(女性别用F,男性别用M标识)\n");
for (i = 0; i < num; i++)
{
printf("请输入第%d个跳舞者的信息\n", i + 1);
scanf("%s %c", &dancer[i].name, &dancer[i].sex);
}
DancePartner(dancer, num);
return 0;
}