婚恋交友项目
boy.h:
#pragma once
#include <string>
#include <vector>
#include "Single.h"
using namespace std;
class Girl;
class Boy : public Single
{
public:
Boy();
Boy(int age, string name, int salary);
~Boy();
Boy(const Boy& other);
//int getAge()const;
//string getName()const;
int getSalary()const;
bool satisfied(const Girl& s) const;
string description() const;
static void inputBoys(vector<Boy>& boys);
static void inputBoy(Boy& boy);
private:
int salary;
};
boy.cpp
#include "Boy.h"
#include "Girl.h"
#include <iostream>
#include <sstream>
#define SALARY_FACTOR 0.006
Boy::Boy() {
}
Boy::Boy(const Boy& other) :Single(other.getName(), other.getAge()) {
salary = other.salary;
//name = other.name;
//age = other.age;
}
Boy::Boy(int age, string name, int salary) :Single(name, age) {
this->salary = salary;
}
Boy::~Boy() {
}
/*
int Boy::getAge() const {
return age;
}
string Boy::getName() const {
return name;
}
*/
int Boy::getSalary() const {
return salary;
}
bool Boy::satisfied(const Girl& s) const {
if (s.getYanZhi() >= salary * SALARY_FACTOR) {
return true;
}
else {
return false;
}
}
string Boy::description()const {
stringstream ret;
//ret << name << "-男-薪资(" << salary << ")-年龄(" << age << ")";
ret << "性别:男"
<<"\t\t\t姓名:" << getName()
<< "\t\t\t薪资:" << getSalary()
<< "\t\t\t年龄:" << getAge();
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;
n++;
boys.push_back(Boy(age, name, salary));
}
}
void Boy::inputBoy(Boy& boy)
{
int age;
string name;
int salary;
cout << "请输入小哥哥的姓名:";
cin >> name;
cout << "请输入小哥哥的年龄:";
cin >> age;
cout << "请输入小哥哥的薪资:";
cin >> salary;
boy = Boy(age, name, salary);
}
girl.h
#pragma once
#include <string>
#include <vector>
#include "Single.h"
using namespace std;
class Boy;
class Girl : public Single
{
public:
Girl();
Girl(int age, string name, int yanZhi);
~Girl();
//int getAge()const;
//string getName()const;
int getYanZhi()const;
bool satisfied(const Boy& s) const;
string description()const;
static void inputGirls(vector<Girl>& girls);
static void inputGirl(Girl& girl);
private:
int yanZhi;
};
girl.cpp
#include "Girl.h"
#include "Boy.h"
#include <sstream>
#include <iostream>
#include <iomanip>
#define YANZHI_FACTOR 100
Girl::Girl() {
}
Girl::Girl(int age, string name, int yanZhi) :Single(name, age) {
this->yanZhi = yanZhi;
}
Girl::~Girl() {
}
/*
int Girl::getAge() const {
return age;
}
string Girl::getName() const {
return name;
}
*/
int Girl::getYanZhi() const {
return yanZhi;
}
bool Girl::satisfied(const Boy& s) const {
if (s.getSalary() >= yanZhi * YANZHI_FACTOR) {
return true;
}
else {
return false;
}
}
string Girl::description() const {
stringstream ret;
//ret << name << "-女-颜值(" << yanZhi << ")-年龄(" << age << ")";
ret << "性别:女"
<< "\t\t\t姓名:" << getName()
<< "\t\t\t颜值:" << setw(3) << setiosflags(ios::left) << getYanZhi()
<< "\t\t\t年龄:" << getAge();
return ret.str();
}
void Girl::inputGirls(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;
n++;
girls.push_back(Girl(age, name, yanZhi));
}
}
void Girl::inputGirl(Girl& girl)
{
int age;
string name;
int yanZhi;
cout << "请输入小姐姐的姓名:";
cin >> name;
cout << "请输入小姐姐的颜值:";
cin >> yanZhi;
cout << "请输入小姐姐的年龄:";
cin >> age;
girl = Girl(age, name, yanZhi);
}
Database.h
#pragma once
#include <vector>
#include "Boy.h"
#include "Girl.h"
#include <string>
using namespace std;
class Database
{
public:
Database();
void init();
void print();
void autoPair();
void addOne(Boy& boy);
void addOne(Girl& girl);
private:
void initBoysFormFile();
void initGirlsFormFile();
void savaBoys();
void savaGirls();
vector<Boy> boys;
vector<Girl> girls;
};
Database.cpp
#include "Database.h"
#include <fstream>
#include <iostream>
#include <iomanip>
#define GIRLS_FILE "girls.txt"
#define BOYS_FILE "boys.txt"
using namespace std;
Database::Database()
{
}
void Database::savaBoys()
{
ofstream stream;
stream.open(BOYS_FILE);
if (!stream.is_open())
{
cout << BOYS_FILE << "写入失败" << endl;
exit(1);
}
for (int i = 0; i < boys.size(); i++)
{
stream << boys[i].description() << endl;
}
stream.close();
}
void Database::savaGirls()
{
ofstream stream;
stream.open(GIRLS_FILE);
if (!stream.is_open())
{
cout << BOYS_FILE << "写入失败" << endl;
exit(1);
}
for (int i = 0; i < girls.size(); i++)
{
stream << girls[i].description() << endl;
}
stream.close();
}
void Database::initBoysFormFile()
{
ifstream stream;
stream.open(BOYS_FILE);
if (!stream.is_open())
{
cout << "---输入基础用户【男嘉宾】数据---" << endl;
Boy::inputBoys(this->boys);
savaBoys();
stream.close();
return;
}
while (1)
{
string line;
int salary;
int age;
char name[64]="";
getline(stream, line);
if (stream.eof())
{
break;
}
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(age, (string)name, salary));
}
}
void Database::initGirlsFormFile()
{
ifstream stream;
stream.open(GIRLS_FILE);
if (!stream.is_open())
{
cout << "---输入基础用户【女嘉宾】数据---" << endl;
Girl::inputGirls(this->girls);
savaGirls();
stream.close();
return;
}
while (1)
{
string line;
char name[64]="";
int age;
int yanZhi;
getline(stream, line);
if (stream.eof())
{
break;
}
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(age, (string)name, yanZhi));
}
}
void Database::init()
{
initBoysFormFile();
initGirlsFormFile();
}
void Database::print()
{
cout << "男嘉宾信息:\n";
for (int i = 0; i < boys.size(); i++)
{
cout << boys[i].description() << endl;
}
cout << "女嘉宾信息:\n";
for (int i = 0; i < girls.size(); i++)
{
cout << girls[i].description() << endl;
}
}
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].description() << endl;
cout << girls[j].description() << endl;
cout << line << endl;
}
}
}
}
void Database::addOne(Boy& boy)
{
boys.push_back(boy);
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.description() << endl;
cout<<girls[j].description() << endl;
cout << line << endl;
}
}
}
void Database::addOne(Girl& girl)
{
girls.push_back(girl);
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.description() << endl;
cout << boys[j].description() << endl;
cout << line << endl;
}
}
}
single.h
#pragma once
#include <string>
using namespace std;
class Single
{
public:
Single();
Single(string name, int age);
~Single();
int getAge()const;
string getName()const;
private:
int age;
string name;
};
single.cpp
#include "Single.h"
Single::Single()
{
this->name = "无名";
this->age = 0;
}
Single::Single(string name, int age)
{
this->name = name;
this->age = age;
}
Single::~Single()
{
}
int Single::getAge() const {
return age;
}
string Single::getName() const {
return name;
}
main.cpp
#include <stdio.h>
#include <iostream>
#include <Windows.h>
#include <string>
#include <vector>
#include "Boy.h"
#include "Girl.h"
#include <vector>
#include "Database.h"
int main(void) {
Database data;
data.init();
Boy boy;
Boy::inputBoy(boy);
data.addOne(boy);
Girl girl;
Girl::inputGirl(girl);
data.addOne(girl);
system("pause");
return 0;
}
posted on 2022-09-13 20:12 会飞的鱼-blog 阅读(390) 评论(0) 编辑 收藏 举报 来源
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现