#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#define MaxNumber 100
typedef struct{
char name[20];
char sex[4]; /*性别,‘F’表示女性,‘M’表示男性*/
}Person;
/* 将队列中元素的数据类型改为Person*/
typedef struct
{ Person data[MaxNumber];
int front;
int rear;
}CirQueue;
CirQueue *InitQueue()
{ CirQueue *q;
q=(CirQueue*)malloc(sizeof(CirQueue));
q-> front=q-> rear=0;
return q;
}
int QueueEmpty(CirQueue *q)
{ return(q-> front==q-> rear);
}
int EnQueue(CirQueue *q,Person x)
{ if((q-> rear+1)%MaxNumber==q-> front)
{ printf( "/nOverflow !/n ");
return 0;
}
q-> data[q-> rear]=x;
q-> rear=(q-> rear+1)%MaxNumber;
return 1;
}
Person DeQueue(CirQueue *q)
{ Person x;
if(q-> front==q-> rear)
{ printf( "/nThe queue is empty ! Can 't delete !/n ");
exit(0);
}
x=q-> data[q-> front];
q-> front=(q-> front+1)%MaxNumber;
return x;
}
void DancePartner(Person dancer[],int num)
{ /*结构数组dancer中存放跳舞的男女,num是跳舞的人数*/
CirQueue *Mdancers,*Fdancers;
int i, count=0;
Person p;
Mdancers=InitQueue(); /*男士队列初始化*/
Fdancers=InitQueue(); /*女士队列初始化*/
for(i=0;i <num;i++) /*依次将跳舞者依其性别入队*/
{
p=dancer[i];
if(strcmp(p.sex, "f")==0)
EnQueue(Fdancers,p); /*排入女队*/
else EnQueue(Mdancers,p); /*排入男队*/
}
printf( "/nThe dancing partners are:/n ");
while(!QueueEmpty(Fdancers)&&!QueueEmpty(Mdancers))
{ /*依次输入男女舞伴名*/
count++;
p=DeQueue(Fdancers); /*女士出队*/
printf( "%s/t ",p.name); /*打印出队女士名*/
p=DeQueue(Mdancers); /*男士出队*/
printf( "%s/n ",p.name); /*打印出队男士名*/
}
if(!QueueEmpty(Fdancers)) /*输出女士剩余人数及队头女士的名字*/
{
printf( "/n There are %d women waiting for the next round./n ",num-2*count);
p=DeQueue(Fdancers);
printf( "%s will be the first to get a partner. /n ",p.name);
printf( "/n ");
}
if(!QueueEmpty(Mdancers)) /*输出男队剩余人数及队头者名字*/
{
printf( "/n There are %d men waiting for the next round./n ",num-2*count);
p=DeQueue(Mdancers);
printf( "%s will be the first to get a partner. /n ",p.name);
printf( "/n ");
}
}
int GetDancersInfo(Person dancer[])
{ int count=0;
Person p;
while (1)
{ printf( "Input the sex:/n ");
scanf( "%s",p.sex);
if(strcmp(p.sex,"0")==0) break;
printf( "Input the name:/n ");
scanf( "%s",p.name);
dancer[count]=p;
count++;
}
return count;
}
void main()
{ int DancersNum;
Person Dancers[MaxNumber];
DancersNum=GetDancersInfo(Dancers);
if(DancersNum!=0) DancePartner(Dancers,DancersNum);
getchar();
}