第七站:C++面向对象训练

1:介绍

创建两个类,一个boy类,一个girl类,实现对两个类的数据输入,并通过main函数,对两个类的成员进行比较匹配

涉及:

vector容器,面向对象,const关键字,  stringstream(sstream.h) 

stringstream用法

stringstream ret;//可以将写入的数据转换成字符串
ret << "姓名:" << name << "-年龄-" << age << "-颜值-" <<yanZhi<< endl;
return ret.str();

vector是一个顺序容器(长度自动分配),类型自适应

用法:<vector>头文件

一维:vector<数据类型> 容器名   注:类是一种特殊的数据类型,也能放

二维:vector<vector<数据类型>> 容器名

例子:详细例子可以参考主页天天酷跑的游戏

二维的
vector<vector<IMAGE>> ObstractIMG;//image ObstractIMG[][]

IMAGE imgPillar[pillarNum];
一维的
vector<IMAGE> imgHArray;
sprintf_s(name,sizeof(name) ,"res/h1.png");
loadimage(&imgPillar[0], name, 63, 260, true);
通过push.back先往一维里放
imgHArray.push_back(imgPillar[0]);
然后再将一维的放进二维的
ObstractIMG.push_back(imgHArray);

分布:

每一个类都有.h声明头文件.cpp实现文件

2:girl类

.h

#pragma once
#include <iostream>
#include <string>
#include <vector>

using namespace std;
class Boy;
class Girl{
public:
	Girl();
	~Girl();
	Girl( string name,  int age,  int yanZhi);
	string getName()const;
	int getAge() const;
	int getYanZhi()const;
	bool satisfied(const Boy& boy) const;
	string describe()const;
	static void inputGirls(vector<Girl>& girl);

private:
	string name;
	int age;
	int yanZhi;
};

.cpp

#include "Girl.h"
#include <sstream>
#include "Boy.h"
//设置一个颜值系数

#define YANZHI_FACTOR 100
Girl::Girl() {
	name = "";
	age = 0;
	yanZhi = 0;
}
Girl::~Girl() {

}

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;
}
bool Girl::satisfied(const Boy& b) const{
	if (b.getSalary() >= yanZhi * YANZHI_FACTOR) {
		return true;
	}
	return false;
}

string Girl::describe() const {
	stringstream ret;//可以将写入的数据转换成字符串
	ret << "姓名:" << name << "-年龄-" << age << "-颜值-" <<yanZhi<< endl;
	return ret.str();
}

void Girl::inputGirls(vector<Girl>& girl){
	string name;
	int age;
	int yanZhi1;
	int n = 1;
	while (n) {
		cout << "请输入女方第" << n << "个人的年龄:";
		cin >> age;
		if (age == 0) {
			break;
		}
		cout << "请输入第" << n << "个人的姓名:";
		cin >> name;
	
		cout << "请输入第" << n << "个人的颜值:";
		cin >> yanZhi1;
		n++;
		girl.push_back(Girl(name, age, yanZhi1));
	}
}

3:boy类

.h

#pragma once
#include <iostream>
#include <string>
#include <vector>

using namespace std;
class Girl;
class Boy{
public :
	Boy();
	~Boy();
	Boy(string name, int age, int salary);
	Boy(const Boy& other);
	string getName() const;
	int getAge() const;
	int getSalary() const;
	bool satisfied(const Girl& girl) const;
	string describe() const;
	static void inputBoys(vector<Boy> &boy);

private:
	string name;
	int age;
	int salary;
};

.cpp

#include "Boy.h"
#include <sstream>
#include "Girl.h"

//定义一个薪资参数
#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;
}
Boy::Boy(const Boy& other){
	salary = other.salary;
	name = other.name;
	age = other.age;
}
Boy::~Boy() {

}

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

int Boy::getAge() const{
	return age;
}
int Boy::getSalary()const {
	return salary;
}
bool Boy::satisfied(const Girl& girl) const{
	if (girl.getYanZhi() >= salary * SALARY_FACTOR) {
		return true;
	}
	return false;
}
string Boy::describe() const {
	stringstream ret;//可以将写入的数据转换成字符串
	ret << "姓名:" << name << "-年龄-" << age << "-薪资-" <<salary<< endl;
	return ret.str();
}

void Boy::inputBoys(vector<Boy> &boy){
	string name;
	int age;
	int salary;
	int n = 1;
	while (n){
		cout << "请输入第" << n << "个人的年龄:";
		cin >> age;
		if (age == 0) {
			break;
		}
		cout << "请输入第"<<n<<"个人的姓名:";
		cin >> name;
		cout << "请输入第" << n << "个人的薪资:";
		cin >> salary;
		n++;
		boy.push_back(Boy(name, age, salary));
	}
}

4:main主函数

#include "Boy.h"
#include "Girl.h"
#include <iostream>
#include <vector>
using namespace std;
void autoCompere(vector<Boy>& boys, 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].describe() << "\n---匹配成功---\n" << girls[j].describe()<<endl;
			}
			else {
				cout << "匹配失败!!" << endl;
			}
			cout << endl;
		}
	}
}

int main(void) {
	vector<Boy> boys;
	vector<Girl> girls;
	Boy::inputBoys(boys);
	Girl::inputGirls(girls);
	cout << "\n\n----匹配中----" << endl;
	autoCompere(boys, girls);
	return 0;
}

5:实现

 

优化:使用文件读写,进行匹配

使用database的类,来实现初始化,输入数据,拿出数据 ,然后格式匹配 配对

.h

#pragma once
#include "Boy.h"
#include "Girl.h"
class Database{
public:
	void init();
	void autoPair();
	void print();
private:
	vector<Boy> boys;
	vector<Girl> girls;

	//用文件中的信息来初始化boys
	void initBoysFromFile();
	//用文件中的信息来初始化girls
	void initGirlFromFile();
	//保存boys的信息到文件中
	void saveBoys();
	//保存girls的信息到文件中
	void saveGirls();
};

 .cpp

#include "Database.h"
#include <fstream>
#include <vector>
#define BOYS_FILE "boys.txt"
#define GIRL_FILE "girls.txt"
using namespace std;
void Database::init()
{
	initBoysFromFile();
	initGirlFromFile();
}

void Database::autoPair(){
	cout << endl << "<=====自动配对结果=====>" << endl;
	string line(100, '-');
	cout << line << endl;
	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].describe() << endl << girls[j].describe() << endl;
				cout << line << endl;
			}
			else {
				cout << "匹配失败!!" << endl;
			}
			cout << endl;
		}
	}
}

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

void Database::initBoysFromFile(){
	//最开始没有文件,设置一个保存方法,
	//用户需要输入基础信息
	ifstream rstream;

	rstream.open(BOYS_FILE);
	if (!rstream.is_open()) {
		//当没有文件就创建文件输入数据
		cout << "====输入基础的用户[男]的数据====" << endl;
		Boy::inputBoys(this->boys);
		saveBoys();
		rstream.close();
		return;
	}
	while (1) {
		string line;
		char name[32];
		int age;
		int salary;
		getline(rstream, line);
		if (rstream.eof())
		{
			break;
		}
	/*
	ret << "姓名:" << name <<"\t\t\t年龄:" << age 
		<<"\t\t\t薪资:" << salary <<"\t\t\t男" << endl;
	*/
	int ret = sscanf_s(line.c_str(),"性别男 年龄:%d 姓名:%s 薪资:%d",
			&age,name, sizeof(name) ,&salary);

	if (ret <= 0) {
		cout << "数据格式失败男" << endl;
		exit(1);
	}
	boys.push_back(Boy(string(name), age, salary));
	}
}

void Database::initGirlFromFile(){
	//最开始没有文件,设置一个保存方法,
	//用户需要输入基础信息
	ifstream rstream;

	rstream.open(GIRL_FILE);
	if (!rstream.is_open()) {
		cout << "====输入基础的用户[女]的数据====" << endl;
		Girl::inputGirls(this->girls);
		saveGirls();
		rstream.close();
        return 0;
	}
	while (1) {
		string line;
		char name[32];
		int age;
		int yanzhi;
		getline(rstream, line);
		if (rstream.eof())
		{
			break;
		}
		/*
		ret << "姓名:" << name << "\t\t\t年龄:" << age
		<< "\t\t\t颜值:"<<setw(3)<<left << yanZhi <<"\t\t\t女" << endl;
		*/
		int ret = sscanf_s(line.c_str(), "性别女 年龄:%d 姓名:%s 颜值:%d",
			 &age, name, sizeof(name), &yanzhi);

		if (ret <= 0) {
			cout << "数据格式失败女" << endl;
			exit(1);
		}
		girls.push_back(Girl(string(name), age, yanzhi));
	}
}

void Database::saveBoys()
{
	ofstream stream;
	stream.open(BOYS_FILE);
	if (!stream.is_open()) {
		cout << BOYS_FILE << "文件打开失败";
		exit(1);
	}
	for (int i = 0; i < boys.size(); i++){
		stream << boys[i].describe();
	}
}

void Database::saveGirls(){
	ofstream stream;
	stream.open(GIRL_FILE);
	if (!stream.is_open()) {
		cout << GIRL_FILE << "文件打开失败";
		exit(1);
	}
	for (int i = 0; i < girls.size(); i++) {
		stream << girls[i].describe();
	}
}


main.cpp

#include "Boy.h"
#include "Girl.h"
#include <iostream>
#include <vector>
#include "Database.h"
using namespace std;

int main(void) {
	Database data;
	data.init();
	data.print();
	data.autoPair();
	system("pause");
	return 0;
}

添加功能实现,单个用户输入实现匹配,并将输入信息添加进文件

.h

static void inputBoy(Boy& boy);

void addOne(Boy& boy);
void addOne(Girl& girl);

 .cpp

void Boy::inputBoy(Boy& boy){
	

	string name;
	int age;
	int salary;
	
	cout << "请输入小哥哥的年龄:";
	cin >> age;
		
	cout << "请输入小哥哥的姓名:";
	cin >> name;
	cout << "请输入小哥哥的薪资:";
	cin >> salary;
	boy = Boy(name, age, salary);
}

data.cpp

void Database::addOne(Boy& boy){
	boys.push_back(boy);
	saveBoys();
	cout << endl << "<=====自动配对结果=====>" << endl;
	string line(100, '-');
	cout << line << endl;
	for (int j = 0; j < girls.size(); j++) {
		if (boy.satisfied(girls[j]) && girls[j].satisfied(boy)) {
			cout << boy.describe() << endl << girls[j].describe() << endl;
			cout << line << endl;
		}
		else {
			cout << "匹配失败!!" << endl;
		}
		cout << endl;
	}
}


void Database::saveBoys()
{
	ofstream stream;
	stream.open(BOYS_FILE,ios::app);
	if (!stream.is_open()) {
		cout << BOYS_FILE << "文件打开失败";
		exit(1);
	}
	for (int i = 0; i < boys.size(); i++){
		stream << boys[i].describe();
	}
}


主函数

#include "Boy.h"
#include "Girl.h"
#include <iostream>
#include <vector>
#include "Database.h"
using namespace std;

int main(void) {
	Database data;
	data.init();
	data.print();
	Boy boy;
	Boy::inputBoy(boy);
	data.addOne(boy);
	data.print();
	Girl girl;
	Girl ::inputGirl(girl);
	data.addOne(girl);
	data.print();
	system("pause");
	return 0;
}

优化:使用运算符重载函数进行最优匹配

 

 .h

database.h
#pragma once
#include "Boy.h"
#include "Girl.h"
class Database{
public:
	void init();
	void autoPair();
	void autoPairBest();
	void print();

	void addOne(Boy& boy);
	void addOne(Girl& girl);
private:
	vector<Boy> boys;
	vector<Girl> girls;

	//用文件中的信息来初始化boys
	void initBoysFromFile();
	//用文件中的信息来初始化girls
	void initGirlFromFile();
	//保存boys的信息到文件中
	void saveBoys();
	//保存girls的信息到文件中
	void saveGirls();
};

