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