主题:如何把算法转化为程序?
下面这个算法是用C语言编写的,如何才能把这个算法转换成一个可以被计算机执行的程序?
typedef struct{
char name[20];
char sex; //性别,'F'表示女性,'M'表示男性
}Person;
typedef Person DataType; //将队列中元素的数据类型改为Person
void DancePartner(Person dancer[],int num)
{//结构数组dancer中存放跳舞的男女,num是跳舞的人数。
int i;
Person p;
CirQueue Mdancers,Fdancers;
InitQueue(&Mdancers);//男士队列初始化
InitQueue(&Fdancers);//女士队列初始化
for(i=0;i<num;i++){//依次将跳舞者依其性别入队
p=dancer[i];
if(p.sex=='F')
EnQueue(&Fdancers.p); //排入女队
else
EnQueue(&Mdancers.p); //排入男队
}
printf("The dancing partners are: \n \n");
while(!QueueEmpty(&Fdancers)&&!QueueEmpty(&Mdancers)){
//依次输入男女舞伴名
p=DeQueue(&Fdancers); //女士出队
printf("%s ",p.name);//打印出队女士名
p=DeQueue(&Mdancers); //男士出队
printf("%s\n",p.name); //打印出队男士名
}
if(!QueueEmpty(&Fdancers)){ //输出女士剩余人数及队头女士的名字
printf("\n There are %d women waitin for the next round.\n",Fdancers.count);
p=QueueFront(&Fdancers); //取队头
printf("%s will be the first to get a partner. \n",p.name);
}else
if(!QueueEmpty(&Mdancers)){//输出男队剩余人数及队头者名字
printf("\n There are%d men waiting for the next round.\n",Mdacers.count);
p=QueueFront(&Mdancers);
printf("%s will be the first to get a partner.\n",p.name);
}
}//DancerPartners
typedef struct{
char name[20];
char sex; //性别,'F'表示女性,'M'表示男性
}Person;
typedef Person DataType; //将队列中元素的数据类型改为Person
void DancePartner(Person dancer[],int num)
{//结构数组dancer中存放跳舞的男女,num是跳舞的人数。
int i;
Person p;
CirQueue Mdancers,Fdancers;
InitQueue(&Mdancers);//男士队列初始化
InitQueue(&Fdancers);//女士队列初始化
for(i=0;i<num;i++){//依次将跳舞者依其性别入队
p=dancer[i];
if(p.sex=='F')
EnQueue(&Fdancers.p); //排入女队
else
EnQueue(&Mdancers.p); //排入男队
}
printf("The dancing partners are: \n \n");
while(!QueueEmpty(&Fdancers)&&!QueueEmpty(&Mdancers)){
//依次输入男女舞伴名
p=DeQueue(&Fdancers); //女士出队
printf("%s ",p.name);//打印出队女士名
p=DeQueue(&Mdancers); //男士出队
printf("%s\n",p.name); //打印出队男士名
}
if(!QueueEmpty(&Fdancers)){ //输出女士剩余人数及队头女士的名字
printf("\n There are %d women waitin for the next round.\n",Fdancers.count);
p=QueueFront(&Fdancers); //取队头
printf("%s will be the first to get a partner. \n",p.name);
}else
if(!QueueEmpty(&Mdancers)){//输出男队剩余人数及队头者名字
printf("\n There are%d men waiting for the next round.\n",Mdacers.count);
p=QueueFront(&Mdancers);
printf("%s will be the first to get a partner.\n",p.name);
}
}//DancerPartners