2019.3.26 使用简单的类知识改进的书籍数据计算程序

头文件Book.h:

#pragma once
#include 

struct ymd	//用于表示年月日的结构
{
	int year;
	int month;
	int day;
};

class Book		//书籍类
{
private:
	std::string name;
	long nword;
	double time;
	long rspeed;
	ymd start;
	ymd end;
	int rating;
	std::string type;
	void set_speed() { rspeed = nword / (time * 60); }
public:
	Book(const std::string & na,long wo=0,double ti=0,int ra=0);
	Book();
	void get_info(long wo, double ti, int ra);
	void get_ymd(int a);
	void get_type(std::string ty);
	void show();
	void reset();
	~Book();
};

Book.cpp:

#include "Book.h"
#include 

Book::Book(const std::string & na, long wo, double ti, int ra)	//类的构造函数
{
	name = na;
	if (wo < 0) {
		std::cerr << "长度不合法。" << name << "的长度设置为0。\n";
		nword = 0;
	}
	else
	{
		nword = wo;
	}
	if (ti < 0) {
		std::cerr << "长度不合法。" << name << "的阅读时间设置为0。\n";
		time = 0;
	}
	else
	{
		time = ti;
	}
	if (ra< 0) {
		std::cerr << "长度不合法。" << name << "的评分设置为0。\n";
		rating = 0;
	}
	else
	{
		rating = ra;
	}
	set_speed();
}

Book::Book()		//默认构造函数
{
	name = "untitled";
	nword = 0;
	time = 0;
	rating = 0;
	start = { 0,0,0 };
	end = { 0,0,0 };
	type = "unknown";
	set_speed();
}

void Book::get_info(long wo, double ti, int ra)		//输入字数时间评分的函数
{
	if (wo < 0) {
		std::cerr << "长度不合法。" << name << "的长度设置为0。\n";
		nword = 0;
	}
	else
	{
		nword = wo;
	}
	if (ti < 0) {
		std::cerr << "长度不合法。" << name << "的阅读时间设置为0。\n";
		time = 0;
	}
	else
	{
		time = ti;
	}
	if (ra< 0) {
		std::cerr << "长度不合法。" << name << "的评分设置为0。\n";
		rating = 0;
	}
	else
	{
		rating = ra;
	}
	set_speed();
}

void Book::get_ymd(int a)			//输入年月日的函数
{
	int y, m, d;
	std::cout << "请输入年:";
	std::cin >> y;
	std::cout << "请输入月:";
	std::cin >> m;
	std::cout << "请输入日:";
	std::cin >> d;
	if (a == 0) {
		start.year = y;
		start.month = m;
		start.day = d;
	}
	if (a == 1) {
		end.year = y;
		end.month = m;
		end.day = d;
	}
}

void Book::get_type(std::string ty)		//输入类别的函数
{
	type = ty;
}

void Book::show()			//输出函数
{
	std::cout << "书籍名为《" << name << "》。长度为" << nword << "字。\n"
		<< "阅读时间为" << time << "小时。\n"
		<< "平均每分钟阅读" << rspeed << "字。\n"
		<< "这是一本" << type << "小说,个人评分为" << rating << "/10分。\n"
		<< "从" << start.year << "年" << start.month 
		<< "月" << start.day << "日开始阅读,到"
		<< end.year << "年" << end.month << "月" << end.day << "日为止。\n";
}

void Book::reset()			//重置初始化函数
{
	name = "untitled";
	nword = 0;
	time = 0;
	rating = 0;
	start = { 0,0,0 };
	end = { 0,0,0 };
	type = "unknown";
	set_speed();
}

Book::~Book()		//析构函数
{
}

main函数:

#include "Book.h"
#include 
#include 

void menu();
Book get_book();
void read_book(int n,Book li[]);
void delete_book(int n,Book li[]);

static int count = 0;

int main()
{
	Book test[100];		//创建对象数组
	while (1) {			//菜单循环
		int a;
		int temp;
		std::string yn;
		menu();
		std::cin >> a;
		switch (a) {	//选项的分支函数
		case 1:
			test[count] = get_book();
			test[count].show();
			count++;
			break;
		case 2:
			system("cls");
			std::cout << "目前共有" << count << "条记录。\n";
			std::cout << "请输入您要查看的编号:";
			std::cin >> temp;
			system("cls");
			read_book(temp,test);
			break;
		case 3:
			system("cls");
			std::cout << "目前共有" << count << "条记录。\n请输入要删除的序号:";
			std::cin >> temp;
			std::cout << "您要删除的是不是这一本书?\n";
			read_book(temp, test);
			std::cout << "是否确定?(Y/N)";
			std::cin >> yn;
			if (yn == "Y" || yn == "y") {
				delete_book(temp, test);
			}
			else if (yn == "N" || yn == "n") {
				std::cout << "取消删除!返回菜单。";
				break;
			}
			else {
				std::cout << "输入无效!返回菜单。";
				break;
			}
			std::cout << "删除完成!\n";
			break;
		case 4:
			system("cls");
			std::cout << "施工中……\n";
			break;
		case 5:
			system("cls");
			std::cout << "施工中……\n";
			break;
		case 0:
			system("cls");
			std::cout << "是否确定退出?(Y/N)";
			std::cin >> yn;
			if (yn == "Y" || yn == "y") {
				exit(0);
			}
			else if (yn == "N" || yn == "n") {
				std::cout << "取消退出!返回菜单。";
				break;
			}
			else {
				std::cout << "输入无效!返回菜单。";
				break;
			}
			break;
		default:
			system("cls");
			std::cout << "输入无效!\n";
		}
		std::cout << "按任意键返回。";
		std::cin.get();
		std::cin.get();
		menu();
	}
    return 0;
}


void menu()		//菜单函数
{
	using namespace std;
	system("cls");
	cout << "欢迎使用李呤泽编写的书籍阅读历史管理软件!\n";
	cout << "======菜单======\n";
	cout << "|(1).录入书籍。|\n";
	cout << "|(2).查看列表。|\n";
	cout << "|(3).删除记录。|\n";
	cout << "|(4).存档。    |\n";
	cout << "|(5).读档。    |\n";
	cout << "|(0).离开。    |\n";
	cout << "================\n";
	cout << "请输入选项序号:";
}

Book get_book()		//录入书籍函数
{
	using namespace std;
	string name;
	long nword;
	double time;
	int rating;
	string type;
	system("cls");
	cout << "请输入书名:";
	cin >> name;
	Book bo(name);
	cout << "请输入书籍类型:";
	cin >> type;
	bo.get_type(type);
	cout << "请输入字数:";
	cin >> nword;
	cout << "请输入阅读时间:";
	cin >> time;
	cout << "请输入评分(10分满):";
	cin >> rating;
	bo.get_info(nword, time, rating);
	cout << "请输入起始时间。";
	bo.get_ymd(0);
	cout << "请输入结束时间。";
	bo.get_ymd(1);
	cout << "输入完成!\n";
	cin.get();
	cin.get();
	system("cls");
	return bo;
}

void read_book(int n,Book li[])			//输出书籍函数
{
	li[n-1].show();
}

void delete_book(int n, Book li[])		//删除书籍函数
{
	int i;
	for (i = n - 1;i < count;i++) {
		li[i] = li[i + 1];
	}
	li[count].reset();
	count--;
}


程序还未完成。

你可能感兴趣的:(C++,个人练习)