C++婚恋交友系统

实现的功能,将数据写入文件中,将文件中的数据读出。

实现泛化交友匹配

实现最佳交友

main.cpp

#include<vector>
#include<iostream>
#include"Boy.h"
#include"Girl.h"
#include"Database.h"


int main()
{
	//Database data;
	//data.init();
	//Boy boy;
	//Boy::inputBoy(boy);//输入boy信息

	//data.addOne(boy);

	//Girl girl;
	//Girl::inputGirl(girl);//输入girl信息
	//data.addOne(girl);

	Database data;
	data.init();
	data.print();
	data.autoPair();
	data.autoPairBest();


	system("pause");
	return 0;
}

Boy.h

#pragma once
#include<string>
#include<vector>
#include<fstream>
#include"Single.h"
using namespace std;

class Girl;

class Boy : public Single
{
public:
	Boy();
	Boy(string name, int age, int salary);

	//int getAge() const;
	//string getName() const;
	int getSalary() const;
	string description() const;

	bool statisfied(const Girl&girl) const;

	bool operator>(const Boy& boy);

	static void inputBoys(vector<Boy>&boys);
	static void inputBoy(Boy& boy);

	friend ostream& operator<<(ostream& os, Boy& boy);

protected:
	//int age;
	//string name;
	int salary; //薪资

};

ostream& operator<<(ostream& os, Boy& boy);


Boy.cpp

#pragma once
#include<string>
#include<vector>
#include<fstream>
#include"Single.h"
using namespace std;

class Girl;

class Boy : public Single
{
public:
	Boy();
	Boy(string name, int age, int salary);

	//int getAge() const;
	//string getName() const;
	int getSalary() const;
	string description() const;

	bool statisfied(const Girl&girl) const;

	bool operator>(const Boy& boy);

	static void inputBoys(vector<Boy>&boys);
	static void inputBoy(Boy& boy);

	friend ostream& operator<<(ostream& os, Boy& boy);

protected:
	//int age;
	//string name;
	int salary; //薪资

};

ostream& operator<<(ostream& os, Boy& boy);


Girl.h

#pragma once
#include<string>
using namespace std;
#include<vector>
#include"Single.h"
#include<fstream>

class Boy;

class Girl : public Single
{
public:
	Girl();
	Girl(string name, int age, int yanZhi);

	//int getAge() const;
	//string getName() const;
	int getYanZhi() const;
	bool satisfied(const Boy&boy) const;
	string description() const;
	bool operator>(const Girl& girl);


	static void inputGirls(vector<Girl>&girls);
	static void inputGirl(Girl& girl);
	
	friend ostream& operator<<(ostream& os, Girl& girl);


protected:
	//int age;
	//string name;
	int yanZhi;//颜值

};
ostream& operator<<(ostream& os, Girl& girl);

Girl.cpp

#include "Girl.h"
#include<sstream>//stringstream
#include"Boy.h"
#include<iostream>


#define YANZHI_FACTOR 100    //颜值系数

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

Girl::Girl(string name, int age, int yanZhi) :Single(name,age)
{
	//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.getSalary()>=yanZhi*YANZHI_FACTOR)
	{
		return true;
	}
	else
	{
		return false;
	}
	
}

string Girl::description()const
{
	stringstream ret;
	ret << "性别:女 姓名:" << name 
		<< " 颜值:" << yanZhi 
		<< " 年龄:" << age;
	return ret.str();
}

bool Girl::operator>(const Girl & girl)
{
	return yanZhi > girl.yanZhi;
}

void Girl::inputGirls(vector<Girl> & girls)
{
	int age;
	string name;
	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++;
	}
}

void Girl::inputGirl(Girl & girl)
{
	string name;
	int age;
	int yanZhi;

	cout << "请输入小姐姐的姓名【输入QUIT结束】" << endl;
	cin >> name;
	
	cout << "请输入小姐姐的年龄" << endl;
	cin >> age;

	cout << "请输入小姐姐的颜值" << endl;
	cin >> yanZhi;
	girl = Girl(name, age, yanZhi);
}

ostream & operator<<(ostream & os, Girl & girl)
{
	// TODO: 在此处插入 return 语句
	os << "性别:女 姓名:" << girl.name
		<< " 颜值:" << girl.yanZhi
		<< " 年龄:" << girl.age;
	return os;
}

Single.h

#pragma once
#include<string>
using namespace std;
class Single
{
public:
	Single();
	Single(const string &name, const int &age);
	~Single();
	string getName()const;
	int getAge()const;

protected:
	string name;
	int age;
};

Single.cpp

#include "Single.h"

Single::Single()
{
	name = "无名";
	age = 0;
}

Single::Single(const string & name, const int & age)
{
	this->name = name;
	this->age = age;
}

Single::~Single()
{
}

string Single::getName()const
{
	return name;
}

int Single::getAge()const
{
	return age;
}

DataBase.h

#pragma once
#include<vector>
#include"Boy.h"
#include"Girl.h"

/*
	需求:要使用文件来保存用户信息
	分析:设计一个类,来实现信息的保存功能
				Database 数据库
	功能:
			init()//初始化,从文件中读取数据信息,来初始化用户数据
			autoPair()//自动配对
			print() //打印该数据库中的所有用户信息

	数据:vector<Boy>boys  //所有单身男信息
				vector<Girl>girls  //所有单身女信息
*/
class Database
{
public:
	Database();

	//初始化,从文件中读取数据信息,来初始化用户数据
	void init();

	//自动配对
	void autoPair();

	//实现自动最佳配对
	void autoPairBest();

	//打印该数据库中的所有用户信息
	void print();

	//添加单个用户信息
	void addOne(Boy& boy);
	void addOne(Girl& girl);

private:
	//所有单身男信息
	vector<Boy>boys;  

	//所有单身女信息
	vector<Girl>girls;  

	//初始化男嘉宾信息
	void initBoysFromFile();

	//初始化女嘉宾信息
	void initGirlsFromFile();

	//保存boys的信息到文件中
	void saveBoys();

	//保存girls的信息到文件中
	void saveGirls();

};

DataBase.cpp

#include<fstream>
#include<iostream>
#include<string>
#include "Database.h"
#include"Boy.h"
//#include"Girl.h"

using namespace std;

#define BOY_FILE  "boys.txt"
#define GIRL_FILE  "girls.txt"

Database::Database()
{
}

void Database::init()
{
	//从已经保存的文件中,读取用户信息,
	//用来初始化内部数据boys和girls
	initBoysFromFile();
	initGirlsFromFile();


}

void Database::autoPair()
{
	cout << "自动配对结果" << endl;
	string line(100, '-');
	cout << line << endl;
	for (int i = 0; i < (int)boys.size(); i++)
	{
		for (int j = 0; j < (int)girls.size(); j++)
		{
			if (boys[i].statisfied(girls[j]) && girls[j].satisfied(boys[i]))
			{
				cout << line << endl;
				cout << boys[i].description() << endl;
				cout << girls[j].description() << endl;
			}
		}
	}
}

void Database::autoPairBest()
{
	cout << "--------------自动配对最佳伴侣结果--------------" << endl;
	string line(50, '-');
	cout << line << endl;
	for (int i = 0; i < (int)boys.size(); i++)
	{
		Girl *girlBest = NULL;
		for (int j = 0; j < (int)girls.size(); j++)
		{
			if (boys[i].statisfied(girls[j]) && girls[j].satisfied(boys[i]))
			{
				if (!girlBest)
				{
					girlBest = &girls[j];

				}
				else if(girls[j]>*girlBest)
				{
					girlBest = &girls[j];
				}
				/*cout << line << endl;
				cout << boys[i].description() << endl;
				cout << girls[j].description() << endl;*/
			}
		}
		if (girlBest)
		{
			cout << line << endl;
			cout << boys[i] << endl;
			cout << *girlBest << endl;
		}
		
	}
	cout << "*********************************************" << endl;

	for (int i = 0; i < (int)girls.size(); i++)
	{
		Boy *boyBest = NULL;
		for (int j = 0; j < (int)boys.size(); j++)
		{
			if (boys[j].statisfied(girls[i]) && girls[i].satisfied(boys[j]))
			{
				if (!boyBest)
				{
					boyBest = &boys[j];

				}
				else if (boys[j] >*boyBest)
				{
					boyBest = &boys[j];
				}
				
			}
		}
		if (boyBest)
		{
			
			cout << girls[i] << endl;
			cout << *boyBest << endl;
			cout << line << endl;
		}
	}
	

}

