标题:队列中的舞伴问题,不知道哪里出现了错误,求指点迷津
只看楼主
一下无语了
Rank: 2
等 级:论坛游民
帖 子:4
专家分:20
注 册:2014-2-5
结帖率:100%
 问题点数:0 回复次数:0 
队列中的舞伴问题,不知道哪里出现了错误,求指点迷津
#include <stdio.h>
#include<stdlib.h>
#define queuesize 10
typedef struct{
    char name[20];
    char sex;
}Person;
typedef Person Datatype;

typedef struct
{
Datatype data[10];
int front,rear,count;
}CirQueue;


CirQueue *InitQueue()
{
CirQueue *q;
q=malloc(sizeof(CirQueue));
q->front=q->rear=0;
q->count=0;
return q;
}


int EnQueue(CirQueue *q,Datatype e)
{
if((q->rear+1)%queuesize==q->front)
{printf("队满");
return -1;
}
else
{   q->count++;
    if(q->front==q->rear)
    q->data[q->front]=q->data[q->rear]=e;
    else{
         q->rear=(q->rear+1)%queuesize;
         q->data[q->rear]=e;
    }
return 1;
}
}


int QueueEmpty(CirQueue *q)
{if(q->front==q->rear) return 1;
else return 0;
}


Datatype DeQueue(CirQueue *q)
{Datatype e;
e=q->data[q->front];
q->front=(q->front+1)%queuesize;
q->count--;
return e;
}

Datatype QueueFront(CirQueue *q)
{if(QueueEmpty(q)!=0)
printf("队空");
return q->data[q->front];
}
 

void dancePartners(Person dancer[],int num)
{
    int i;
    Person p;
    CirQueue *Mdancers;
    CirQueue *Fdancers;
    Mdancers=InitQueue();
    Fdancers=InitQueue();
    for(i=0;i<num;i++){
        p=dancer[i];
        if(p.sex=='m')
        EnQueue(Mdancers,p);
        if(p.sex=='f')
        EnQueue(Fdancers,p);
    }
    printf("The dancing partners are:\n");
    while(QueueEmpty(Fdancers)==0&&QueueEmpty(Mdancers)==0)
    {
        p=DeQueue(Fdancers);
        printf("%s",p.name);
        p=DeQueue(Mdancers);
        printf("%s",p.name);
    }
    if(!QueueEmpty(Fdancers))
    {
        printf("\n There are %d women waiting for the next round.\n",Fdancers->count);
        p=QueueFront(Fdancers);
        printf("%s will be the first to get a partener.\n",p.name);
    }
    else if(!QueueEmpty(Mdancers))
    {
        printf("\n There are %d men waiting for the next round.\n",Mdancers->count);
        p=QueueFront(Mdancers);
        printf("%s will be the first to get a partener.\n",p.name);
    }
}

void main()
{
    Person dancer[50];
    int i,num;;
    printf("男女总人数为\n");
    scanf("%d",&num);
    printf("输入舞者的姓名和性别\n");
    for(i=0;i<num;i++)
    scanf("%s %c",dancer[i].name,&dancer[i].sex);
    dancePartners(dancer,num);
}
搜索更多相关主题的帖子: include return count 
2014-03-27 12:43



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-429241-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.114089 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved