Boy.h:
#pragma once #include <string> #include <vector> using namespace std; class Girl; class Boy { public: Boy(); Boy(int age,string name,int salary); int getAge()const; string getName()const; int getSalary()const; bool isSatisfied(const Girl& girl)const; string description()const; static void inputBoys(vector<Boy>& boys); ~Boy(); private: int age; string name; int salary; };
Girl.h:
#pragma once #include <iostream> #include <vector> using namespace std; class Boy; class Girl { public: Girl(); Girl(int age, string name, int faceScore); int getAge()const; string getName()const; int getFaceScore()const; bool isSatisfied(const Boy& boy)const; string description()const; static void inputGirls(vector<Girl> &girls); ~Girl(); private: int age; string name; int faceScore; //颜值 };
Boy.cpp:
#include <sstream> #include "Boy.h" #include "Girl.h" #define SALARY_FACTORY 0.006 Boy::Boy() { age = 0; name = ""; salary = 0; } Boy::Boy(int age, string name, int salary) { this->age = age; this->name = name; this->salary = salary; } int Boy::getAge()const { return age; } string Boy::getName() const { return name; } int Boy::getSalary()const { return salary; } bool Boy::isSatisfied(const Girl& girl)const { int faceScore = salary * SALARY_FACTORY; if (faceScore > 100) { faceScore = 100; } if (girl.getFaceScore() >= faceScore) { return true; } else { return false; } } void Boy::inputBoys(vector<Boy>& boys) { int age; string name; int salary; int n = 1; while (1) { cout << "请输入第" << n << "位小哥哥的年龄【输入0结束】:"; cin >> age; if (age == 0) { break; } cout << "请输入第" << n << "位小哥哥的姓名:"; cin >> name; cout << "请输入第" << n << "位小哥哥的薪资:"; cin >> salary; n++; boys.push_back(Boy(age, name, salary)); } } string Boy::description()const { stringstream ret; ret << name << "-男-薪资(" << salary << ")-年龄(" << age << ")"; return ret.str(); } Boy::~Boy() { }
Girl.cpp:
#include "Girl.h" #include "Boy.h" #include <sstream> #define FACESCORE_FACTORY 100 Girl::Girl() { name = ""; age = 0; faceScore = 0; } Girl::Girl(int age, string name,int faceScore){ this->name = name; this->age = age; this->faceScore = faceScore; } int Girl::getAge()const { return age; } string Girl::getName()const { return name; } int Girl::getFaceScore()const { return faceScore; } bool Girl::isSatisfied(const Boy& boy)const { if (boy.getSalary() >= faceScore * FACESCORE_FACTORY) { return true; } else { return false; } } void Girl::inputGirls(vector<Girl> &girls) { int age; string name; int faceScore; int n = 1; while (1) { cout << "请输入第" << n << "位小姐姐的年龄【输入0结束】:"; cin >> age; if (age == 0) { break; } cout << "请输入第" << n << "位小姐姐的姓名:"; cin >> name; cout << "请输入第" << n << "位小姐姐的颜值:"; cin >> faceScore; n++; girls.push_back(Girl(age, name, faceScore)); } } string Girl::description()const { stringstream ret; ret << name << "-女-颜值(" << faceScore << ")-年龄(" << age << ")"; return ret.str(); } Girl::~Girl() { }
主函数main.cpp:
#include <iostream> #include <vector> #include "Girl.h" #include "Boy.h" using namespace std; 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].isSatisfied(girls[j]) && girls[j].isSatisfied(boys[i])) { cout << boys[i].description() << "<==>" << girls[j].description() << endl; } } } } int main() { vector<Boy> boys; vector<Girl> girls; Boy::inputBoys(boys); Girl::inputGirls(girls); cout << "\n--------------------------结果------------------------\n"; autoPair(boys,girls); system("pause"); return 0; }