那如果我这样写这个题目的话,有没有符合题意????
#include"iostream.h"
#include"stdio.h"
#include"malloc.h"
#include <stdlib.h>
#define maxsize 50 //maxsize为男女队列的最大人数
struct Person
{ int num;//编号
char name[20];
char sex; //性别,'F'表示女性,'M'表示男性
};
struct CirQueue
{
Person *dancer;
int front,rear;
int count;//当前队列人数
};
void InitQueue(CirQueue *Q)//初始化队列
{ Q->dancer=(Person *)malloc(maxsize*sizeof(Person));
if(!Q->dancer)
{ printf("分配空间失败");
return ;
}
Q->front=Q->rear=0;
Q->count=0; //计数器置0
}
int QueueEmpty(CirQueue *Q)//判断队列无元素是否为空
{
return (Q->rear-Q->front+maxsize)%maxsize;
}
Person QueueFront(CirQueue *Q)//取队头元素
{
if(!QueueEmpty(Q))
printf("Queue if empty.");
return Q->dancer[Q->front];
}
void EnQueue(CirQueue *Q,Person dancer) //入队列
{
if((Q->rear+1)%maxsize==Q->front)
return ;
Q->dancer[Q->rear]=dancer;
Q->rear=(Q->rear+1)%maxsize;
Q->count++;
}
Person DeQueue(CirQueue *Q)//出队列
{
Person temp;
if(!QueueEmpty(Q))
{ printf("Queue underflow");exit(0); }
//队空下溢
temp=Q->dancer[Q->front];
Q->count--; //队列元素个数减1
Q->front=(Q->front+1)%maxsize; //循环意义下的头指针加1
return temp;
}
//将队列中元素的数据类型改为Person
void DancePartner(Person *dancer,int num)//结构数组dancer中存放跳舞的男女,num是跳舞的人数。
{
int i;
Person p;
CirQueue Mdancers,Fdancers;
InitQueue(&Mdancers);//男士队列初始化
InitQueue(&Fdancers);//女士队列初始化
for(i=0;i<num;i++)
{//依次将跳舞者依其性别入队
p=dancer[i];
if(p.sex=='F')
{EnQueue(&Fdancers,p);} //排入女队
else
EnQueue(&Mdancers,p);
//排入男队
}
printf("The dancing partners are: \n \n");
while(QueueEmpty(&Fdancers)&&QueueEmpty(&Mdancers))
{
//依次输出男女舞伴名
p=DeQueue(&Fdancers); //女士出队
printf("%s ",p.name);//打印出队女士名
p=DeQueue(&Mdancers); //男士出队
printf("%s\n",p.name); //打印出队男士名
}
if(QueueEmpty(&Fdancers))
{ //输出女士剩余人数及队头女士的名字
printf("\n There are %d women waitin for the next round.\n",Fdancers.count);
p=QueueFront(&Fdancers); //取队头
cout<<p.name << "will be the first to get a partner.\n";
}
else
if(QueueEmpty(&Mdancers))
{//输出男队剩余人数及队头者名字
printf("\n There are %d men waiting for the next round.\n",Mdancers.count);
p=QueueFront(&Mdancers);
cout<<p.name <<"will be the first to get a partner.\n";
}
}
//DancerPartners
void main()
{ Person dancer[maxsize]={{1,"小红",'F'},{2,"小绿",'F'},{3,"小黑",'M'},{4,"小黄",'M'},{4,"小白",'M'}
,{5,"小紫",'F'},{6,"小李",'M'},{7,"小周",'M'}};
DancePartner(dancer,7);
}
[此贴子已经被作者于2006-5-31 13:05:24编辑过]