面向对象编程(智能婚恋交友系统)

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值