gilr.h
#pragma once
#include <iostream>
#include <string>
#include <vector>
#include "Single.h"
using namespace std;
class Boy;
class Girl :public Single {
public:
	~Girl();
	Girl();
	Girl( string name,  int age,  int yanZhi);
	/*string getName()const;
	int getAge() const;*/
	int getYanZhi()const;
	bool satisfied(const Boy& boy) const;
	//使用比较重载运算符计算最优
	bool operator>(const Girl& girl);
	string describe()const;
	static void inputGirls(vector<Girl>& girl);
	static void inputGirl(Girl& girl);
private:
	/*string name;
	int age;*/
	int yanZhi;
	friend ostream &operator<< (ostream & os, const Girl & girl);
};

ostream& operator<< (ostream& os, const Girl& girl);
boy.h
#pragma once
#include <iostream>
#include <string>
#include <vector>
#include "Single.h"
using namespace std;
class Girl;
class Boy :public Single{
public :
	~Boy();
	Boy();
	Boy(string name, int age, int salary);
	Boy(const Boy& other);
	/*string getName() const;
	int getAge() const;*/
	int getSalary() const;
	bool satisfied(const Girl& girl) const;
	string describe() const;
	static void inputBoys(vector<Boy> &boy);
	static void inputBoy(Boy& boy);
	//使用比较运算符重载计算最优
	bool operator>(const Boy& boy);
private:
	/*string name;
	int age;*/
	int salary;
	friend ostream& operator<<(ostream& os, const Boy& boy);
};
ostream& operator<<(ostream& os, const Boy& boy);

.cpp

boy.cpp
#include "Database.h"
#include <fstream>
#include <vector>
#include "Girl.h"
#include "Boy.h"
#define BOYS_FILE "boys.txt"
#define GIRL_FILE "girls.txt"
using namespace std;
void Database::init()
{
	initBoysFromFile();
	initGirlFromFile();
}

void Database::autoPair(){
	cout << endl << "<=====自动配对结果=====>" << endl;
	string line(100, '-');
	cout << line << endl;
	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].describe() << endl << girls[j].describe() << endl;
				cout << line << endl;
			}
			else {
				cout << "匹配失败!!" << endl;
			}
			cout << endl;
		}
	}
}

void Database::autoPairBest()
{
	cout << endl << "<=====自动配对最优结果=====>" << endl;
	string line(100, '-');
	cout << line << endl;
	cout << "=======男方结果=======" << endl;
	for (int  i = 0; i < boys.size(); i++)
	{
		Girl* bestGirl = NULL;
		for (int j = 0; j < girls.size(); j++) {
			if (boys[i].satisfied(girls[j]) && girls[j].satisfied(boys[i])) {
				if (bestGirl == NULL) {
					bestGirl = &girls[j];
				}else if (*bestGirl > girls[j]) {
					bestGirl = &girls[j];
				}
			}
			else {
				cout << "匹配失败" << endl;
			}
		}
		if (bestGirl)
		{
			cout << boys[i];
			cout << *bestGirl << endl;
			cout << line << endl;
		}
	}
	cout << "=======女方结果=======" << endl;
	for (int i = 0; i < girls.size(); i++)
	{
		Boy* bestBoy =NULL;
		for (int j = 0; j < boys.size(); j++) {
			if (girls[i].satisfied(boys[j]) && boys[j].satisfied(girls[i])) {
				if (bestBoy == NULL) {
					bestBoy = &boys[j];
				}
				else if (*bestBoy > boys[j]) {
					bestBoy = &boys[j];
				}
			}
			else {
				cout << "匹配失败" << endl;
			}
		}
		if (bestBoy)
		{
			cout << girls[i];
			cout << *bestBoy << endl;
			cout << line << endl;
		}
	}
}

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



void Database::addOne(Boy& boy){
	boys.push_back(boy);
	saveBoys();
	cout << endl << "<=====自动配对结果=====>" << endl;
	string line(100, '-');
	cout << line << endl;
	for (int j = 0; j < girls.size(); j++) {
		if (boy.satisfied(girls[j]) && girls[j].satisfied(boy)) {
			cout << boy.describe() << endl << girls[j].describe() << endl;
			cout << line << endl;
		}
		else {
			cout << "匹配失败!!" << endl;
		}
		cout << endl;
	}
}

