1、Boy的头文件
#pragma once
#include<string>
#include<vector>
#include<sstream>
using namespace std;
class Girl;
class Boy
{
public:
Boy();
Boy(string name,int age, int salary);
string getName() const;
int getAge() const;
int getSalary() const;
bool statisfied(const Girl&girl) const;
string description()const;
static void inputBoys(vector<Boy>&boys);
private:
string name;
int age;
int salary;
};
Boy.cpp文件
#include "Boy.h"
#include"Girl.h"
#include<iostream>
#define SALARY_FACTOR 0.006
Boy::Boy()
{
name = "";
age = 0;
salary = 0;
}
Boy::Boy(string name, int age, int salary)
{
this->name = name;
this->age = age;
this->salary = salary;
}
string Boy::getName() const
{
return name;
}
int Boy::getAge() const
{
return age;
}
int Boy::getSalary() const
{
return salary;
}
bool Boy::statisfied(const Girl&girl) const
{
if (girl.getYanzhi()>=salary*SALARY_FACTOR)
{
return true;
}
else
{
return false;
}
}
string Boy::description()const
{
stringstream ret;
ret << name << "-的年龄-" << age << " 薪水为:" << salary;
return ret.str();
}
void Boy::inputBoys(vector<Boy>& boys)
{
string name;
int age;
int salary;
int n = 1;
while (1)
{
cout << "请输入第" << n << "位小哥哥的姓名【输入quit结束】" << endl;
cin >> name;
if (name =="quit")
{
break;
}
cout << "请输入第" << n << "位小哥哥的年龄" << endl;
cin >> age;
cout << "请输入第" << n << "位小哥哥的薪水" << endl;
cin >> salary;
boys.push_back(Boy(name, age, salary));
n++;
}
}
Girl.h头文件
#pragma once
#include<string>
#include<vector>
using namespace std;
class Boy;
class Girl
{
public:
Girl();
Girl(string name, int age, int yanZhi);
string getName() const;
int getAge() const;
int getYanzhi() const;
string description() const;
bool statisfied(const Boy&boy) const;
static void inputGirls(vector<Girl>&girl);
private:
string name;
int age;
int yanZhi;
};
Girl.cpp文件
#include "Girl.h"
#include"Boy.h"
#include<sstream>
#include<vector>
#include<iostream>
#define YANZHI_FACTOR 100
class Boy;
Girl::Girl()
{
name = " ";
age = 0;
yanZhi = 0;
}
Girl::Girl(string name, int age, int yanZhi)
{
this->name =name;
this->age =age;
this->yanZhi = yanZhi;
}
string Girl::getName() const
{
return name;
}
int Girl::getAge()const
{
return age;
}
int Girl::getYanzhi() const
{
return yanZhi;
}
string Girl::description() const
{
stringstream ret;
ret << name << "-的年龄为-" << age << "-颜值-" << yanZhi;
return ret.str();
}
bool Girl::statisfied(const Boy&boy) const
{
if (boy.getSalary() >= yanZhi * YANZHI_FACTOR)
{
return true;
}
else
{
return false;
}
}
void Girl::inputGirls(vector<Girl>& girls)
{
string name;
int age;
int yanZhi;
int n = 1;
while (1)
{
cout << "请输入第" << n << "位小哥哥的姓名【输入quit结束】" << endl;
cin >> name;
if (name == "quit")
{
break;
}
cout << "请输入第" << n << "位小哥哥的年龄" << endl;
cin >> age;
cout << "请输入第" << n << "位小哥哥的薪水" << endl;
cin >> yanZhi;
girls.push_back(Girl(name, age, yanZhi));
n++;
}
}
main主函数:
#include<iostream>
#include"Boy.h"
#include"Girl.h"
#include<Windows.h>
void autopair(const vector<Boy>&boys, const vector<Girl>&girls)
{
for (int i = 0; i < boys.size(); i++)
{
for (int j = 0; j < girls.size(); j++)
{
if (boys[i].statisfied(girls[j]) &&girls[j].statisfied(boys[i]))
{
cout << boys[i].description() << "<<==>>"<<girls[j].description() << endl;
}
}
}
}
int main()
{
vector<Boy>boys;
vector<Girl>girls;
Boy::inputBoys(boys);
Girl::inputGirls(girls);
autopair(boys, girls);
system("pause");
return 0;
}