书店管理系统----涵盖C++大部分主题

文件1:sales_item.h

内容:定义一个销售记录的虚基类

#ifndef  SALESITEMS_H
#define  SALESITEMS_H

#include 
#include 
using namespace std ;

// 定义一个销售记录的抽象类
class Sales_item
{
public:
	// 默认构造函数
	Sales_item() : sold_number(0), unit_price(0) , flag(0)  //成员初始化列表
	{
	} 

	// 拷贝构造函数
	Sales_item( const Sales_item& item ) ;

	// 重载赋值运算符
	Sales_item& operator = ( const Sales_item& ) ;

	virtual double Gettotal_price() = 0 ; //纯虚函数定义 ,功能是返回这本书总共卖的钱

	double aver_price() ;

public: 
	string isbn ;			//这本书的ISBN
	unsigned sold_number;	//销售册数
	double unit_price;		//每册数的单价
	double total_price ;	//这本书总共卖的钱
	int flag ;
} ;

#endif

文件2:sales_item.cpp

内容:头文件sales_items.h的实现文件


#include "sales_item.h"

double Sales_item::Gettotal_price() 
{
	return total_price ;
}

double Sales_item::aver_price()
{
	return total_price / sold_number ;
}

Sales_item::Sales_item( const Sales_item& item )
{
	isbn = item.isbn ;
	sold_number = item.sold_number ;
	total_price = item.total_price ;
	unit_price = item.unit_price   ;
}

Sales_item& Sales_item::operator = ( const Sales_item& item )
{
	isbn = item.isbn ;
	sold_number = item.sold_number ;
	total_price = item.total_price ;
	unit_price = item.unit_price   ;

	return *this ;
}








文件3:mySales_item.h

内容:继承自文件sales_item.h中的类

#ifndef MYSALESITEM_H
#define MYSALESITEM_H

#include "Sales_item.h"
#include 
#include 
#include 
using namespace std ;

class MySales_item ;

bool operator== ( const MySales_item & item1 , const MySales_item& item2 ) ;
bool operator == ( const MySales_item& item , const string& isbn ) ;

class MySales_item : public Sales_item
{
public:

	MySales_item( int sold_number = 0 , int unit_price =  0 , int flag = 0 ) :
	Sales_item() 
	{ }  // 默认构造函数

	MySales_item( const MySales_item & item ) ;  // 拷贝构造函数

	virtual double Gettotal_price() ;  // 返回共卖的钱

	friend bool operator== ( const MySales_item & item1 , const MySales_item& item2 ) ;

	friend bool operator == ( const MySales_item& item , const string& isbn ) ;

	MySales_item& operator= ( const MySales_item & item ) ;

    MySales_item& operator+= ( MySales_item & item ) ;

    friend ostream & operator<< ( ostream & os , const MySales_item & theItem )  ;
    friend istream & operator>> ( istream & is , MySales_item & theItem ) ;
} ;

#endif

文件4:mysales_item.cpp

内容:实现头文件mysales_item.h中的内容


#include "MySales_item.h"

/////////////////////////////////////////////////////////////////////////
///
///  关于继承的复制控制:
///
///  如果派生类显示定义了自己的赋值构造函数或赋值操作符,则该定义
///  将完全覆盖默认的定义.被继承类的复制构造函数和赋值操作符负责对
///  基类成分以及类的自己的成员进行复制和赋值。
//
////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////

double MySales_item::Gettotal_price()
{
	total_price = sold_number * unit_price ;
	return total_price ;
}

// 首先将派生类对象转化为基类对象的引用,调用基类的拷贝构造函数Sales_item( theItems )
// 然后调用派生类的拷贝构造函数
MySales_item::MySales_item( const MySales_item& theItems ) : Sales_item( theItems )  // 拷贝构造函数
{
}

bool operator== ( const MySales_item & item1 , const MySales_item& item2 )
{
	return item1.isbn == item2.isbn ;
}

bool operator == ( const MySales_item& item , const string& isbn )
{
	return item.isbn == isbn ;
}

MySales_item& MySales_item::operator+= (  MySales_item& theItems ) 
{
	 sold_number += theItems.sold_number ;
	 total_price += theItems.total_price ;
	 return *this ;
}

MySales_item& MySales_item::operator= ( const MySales_item& theItems )
{
	if( &theItems != this )  // 必须防止自身赋值
	{
		Sales_item::operator =( theItems ) ;
	}
	return *this ;
}