void Database::addOne(Girl& girl){
	girls.push_back(girl);
	saveGirls();
	cout << endl << "<=====自动配对结果=====>" << endl;
	string line(100, '-');
	cout << line << endl;
	for (int j = 0; j < boys.size(); j++) {
		if (girl.satisfied(boys[j]) && boys[j].satisfied(girl)) {
			cout << girl.describe() << endl << boys[j].describe() << endl;
			cout << line << endl;
		}
		else {
			cout << "匹配失败!!" << endl;
		}
		cout << endl;
	}
}

void Database::initBoysFromFile(){
	//最开始没有文件,设置一个保存方法,
	//用户需要输入基础信息
	ifstream rstream;

	rstream.open(BOYS_FILE);
	if (!rstream.is_open()) {
		//当没有文件就创建文件输入数据
		cout << "====输入基础的用户[男]的数据====" << endl;
		Boy::inputBoys(this->boys);
		saveBoys();
		rstream.close();
		//如果当前不存在数据,那么输入完数据后,函数调用就应该返回了。
		return;
	}
	while (1) {
		string line;
		char name[32];
		int age;
		int salary;
		getline(rstream, line);
		if (rstream.eof())
		{
			break;
		}
	/*
	ret << "姓名:" << name <<"\t\t\t年龄:" << age 
		<<"\t\t\t薪资:" << salary <<"\t\t\t男" << endl;
	*/
	int ret = sscanf_s(line.c_str(),"性别男 年龄:%d 姓名:%s 薪资:%d",
			&age,name, sizeof(name) ,&salary);

	if (ret <= 0) {
		cout << "数据格式失败男" << endl;
		exit(1);
	}
	boys.push_back(Boy(string(name), age, salary));
	}
}

void Database::initGirlFromFile(){
	//最开始没有文件,设置一个保存方法,
	//用户需要输入基础信息
	ifstream rstream;

	rstream.open(GIRL_FILE);
	if (!rstream.is_open()) {
		cout << "====输入基础的用户[女]的数据====" << endl;
		Girl::inputGirls(this->girls);
		saveGirls();
		rstream.close();
		return;
	}
	while (1) {
		string line;
		char name[32];
		int age;
		int yanzhi;
		getline(rstream, line);
		if (rstream.eof())
		{
			break;
		}
		/*
		ret << "姓名:" << name << "\t\t\t年龄:" << age
		<< "\t\t\t颜值:"<<setw(3)<<left << yanZhi <<"\t\t\t女" << endl;
		*/
		int ret = sscanf_s(line.c_str(), "性别女 年龄:%d 姓名:%s 颜值:%d",
			 &age, name, sizeof(name), &yanzhi);

		if (ret <= 0) {
			cout << "数据格式失败女" << endl;
			exit(1);
		}
		girls.push_back(Girl(string(name), age, yanzhi));
	}
}

void Database::saveBoys()
{
	ofstream stream;
	stream.open(BOYS_FILE);
	if (!stream.is_open()) {
		cout << BOYS_FILE << "文件打开失败";
		exit(1);
	}
	for (int i = 0; i < boys.size(); i++){
		stream << boys[i].describe();
	}
}

void Database::saveGirls(){
	ofstream stream;
	stream.open(GIRL_FILE);
	if (!stream.is_open()) {
		cout << GIRL_FILE << "文件打开失败";
		exit(1);
	}
	for (int i = 0; i < girls.size(); i++) {
		stream << girls[i].describe();
	}
}
girl.cpp
#include "Girl.h"
#include <sstream>
#include "Boy.h"
#include <iomanip>
//设置一个颜值系数

#define YANZHI_FACTOR 100
Girl::~Girl() {

}

Girl::Girl()
{
}

