找舞伴

来源:互联网 发布:首批网络空间安全学院 编辑:程序博客网 时间:2024/04/29 05:38

Main.cpp

#include<stdio.h>

#include"cirqueue.h"
#include"pairing.h"


int main(void)
{
int i,num;
scanf("%d",&num);
getchar();
cirqueue q;
for(i=0;i<num;i++)
{
gets((q.person[i]).name);
//getchar();
(q.person[i]).sex=getchar();
getchar();
}
dancepartners(q.person,num);

return 0;

}


cirqueue.h

#ifndef cirqueue_h
#define cirqueue_h
#define queuesize 100


typedef struct
{
char name[20];
char sex;
}dancer;


typedef struct
{
dancer person[queuesize];
int front;
int rear;
int count;
}cirqueue;


void init(cirqueue *q);
int is_empty(cirqueue *q);
int is_full(cirqueue *q);
void enqueue(cirqueue *q,dancer p);
dancer dequeue(cirqueue *q);


#endif


cirqueue.cpp

#include<stdio.h>
#include<stdlib.h>
#include"cirqueue.h"


void init(cirqueue *q)
{
q->front=q->rear=0;
q->count=0;
}


int is_empty(cirqueue *q)
{
return q->count==0;
}


int is_full(cirqueue *q)
{
return q->count==queuesize;
}


void enqueue(cirqueue *q,dancer p)
{
if(is_full(q))
{
printf("overflow!\n");
exit(EXIT_FAILURE);
}
q->count++;
q->person[q->rear]=p;
q->rear=(q->rear+1)%queuesize;
}


dancer dequeue(cirqueue *q)
{
dancer x;
if(is_empty(q))
{
printf("underflow!\n");
exit(EXIT_FAILURE);
}
q->count--;
x=q->person[q->front];
q->front=(q->front+1)%queuesize;
return x;
}


pairing.h

#ifndef pairing_h
#define pairing_h
#include"cirqueue.h"


void dancepartners(dancer person[],int i);


#endif


pairing.cpp

#include<stdio.h>
#include"cirqueue.h"




void dancepartners(dancer person[],int num)
{
int i;
dancer p;
cirqueue MQueue,FQueue;
init(&MQueue);
init(&FQueue);
for(i=0;i<num;i++)
{
if(person[i].sex=='F')
enqueue(&FQueue,person[i]);
else
enqueue(&MQueue,person[i]);
}
printf("the dancers are:\n");
while(!is_empty(&FQueue)&&!is_empty(&MQueue))
{
p=dequeue(&FQueue);
printf(" %s ",p.name);
p=dequeue(&MQueue);
printf(" %s \n",p.name);
}
if(!is_empty(&FQueue))
{
printf(" there are %d women waiting for the nxt round!\n",FQueue.count);
p=dequeue(&FQueue);
printf("%s will be the first in the next round!\n",p.name);
}
if(!is_empty(&MQueue))
{
printf(" there are %d men waiting for the nxt round!\n",MQueue.count);
p=dequeue(&MQueue);
printf("%s will be the first in the next round!\n",p.name);
}
}