ostream& operator<< ( ostream & os , const MySales_item& theItem ) 
{
	os << theItem.isbn << "\t" << theItem.sold_number << "\t" 
	   << theItem.unit_price << "\t" << theItem.total_price << endl ; 

	return os ;
}

istream& operator>> ( istream & is , MySales_item& theItem ) 
{
	is >> theItem.isbn >> theItem.sold_number >> theItem.unit_price ;

	// 检查输入是否成功
	if( is ) 
	{
        theItem.total_price = theItem.sold_number * theItem.unit_price ;
	}
	else
	{
        theItem  =  MySales_item() ;   // 输入失败,设定对象为默认的状态
	}
	return is ;
}






文件5:salesItemHanding.h

内容:管理类


#ifndef SALESITEMHANDING_H
#define SALESITEMHANDING_H

#include 
#include "MySales_item.h"
using namespace std ;

class SalesItemHanding
{
public:
	void inputSalesInformation() ;        // 输入书的销售信息

	void browseSalesInformation() ;       // 浏览交易信息

	void inquirySalesInformation() ;	  // 查询销售信息

	void statisticsSalesInformation() ;   // 统计销售信息

	void sortInformation( SalesItemHanding& item ) ;          // 对书目进行排序

	void MySort( string )   ;               // 下面重载了三个排序函数
	void MySort( double )   ;
	void MySort( unsigned ) ;

    bool MyLess( const MySales_item& item1 , const MySales_item& item2  ) ;  // 自定义排序规则

public:
	vector  AllSalesInformation ;   // 记录所有的销售信息
} ;

#endif

文件6:SalesItemhanding.cpp

内容:管理类的实现

#include "SalesItemHanding.h"
#include 
#include 
#include 
#include 
#include 
using namespace std ;

// 输入书的销售信息
void SalesItemHanding::inputSalesInformation()    // 输入书的销售信息
{
	cout << "请输入书的销售信息(输入格式为ISBN   售出数量  单价 , 最后以0 0 0 结尾 )" << endl ;
	cout << "ISBN" << "\t" << "售出数量" << "\t" << "单价" << endl ;

	MySales_item  item ;
	string  str( "0" ) ;

	while( 1 )
	{
		cin >> item ;
		if( item.isbn == str && item.unit_price == 0 )
		{
			break ;
		}
		AllSalesInformation.push_back( item ) ;
	}

	return  ;
}


void SalesItemHanding::browseSalesInformation()  // 浏览交易信息
{
	cout << "ISBN" << "\t" << "售出数量" << "\t" << "单价" << "\t" << "卖出书总共的钱" << endl ;

	vector  vec   ;
	vec = AllSalesInformation ;

	vector::iterator  it , it1 ;

	for( it = vec.begin() ; it != vec.end() ; it++ )
	{
		if( it->flag == 1 )
		{
			continue ;
		}

		for( it1 = it + 1 ; it1 != vec.end() ; it1++ )
		{
			if( *it == *it1 )
			{
				*it += *it1   ;
				it1->flag = 1 ;
			}
		}
	}

    
	for( it = vec.begin() ; it != vec.end() ; it++ )
	{
		if( it->flag != 1 )
		{
		   cout << *it ; 
		}
	}

	return ;
}

struct cmp //: public binary_function
{
	bool operator() ( const string& isbn , const MySales_item& sale )
	{
		return sale.isbn == isbn ;
	}
} ;

void SalesItemHanding::inquirySalesInformation()   // 查询销售信息
{
	string isbn ;
	cout << "请输入要查询的书目的ISBN:" ;
	cin >> isbn ;

	vector::iterator it ; 
    it = find( AllSalesInformation.begin() , AllSalesInformation.end() , isbn ) ;

	if( it == AllSalesInformation.end() )
	{
		cout << "No Found!" << endl ;
	}
	else
	{
		cout << *it ;
	}
}

void SalesItemHanding::statisticsSalesInformation()   // 统计销售信息
{
	ofstream  writeFile( "salesInformation.txt" , ios::out | ios::app ) ;

	vector::iterator  it , it1 ;

	for( it = AllSalesInformation.begin() ; it != AllSalesInformation.end() ; it++ )
	{
		if( it->flag == 1 )
		{
			continue ;
		}

		for( it1 = it + 1 ; it1 != AllSalesInformation.end() ; it1++ )
		{
			if( *it == *it1 )
			{
				*it += *it1   ;
				it1->flag = 1 ;
			}
		}
	}

    writeFile << "ISBN" << "\t" << "销量" << "\t" << "总进账" << "\t" << "平均售价" << endl ;
	for( it = AllSalesInformation.begin() ; it != AllSalesInformation.end() ; it++ )
	{
		if( it->flag != 1 )
		{
		   writeFile << it->isbn << "\t" << it->sold_number << "\t" 
			         << it->total_price << "\t" << it->aver_price() << endl  ; 
		}
	}

}