Girl::Girl( string name,  int age,  int yanZhi):Single(name,age) {
	/*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;
}
bool Girl::satisfied(const Boy& b) const{
	if (b.getSalary() >= yanZhi * YANZHI_FACTOR) {
		return true;
	}
	return false;
}

bool Girl::operator>(const Girl& girl)
{
	if (this->yanZhi<girl.yanZhi)
	{
		return true;
	}
	else {
		return false;
	}
}

string Girl::describe() const {
	stringstream ret;//可以将写入的数据转换成字符串
	//ret << "姓名:" << getName() << "-年龄-" << getAge() << "-颜值-" <<yanZhi<< endl;
	ret <<"性别女" << "\t\t\t年龄:" << age << "\t\t\t姓名:" << name
		<< "\t\t\t颜值:"<<setw(3)<<left << yanZhi<< endl;
	return ret.str();
}

void Girl::inputGirls(vector<Girl>& girl){
	string name;
	int age;
	int yanZhi1;
	int n = 1;
	while (n) {
		cout << "请输入女方第" << n << "个人的年龄[0]退出:";
		cin >> age;
		if (age == 0) {
			break;
		}
		cout << "请输入第" << n << "个人的姓名:";
		cin >> name;
	
		cout << "请输入第" << n << "个人的颜值:";
		cin >> yanZhi1;
		n++;
		girl.push_back(Girl(name, age, yanZhi1));
	}
}

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

	cout << "请输入小姐姐的年龄:";
	cin >> age;
	cout << "请输入小姐姐的姓名:";
	cin >> name;

	cout << "请输入小姐姐的颜值:";
	cin >> yanZhi1;

	girl = Girl(name, age, yanZhi1);
}

ostream& operator<<(ostream& os, const Girl& girl)
{
	os<< "性别女" << "\t\t\t年龄:" << girl.age << "\t\t\t姓名:" << girl.name
		<< "\t\t\t颜值:" << setw(3) << left << girl.yanZhi << endl;
	return os;
}
database.cpp
#include "Database.h"
#include <fstream>
#include <vector>
#include "Girl.h"
#include "Boy.h"
#define BOYS_FILE "boys.txt"
#define GIRL_FILE "girls.txt"
using namespace std;
void Database::init()
{
	initBoysFromFile();
	initGirlFromFile();
}

void Database::autoPair(){
	cout << endl << "<=====自动配对结果=====>" << endl;
	string line(100, '-');
	cout << line << endl;
	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].describe() << endl << girls[j].describe() << endl;
				cout << line << endl;
			}
			else {
				cout << "匹配失败!!" << endl;
			}
			cout << endl;
		}
	}
}

void Database::autoPairBest()
{
	cout << endl << "<=====自动配对最优结果=====>" << endl;
	string line(100, '-');
	cout << line << endl;
	cout << "=======男方结果=======" << endl;
	for (int  i = 0; i < boys.size(); i++)
	{
		Girl* bestGirl = NULL;
		for (int j = 0; j < girls.size(); j++) {
			if (boys[i].satisfied(girls[j]) && girls[j].satisfied(boys[i])) {
				if (bestGirl == NULL) {
					bestGirl = &girls[j];
				}else if (*bestGirl > girls[j]) {
					bestGirl = &girls[j];
				}
			}
			else {
				cout << "匹配失败" << endl;
			}
		}
		if (bestGirl)
		{
			cout << boys[i];
			cout << *bestGirl << endl;
			cout << line << endl;
		}
	}
	cout << "=======女方结果=======" << endl;
	for (int i = 0; i < girls.size(); i++)
	{
		Boy* bestBoy =NULL;
		for (int j = 0; j < boys.size(); j++) {
			if (girls[i].satisfied(boys[j]) && boys[j].satisfied(girls[i])) {
				if (bestBoy == NULL) {
					bestBoy = &boys[j];
				}
				else if (*bestBoy > boys[j]) {
					bestBoy = &boys[j];
				}
			}
			else {
				cout << "匹配失败" << endl;
			}
		}
		if (bestBoy)
		{
			cout << girls[i];
			cout << *bestBoy << endl;
			cout << line << endl;
		}
	}
}

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



void Database::addOne(Boy& boy){
	boys.push_back(boy);
	saveBoys();
	cout << endl << "<=====自动配对结果=====>" << endl;
	string line(100, '-');
	cout << line << endl;
	for (int j = 0; j < girls.size(); j++) {
		if (boy.satisfied(girls[j]) && girls[j].satisfied(boy)) {
			cout << boy.describe() << endl << girls[j].describe() << endl;
			cout << line << endl;
		}
		else {
			cout << "匹配失败!!" << endl;
		}
		cout << endl;
	}
}

void Database::addOne(Girl& girl){
	girls.push_back(girl);
	saveGirls();
	cout << endl << "<=====自动配对结果=====>" << endl;
	string line(100, '-');
	cout << line << endl;
	for (int j = 0; j < boys.size(); j++) {
		if (girl.satisfied(boys[j]) && boys[j].satisfied(girl)) {
			cout << girl.describe() << endl << boys[j].describe() << endl;
			cout << line << endl;
		}
		else {
			cout << "匹配失败!!" << endl;
		}
		cout << endl;
	}
}

void Database::initBoysFromFile(){
	//最开始没有文件,设置一个保存方法,
	//用户需要输入基础信息
	ifstream rstream;

	rstream.open(BOYS_FILE);
	if (!rstream.is_open()) {
		//当没有文件就创建文件输入数据
		cout << "====输入基础的用户[男]的数据====" << endl;
		Boy::inputBoys(this->boys);
		saveBoys();
		rstream.close();
		//如果当前不存在数据,那么输入完数据后,函数调用就应该返回了。
		return;
	}
	while (1) {
		string line;
		char name[32];
		int age;
		int salary;
		getline(rstream, line);
		if (rstream.eof())
		{
			break;
		}
	/*
	ret << "姓名:" << name <<"\t\t\t年龄:" << age 
		<<"\t\t\t薪资:" << salary <<"\t\t\t男" << endl;
	*/
	int ret = sscanf_s(line.c_str(),"性别男 年龄:%d 姓名:%s 薪资:%d",
			&age,name, sizeof(name) ,&salary);

	if (ret <= 0) {
		cout << "数据格式失败男" << endl;
		exit(1);
	}
	boys.push_back(Boy(string(name), age, salary));
	}
}

