相亲系统(文件版)

分析;
需要定义两个 类型男孩类(姓名,年龄,薪水)和女孩类(姓名,年龄,薪水)这里为了方便用户实现就用来这三个数据,其中两个数据相同,所以可以先定义一个父类
父类的头文件;Single.h

#pragma once
#include<iostream>
#include<string>
using namespace std;
class Single
{
public:
 Single(string name,int age);
 int  getage();
 string getname();
protected:
 string name;
 int age;
};

源文件:

#include "Single.h"
Single::Single(string name, int age)
{
 this->name = name;
 this->age = age;
}
int Single::getage()
{
 return age;
}
string Single::getname()
{
 return name;
}

再分别定义男孩类和女孩类
其中的功能包括:介绍自己,与女孩进行匹配,输入男孩信息
Boy.h

#pragma once
#include"Single.h"
#include"Girl.h"
#include<vector>
class Girl;
class Boy:public Single
{
public:
 Boy(string name,int age,int salary);
 int getsalary();
 string discaption();
 bool satisfty(Girl&girl)const;
 static void input(vector<Boy>&boys);
private:
 int salary;
};

源文件:

#include "Boy.h"
#include<sstream>
#define SALARY_PER 0.006
Boy::Boy(string name, int age, int salary)
 :Single(name,age)
{
 this->salary = salary;
}
int Boy::getsalary()
{
 return salary;
}
string Boy::discaption()
{
 stringstream ret;
 ret << "姓名:" << name
  << "\t\t年龄:" << age
  << "\t\t薪水:" << salary;
 return ret.str();
}
bool Boy::satisfty(Girl& girl) const
{
 if ((salary * SALARY_PER) > girl.getfacescore()) {
  return true;
 }
 return false;
}
void Boy::input(vector<Boy>& boys)
{
 int age;
 string name;
 int salary;
 int n = 1;
 while (1) {
  cout << "输入0退出存贮" << endl;
  cout << "请输入第" << n << "位男生的年龄:";
  cin >> age;
  if (age == 0)
   break;
  cout << "请输入姓名:";
  cin >> name;
  cout << "请输入薪水:";
  cin >> salary;
  boys.push_back(Boy(name,age,salary));
 }
}

女孩类的也相似:
Girl.h:

#pragma once
#include"Single.h"
#include"Boy.h"
#include<vector>
class Boy;
class Girl:public Single
{
public:
 Girl(string name,int age,int facescore);
 int getfacescore();
 string discaption();
 bool satisfty(Boy& boy);
 static void input(vector<Girl>& girls);
private:
 int facescore;
};

源文件:

#include "Girl.h"
#include<sstream>
#include<iostream>
#define FACE_PER 100
Girl::Girl(string name, int age, int facescore)
 :Single(name,age)
{
 this->facescore = facescore;
}
int Girl::getfacescore()
{
 return facescore;
}
string Girl::discaption()
{
 stringstream ret;
 ret << "姓名:" << name 
  << "\t\t年龄:" << age
  << "\t\t颜值:" << facescore;
 return ret.str();
}
bool Girl::satisfty(Boy& boy)
{
 if ((facescore * FACE_PER) < boy.getsalary()) {
  return true;
 }
 return false;
}
void Girl::input(vector<Girl>& girls)
{
 int age;
 string name;
 int facescore;
 int n = 1;
 while (1) {
  cout << "输入0退出存贮" << endl;
  cout << "请输入第" << n << "位女生的年龄:";
  cin >> age;
  if (age == 0)
   break;
  cout << "请输入姓名:";
  cin >> name;
  cout << "请输入颜值:";
  cin >> facescore;
  girls.push_back(Girl(name, age, facescore));
 }
}

再定义一个数据库,
功能:
1用来将数据放入到文件,或者将文件中的数据放到容器中;
2打印男孩女孩的信息
3进行配对
Datebase.h