void Database::print()
{
	cout << "男嘉宾信息" << endl;
	for (int i = 0; i < boys.size(); i++)
	{
		cout << boys[i].description() << endl;
	}

	cout << "女嘉宾信息" << endl;
	for (int j = 0; j < boys.size(); j++)
	{
		cout << girls[j].description() << endl;
	}

}

void Database::addOne(Boy & boy)
{
	boys.push_back(boy);

	cout << "自动配对结果" << endl;
	string line(100, '-');
	cout << line << endl;
	for (int j = 0; j < (int)girls.size(); j++)
	{
		if (boy.statisfied(girls[j]) && girls[j].satisfied(boy))
		{
			cout << boy.description() << endl;
			cout << girls[j].description() << endl;
			cout << line << endl;
		}
	}


}

void Database::addOne(Girl & girl)
{
	girls.push_back(girl);

	cout << "自动配对结果" << endl;
	string line(100, '-');
	cout << line << endl;

	for (int j = 0; j < (int)boys.size(); j++)
	{
		if (girl.satisfied(boys[j]) && boys[j].statisfied(girl))
		{
			cout << girl.description() << endl;
			cout << boys[j].description() << endl;
			cout << line << endl;
		}

	}
}

void Database::initBoysFromFile()
{
	//打开文件,读文件,初始化boys
	ifstream stream;
	stream.open(BOY_FILE);
	if (!stream.is_open())
	{
		//最开始没有文件
		//假设有这个需求
		//最开始没用文件时,让用户输入基础用户数据
		cout << "===输入基础用户【男嘉宾】数据==="<< endl;
		Boy::inputBoys(this->boys);
		saveBoys();
		stream.close();
		return;
	}

	//boys.txt打开成功
	while (1)
	{ 
		string line;
		char name[64]="";
		int salary;
		int age;
		getline(stream, line);
		if (stream.eof())
		{
			break;
		}
		//解析读到的一行数据line
		//todo
		//文件的格式对比:   性别:男\t\t\t姓名:zhs\t\t\t薪资:35000\t\t\t年龄:24
		int ret =sscanf_s(line.c_str(), "性别:男 姓名:%s 薪资:%d 年龄:%d",name,sizeof(name),&salary,&age);
		if (ret<=0)
		{
			cout << "男嘉宾数据库匹配失败" << endl;
			exit(1);
		}
		boys.push_back(Boy(string(name), age, salary));
		 
	}
}

void Database::initGirlsFromFile()
{
	//打开文件,读文件,初始化girls
	ifstream stream;
	stream.open(GIRL_FILE);
	if (!stream.is_open())
	{
		cout << "===输入基础用户【女嘉宾】数据" << endl;
		Girl::inputGirls(this->girls);
		saveGirls();
		stream.close();
		return;
	}
	while (1)
	{
		string line;
		char name[64] = "";
		int yanZhi;
		int age;
		getline(stream, line);
		if (stream.eof())
		{
			break;
		}
		//解析读到的一行数据line
		//todo
		//文件的格式对比:   性别:女\t\t\t姓名:zhs\t\t\t颜值:90\t\t\t年龄:24
		int ret =sscanf_s(line.c_str(),"性别:女 姓名:%s 颜值:%d 年龄:%d",name,sizeof(name),&yanZhi,&age);
		if (ret<=0)
		{
			cout << "女嘉宾数据库匹配失败" << endl;
			exit(1);
		}
		girls.push_back(Girl(string(name), age, yanZhi));
	}
}

void Database::saveBoys()
{
	//打开文件,把所有boys中的单身男信息写入文件
	ofstream stream;
	stream.open(BOY_FILE);
	if (!stream.is_open())
	{
		cout << BOY_FILE << "写入失败" << endl;
		exit(1);
	}

	for (int i = 0; i < boys.size(); i++)
	{
		stream << boys[i].description() << endl;
	}
	stream.close();
}

void Database::saveGirls()
{
	//打开文件,把所有girls中的单身女信息写入文件
	ofstream stream;
	stream.open(GIRL_FILE);
	if (!stream.is_open())
	{
		cout << GIRL_FILE << "写入失败" << endl;
		exit(1);
	}

	for (int i = 0; i <girls.size(); i++)
	{
		stream << girls[i].description() << endl;
	}
	stream.close();
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值