假设在周末舞会上,男士和女士进入舞厅时,各自排成一队。跳舞开始时,依次从男队和女队的队头上各出一人配成舞伴。若两队初始人数不相同,则较长的那一队中未配对者等待下一轮舞曲。现要求写一算法模拟上述舞伴配对问题。
功能:
1.先建立一个所有到达现场的舞者(不分男女)的队列,并依次进队
2.将上述队列中的来宾按性别分别安排进入男队或女队
3.分别从男队和女队出队队首元素并配对输出相应舞伴信息
4.将还有客人等待的队列的队首客人输出(名称)
5.设计至少4项相关信息查询或统计功能(查询某人是否在舞会现场、查询他是否正在跳舞、统计到会人数、统计男女人数、统计正在等待的人数等)
要求:
1.用循环队列组织数据,实现程序功能。
2.用链式结构的单循环队列组织数据,实现程序功能。
这些是我已经查资料和翻书可以实现1 2 3 4项,但是能力不够不会写菜单,有人可以帮忙解答一下怎么补菜单嘛
#include<stdio.h>
#define MAXSIZE 100
//定义人员信息
typedef struct
{
char name[20];
char sex;
} datatype;
//定义舞伴循环队列
typedef struct
{
int front,rear;
int count;
datatype dancers[MAXSIZE];
} cirqueue;
//置空队
void *Init_cirqueue(cirqueue *q) //队列的初始化
{
q->front=q->rear=NULL;
q->count=0;
}
//判队空,队空返回1
int Empty_cirqueue(cirqueue *q)
{
if(q->front ==q->rear ) return 1;
else return 0;
}
//进队函数
int In_cirqueue(cirqueue *q,datatype x)
{
if ((q->rear+1)%MAXSIZE==q->front)
{
printf("队满");
return 1;
}
else
{
q->count++;
q->rear=(q->rear+1)%MAXSIZE;
q->dancers[q->rear]=x;
return 1;
}
}
//出队
int Out_cirqueue(cirqueue *q, datatype x)
{
if (q->front == q->rear)
{
printf("队空");
return 1;
}
else
{
q->dancers[q->rear]=x;
q->count--;
q->front=(q->front+1)% MAXSIZE;
return 1;
}
}
//访问对头元素
datatype queuefront(cirqueue *q)
{
return q->dancers[q->front+1];
}
//舞伴输入函数
void dancepartners(datatype dancers[],int num)
{
int i;
datatype p;
cirqueue *maledancer,*femaledancer,*people;
maledancer=(cirqueue*)malloc(sizeof(cirqueue));
femaledancer=(cirqueue*)malloc(sizeof(cirqueue));
people=(cirqueue*)malloc(sizeof(cirqueue));
Init_cirqueue(maledancer);
Init_cirqueue(femaledancer);
Init_cirqueue(people);
//先进入总队
for(i=0;i<num;i++)
{
p=dancers[i];
In_cirqueue(people,dancers[i]);
}
//男女分别入队
for(i=0; i<num; i++)
{
if(dancers[i].sex=='f')//女的进队列femaledancer//
{
In_cirqueue(femaledancer,dancers[i]);
printf("%s进女队\n",dancers[i].name);
}
else//男的进队列maledancer//
{
In_cirqueue(maledancer,dancers[i]);
printf("%s进男队\n",dancers[i].name);
}
}
//男女配对
printf("配对情况如下:\n");
while (!Empty_cirqueue(femaledancer)&&!Empty_cirqueue(maledancer))//当两个队列都不为空时,出队配对//
{
p=queuefront(maledancer);
printf("男:%s",p.name);
Out_cirqueue(maledancer, p);
p=queuefront(femaledancer);
printf("和女:%s配对成功\n",p.name);
Out_cirqueue(femaledancer, p);
}
if(!Empty_cirqueue(femaledancer))//女队不为空,输出女队第一个等待人姓名//
{
printf("女队中还有%d个人在等待!\n",femaledancer->count);
p=queuefront(femaledancer);
printf("女队中第一个等待的是:%s\n",p.name);
}
else if(!Empty_cirqueue(maledancer))//男队不为空,输出男队第一个等待人姓名//
{
printf("男队中还有%d个人在等待!\n",maledancer->count);
p=queuefront(maledancer);
printf("男队中第一个等待的是:%s\n",p.name);
}
else printf("^-^没有人剩余^-^!\n");
}
void main()
{
datatype p,dancers[MAXSIZE];
int i,num;
printf("输入男女dancer的总人数:\n");
scanf("%d",&num);
printf("*******************************************************\n");
printf("请输入名字和性别(名字 性别:m为男,w为女)\n");
printf("*******************************************************\n");
for(i=0; i<num; i++)
{
printf("请输入名字:\n");
scanf("%s",&dancers[i].name);
getchar();
printf("请输入性别(性别:m为男,f为女):\n");
scanf("%c",&dancers[i].sex);
if(dancers[i].sex!='f'&&dancers[i].sex!='m')//判断输入信息是否合法//
printf("第%d个人的信息有误,请重新输入",i--);
}
dancepartners(&dancers[0],num);
getchar();
}
switch语句建立菜单
printf("按 1 查询某人是否在舞会现场\n");
printf("按 2 查询某人是否正在跳舞\n");
printf("按 3 统计到会人数\n");
printf("按 4 统计男女人数\n");
scanf("%d",&n);
switch(n):
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
default:
break;