欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

第二次

程序员文章站 2022-06-02 16:06:37
...

GradeSysterm类的设计
GradeSysterm.h

#pragma once
class GradeSysterm
{
public:
	int BookS = 0;//书籍状况
	int BookT = 0;//书籍年份
	int BookN = 0;//书籍批注情况
	float BookPiece = 0.0;//书籍价格
	float grade = 1.0;//折扣
public:
	int countBookS(int BookS);
	int countBookT(int BookT);
	int countBookN(int BookN);
	int subdiscount(int BookS, int BookT, int BookN);
	int Grade(int BookS, int BookT, int BookN);
	float countPrice(float grade,float BookPrice);
};

GradeSysterm.cpp

#include "GradeSysterm.h"
#include <math.h> 
int GradeSysterm::countBookS(int BookS)//根据书籍情况的折扣
{
	int Discount = 0.0;
	switch (BookS)
	{
	case 0:
		Discount = 0.9;
		break;
	case 1:
		Discount = 0.85;
		break;
	case 2:
		Discount = 0.8;
		break;
	case 3:
		Discount = 0.7;
		break;
	case 4:
		Discount = 0.5;
		break;
	case 5:
		Discount = 0.3;
		break;
	}
	return Discount;
}
int GradeSysterm::countBookT(int BookT)
{
	int Discount = 0.0;
	switch (BookT)
	{
	case 0:
		Discount = 0.9;
		break;
	case 1:
		Discount = 0.85;
		break;
	case 2:
		Discount = 0.6;
		break;
	case 3:
		Discount = 2;//当discount的值=2时返回不适合出售的信息
		break;
	}
	return Discount;
}
int GradeSysterm::countBookN(int BookN)
{
	int Discount = 0.0;
	switch (BookN)
	{
	case 0:
		Discount = 1.05;
		break;
	case 1:
		Discount = 1;
		break;
	case 2:
		Discount = 0.9;
		break;
	case 3:
		Discount = 0.6;
		break;
	}
	return Discount;
}
int GradeSysterm::subdiscount(int BookS, int BookT, int BookN)
{
	int Discount1, Discount2, Discount3, Discount;
	Discount1 = countBookS(BookS);
	Discount2 = countBookN(BookN);
	Discount3 = countBookT(BookT);
	Discount = Discount1 * Discount2 * Discount3;
	return Discount;
}
int GradeSysterm::Grade(int BookS, int BookT, int BookN)
{
	int rate, grade;
	rate = subdiscount(BookS, BookT, BookN);
	rate = floor(rate * 10);
	grade = rate / 10;
	return grade;
}
float GradeSysterm::countPrice(float grade, float BookPrice)
{
	BookPrice = BookPrice * grade;
	return BookPrice;
}

采用管道过滤器风格
只需要传入数据便可以返回对应的值
优点
– 由于每个组件行为不受其他组件的影响,整个系统的行为易于理解
– 支持功能模块的重用:任意两个过滤器只要相互间所传输的数据格式
上达到一致,就可以连接在一起
– 系统易于维护和扩展:新的过滤器容易加入到系统中,旧的过滤器也
可被改进的过滤器替换
– 支持特殊的分析,如吞吐量分析、死锁分析
– 支持并发:每个过滤器既可独立运行,也可和其它过滤器并发执行
CBook类
CBook.h


#pragma once
#define NUM1 128           //图书名称和Isbn编号最大长度
#define NUM2 50           //图书价格及作者最大长度
class CBook {
public:
	CBook(){}             //无参构造方法
	CBook(char* cName, char* cIsbn, char* cPrice, char* cAuthor);//有参构造方法
	~CBook(){}            //析构函数
public:
	char* GetName();//获取图书名称
	void SetName(char* cName);//设置图书名称
	char* GetIsbn();//获取图书ISBN编号
	void SetIsbn(char* cIsbn);//设置图书ISBN编号
	char* GetPrice();//获取图书价格
	void SetPrice(char* cPrice);//设置图书价格
	char* GetAuthor();//获取图书作者
	void SetAuthor(char* cAuthor);//设置图书作者
	void WriteData();             //写入数据
	void DeleteData(int iCount);  //删除数据
	void GetBookFromFile(int iCount);  //从文件中读取图书记录
protected:
	char m_cName[NUM1];
	char m_cIsbn[NUM1];
	char m_cPrice[NUM2];
	char m_cAuthor[NUM2];
};

CBook.cpp

