C++Primer(第七章课后习题程序题源代码)

7.1

#include<iostream>
#include"Sales_data.h"
using namespace std;

int main()
{
    cout << "请输入交易记录(ISBN,销售量,原价,实际售价):" << endl;
    Sales_data total;
    if (cin >> total)
    {
        Sales_data trans;
        while (cin >> trans)
        {
            if (total.isbn() == trans.isbn())
                total += trans;
            else
            {
                cout << total << endl;
                total = trans;
            }
        }
        cout << total << endl;
    }
    else
    {
        cerr << "No data?!" << endl;
        return -1;
    }
    return 0;
}

7.2

class Sales_data
{
private:
    string bookNo;
    unsigned units_sold = 0;
    double sellingprice = 0.0;
    double saleprice = 0.0;
    double discount = 0.0;
public:
    string isbn() const{ return bookNo; }
    Sales_data & combine(const Sales_data &rhs)
    {
        units_sold += rhs.units_sold;
        saleprice = (rhs.saleprice*rhs.units_sold + saleprice*units_sold) / (rhs.units_sold + units_sold);
        if (sellingprice != 0)
            discount = saleprice / sellingprice;
        return *this;
    }
};

7.3

#include<iostream>
#include"Sales_data.h"
using namespace std;

int main()
{
    cout << "请输入交易记录(ISBN,销售量,原价,实际售价):" << endl;
    Sales_data total;
    if (cin >> total)
    {
        Sales_data trans;
        while (cin >> trans)
        {
            if (total.isbn() == trans.isbn())
                total.combine(trans);
            else
            {
                cout << total << endl;
                total = trans;
            }
        }
        cout << total << endl;
    }
    else
    {
        cerr << "No data?!" << endl;
        return -1;
    }
    return 0;
}

7.5

class Person
{
private:
    string strName;
    string strAddress;
public:
    string getName() const{ return strName; }
    string getAddress() const{ return strAddress; }
};

7.7

Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
{
    Sales_data sum = lhs;
    sum.combine(rhs);
    return sum;
}

std::istream &read(std::istream &is, Sales_data &item)
{
    is >> item.bookNo >> item.units_sold >> item.sellingprice >> item.saleprice;
    return is;
}

std::ostream &print(std::ostream &os, const Sales_data &item)
{
    os << item.isbn() << " " << item.units_sold << " " << item.Sellingprice << " " << item.saleprice << " " << item.discount;
    return os;
}

7.7

#include<iostream>
#include"Sales_data.h"
using namespace std;

int main()
{
    cout << "请输入交易记录(ISBN,销售量,原价,实际售价):" << endl;
    Sales_data total;
    if (read(cin,total))
    {
        Sales_data trans;
        while (read(cin,trans))
        {
            if (total.isbn() == trans.isbn())
                total += trans;
            else
            {
                print(cout, total);
                cout << endl;
                total = trans;
            }
        }
        print(cout, total);
        cout << endl;
    }
    else
    {
        cerr << "No data?!" << endl;
        return -1;
    }
    return 0;
}

7.9

std::istream& read(std::istream &is, Person &per)
{
    is >> per.strName >> per.strAddress;
    return is;
}
std::ostream& print(std::ostream &os, const Person &per)
{
    os << per.getName() << per.getAddress();
    return os;
}

7.24

class Screen
{
private:
    unsigned height = 0; width = 0;
    unsigned cursor = 0;
    string contents;
public :
    Screen() = default;
    Screen(unsigned ht, unsigned wd) :height(ht), width(wd), contents(ht *wd ,' '){ };
    Screen(unsigned ht,unsigned wd,char c)
        :height(ht), width(wd), contents(ht *wd, c){ }
};

7.26

class Sales_data
{
public:
    double avg_price() const
    {
        if (units_sold)
            return revenue / units_sold;
        else
            return 0;
    }
};
class Sales_data
{
    double avg_price() const
}
inline double Sales_data::avg_price() const
{
    if (units_sold)
            return revenue / units_sold;
    else
            return 0;
}