#pragma once
#include<vector>
#include"Boy.h"
#include"Girl.h"
class Datebase
{
public:
 Datebase();
 void init();//初始化, 从文件中读取数据信息, 来初始化用户数据
 void atuopair();//自动配对
 void print();// 打印该数据库中的所有用户信息
private:
 vector<Boy>boys;//所有的单身男信息
 vector<Girl>girls;//所有单生女信息
 void initboys();//初始化男生信息
 void initgirls();//初始化女生信息
 void saveboy();  //将男生的数据放到文件中
 void savegirl();//将女生的数据放到文件中
};

源文件:

#include "Datebase.h"
#include<fstream>
#include<stdio.h>
#include<iostream>
#include<vector>
#include<sstream>
#define BOY_FILE "男生.txt"
#define GRIL_FILE  "女生.txt"
Datebase::Datebase()
{
}
void Datebase::init()
//初始化, 从文件中读取数据信息, 来初始化用户数据
{
 initboys();//初始化男生信息
 initgirls();//初始化女生信息
}
void Datebase::atuopair()
{
 string line(100, '-');
 cout << "配对成功:" << endl;
 for (int i = 0;i < girls.size();i++) {
  for (int j = 0; j < boys.size();j++) {
   if (girls[i].satisfty(boys[j]) &&
    boys[j].satisfty(girls[i])) {
    cout << boys[j].discaption() << endl;
    cout << girls[i].discaption() << endl;
    cout << line << endl;
   }
  }
 }
}
void Datebase::print()
{
 cout << "男嘉宾的信息:" << endl;
 for (int i = 0;i < boys.size();i++) {
  cout << boys[i].discaption() << endl;
 }
 cout <<endl<< "女嘉宾的信息:" << endl;
 for (int i = 0;i < girls.size();i++) {
  cout << girls[i].discaption() << endl;
 }
}
void Datebase::initboys() {
 ifstream input;
 input.open(BOY_FILE);
 if (!input.is_open()) {
  //让用户输入数据
  cout << "没有初始化 请输入男嘉宾的信息:";
  Boy::input(this->boys);
  saveboy();
  input.close();
  return;
 }
 while (1) {
  char name[64];
  int age;
  int salary;
  string line;
  getline(input, line);
  if (input.eof()) {
   break;
  }
  int 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 (string(name),age,salary));
 }
}
void Datebase::initgirls()
{
 ifstream input;
 input.open(GRIL_FILE);
 if (!input.is_open()) {
  //让用户输入数据
  cout << "没有初始化 请输入女嘉宾的信息:";
  Girl::input(this->girls);
  savegirl();
  input.close();
  return;
 }
 while (1) {
  char name[64];
  int age;
  int facescore;
  string line;
  getline(input, line);
  if (input.eof()) {
   break;
  }
  int ret = sscanf_s(line.c_str(), "姓名:%s 年龄:%d 颜值:%d", name, sizeof(name), &age, &facescore);
  if (ret <= 0) {
   cout << "读取失败" << endl;
   exit(1);
  }
  girls.push_back(Girl(string(name), age, facescore));
 }
}
void Datebase::saveboy()
{
 ofstream stream;
 stream.open(BOY_FILE);
 if (!stream.is_open()) {
  cout << "您输入的文件打卡失败!" << endl;
 }
 for (int i = 0;i < boys.size();i++) {
  //ret << "姓名:" << name
  //<< "\t\t年龄:" << age
   //<< "\t\t薪水:" << salary;
  stream << boys[i].discaption() << endl;
 }
 stream.close();
}
void Datebase::savegirl()
{
 ofstream stream;
 stream.open(GRIL_FILE);
 if (!stream.is_open()) {
  cout << "您输入的文件打卡失败!" << endl;
 }
 for (int i = 0;i < girls.size();i++) {
  //ret << "姓名:" << name
  //<< "\t\t年龄:" << age
   //<< "\t\t颜值:" << salary;
  stream << girls[i].discaption() << endl;
 }
 stream.close();
}

功能展示:
在这里插入图片描述
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值