#include "Book.h"
#include <string.h>
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
CBook::CBook(char* cName, char* cIsbn, char* cPrice, char* cAuthor)
 {
	strncpy(m_cName, cName, NUM1);
	strncpy(m_cIsbn, cIsbn, NUM1);
	strncpy(m_cPrice, cPrice, NUM2);
	strncpy(m_cAuthor, cAuthor, NUM2);
}
char* CBook::GetName()
 {
	return m_cName;
}
void CBook::SetName(char* cName) 
{
	strncpy(m_cName, cName, NUM1);
}
char* CBook::GetIsbn()
{
	return m_cIsbn;
}
void CBook::SetIsbn(char* cIsbn) 
{
	strncpy(m_cName, cIsbn, NUM1);
}
char* CBook::GetPrice() 
{
	return m_cPrice;
}
void CBook::SetPrice(char* cPrice) 
{
	strncpy(m_cPrice, cPrice, NUM2);
}
char* CBook::GetAuthor() 
{
	return m_cAuthor;
}
void CBook::SetAuthor(char* cAuthor)
{
	strncpy(m_cAuthor, cAuthor, NUM2);
}
//函数WriteDate,GetBookFromFile,DeleteData是类对象对写文件的函数,相当于操作数据库的接口
void CBook::WriteData() 
{
	ofstream ofile;
	ofile.open("book.dat", ios::binary | ios::app);//以二进制格式(binary)打开book.dat, app:每次写操作前均定位到文件末尾
	try 
	{
		ofile.write(m_cName, NUM1);//写入图书的信息
		ofile.write(m_cIsbn, NUM1);
		ofile.write(m_cPrice, NUM2);
		ofile.write(m_cAuthor, NUM2);
	}
	catch (...) 
	{//catch(…)能够捕获多种数据类型的异常对象
		throw "file error occurred";
		ofile.close();
	}
	ofile.close();
}
//成员函数DeleteData负责将图书信息从文件中删除
void CBook::DeleteData(int iCount) 
{//删掉第iCount条记录
	long respos;
	int iDataCount = 0;
	/*头文件fstarem定义了3各类型来支持文件IO,ifstream从一个给定文件读取数据,
	ofatream向一个给定文件写入数据,以及fstream可以读写给定文件*/
	fstream file;
	fstream tmpfile;
	ofstream ofile;
	char cTempBuf[NUM1 + NUM1 + NUM2 + NUM2];
	file.open("book.dat", ios::binary | ios::in | ios::out);//二进制/读/写
	tmpfile.open("temp.dat", ios::binary | ios::out | ios::in | ios::trunc);
	/*seekg()是对输入文件定位,它有两个参数:第一个参数是偏移量,第二个参数是基地址。*/
	file.seekg(0, ios::end); //基地址为文件结束处,偏移地址为0,于是指针定位在文件结束处
	respos = file.tellg();//返回当前定位指针的位置,也代表着输入流的大小。
	iDataCount = respos / (NUM1 + NUM1 + NUM2 + NUM2);//总记录数=文件大小/基本量
	if (iCount<0 || iCount>iDataCount) {
		throw "Input number error";
	}
	else 
	{
		file.seekg((iCount)*(NUM1 + NUM1 + NUM2 + NUM2), ios::beg);
		for (int j = 0; j < (iDataCount - iCount); j++) 
		{
			memset(cTempBuf, 0, NUM1 + NUM1 + NUM2 + NUM2);//将cTempBuf清零
			file.read(cTempBuf, NUM1 + NUM1 + NUM2 + NUM2);//将file中的一条记录读入cTempBuf
			tmpfile.write(cTempBuf, NUM1 + NUM1 + NUM2 + NUM2);//将cTempBuf中的一条记录写入tmpfile
		}
		file.close();
		tmpfile.seekg(0, ios::beg);//将指针定位在文件头
		ofile.open("book.dat");
		ofile.seekp((iCount - 1)*(NUM1 + NUM1 + NUM2 + NUM2), ios::beg);//将指针定位到第iCount-1条记录的结束位置
		for (int i = 0; i < (iDataCount - iCount); i++) 
		{
			memset(cTempBuf, 0, NUM1 + NUM1 + NUM2 + NUM2);
			tmpfile.read(cTempBuf, NUM1 + NUM1 + NUM2 + NUM2);
			ofile.write(cTempBuf, NUM1 + NUM1 + NUM2 + NUM2);//将cTempBuf中的一条记录写入ofile
		}
	}
	tmpfile.close();
	ofile.close();
	remove("temp.dat");
}
//成员函数GetBookFromFile从文件中读取数据来构建对象
void CBook::GetBookFromFile(int iCount) 
{
	char cName[NUM1];
	char cIsbn[NUM1];
	char cPrice[NUM2];
	char cAuthor[NUM2];
	ifstream ifile;
	ifile.open("book.dat", ios::binary);
	try 
	{
		ifile.seekg(iCount*(NUM1 + NUM1 + NUM2 + NUM2), ios::beg);
		ifile.read(cName, NUM1);
		if (ifile.tellg() > 0)
			strncpy(m_cName, cName, NUM1);
		ifile.read(cIsbn, NUM1);
		if (ifile.tellg() > 0)
			strncpy(m_cIsbn, cIsbn, NUM1);
		ifile.read(cPrice, NUM2);
		if (ifile.tellg() > 0)
			strncpy(m_cPrice, cPrice, NUM2);
		ifile.read(cAuthor, NUM2);
		if (ifile.tellg() > 0)
			strncpy(m_cAuthor, cAuthor, NUM2);
	}
	catch (...)
	{
		throw "file error occured";
		ifile.close();
	}
	ifile.close();
}

面向对象设计模式
对象隐藏了其实现细节,可在不影响其它对象的情况下改变对象
的实现,简单、方便,且具有很高的安全性和可靠性
设计者可将一些数据存取操作的问题分解成一些交互的代理程序
的集合