1.21

#include<iostream>
#include<string>
using namespace std;

class Window_mgr
{
public:
    void clear();
};
class Screen
{
    friend void Window_mgr::clear();
private:
    unsigned height = 0, width = 0;
    unsigned cursor = 0;
    string contents;
public:
    Screen() = default;
    Screen(unsigned ht, unsigned wd, char c)
        :height(ht), width(wd), contents(ht*wd, c);
};
void Window_mgr::clear();
{
    Screen myScreen(10, 20, 'X');
    cout << "清理之前myScrenn的内容是:"<<endl;
    cout << myScreen.contexts << endl;
    myScreen.contexts << endl;
    cout << myScrean.contents = " ";
    cout << 清理之后myScreen的内容是 << endl;
    cout << my.Screen.contexts << endl;

}

int main()
{
    Window.mgr w;
    w.clear();
    return 0;
}

7.40

class Book
{
private:
    string Name, ISBN, Author, Publisher;
    double Price = 0;
public:
    Book() = default;
    Book(const string &n, const string &I, double pr, const string &a, const string &p)
    {
        Name = n;
        ISBN = I;
        Price = pr;
        Author = a;
        Publisher = p;
    }
    Book(std::istream &is){
        is >> *this;
    }
};

class Tree
{
private:
    string Name;
    unsigned Age = 0;
    double Height = 0;
public:
    Tree() = default;
    Tree(const string &n, unsigned a, double h) :Name(n), Age(a), Height(h);
};

7.41

#include<iostream>
#include<string>
using namespace std;

class Sales_data{
    friend std::istream &read(std::istream &is, Sales_data &item);
    friend std::ostream &print(std::ostream &os, const Sales_data &item);
public:
    Sales_data(const string &book, unsigned num, double sellp, double salep) :
        bookNo(book), units_sold(num), sellingprice(sellp), saleprice(salep)
    {
        if (sellingprice)
            discount = saleprice = sellingprice;
        cout << "该构造函数接受书号,销售量,原价,实际售价四个信息" << endl;
    }
    Sales_data() :Sales_data("", 0, 0, 0)
    {
        cout << "该构造函数无须接受任何信息" << endl;
    }
    Sales_data(const string &book) :Sales_data(book, 0, 0, 0)
    {
        cout << "该构造函数接受书号信息" << endl;
    }
    Sales_data(std::istream &is) :Sales_data()
    {
        read(is, *this);
        cout << "该构造函数接受用户输入的信息" << endl;
    }
public :
    std::string bookNo;
    unsigned units_sold = 0;
    double sellingprice = 0.0;
    double saleprice = 0.0;
    double discount = 0.0;
};

std::istream &read(std::istream &is, Sales_data &item)
{
    is >> item.bookNo >> item.units_sold >> item.sellingprice >> item.saleprice;
    return is;
}

std::ostream &print(std::ostream &os, Sales_data &item)
{
    os << item.bookNo << "" << item.units_sold << " " << item.sellingprice << " " << item.saleprice << " " << item.discount;
    return os;
}

int main()
{
    Sales_data first("978-7-121-15535-2", 85, 128, 109);
    Sales_data second;
    Sales_data third("978-7-121-15535-2");
    Sales_data last(cin);
    return 0;
}

7.42

class Book
{
private:
    string Name, ISBN, Author, Publisher;
    double price = 0;
public:
    Book(const string&n, const string &I, double pr, const string &a, const string &p)
        :Name(n), ISBN(I), Price(pr), Author(a), Publisher(p){}
    Book() :Book("", "", 0, "", ""){}
    Book(std::istream &is) :Book(){ is >> *this; }
};

7.43

#include<iostream>
#include<string>
using namespace std;

class NoDefault
{
public:
    NoDefault(int i)
    {
        val = i;
    }
    int val;
};

class C
{
public :
    NoDefault nd;
    C(int i = 0) :nd(i){}
};

int main()
{
    C c;
    cout <<c.nd.val << endl;
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值