void Database::initGirlFromFile(){
	//最开始没有文件,设置一个保存方法,
	//用户需要输入基础信息
	ifstream rstream;

	rstream.open(GIRL_FILE);
	if (!rstream.is_open()) {
		cout << "====输入基础的用户[女]的数据====" << endl;
		Girl::inputGirls(this->girls);
		saveGirls();
		rstream.close();
		return;
	}
	while (1) {
		string line;
		char name[32];
		int age;
		int yanzhi;
		getline(rstream, line);
		if (rstream.eof())
		{
			break;
		}
		/*
		ret << "姓名:" << name << "\t\t\t年龄:" << age
		<< "\t\t\t颜值:"<<setw(3)<<left << yanZhi <<"\t\t\t女" << endl;
		*/
		int ret = sscanf_s(line.c_str(), "性别女 年龄:%d 姓名:%s 颜值:%d",
			 &age, name, sizeof(name), &yanzhi);

		if (ret <= 0) {
			cout << "数据格式失败女" << endl;
			exit(1);
		}
		girls.push_back(Girl(string(name), age, yanzhi));
	}
}

void Database::saveBoys()
{
	ofstream stream;
	stream.open(BOYS_FILE);
	if (!stream.is_open()) {
		cout << BOYS_FILE << "文件打开失败";
		exit(1);
	}
	for (int i = 0; i < boys.size(); i++){
		stream << boys[i].describe();
	}
}

void Database::saveGirls(){
	ofstream stream;
	stream.open(GIRL_FILE);
	if (!stream.is_open()) {
		cout << GIRL_FILE << "文件打开失败";
		exit(1);
	}
	for (int i = 0; i < girls.size(); i++) {
		stream << girls[i].describe();
	}
}

main.cpp
#include "Boy.h"
#include "Girl.h"
#include <iostream>
#include <vector>
#include "Database.h"
using namespace std;

