项目12

需求: 要使用文件来保存用户信息
分析: 设计一个类, 来实现信息的保存功能
Database 数据库

代码实现:

1. single父类
头文件

#pragma once
#include <string>

using namespace std;

class Single
{
public:
	Single();
	Single(int age, const string &name);
	int getAge() const;
	string getName() const;

private:
	int age;
	string name;
};

实现方法

#include "Single.h"

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

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

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

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

2. Boy类
头文件

#pragma once
#include <vector>
#include "Single.h"

class Girl;

class Boy : public Single
{
public:
	Boy();
	Boy(int age, string name, int salary);
	int getSalry() const;
	bool satisfied(const Girl& girl) const;
	string Description() const;
	static void inputBoys(vector <Boy> &boys);	//输入多个男孩的信息
private:
	int salary;
};

实现方法

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

#define SALARY_RATIO 0.003	//薪资系数

Boy::Boy() {
	salary = 0;
}

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

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 << getName() << "的年龄:" << getAge() << " 薪资:" << 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++;
	}
}

3. Girl类
头文件

#pragma once
#include <vector>
#include "Single.h"

class Boy;

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

实现方法

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

#define YANZHI_RATIO 300	//颜值系数

Girl::Girl() {
	yanZhi = 0;
}

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

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 << getName() << "的年龄:" << getAge() << " 颜值:" << 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++;
	}
}

4. Database类
头文件

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

/*
	设计文件类来存储用户数据
	功能:
		init() 初始化数据,读取文件中的数据
		autoMatch() 自动匹配男生和女生
		print() 打印所有用户的信息

	数据:
		vector<Boy> boys 男生信息
		vector<Girl> girls 女生信息
*/

class DataBase
{
public:
	DataBase();

	void init();
	void autoMatch();
	void print();
	void inputBoy(Boy boy);
	void inputGirl(Girl girl);

private:
	vector<Boy> boys;
	vector<Girl> girls;
	void loadBoyFile();
	void loadGirlFile();
	void writeBoyFile();
	void writeGirlFile();
};

实现

#include <fstream>
#include <iostream>
#include "DataBase.h"

DataBase::DataBase()
{
}

void DataBase::init()
{
	loadBoyFile();
	loadGirlFile();
}

void DataBase::autoMatch()
{
	cout << "匹配成功" << endl;
	string line = string(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].getName()  << endl;
				cout << girls[j].getName() << endl;
				cout << line << endl;
			}
		}
	}
}

void DataBase::print()
{
	cout << endl;
	cout << "男生的信息" << endl;
	for (auto i = boys.begin(); i != boys.end(); i++) {
		cout << (*i).Description() << endl;
	}
	cout << endl;
	cout << "女生的信息" << endl;
	for (auto j = girls.begin(); j != girls.end(); j++) {
		cout << (*j).Description() << endl;
	}
}

void DataBase::inputBoy(Boy boy)
{
	boys.push_back(boy);
	cout << "匹配成功" << endl;
	string line = string(100, '-');
	cout << line << endl;

	for (int j = 0; j < girls.size(); j++) {
		if (boy.satisfied(girls[j]) && girls[j].satisfied(boy)) {
			cout << boy.getName() << endl;
			cout << girls[j].getName() << endl;
			cout << line << endl;
		}
	}
	
}

void DataBase::inputGirl(Girl girl)
{
	girls.push_back(girl);
	cout << "匹配成功" << endl;
	string line = string(100, '-');
	cout << line << endl;

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

void DataBase::loadBoyFile()
{
	ifstream stream;
	
	stream.open("boy.txt");

	if (!stream.is_open()) {
		cout << "文件打开失败,请输入基础数据" << endl;
		writeBoyFile();
		stream.close();
		return;
	}
	while (1)
	{
		string line;
		char name[32]="";
		int age;
		int salary;

		getline(stream, line);
		if (stream.eof()) {
			break;
		}
		auto ret = sscanf_s(line.c_str(), "性别:男 姓名:%s 年龄:%d 薪资:%d", name, sizeof(name), &age, &salary);
		if (ret<=0) {
			cout << "数据格式错误,匹配失败" << endl;
			exit(1);
		}
		boys.push_back(Boy(age, string(name), salary));
	}
	stream.close();
}

void DataBase::loadGirlFile()
{
	ifstream stream;	
	stream.open("girl.txt");

	if (!stream.is_open()) {
		cout << "文件打开失败,请输入基础数据" << endl;
		writeGirlFile();
		return;
	}
	while (1)
	{
		string line;
		char name[32];
		int age;
		int yanZhi;

		getline(stream, line);
		if (stream.eof()) {
			break;
		}
		auto ret = sscanf_s(line.c_str(), "性别:女 姓名:%s 年龄:%d 颜值:%d", name, sizeof(name), &age, &yanZhi);

		if (ret<=0) {
			cout << "数据格式错误,匹配失败" << endl;
			exit(1);
		}
		girls.push_back(Girl(age,string(name), yanZhi));
	}
	stream.close();
}

void DataBase::writeBoyFile()
{
	ofstream stream;

	Boy::inputBoys(boys);
	stream.open("boy.txt");
	if (!stream.is_open()) {
		cout << "文件写入失败" << endl;
		exit(1);
	}
	
	for (auto boy = boys.begin(); boy != boys.end(); boy++) {
		stream << (*boy).Description() << endl;		//原未写endl 文件中输入内容全部显示为一行
	}
	stream.close();
}

void DataBase::writeGirlFile()
{
	ofstream stream;

	Girl::iputGirls(girls);
	stream.open("girl.txt");
	if (!stream.is_open()) {
		cout << "文件写入失败" << endl;
		exit(1);
	}

	for (auto girl = girls.begin(); girl != girls.end(); girl++) {
		stream << (*girl).Description() << endl;
	}
	stream.close();
}

5. 调用

#include <vector>
#include <iostream>
#include "DataBase.h"

int main() {
	DataBase data;
	data.init();
//	data.autoMatch();

	Boy boy;
	boy.inputBoy(boy);
	data.inputBoy(boy);

	Girl girl;
	girl.inputGirl(girl);
	data.inputGirl(girl);

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值