我把程序从.cpp改成.c后,不知道哪里出错了,求高手指点!
#include<stdio.h>#include <stdlib.h>
#include <conio.h>
#define OK 1
#define OVERFLOW -1
#define ERROR 0
typedef int Status;
typedef int QElemType;
int boynum = 0,girlnum = 0,song_num=0; //全局变量
//-------循环队列-队列的顺序存储结构----
typedef struct /* 定义结点元素结构 */
{
QElemType *data;
int front;
int rear;
}SqQueue;
//---循环队列的基本算法--------
Status InitQueue (SqQueue Q,int num) // 构造一个空循环队列Q
{
Q.data = (QElemType *)malloc((num+1)*sizeof(QElemType));
if(!Q.data)exit(OVERFLOW);
Q.front = Q.rear = 0;
return OK;
}// InitQueue
Status EnQueue (SqQueue Q,QElemType e,int num)
// 入队列,插入元素e为Q的新的队尾元素
{
Q.data = (QElemType *)malloc((num+1)*sizeof(QElemType));
if((Q.rear+1)%(num+1) == Q.front) return ERROR; //队列满
Q.data[Q.rear] = e;
Q.rear = (Q.rear+1)%(num+1);
return OK;
}// EnQueue
Status DeQueue(SqQueue Q,QElemType e,int num)
// 若队列不空,则删除Q的对头元素,用e返回其值,并返回OK/,否则返回ERROR
{ if(Q.front==Q.rear) return ERROR;
e=Q.data[Q.front];
Q.front=(Q.front+1)%(num+1);
return OK;
}// DeQueue
Status judge() // 判断输入每位是否满足('0'-'9')并且转换为数字
{
int i,j=0,k=0,s=0,h=0;
char a[60];
gets(a);
for(i=0;a[i]!='\0';i++) //记录最后一个非空格字符的位置
if(a[i]!=' ') h=i;
for(i=0;i<=h;i++) //去除前面的空格及判断中间是否有空格
{
j++;
if(a[i]==' ')
{
k++;
if(j!=k) return ERROR;
continue;
}
if(a[k]=='0') return ERROR; //第一个非空格字符是否为0
if(a[i]<'0'||a[i]>'9') return ERROR; //字符是否在0-9之间
else
s=s*10+a[i]-48; //将字符串转换成相应的数值
if(s>32768) return ERROR; //数字在1-32768之间
}
return s;
}
Status check() //正确输入数据
{
int p=0;
while(!p)
{
p=judge();
if(!p) printf("\t数据输入错误!请重新输入:");
}
return p;
}// check
Status bianhao(SqQueue boys,SqQueue girls)
// 队列初始化及对男女生进行编号
{
int i;
InitQueue(boys,boynum);
for(i=1; i<=boynum; i++)
EnQueue(boys,i,boynum);
InitQueue(girls,girlnum);
for(i=1; i<=girlnum; i++)
EnQueue(girls,i,girlnum);
return OK;
}// peidui
Status input(SqQueue boys,SqQueue girls) // 输入男女生人数及曲目数
{
printf("\t请输入男生人数:");
boynum = check();
printf("\t请输入女生人数:");
girlnum = check();
printf("\t请输入曲目数:");
song_num = check();
printf("\n\n\n");
return OK;
}// input
Status output(SqQueue boys,SqQueue girls)
{
int m=0,w=0;
DeQueue(boys,m,boynum);
DeQueue(girls,w,girlnum);
printf(" %3d<->%-3d ",m,w);
EnQueue(boys,m,boynum);
EnQueue(girls,w,girlnum);
return OK;
}
Status All_peidui(SqQueue boys,SqQueue girls) // 输出每曲配对情况
{
int min,k,p,q;
bianhao(boys,girls);
if (boynum>girlnum) min=girlnum;
else min=boynum;
printf("\n\n");
for (p=1;p<=song_num; ++p)
{
printf("\t---------------第%d支舞曲配对情况为---------------\n",p);
if (min<5) k=min;
else k=5;
for (q=0;q<k;q++)
printf(" 男<->女 ");
printf("\n");
for( q=1;q<=min;q++)
{
output(boys,girls);
if (q%5==0) printf("\n");
}
printf("\n\n");
}
return OK;
}// All_peidui
Status K_peidui(SqQueue boys,SqQueue girls)
// 对第K曲进行配对,并输出配对情况
{
int min,max,t,i,p1,p2,k,a;
bianhao(boys,girls);
do
{
printf("\t请输入您想知道的配对情况的曲目编号:");
k = check();
if(k>song_num) printf("\t所选曲目超出曲目总数!\n");
} while (k>song_num);
min = boynum>girlnum ? girlnum:boynum;
max = boynum>girlnum ? boynum:girlnum;
printf("\n\n\n\t\t\t第%d曲配对情况如下\n",k);
t=min*(k-1)%max;
if(boynum>girlnum)
for (i=1;i<=t;i++)
{
DeQueue(boys,p1,boynum);
EnQueue(boys,p1,boynum);
}
else
for (i=1;i<=t;i++)
{
DeQueue(girls,p2,girlnum);
EnQueue(girls,p2,girlnum);
}
if (min<5) a = min;
else a = 5;
for (i=0;i<a;i++)
printf(" 男<->女 ");
printf("\n");
for(i=1;i<=min;i++)
{
output(boys,girls);
if (i%5==0) printf("\n");
}// for
printf("\n\n");
return OK;
}// K_peidui
int main()
{
int i;
SqQueue boys,girls;
system("title 学生搭配问题-----路荣荣"); //标题
while(1){
printf("\t ╔══════════════════════╗\n");
printf("\t ║ 学生搭配问题-----路荣荣 ║\n");
printf("\t ║ ║\n");
printf("\t ║ 1.输入数据 ║\n");
printf("\t ║ 2.输出每曲配对情况 ║\n");
printf("\t ║ 3.求出第k曲的情况 ║\n");
printf("\t ║ 4.退出 ║\n");
printf("\t ╚══════════════════════╝\n");
printf("\t\t 男:%d人 女:%d人 曲目数:%d曲\n",
boynum,girlnum,song_num);
do //对用户输入进行容错处理
{
printf("\n\t*请选择相应功能(1-4):");
i=check();
if( i!=1 && i!=2&& i!=3 && i!=4)
{
printf("\t请检查您的输入在1~4之间!\n");
printf("\t请再输入一遍!");
}
}
while( i!=1 && i!=2 && i!=3 && i!=4);
if(i==1)
{
input(boys,girls);
system("cls");
continue;
}
else if(i==2)
All_peidui(boys,girls);
else if(i==3)
K_peidui(boys,girls);
else
exit(0);
system("pause"); //任意键继续
system("cls"); //清屏
}// while
return OK;
}