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;
}

运行结果:

原文地址:http://www.cnblogs.com/smartlearn/p/16886788.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性