普通人卧推多少:这个怎么改成C++

来源:百度文库 编辑:杭州交通信息网 时间:2024/05/02 00:35:26
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
static int m=0,w=0;
struct people
{
char name[20];
char sex;
char hobby[10][20];
int hobby_num,match;
}men[20],women[20];
int newclient()
{
int i;
char sex;
scanf("%s",&sex);
if(m>19)
{
cout<<"You can't int more.";
return 0;
}
if(w>19)
{
printf("You can't int more.");
return 0;
}
if(sex=='m')
{
men[m].sex=sex;
men[m].match=-1;
scanf("%s",&men[m].name);
scanf("%d",&men[m].hobby_num);
for(i=0;i<men[m].hobby_num;i++)
{
scanf("%s",&men[m].hobby[i]);
}
m++;
}
else if(sex=='f')
{
women[w].sex=sex;
women[w].match=-1;
scanf("%s",&women[w].name);
scanf("%d",&women[w].hobby_num);
for(i=0;i<women[w].hobby_num;i++)
{
scanf("%s",&women[w].hobby[i]);
}
w++;
}
printf(" %d men && %d women\n",m,w);
return 0;
}
int match(){
int i,j,x,y,a;
printf("match start...\n");
for(i=0; i<m; i++)
{
for(j=0; j<w; j++)
{
for(x=0; x<men[i].hobby_num; x++)
{
for(y=0; y<women[j].hobby_num; y++)
{
if(!strcmp(men[i].hobby[x], women[j].hobby[y]))
{
if((men[i].match==0 || men[i].match==-1) && (women[j].match==0 || women[j].match==-1))
{
men[i].match=j+1;
women[j].match=i+1;
}
else
{
if(men[i].match==0 || men[i].match==-1)
{
men[i].match=-1;
}
else if (women[j].match==0 || women[j].match==-1)
{
women[j].match=-1;
}
}
}
else
{
a++;
if(a==men[i].hobby_num * women[j].hobby_num)
{
men[i].match=-1;
women[j].match=-1;
}
}
}
}
}
}
return 0;
}
int unmatch()
{
int i;
char a[20];
scanf("%s",a);
for(i=0;i<w;i++)
{
if(men[i].match!=-1 && women[i].match!=-1)
{
if(!strcmp(a,men[i].name))
{
men[i].match=-1;
women[i].match=-1;
}
if(!strcmp(a,women[i].name))
{
men[i].match=-1;
women[i].match=-1;
}
}
}
return 0;
}
int printmatch()
{
int i;
printf("They are matched\n");
for(i=0; i<20; i++)
{
if(men[i].match!=-1 && men[i].match!=0)
{
printf(" %s && %s \n", men[i].name, women[men[i].match-1].name);
}
}
printf("\n");
return 0;
}
int printother()
{
int i;
printf("unmatched Men:\n");
for(i=0; i<m;i++)
{
if(men[i].match ==-1 || men[i].match==0)
{
printf(" %s\n", men[i].name);
}
}
printf("unmatched Women:\n ");
for(i=0; i<w; i++)
{
if(women[i].match == -1 || women[i].match==0)
{
printf("%s\n", women[i].name);
}

}
return 0;
}
int main()
{
int i;
for(i=0;;i++)
{
char command[10];
printf("Choose one command:");
printf("newclient, match, unmatch, printmatch, printother, quit\n");
scanf("%s",command);
if(!strcmp(command,"newclient"))
{
newclient();
}
else if(!strcmp(command,"match"))
{
match();
}
else if(!strcmp(command,"unmatch"))
{
unmatch();
}
else if(!strcmp(command,"printmatch"))
{
printmatch();
}
else if(!strcmp(command,"printother"))
{
printother();
}
else if(!strcmp(command,"quit"))
{
break;
}
else
{
printf("Command is wrong.\n");
}
}
return 0;
}

呼呼,终于帮你改完了~~~~呵呵,调试通过,符合C++习惯.

