项目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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 Java 语言定义一个类,实现一个运动会竞赛日程的编制。 具体内容如下: 1. 定义数据结构(类)并实现相关算法(类成员):n 个运动员(Player), m 个项目(Item),每人最多可参加 k 个项目,每人每个项目用时为 t, 请安排 m 个项目的比赛时间(包括开始时间、预计结束时间)和每位 运动员的参赛时间表,要求整个赛事时间越短越好。 2. 编写主程序,并以给定参数和数据运行程序:设 n=50,m=20,k=3,t =10 分钟,竞赛时间为 9:00-21:00,中间不休息。运动员参赛情况如下:项目1有运动员01,02,05,06,07,32,34,38,40,42,45,46号参加,项目2有运动员03,08,10,19,21,33,35,39,41,43,44,47号参加,项目3有运动员01,04,11,18,20,23,25,32,36,48号参加,项目4有运动员03,12,13,16,22,24,27,29,33,37,38,47,49号参加,项目5有运动员02,05,07,09,26,28,30,46,50号参加,项目6有运动员17,34,39,40,41,42,45号参加,项目7有运动员06,14,31,35,44号参加,项目8有运动员10,13,15,47号参加,项目9有运动员01,05,11,16,18,20,28,43号参加,项目10有运动员04,08,21,32,37,38号参加,项目11有运动员02,12,25,29,31,43号参加,项目12有运动员06,14,17,19,35,36,48号参加,项目13有运动员04,15,18,24,27,31,33,41,42,45,46号参加,项目14有运动员07,13,23,26,28,30,37,44号参加,项目15有运动员03,09,10,14,20,21,22,34,36,50号参加,项目16有运动员08,40,49号参加,项目17有运动员30,49号参加,项目18有运动员09,27,39号参加,项目19有运动员15,17,26,48号参加,项目20有运动员16,19,22,23,24,25号参加。3. 运行结果以文件方式输出: 文件 1:命名为 Item.txt,包括每个项目的开始和结束时间;运动会 比赛总时长(xx 小时 xx 分钟); 文件 2:命名为 Players.txt,包括每位运动员的参赛时间表;
05-29
以下是使用Java语言定义一个类,实现一个运动会竞赛日程的编制的代码示例: ```java import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; public class SportsMeetingSchedule { private int n; // 运动员数量 private int m; // 项目数量 private int k; // 每人最多可参加的项目数量 private int t; // 每人每个项目用时(单位:分钟) private Date startTime; // 赛事开始时间 private Date endTime; // 赛事结束时间 private List<Player> players; // 运动员列表 private List<Item> items; // 项目列表 public SportsMeetingSchedule(int n, int m, int k, int t, Date startTime, Date endTime, List<Player> players, List<Item> items) { this.n = n; this.m = m; this.k = k; this.t = t; this.startTime = startTime; this.endTime = endTime; this.players = players; this.items = items; } // 计算比赛总时长 private int getTotalTime() { int totalTime = 0; for (Item item : items) { totalTime += item.getEndTime().getTime() - item.getStartTime().getTime(); } return totalTime / (1000 * 60); } // 生成项目的开始和结束时间 private void generateItemSchedule() { // 计算每个项目需要的时间 int timePerItem = m > 0 ? (endTime.getTime() - startTime.getTime()) / m : 0; Date startTimeOfItem = startTime; for (Item item : items) { Date endTimeOfItem = new Date(startTimeOfItem.getTime() + timePerItem); item.setStartTime(startTimeOfItem); item.setEndTime(endTimeOfItem); startTimeOfItem = endTimeOfItem; } } // 生成运动员的参赛时间表 private void generatePlayerSchedule() { for (Player player : players) { List<Item> itemsCopy = new ArrayList<>(items); Collections.shuffle(itemsCopy); // 随机打乱项目列表 int count = 0; for (Item item : itemsCopy) { if (count >= k) { break; // 超过每人最多可参加的项目数量,退出循环 } if (item.getRemainingCapacity() > 0) { // 该项目还有剩余名额 item.addPlayer(player); item.setRemainingCapacity(item.getRemainingCapacity() - 1); count++; } } } } // 输出项目的开始和结束时间到文件 private void outputItemScheduleToFile(String fileName) throws IOException { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); PrintWriter writer = new PrintWriter(new FileWriter(fileName)); for (Item item : items) { writer.println("项目" + item.getId() + ":" + dateFormat.format(item.getStartTime()) + " - " + dateFormat.format(item.getEndTime())); } writer.println("比赛总时长:" + getTotalTime() + "分钟"); writer.close(); } // 输出运动员的参赛时间表到文件 private void outputPlayerScheduleToFile(String fileName) throws IOException { PrintWriter writer = new PrintWriter(new FileWriter(fileName)); for (Player player : players) { writer.println("运动员" + player.getId() + "的参赛时间表:"); for (Item item : player.getItems()) { writer.println("项目" + item.getId() + ":" + item.getStartTime() + " - " + item.getEndTime()); } } writer.close(); } // 运行程序 public void run() throws IOException { generateItemSchedule(); generatePlayerSchedule(); outputItemScheduleToFile("Item.txt"); outputPlayerScheduleToFile("Players.txt"); } public static void main(String[] args) throws IOException { int n = 50; int m = 20; int k = 3; int t = 10; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date startTime = dateFormat.parse("2022-01-01 09:00:00"); Date endTime = dateFormat.parse("2022-01-01 21:00:00"); List<Player> players = new ArrayList<>(); for (int i = 1; i <= n; i++) { players.add(new Player(i, k, t)); } List<Item> items = new ArrayList<>(); items.add(new Item(1, 12)); items.add(new Item(2, 12)); items.add(new Item(3, 10)); items.add(new Item(4, 13)); items.add(new Item(5, 8)); items.add(new Item(6, 7)); items.add(new Item(7, 5)); items.add(new Item(8, 3)); items.add(new Item(9, 8)); items.add(new Item(10, 6)); items.add(new Item(11, 6)); items.add(new Item(12, 7)); items.add(new Item(13, 11)); items.add(new Item(14, 7)); items.add(new Item(15, 10)); items.add(new Item(16, 3)); items.add(new Item(17, 2)); items.add(new Item(18, 3)); items.add(new Item(19, 4)); items.add(new Item(20, 6)); SportsMeetingSchedule schedule = new SportsMeetingSchedule(n, m, k, t, startTime, endTime, players, items); schedule.run(); } } class Player { private int id; private int maxItemCount; private int timePerItem; private List<Item> items; public Player(int id, int maxItemCount, int timePerItem) { this.id = id; this.maxItemCount = maxItemCount; this.timePerItem = timePerItem; this.items = new ArrayList<>(); } public int getId() { return id; } public List<Item> getItems() { return items; } public void addItem(Item item) { items.add(item); } public int getMaxItemCount() { return maxItemCount; } public int getTimePerItem() { return timePerItem; } } class Item { private int id; private int capacity; private int remainingCapacity; private int time; private Date startTime; private Date endTime; private List<Player> players; public Item(int id, int capacity) { this.id = id; this.capacity = capacity; this.remainingCapacity = capacity; this.time = 0; this.players = new ArrayList<>(); } public int getId() { return id; } public int getCapacity() { return capacity; } public int getRemainingCapacity() { return remainingCapacity; } public void setRemainingCapacity(int remainingCapacity) { this.remainingCapacity = remainingCapacity; } public int getTime() { return time; } public void setTime(int time) { this.time = time; } public Date getStartTime() { return startTime; } public void setStartTime(Date startTime) { this.startTime = startTime; } public Date getEndTime() { return endTime; } public void setEndTime(Date endTime) { this.endTime = endTime; } public List<Player> getPlayers() { return players; } public void addPlayer(Player player) { players.add(player); player.addItem(this); setTime(Math.max(getTime(), player.getTimePerItem())); } } ``` 运行程序后,会生成两个文件:Item.txt和Players.txt。其中,Item.txt包括每个项目的开始和结束时间以及比赛总时长;Players.txt包括每位运动员的参赛时间表。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值