// 按照书目的isbn进行排序
bool MyLess_isbn( const MySales_item & item1 , const MySales_item & item2  )  // 自定义比较规则
{
	return item1.isbn < item2.isbn ;
}


// 按照书目的售出数量进行排序
bool MyLess_number( const MySales_item & item1 , const MySales_item & item2  )  // 自定义比较规则
{
	return item1.sold_number < item2.sold_number ;
}

// 按照书目的出售单价进行排序
bool MyLess_price( const MySales_item & item1 , const MySales_item & item2  )  // 自定义比较规则
{
	return  item1.unit_price < item2.unit_price ;
}


//  重载了三个排序函数
void SalesItemHanding::MySort( string )       // 按照isbn进行排序
{
	sort( AllSalesInformation.begin() , AllSalesInformation.end() , MyLess_isbn ) ;
	cout << "排序成功!" << endl ;
}

void SalesItemHanding::MySort( double )       // 按照单价进行排序
{
	sort( AllSalesInformation.begin() , AllSalesInformation.end() , MyLess_price) ;
	cout << "排序成功!" << endl ;
}

void SalesItemHanding::MySort( unsigned )     // 按照售出数量进行排序
{
	sort( AllSalesInformation.begin() , AllSalesInformation.end() , MyLess_number) ;   
	cout << "排序成功!" << endl ;
}

void SalesItemHanding::sortInformation( SalesItemHanding& item )
{
	int nChoose ;
	while( 1 )
	{
		cout << "=====================排序===========================" << endl ;
		cout << "=                                                  =" << endl ;
		cout << "=        1.根据ISBN排序     2.根据单价排序         =" << endl ;
		cout << "=        3.根据售出数量排序 4.返回主菜单           =" << endl ;
		cout << "=        5.退出系统                                =" << endl ;
		cout << "=                                                  =" << endl ;
		cout << "====================================================" << endl ;
	
		cout << "输入选择:" ;
		cin >> nChoose ;
		fflush(stdin) ;
		system( "cls" ) ;

		switch( nChoose )
		{
		case 1 :
			item.MySort( "" ) ; break ;
		case 2 :
			item.MySort( 0.0 ) ; break ;
		case 3 :
			item.MySort( ( unsigned int )1 ) ; break ;
		case 4 :
			return ;
		case 5 :
			exit( 0 ) ;
		default :
			cout << "输入错误!" << endl ;
			break ;
		}
	}
}
			












文件7:main.cpp

内容:测试主函数


#include "SalesitemHanding.h"

void Menu( SalesItemHanding& ) ;

int main()
{
    SalesItemHanding  item ;
    
	Menu( item ) ;

	return 0 ;
}

void Menu( SalesItemHanding& item )
{
	int nChoose ;
	while( 1 )
	{
		cout << "======================书店管理系统========================" << endl ;
		cout << "=                                                        =" << endl ;
		cout << "=            1.输入销售信息     2. 查询销售信息          =" << endl ;
		cout << "=            3.浏览销售信息     4. 统计销售信息          =" << endl ;
		cout << "=            5.排序             6.退出                   =" << endl ;
		cout << "=                                                        =" << endl ;
		cout << "=                                                        =" << endl ;
		cout << "==========================================================" << endl ;

		cout << "请选择:" ;
		cin >> nChoose ;
		fflush(stdin) ;
		system( "cls" ) ;

		switch( nChoose )
		{
		case 1 :
			item.inputSalesInformation() ;  break ;
		case 2 :
			item.inquirySalesInformation() ; break ;
		case 3 :
            item.browseSalesInformation() ; break ;
		case 4 :
			item.statisticsSalesInformation() ; break ;
		case 5 :
			item.sortInformation( item ) ; break ;
		case 6 :
			exit( 0 ) ;
		default :
			cout << "输入错误,请重新输入!" << endl ;
			break ;
		}
	}
}


你可能感兴趣的:(C/C++)