/*
友情提示,本程序分为7个文件3个cpp文件,3个头文件,还有最后一个执行文件,如用后有不懂的可随时和私信联系编者
*/
Single.h
#pragma once
#include
using namespace std;
class Single
{
int getAge();
string getName();
public:
Single();
~Single();
Single(string name, int age);
string getName()const;
int getAge()const;
protected:
string name;
int age;
};
Single.cpp
#include “Single.h”
Single::Single()
{
}
Single::~Single()
{
}
Single::Single(string name, int age){
this->name = name;
this->age = age;
}
string Single::getName()const{
return name;
}
int Single::getAge()const{
return age;
}
Girl.h:
#pragma once
#include
#include
#include
#include"Boy.h"
using namespace std;
class Boy;
class Girl:public Single
{
public:
Girl();
~Girl();
Girl(string &name, int age, int yanzhi);
//string getName();
int getYanzhi()const;
//int getAge();
bool satisfied(const Boy&boy);
string description();
static void pushgirls(vector<Girl>&girls);
private:
//string name;
int yanzhi;
//int age;
};
Girl.cpp:
#include “Girl.h”
#include
#define _YANZHIFACTOR 100
Girl::Girl()
{
}
Girl::~Girl()
{
}
Girl::Girl(string &name, int age, int yanzhi):Single(name,age){
//this->name = name;
//this->age = age;
this->yanzhi = yanzhi;
}
/*string Girl::getName(){
return name;
}*/
int Girl::getYanzhi()const{
return yanzhi;
}
/*int Girl::getAge(){
return age;
}*/
bool Girl::satisfied(const Boy&boy){
if (boy.getSalary() >= yanzhi*_YANZHIFACTOR){
return true;
}
else{
return false;
}
}
string Girl::description(){
stringstream ret;
ret << “姓名:” <<name<< “-年龄-” << age << “-颜值-” << yanzhi ;
return ret.str();
}
void Girl::pushgirls(vector&girls){
int age;
string name;
int yanzhi;
int n=1;
while (1){
cout << "请输入第" << n << "位女嘉宾的年龄【输入0结束】:" ;
cin >> age;
if (age == 0){
break;
}
cout << "请输入第"<<n<<"位女嘉宾的姓名:" ;
cin >> name;
cout << "请输入第" << n << "位女嘉宾的颜值系数:" ;
cin >> yanzhi;
girls.push_back(Girl(name, age, yanzhi));
n++;
}
}
Boy.h
#pragma once
#include
#include
#include
#include"Single.h"
using namespace std;
class Girl;
class Boy:public Single
{
public:
Boy();
~Boy();
Boy(string name,int age,int salary);
//string getName();
int getSalary()const;
//int getAge();
bool satisfied(const Girl&girl);
string description();
static void pushBoys(vector<Boy>&boys);
private:
//string name;
//int age;
int salary;
};
Boy.cpp
#include “Boy.h”
#include
#include"Girl.h"
#define _Salayfactor 0.006
Boy::Boy()
{
}
Boy::~Boy()
{
}
Boy::Boy(string name, int age, int salary):Single(name,age){
//this->name = name;
//this->age = age;
this->salary = salary;
}
/*string Boy::getName(){
return name;
}*/
int Boy::getSalary()const{
return salary;
}
/*int Boy::getAge(){
return age;
}*/
bool Boy::satisfied(const Girl&girl){
int yanzhi = _Salayfactor*salary;
if (yanzhi >= 100){
yanzhi = 100;
}
if (girl.getYanzhi() >= yanzhi){
return true;
}
else{
return false;
}
}
string Boy::description(){
stringstream ret;
ret << "姓名:" << name << "-年龄-" << "-薪资-" << salary ;
return ret.str();
}
void Boy::pushBoys(vector&boys){
string name;
int age;
int salary;
int n = 1;
while (1){
cout << "请输入第"<<n<<"位男嘉宾的年龄【输入0结束】:" ;
cin >> age;
if (age == 0){
break;
}
cout << "请输入第" << n << "位男嘉宾的姓名:" ;
cin >> name;
cout << "请输入第"<<n<<"位男嘉宾的薪资:";
cin >> salary;
boys.push_back(Boy(name, age, salary));
n++;
}
}
main.cpp
#include"Boy.h"
#include"Girl.h"
#include<Windows.h>
void autopair(vector&boys, vector&girls){
for (int i = 0; i < boys.size(); i++){
for (int j = 0; j < girls.size(); j++){
if (boys[i].satisfied(girls[j])&&girls[j].satisfied(boys[i])){
cout << boys[i].description() << "<<---->>" << girls[j].description() << endl;
}
}
}
}
int main(void){
vectorboys;
vectorgirls;
Boy::pushBoys(boys);
Girl::pushgirls(girls);
cout << "配对结果" << endl;
autopair(boys, girls);
system("pause");
return 0;
}