C++实验报告BOOK类

#include<iostream>
#include<cstring>
using namespace std;
class Date
{
private:
    int year, month, day;
public:
    Date(int y,int m,int d)
    {
        year = y;
        month = m;
        day = d;
    }
    Date()
    {
        year = 1000;
        month = 1;
        day = 1;
    }
    void showdate();
    int getyear()
    {
        return year;
    }
    int getmonth()
    {
        return month;
    }
    int getday()
    {
        return day;
    }
};
class Book
{
private:
    char *name;
    char *author;
    int sale; //销量
    const double price;//定价
    static double total_price;//所有书目总销售额
    Date date;//出版日期
public: 
    Book();
    Book(char *na, char *a, int sl, int y, int m, int d, double p);
    Book(const Book &rb);
    ~Book();
    void print();
    void print() const;
    char *getname();//返回书名
    double count();//计算每本书销售额
    static void computer(Book bk[],int n);//计算总销售额
    double get_total(Book bk[]);
    int getsale();
    //void find(Book bk[],char bn[]);
    
};
double Book::total_price = 0;
Book::Book():price(35),date(2000,1,1)
{
    name = new char[10];
    strcpy(name, "c++");
    author = new char[10];
    strcpy(author, "程磊");
    sale = 20;
}
Book::Book(char *na, char *a, int sl, int y, int m, int d, double p) : date( y,  m, d),price(p)
{
    name = new char[strlen(na) + 1];
    strcpy(name, na);
    author = new char[strlen(a) + 1];
    strcpy(author, a);
    sale = sl;
}
Book::Book(const Book &rb):date(rb.date),price(rb.price)
{
    name = new char[strlen(rb.name) + 1];
    strcpy(name, rb.name);
    author = new char[strlen(rb.author) + 1];
    strcpy(author, rb.author);
    sale = rb.sale;
}
Book::~Book()
{
    delete[] name;
    delete[] author;
}
void Date::showdate()
{
    cout << "year:" << year << "month:" << month << "day:" << day;
}
/*void sort(Book bk[])//按照销量递减显示书的信息
{
    	int *list=new int[5];
	for(int i=0;i<5;i++)
	{
		list[i]=bk[i].getsale();
	}
	int temp=0;
	for(int i=0;i<5;i++)
	{
		for(int j=i+1;j<5;j++)
		{
			if (list[i] < list[j]) 
			{
				temp = list[i];
				list[i] = list[j];
				list[j] = temp;
			}
		}
	}
	for (int i=0;i<5;i++) 
	{
		for (int j=0;j<5;j++)
		 {
			if (bk[j].getsale()==list[i])
			 {
				bk[j].print();
			 }
		 }
	}
}*/
void Book::print()
{
    cout << "name:" << name << endl;
    cout<< "author:" << author << endl;
    cout << "sale:" << sale << endl;
    cout << "price  :" << price << endl;
    cout << "year: " << date.getyear() << endl;
    cout << "month: " << date.getmonth() << endl;
    cout << "day: " << date.getday() << endl;
    cout << "This is a general print" << endl;
}

void Book::print()const//输出书籍信息, 只读常函数
{
    cout << "name   :" << name << endl;
	cout << "author :" << author << endl;
	cout << "sale   :" << sale << endl;
	cout << "price  :" << price << endl;
    cout << "This is a const print" << endl;
}

char* Book::getname() 
{
	return name;
}
double Book::count()
{
    double sum = 0.0;
	sum = sale * price;
	return sum;
}
double Book::get_total(Book bk[]) {
	return Book::total_price;
}


void Book::computer(Book bk[],int n)
{
    int i;
	double sum = 0;
	Book::total_price = 0;
	for (i = 0; i < 10; i++)
	{
		sum += (bk[i].sale * bk[i].price);
	}
	Book::total_price = sum;
}
int Book::getsale()
{
    return sale;
}
void sort(Book bk[])
{
    int tem = 0, i = 0,max=0;
    for (i = 0; i < 3;i++)
    {
        if(bk[i].getsale()>bk[max].getsale())
        {
            tem = i;
            i = max;
            max = tem;
        }
    }
}


void find(Book bk[],char bn[])//按书名查找
{
    int i;
    int flag = 0;
    cout<< "书名\t" << "作者\t" << "销量\t" << "出版日期" << endl;

    for (i = 0; i < 10; i++)
    {
        if(strcmp(bk[i].getname(),bn)==0)
        {
            bk[i].print();
            flag = 1;
        }
        
    }if(flag ==0)
	    {
		    cout<<"查找失败"<<endl;
	    }
}
int main()
{
    char bn[20]="活着";
    Book bk[3] = {Book(), Book("活着", "余华", 10005, 2005, 6, 17, 25), Book("围城", "钱钟书", 1000, 2000, 11, 1, 27)};
    //Book bk1;
    //Book bk2("活着", "余华", 10005, 2005, 6, 17, 25);
   // Book bk3(bk2); 
   sort(bk);//没有实现这个功能
   find(bk, bn);
   bk[0].print();
   // bk1.printconst();
    bk[1].print();
    bk[2].print();
   
    system("pause");
}





这里是我的C++作业,里面还有一些没有实现的地方

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值