int main(void) {
	Database data;
	data.init();
	data.print();
	int num = 0;
	cout << "请输入[非零数进行:添加]:" << endl;
	cin >> num;
	bool flag = num ? true : false;
	if (flag) {
		Boy boy;
		Boy::inputBoy(boy);
		data.addOne(boy);
		data.print();
		Girl girl;
		Girl::inputGirl(girl);
		data.addOne(girl);
		data.print();
	}
	data.autoPairBest();
	system("pause");
	return 0;
}

  • 22
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 《C面向对象程序设计第三版答案》是由谭浩强编写的一本与C语言相关的教材辅导答案。C面向对象程序设计是计算机科学中的一门重要课程,谭浩强作为资深教授和编程专家,他撰写的书籍在编程领域拥有很高的权威性。 这本教材答案为学习者提供了对应教材《C面向对象程序设计第三版》的习题答案和思考指导。习题是帮助学生巩固所学知识和提升编程能力的重要方式,通过对答案的学习,学生可以更好地理解并运用相关知识。学习者可以通过对比答案,分析解题思路、吸收优秀的编程风格和技巧,从而提高编程水平。 《C面向对象程序设计第三版答案》按照教材的章节顺序,详细解答了各个章节的习题,包括程序设计题、思考题、应用题等,涵盖了从基础的语法使用到复杂的程序设计技巧,旨在帮助学生全面理解并掌握C语言面向对象编程思想和方法。 除了提供答案,这本教材还包括了一些习题的思考指导,指导学生如何分析问题、拆解问题、确定解决步骤等。这些思考指导帮助学生培养编程思维和解决问题的能力,使他们能够独立思考和解决实际编程任务。 总之,《C面向对象程序设计第三版答案》是一本为学习C语言面向对象程序设计的学生提供的辅助资料,它不仅提供了习题答案,还包括了思考指导,帮助学生提高编程水平和解决实际问题的能力。 ### 回答2: 《C++面向对象程序设计(第3版)》是计算机科学与技术专业学生的主要教材之一,由谭浩强编写。这本书全面介绍了C++编程语言面向对象编程思想和相关的概念、原则与技巧。 该教材内容分为15章,首先从C++的基本概念和语法入手,然后逐渐介绍了面向对象编程的思想和实现。每章的结尾都提供了习题和答案,帮助学生巩固所学知识。 《C++面向对象程序设计(第3版)》的答案是谭浩强根据书中习题所提供的参考答案。这些答案精确明确,清晰易懂,配有详细的解释和示范代码。通过阅读和理解这些答案,学生可以加深对所学知识的理解,提高自己的编程技能。 同时,这本书还提供了大量的示例代码和实践案例,帮助学生将理论知识应用于实际的编程项目中。通过实践,学生可以更好地理解面向对象编程的思想和方法,并培养自己的分析和解决问题的能力。 总之,《C++面向对象程序设计(第3版)》是一本权威性、系统性和实用性并存的教材。通过学习这本书,学生可以全面掌握C++编程语言面向对象编程的相关知识,提高自己的编程能力,并为将来的实际工作打下坚实的基础。 ### 回答3: 《C++面向对象程序设计》(第三版)是谭浩强所著的一本教材,该教材主要介绍了C++面向对象程序设计的基本概念、语法和技巧。全书共分为10个章节,涵盖了面向对象程序设计的各个方面。 第一章介绍了C++的发展历程以及面向对象程序设计的基本概念和特点。第二章详细讲解了C++的基本语法和常用数据类型。第三章重点介绍了C++中的类和对象的概念,并通过具体的示例演示了如何定义和使用类。 第四章讲解了C++的继承和派生,介绍了单继承和多继承的概念以及如何定义和使用派生类。第五章介绍了C++中的多态性,包括静态多态和动态多态的概念以及如何使用虚函数实现动态绑定。 第六章讲解了C++中的运算符重载和类型转换,通过实例说明了如何重载运算符和类型转换函数。第七章介绍了C++中的异常处理机制,讲解了异常的概念和处理方法。 第八章讲解了C++中的文件操作,包括输入输出流、文件读写以及文件指针的相关知识。第九章介绍了C++的模板和泛型编程,包括函数模板和类模板的定义和使用。 第十章介绍了C++中的标准模板库(STL),包括容器、迭代器、算法和函数对象等的使用方法。 《C++面向对象程序设计》(第三版)通过简明扼要的语言和生动具体的示例,全面而深入地介绍了C++面向对象程序设计的基本概念和技巧,适合初学者学习和参考。同时,该教材也提供了丰富的练习题和案例,供读者巩固和应用所学知识。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值