项目十

实现相亲功能,输入男生和女生的信息,然后进行匹配组合

代码实现:

  • 男生类-头文件
#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 getSalry() const;
	bool satisfied(const Girl& girl) const;
	string Description() const;
	static void inputBoys(vector <Boy> &boys);	//输入多个男孩的信息
private:
	int age;
	string name;
	int salary;
};
  • 男生类-方法实现
#include "Boy.h"
#include "Girl.h"
#include <sstream>
#include <iostream>

#define SALARY_RATIO 0.003	//薪资系数

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::getSalry() const {
	return salary;
}
bool Boy::satisfied(const Girl& girl) const {
	int yanzhi = salary * SALARY_RATIO;
	if (yanzhi > 100) {
		yanzhi = 100;
	}
	//如果颜值高于薪资乘颜值系数,表示满意
	if (girl.getYanZhi() >= yanzhi) {
		return true;
	}
	else {
		return false;
	}
}
string Boy::Description() const {
	stringstream ret;
	ret << name << "的年龄:" << age << " 薪资:" << salary;

	return ret.str();
}

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;

		boys.push_back(Boy(age,name,salary));
		n++;
	}
}
  • 女生类-头文件
#pragma once
#include <string>
#include <vector>

using namespace std;

class Boy;

class Girl
{
public:
	Girl();
	Girl(int age,string name,int yanZhi);
	int getAge() const;
	string getName() const;
	int getYanZhi() const;
	string Description() const;
	bool satisfied(const Boy &boy) const;
	static void iputGirls(vector<Girl> &girls);
private:
	int age;
	string name;
	int yanZhi;	//颜值

};
  • 女生类-方法实现
#include "Girl.h"
#include "Boy.h"
#include <sstream>
#include <iostream>

#define YANZHI_RATIO 300	//颜值系数

Girl::Girl() {
	age = 0;
	name = "";
	yanZhi = 0;
}

Girl::Girl(int age, string name, int yanZhi) {
	this->age = age;
	this->name = name;
	this->yanZhi = yanZhi;
}

int Girl::getAge() const{
	return age;
}
string Girl::getName() const{
	return name;
}
int Girl::getYanZhi() const{
	return yanZhi;
}

bool Girl::satisfied(const Boy &boy) const{
	//如果男生的薪资大于自己的颜值*颜值系数,表示满意
	if (boy.getSalry() >= yanZhi * YANZHI_RATIO) {
		return true;
	}
	else
	{
		return false;
	}
}

string Girl::Description() const{
	stringstream ret;
	ret << name << "的年龄:" << age << " 颜值:" << yanZhi;

	return ret.str();

}

void Girl::iputGirls(vector<Girl> &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(age, name, yanZhi));
		n++;
	}
}
  • 调用
#include "Girl.h"
#include "Boy.h"
#include <vector>
#include <iostream>

//自动匹配
void autoMatch(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].satisfied(girls[j]) && girls[j].satisfied(boys[i])) {
				cout << "(" << boys[i].getName() << ")匹配的女生是(" << girls[j].getName() << ")" << endl;
			}
		}
	}
}

int main() {
	vector<Boy> boys;
	vector<Girl> girls;

	Boy::inputBoys(boys);
	Girl::iputGirls(girls);

	cout << "\n。。。。匹配后。。。。\n";
	autoMatch(boys,girls);
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值