#include<string>
#include<stdlib.h>
#include<iostream>
using namespace std;

static int m=0,w=0;
struct people
{
char name[20];
char sex;
char hobby[10][20];
int hobby_num,match;
}men[20],women[20];
int newclient()
{
int i;
char sex;
cin>>sex;
if(m>19)
{
cout<<"You can't int more.";
return 0;
}
if(w>19)
{
cout<<"You can't int more.";
return 0;
}
if(sex=='m')
{
men[m].sex=sex;
men[m].match=-1;
cin>>men[m].name;
cin>>men[m].hobby_num;
for(i=0;i<men[m].hobby_num;i++)
{
cin>>men[m].hobby[i];
}
m++;
}
else if(sex=='f')
{
women[w].sex=sex;
women[w].match=-1;
cin>>women[w].name;
cin>>women[w].hobby_num;
for(i=0;i<women[w].hobby_num;i++)
{
cin>>women[w].hobby[i];
}
w++;
}
cout<<m<<"men &&"<<w<<"women"<<endl;
return 0;
}
int match(){
int i,j,x,y,a;
cout<<"match start..."<<endl;
for(i=0; i<m; i++)
{
for(j=0; j<w; j++)
{
for(x=0; x<men[i].hobby_num; x++)
{
for(y=0; y<women[j].hobby_num; y++)
{
if(!strcmp(men[i].hobby[x], women[j].hobby[y]))
{
if((men[i].match==0 || men[i].match==-1) && (women[j].match==0 || women[j].match==-1))
{
men[i].match=j+1;
women[j].match=i+1;
}
else
{
if(men[i].match==0 || men[i].match==-1)
{
men[i].match=-1;
}
else if (women[j].match==0 || women[j].match==-1)
{
women[j].match=-1;
}
}
}
else
{
a++;
if(a==men[i].hobby_num * women[j].hobby_num)
{
men[i].match=-1;
women[j].match=-1;
}
}
}
}
}
}
return 0;
}
int unmatch()
{
int i;
char a[20];
cin>>a;
for(i=0;i<w;i++)
{
if(men[i].match!=-1 && women[i].match!=-1)
{
if(!strcmp(a,men[i].name))
{
men[i].match=-1;
women[i].match=-1;
}
if(!strcmp(a,women[i].name))
{
men[i].match=-1;
women[i].match=-1;
}
}
}
return 0;
}
int printmatch()
{
int i;
cout<<"They are matched"<<endl;
for(i=0; i<20; i++)
{
if(men[i].match!=-1 && men[i].match!=0)
{
cout<<men[i].name<<"&&"<<women[men[i].match-1].name<<endl;
}
}
cout<<""<<endl;
return 0;
}
int printother()
{
int i;
cout<<"unmatched Men:"<<endl;
for(i=0; i<m;i++)
{
if(men[i].match ==-1 || men[i].match==0)
{
cout<<men[i].name <<""<<endl;
}
}
cout<<"unmatched Women:"<<endl;
for(i=0; i<w; i++)
{
if(women[i].match == -1 || women[i].match==0)
{
cout<< women[i].name <<endl;
}

}
return 0;
}
int main()
{
int i;
for(i=0;;i++)
{
char command[10];
cout<<"Choose one command:";
cout<<"newclient, match, unmatch, printmatch, printother, quit"<<endl;
cin>>command;
if(!strcmp(command,"newclient"))
{
newclient();
}
else if(!strcmp(command,"match"))
{
match();
}
else if(!strcmp(command,"unmatch"))
{
unmatch();
}
else if(!strcmp(command,"printmatch"))
{
printmatch();
}
else if(!strcmp(command,"printother"))
{
printother();
}
else if(!strcmp(command,"quit"))
{
break;
}
else
{
cout<<"Command is wrong."<<endl;
}
}
return 0;
}

为什么要改啊?

C++完全兼容C的
不用改也行