初学者,救救孩子吧
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define OK 1
#define ERROR 0
typedef struct
{
char name[20];
char sex;
}Person;
typedef struct
{
Person m[20];
Person *base;
int front;
int rear;
}SqQueue;
SqQueue *mdancers(SqQueue *mq)这一句可以改成 int InitQueue(SqQueue *mq)这样吗改完后就出现了上面的错误
{
mq = (SqQueue *)malloc(sizeof(SqQueue));
if(!mq -> base) exit(0);
mq -> front = -1;
mq -> rear = -1;
return mq;
}
Inqueue(SqQueue *mq, Person p)
{
if((mq -> rear + 1) % 20 == mq -> front)
{
printf("队满!");
}
else
{
mq -> rear++;
mq -> m[mq->rear] = p;
}
}
int Equeue(SqQueue *mq)
{
if (mq -> front == mq -> rear)
return OK;
else return ERROR;
}
dancers(Person p[20], int num, SqQueue *mq, SqQueue *fq)
{
int i;
Person q;
printf("请输入参与者的姓名,性别\n");
getchar();
for(i = 0; i < num; i++)
{
printf("请输入第%d个舞者的名字:\n", i + 1);
scanf("%s", &p[i].name);
printf("请输入第%d个人的性别(f/m):\n", i + 1);
scanf("%s", &p[i].sex);
while(p[i].sex != 'f' && p[i].sex != 'm')
{
printf("输入错误,请重新输入第%d个人的性别(f/m):\n", i + 1);
scanf("%s", &p[i].sex);
break;
}
}
for(i = 0; i < num; i++)
{
q = p[i];
if (q.sex == 'm') Inqueue(mq, q);
if(q.sex == 'f') Inqueue(fq, q);
}
printf("匹配结果:\n");
while(!Equeue(mq) && !Equeue(fq))
{
mq -> front++;
q = mq -> m[mq -> front];
printf("%s\t", q.name );
fq -> front++;
q = fq -> m[fq -> front];
printf("%s\n", q.name );
}
if(!Equeue(mq))
{
mq -> front++;
q = mq -> m[mq -> front];
printf("下一个等待匹配的人是:%s \n", q.name);
}
if(!Equeue(fq))
{
fq -> front++;
q = fq -> m[fq -> front];
printf("下一个等待匹配的人是:%s \n", q.name);
}
}
int main()
{
int num;
Person p[20];
SqQueue *mq;
SqQueue *fq;
mq = mdancers(mq);
fq = mdancers(fq);
printf("请输入跳舞的男士与女士的总人数:");
scanf("%d", &num);
if(num > 20)
exit(0);
while(num <= 0)//输入人数为负提示重新输入
{
printf("输入错误,请重新输入跳舞的人数:");
scanf("%d", &num);
}
dancers(p, num, mq, fq);
}