C++Primer(第七章课后习题程序题源代码)
2016-09-24 12:28
357 查看
7.1
7.2
7.3
7.5
7.7
7.7
7.9
7.24
7.26
1.21
7.40
7.41
7.42
7.43
#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; }
相关文章推荐
- C++Primer(第六章课后习题)(程序题源代码)
- C++Primer(第十一章课后习题程序题源代码)
- C++Primer(第四章课后习题)(程序题源代码)
- C++Primer(第八章课后习题程序题源代码)
- C++Pirmer(第五章课后习题)(程序题源代码)
- C++Primer第十二章课后习题程序题源代码
- C++Primer(第10章课后程序题源代码)
- C++Primer第三章(第五版) 课后习题 程序题 源代码
- C++Primer 中文版 第五版 第七章课后习题答案
- c++primer 课后习题个人解答 part1 3.4
- 算法导论课后习题解析 第七章
- Python核心编程 第七章课后习题
- C++primer第五版课后习题销售类Sales_data的编写
- Java课后习题——编写程序求100以内的素数并输出
- 编写一个程序,删除每个输入行末尾的换行符和制表符,并删除完全是空格或制表符的行<The C Programming Language>课后习题
- C++Primer 中文版 第五版 第五章课后习题答案
- C和指针课后习题(第七章)
- 《鸟哥linux》--第七章课后习题答案
- C++Primer 中文版 第五版 第三章课后习题答案
- c++primer 课后习题个人解答 part1 4.3