7.1:
#include <iostream>
using namespace std;
struct Sales_data
{
string bookNo;
unsigned unit_sold = 0;
double revenue = 0.0;
};
int main()
{
Sales_data total;
if (cin >> total.bookNo >> total.unit_sold >> total.revenue)
{
Sales_data trans;
while (cin >> trans.bookNo >> trans.unit_sold >> trans.revenue)
{
if (total.bookNo == trans.bookNo)
{
total.unit_sold += trans.unit_sold;
total.revenue += trans.revenue;
}
else
{
cout << total.bookNo << " " << total.unit_sold << " " << total.revenue << endl;
total.bookNo = trans.bookNo;
total.unit_sold = trans.unit_sold;
total.revenue = trans.revenue;
}
}
cout << total.bookNo << " " << total.unit_sold << " " << total.revenue << endl;
}
else
{
cout << " no data !" << endl;
}
system("pause");
return 0;
}
7.3:
#include <iostream>
using namespace std;
struct Sales_data
{
string bookNo;
unsigned unit_sold = 0;
double revenue = 0.0;
Sales_data& combine(const Sales_data& rhs);
string isbn()const
{
return this->bookNo;
}
};
Sales_data& Sales_data::combine(const Sales_data& rhs)
{
this->unit_sold += rhs.unit_sold;
this->revenue += rhs.revenue;
return *this;
}
int main()
{
Sales_data total;
if (cin >> total.bookNo >> total.unit_sold >> total.revenue)
{
Sales_data trans;
while (cin >> trans.bookNo >> trans.unit_sold >> trans.revenue)
{
if (total.isbn() == trans.isbn())
{
total.combine(trans);
}
else
{
cout << total.bookNo << " " << total.unit_sold << " " << total.revenue << endl;
total.bookNo = trans.bookNo;
total.unit_sold = trans.unit_sold;
total.revenue = trans.revenue;
}
}
cout << total.bookNo << " " << total.unit_sold << " " << total.revenue << endl;
}
else
{
cout << " no data !" << endl;
}
system("pause");
return 0;
}
7.5:
#include <iostream>
using namespace std;
struct Person
{
string Show_add()const
{
return this->m_address;
}
string Show_name()const
{
return this->m_name;
}
string m_name;
string m_address;
};
int main()
{
Person p1;
p1.m_name = "张三";
p1.m_address = "花园";
cout << p1.Show_name() << " " << p1.Show_add() << endl;
system("pause");
return 0;
}
7.7:
#include <iostream>
using namespace std;
struct Sales_data
{
string bookNo;
unsigned unit_sold = 0;
double revenue = 0.0;
Sales_data& combine(const Sales_data& rhs);
string isbn()const
{
return this->bookNo;
}
};
Sales_data& Sales_data::combine(const Sales_data& rhs)
{
this->unit_sold += rhs.unit_sold;
this->revenue += rhs.revenue;
return *this;
}
Sales_data add(const Sales_data& s1, const Sales_data& s2)
{
Sales_data temp = s1;
temp.combine(s2);
return temp;
}
istream& read(istream& is, Sales_data& item)
{
double price = 0;
is >> item.bookNo >> item.unit_sold >> price;
item.revenue = item.unit_sold * price;
return is;
}
ostream& print(ostream& os, const Sales_data& item)
{
os << item.bookNo << " " << item.unit_sold << " " << item.revenue;
return os;
}
int main()
{
Sales_data total;
if (read(cin,total))
{
Sales_data trans;
while (read(cin,trans))
{
if (total.isbn() == trans.isbn())
{
total.combine(trans);
}
else
{
print(cout, total) << endl;
total = trans;
}
}
print(cout, total) << endl;
}
else
{
cout << " no data !" << endl;
}
system("pause");
return 0;
}
7.9:
#include <iostream>
using namespace std;
class Person
{
public:
string Show_add()const
{
return this->m_address;
}
string Show_name()const
{
return this->m_name;
}
string m_name;
string m_address;
};
istream& read(istream& is, Person& p1)
{
is >> p1.m_name >> p1.m_address;
return is;
}
ostream& print(ostream& os, const Person& p1)
{
os << "姓名: " << p1.Show_name() << " 住址: " << p1.Show_add() << endl;
return os;
}
int main()
{
Person p1;
if (read(cin, p1))
{
print(cout, p1);
}
system("pause");
return 0;
}
7.11:
#include <iostream>
using namespace std;
struct Sales_data
{
Sales_data() = default;
Sales_data(const string& s) :bookNo(s) {};
Sales_data(const string& s, unsigned n, double p) :bookNo(s), unit_sold(n), revenue(n* p) {};
Sales_data(istream& is);
string bookNo;
unsigned unit_sold = 0;
double revenue = 0.0;
Sales_data& combine(const Sales_data& rhs);
string isbn()const
{
return this->bookNo;
}
};
Sales_data& Sales_data::combine(const Sales_data& rhs)
{
this->unit_sold += rhs.unit_sold;
this->revenue += rhs.revenue;
return *this;
}
Sales_data add(const Sales_data& s1, const Sales_data& s2)
{
Sales_data temp = s1;
temp.combine(s2);
return temp;
}
istream& read(istream& is, Sales_data& item)
{
double price = 0;
is >> item.bookNo >> item.unit_sold >> price;
item.revenue = item.unit_sold * price;
return is;
}
ostream& print(ostream& os, const Sales_data& item)
{
os << item.bookNo << " " << item.unit_sold << " " << item.revenue;
return os;
}
Sales_data::Sales_data(istream& is)
{
read(is, *this);
}
int main()
{
Sales_data s1;
print(cout, s1) << endl;
Sales_data s2("0-201-78345-x");
print(cout, s2) << endl;
Sales_data s3("0-201-78345-x", 3, 20.00);
print(cout, s3) << endl;
Sales_data s4(cin);
print(cout, s4) << endl;
system("pause");
return 0;
}
7.12:
#include <iostream>
using namespace std;
struct Sales_data;//要在read函数之前声明不然read函数找不到sales_data类
istream& read(istream& is, Sales_data& item);//要在sales_data类之前声明read函数!!
struct Sales_data
{
Sales_data() = default;
Sales_data(const string& s) :bookNo(s) {};
Sales_data(const string& s, unsigned n, double p) :bookNo(s), unit_sold(n), revenue(n* p) {};
Sales_data(istream& is) { read(cin, *this); }
string bookNo;
unsigned unit_sold = 0;
double revenue = 0.0;
Sales_data& combine(const Sales_data& rhs);
string isbn()const
{
return this->bookNo;
}
};
Sales_data& Sales_data::combine(const Sales_data& rhs)
{
this->unit_sold += rhs.unit_sold;
this->revenue += rhs.revenue;
return *this;
}
Sales_data add(const Sales_data& s1, const Sales_data& s2)
{
Sales_data temp = s1;
temp.combine(s2);
return temp;
}
istream& read(istream& is, Sales_data& item)
{
double price = 0;
is >> item.bookNo >> item.unit_sold >> price;
item.revenue = item.unit_sold * price;
return is;
}
ostream& print(ostream& os, const Sales_data& item)
{
os << item.bookNo << " " << item.unit_sold << " " << item.revenue;
return os;
}
//Sales_data::Sales_data(istream& is)
//{
// read(is, *this);
//}
int main()
{
Sales_data s1;
print(cout, s1) << endl;
Sales_data s2("0-201-78345-x");
print(cout, s2) << endl;
Sales_data s3("0-201-78345-x", 3, 20.00);
print(cout, s3) << endl;
Sales_data s4(cin);
print(cout, s4) << endl;
system("pause");
return 0;
}
7.13:
#include <iostream>
using namespace std;
struct Sales_data;//要在read函数之前声明不然read函数找不到sales_data类
istream& read(istream& is, Sales_data& item);//要在sales_data类之前声明read函数!!
struct Sales_data
{
Sales_data() = default;
Sales_data(const string& s) :bookNo(s) {};
Sales_data(const string& s, unsigned n, double p) :bookNo(s), unit_sold(n), revenue(n* p) {};
Sales_data(istream& is) { read(cin, *this); }
string bookNo;
unsigned unit_sold = 0;
double revenue = 0.0;
Sales_data& combine(const Sales_data& rhs);
string isbn()const
{
return this->bookNo;
}
};
Sales_data& Sales_data::combine(const Sales_data& rhs)
{
this->unit_sold += rhs.unit_sold;
this->revenue += rhs.revenue;
return *this;
}
Sales_data add(const Sales_data& s1, const Sales_data& s2)
{
Sales_data temp = s1;
temp.combine(s2);
return temp;
}
istream& read(istream& is, Sales_data& item)
{
double price = 0;
is >> item.bookNo >> item.unit_sold >> price;
item.revenue = item.unit_sold * price;
return is;
}
ostream& print(ostream& os, const Sales_data& item)
{
os << item.bookNo << " " << item.unit_sold << " " << item.revenue;
return os;
}
int main()
{
Sales_data total(cin);
if (!total.isbn().empty())
{
while (cin)
{
Sales_data trans(cin);
if (!cin) { break; }
if (total.isbn() == trans.isbn())
{
total.combine(trans);
}
else
{
print(cout, total) << endl;
total = trans;
}
}
print(cout, total) << endl;
}
else
{
cout << " no data !" << endl;
}
system("pause");
return 0;
}
7.15:
#include <iostream>
using namespace std;
class Person;
istream& read(istream& is, Person& p1);
class Person
{
public:
Person() = default;
Person(string name, string address)
{
this->m_name = name;
this->m_address = address;
}
Person(istream& is)
{
read(is, *this);
}
string Show_add()const
{
return this->m_address;
}
string Show_name()const
{
return this->m_name;
}
string m_name;
string m_address;
};
istream& read(istream& is, Person& p1)
{
is >> p1.m_name >> p1.m_address;
return is;
}
ostream& print(ostream& os, const Person& p1)
{
os << "姓名: " << p1.Show_name() << " 住址: " << p1.Show_add() << endl;
return os;
}
int main()
{
Person p1;
if (read(cin, p1))
{
print(cout, p1);
}
Person p2("张三", "花园");
print(cout, p2);
Person p3(cin);
print(cout, p3);
system("pause");
return 0;
}
7.21:
#include <iostream>
using namespace std;
struct Sales_data;//要在read函数之前声明不然read函数找不到sales_data类
istream& read(istream& is, Sales_data& item);//要在sales_data类之前声明read函数!!
class Sales_data
{
friend istream& read(istream& is, Sales_data& item);
friend ostream& print(ostream& os, const Sales_data& item);
friend Sales_data add(const Sales_data& s1, const Sales_data& s2);
public:
Sales_data() = default;
Sales_data(const string& s) :bookNo(s) {};
Sales_data(const string& s, unsigned n, double p) :bookNo(s), unit_sold(n), revenue(n* p) {};
Sales_data(istream& is) { read(cin, *this); }
Sales_data& combine(const Sales_data& rhs);
string isbn()const
{
return this->bookNo;
}
private:
string bookNo;
unsigned unit_sold = 0;
double revenue = 0.0;
};
Sales_data& Sales_data::combine(const Sales_data& rhs)
{
this->unit_sold += rhs.unit_sold;
this->revenue += rhs.revenue;
return *this;
}
Sales_data add(const Sales_data& s1, const Sales_data& s2)
{
Sales_data temp = s1;
temp.combine(s2);
return temp;
}
istream& read(istream& is, Sales_data& item)
{
double price = 0;
is >> item.bookNo >> item.unit_sold >> price;
item.revenue = item.unit_sold * price;
return is;
}
ostream& print(ostream& os, const Sales_data& item)
{
os << item.bookNo << " " << item.unit_sold << " " << item.revenue;
return os;
}
int main()
{
Sales_data total(cin);
if (!total.isbn().empty())
{
while (cin)
{
Sales_data trans(cin);
if (!cin) { break; }
if (total.isbn() == trans.isbn())
{
total.combine(trans);
}
else
{
print(cout, total) << endl;
total = trans;
}
}
print(cout, total) << endl;
}
else
{
cout << " no data !" << endl;
}
system("pause");
return 0;
}
7.22:
#include <iostream>
using namespace std;
class Person
{
friend istream& read(istream& is, Person& p1);
friend ostream& print(ostream& os, const Person& p1);
public:
Person() = default;
Person(string name, string address)
{
this->m_name = name;
this->m_address = address;
}
Person(istream& is)
{
read(is, *this);
}
string Show_add()const
{
return this->m_address;
}
string Show_name()const
{
return this->m_name;
}
private:
string m_name;
string m_address;
};
istream& read(istream& is, Person& p1)
{
is >> p1.m_name >> p1.m_address;
return is;
}
ostream& print(ostream& os, const Person& p1)
{
os << "姓名: " << p1.Show_name() << " 住址: " << p1.Show_add() << endl;
return os;
}
int main()
{
Person p1;
if (read(cin, p1))
{
print(cout, p1);
}
Person p2("张三", "花园");
print(cout, p2);
Person p3(cin);
print(cout, p3);
system("pause");
return 0;
}
7.23:
#include <iostream>
using namespace std;
#include<string>
class Screen
{
public:
using pos = string::size_type;
Screen() = default;
Screen(pos ht, pos wd, char c)
{
this->height = ht;
this->width = wd;
this->contents.assign(ht * wd, c);
}
char get()const
{
return contents[cursor];
}
inline char get(pos r, pos c)const;
Screen& move(pos r, pos c);
private:
pos cursor = 0;
pos height = 0, width = 0;
string contents;
};
char Screen::get(pos r, pos c)const
{
pos row = r * width;
return contents[row + c];
}
inline Screen& Screen::move(pos r, pos c)
{
pos row = r * width;
cursor = row + c;
return *this;
}
int main()
{
system("pause");
return 0;
}
7.24:
#include <iostream>
using namespace std;
#include<string>
class Screen
{
public:
using pos = string::size_type;
Screen() = default;
Screen(pos ht, pos wd, char c)
{
this->height = ht;
this->width = wd;
this->contents.assign(ht * wd, c);
}
Screen(pos ht, pos wd)
{
this->height = ht;
this->width = wd;
this->contents.assign(ht * wd, ' ');
}
char get()const
{
return contents[cursor];
}
inline char get(pos r, pos c)const;
Screen& move(pos r, pos c);
private:
pos cursor = 0;
pos height = 0, width = 0;
string contents;
};
char Screen::get(pos r, pos c)const
{
pos row = r * width;
return contents[row + c];
}
inline Screen& Screen::move(pos r, pos c)
{
pos row = r * width;
cursor = row + c;
return *this;
}
int main()
{
system("pause");
return 0;
}
7.26:
#include <iostream>
using namespace std;
class Sales_data
{
friend istream& read(istream& is, Sales_data& item);
friend ostream& print(ostream& os, const Sales_data& item);
friend Sales_data add(const Sales_data& s1, const Sales_data& s2);
public:
Sales_data() = default;
Sales_data(const string& s) :bookNo(s) {};
Sales_data(const string& s, unsigned n, double p) :bookNo(s), unit_sold(n), revenue(n* p) {};
Sales_data(istream& is) { read(cin, *this); }
Sales_data& combine(const Sales_data& rhs);
string isbn()const
{
return this->bookNo;
}
private:
inline double avg_price()const;
string bookNo;
unsigned unit_sold = 0;
double revenue = 0.0;
};
Sales_data& Sales_data::combine(const Sales_data& rhs)
{
this->unit_sold += rhs.unit_sold;
this->revenue += rhs.revenue;
return *this;
}
Sales_data add(const Sales_data& s1, const Sales_data& s2)
{
Sales_data temp = s1;
temp.combine(s2);
return temp;
}
istream& read(istream& is, Sales_data& item)
{
double price = 0;
is >> item.bookNo >> item.unit_sold >> price;
item.revenue = item.unit_sold * price;
return is;
}
ostream& print(ostream& os, const Sales_data& item)
{
os << item.bookNo << " " << item.unit_sold << " " << item.revenue;
return os;
}
inline double Sales_data::avg_price()const
{
return unit_sold ? revenue / unit_sold : 0;
}
int main()
{
Sales_data total(cin);
if (!total.isbn().empty())
{
while (cin)
{
Sales_data trans(cin);
if (!cin) { break; }
if (total.isbn() == trans.isbn())
{
total.combine(trans);
}
else
{
print(cout, total) << endl;
total = trans;
}
}
print(cout, total) << endl;
}
else
{
cout << " no data !" << endl;
}
system("pause");
return 0;
}
7.27:
#include <iostream>
using namespace std;
#include<string>
class Screen
{
public:
using pos = string::size_type;
Screen() = default;
Screen(pos ht, pos wd, char c):height(ht),width(wd),contents(ht*wd,c) {}
Screen(pos ht, pos wd):height(ht), width(wd), contents(ht* wd, ' ') {}
char get()const
{
return contents[cursor];
}
inline char get(pos r, pos c)const;
inline Screen& move(pos r, pos c);
inline Screen& set(char);
inline Screen& set(pos r, pos c, char ch);
Screen& display(ostream& os)
{
do_display(os);
return *this;
}
const Screen& display(ostream& os)const
{
do_display(os);
return *this;
}
private:
void do_display(ostream& os)const
{
os << contents;
}
pos cursor = 0;
pos height = 0, width = 0;
string contents;
};
char Screen::get(pos r, pos c)const
{
pos row = r * width;
return contents[row + c];
}
inline Screen& Screen::move(pos r, pos c)
{
pos row = r * width;
cursor = row + c;
return *this;
}
inline Screen& Screen::set(char c)
{
this->contents[cursor] = c;
return *this;
}
inline Screen& Screen::set(pos r, pos c, char ch)
{
this->contents[r * width + c] = ch;
return *this;
}
int main()
{
Screen myScreen(5, 5, 'x');
myScreen.move(4, 0).set('#').display(cout);
cout << endl;
myScreen.display(cout);
cout << endl;
system("pause");
return 0;
}
7.31:
class Y;
class X
{
Y* py = 0;
};
class Y
{
X x1;
};
7.32:
#include <iostream>
using namespace std;
#include<string>
#include<vector>
class Screen;
class Window_mgr
{
public:
using ScreenIndex = vector<Screen>::size_type;
void clear(ScreenIndex);
private:
vector<Screen>screens;
};
class Screen
{
friend void Window_mgr::clear(ScreenIndex);
public:
using pos = string::size_type;
Screen() = default;
Screen(pos ht, pos wd, char c) :height(ht), width(wd), contents(ht* wd, c) {}
Screen(pos ht, pos wd) :height(ht), width(wd), contents(ht* wd, ' ') {}
char get()const
{
return contents[cursor];
}
inline char get(pos r, pos c)const;
inline Screen& move(pos r, pos c);
inline Screen& set(char);
inline Screen& set(pos r, pos c, char ch);
Screen& display(ostream& os)
{
do_display(os);
return *this;
}
const Screen& display(ostream& os)const
{
do_display(os);
return *this;
}
private:
void do_display(ostream& os)const
{
os << contents;
}
pos cursor = 0;
pos height = 0, width = 0;
string contents;
};
char Screen::get(pos r, pos c)const
{
pos row = r * width;
return contents[row + c];
}
inline Screen& Screen::move(pos r, pos c)
{
pos row = r * width;
cursor = row + c;
return *this;
}
inline Screen& Screen::set(char c)
{
this->contents[cursor] = c;
return *this;
}
inline Screen& Screen::set(pos r, pos c, char ch)
{
this->contents[r * width + c] = ch;
return *this;
}
void Window_mgr::clear(ScreenIndex i)
{
Screen& s = screens[i];
s.contents = string(s.height * s.width, ' ');
}
7.41:
#include <iostream>
using namespace std;
class Sales_data
{
friend istream& read(istream& is, Sales_data& item);
friend ostream& print(ostream& os, const Sales_data& item);
friend Sales_data add(const Sales_data& s1, const Sales_data& s2);
public:
Sales_data(const string& s, unsigned n, double p) :bookNo(s), unit_sold(n), revenue(n* p)
{
cout << "这是三参数构造函数调用" << endl;
};
Sales_data() :Sales_data(" ", 0, 0) { cout << "这是默认函数调用" << endl; }
Sales_data(string s):Sales_data(s,0,0){ cout << "这是string构造函数调用" << endl; }
Sales_data(istream& is) :Sales_data() {
cout << "这是isteam构造函数调用" << endl;
read(is, *this); };
Sales_data& combine(const Sales_data& rhs);
string isbn()const
{
return this->bookNo;
}
private:
inline double avg_price()const;
string bookNo;
unsigned unit_sold = 0;
double revenue = 0.0;
};
Sales_data& Sales_data::combine(const Sales_data& rhs)
{
this->unit_sold += rhs.unit_sold;
this->revenue += rhs.revenue;
return *this;
}
Sales_data add(const Sales_data& s1, const Sales_data& s2)
{
Sales_data temp = s1;
temp.combine(s2);
return temp;
}
istream& read(istream& is, Sales_data& item)
{
double price = 0;
is >> item.bookNo >> item.unit_sold >> price;
item.revenue = item.unit_sold * price;
return is;
}
ostream& print(ostream& os, const Sales_data& item)
{
os << item.bookNo << " " << item.unit_sold << " " << item.revenue;
return os;
}
inline double Sales_data::avg_price()const
{
return unit_sold ? revenue / unit_sold : 0;
}
int main()
{
Sales_data s1("sad", 1, 1);
Sales_data s2;
Sales_data s3("bad");
Sales_data s4(cin);
system("pause");
return 0;
}
7.43:
#include<iostream>
using namespace std;
class NoDefault
{
public:
NoDefault(int) { cout << "这是nodefault的构造函数" << endl; }
};
class C
{
public:
C():c(0){ cout << "这是C的构造函数" << endl; }
NoDefault c;
};
int main()
{
C c;
system("pause");
return 0;
}
7.50:
#include <iostream>
using namespace std;
class Person
{
friend istream& read(istream& is, Person& p1);
friend ostream& print(ostream& os, const Person& p1);
public:
Person() = default;
Person(string name, string address)
{
this->m_name = name;
this->m_address = address;
}
explicit Person(istream& is)
{
read(is, *this);
}
string Show_add()const
{
return this->m_address;
}
string Show_name()const
{
return this->m_name;
}
private:
string m_name;
string m_address;
};
istream& read(istream& is, Person& p1)
{
is >> p1.m_name >> p1.m_address;
return is;
}
ostream& print(ostream& os, const Person& p1)
{
os << "姓名: " << p1.Show_name() << " 住址: " << p1.Show_add() << endl;
return os;
}
int main()
{
Person p1;
if (read(cin, p1))
{
print(cout, p1);
}
Person p2("张三", "花园");
print(cout, p2);
Person p3(cin);
print(cout, p3);
system("pause");
return 0;
}
7.51:
找的别人的回答QAQ
//Such as a function like that:
int getSize(const std::vector<int>&);
//if vector has not defined its single-argument constructor as explicit. we can use the function like:
getSize(34);
//What is this mean? It's very confused.
//But the std::string is different. In ordinary, we use std::string to replace const char *(the C language). so when we call a function like that:
void setYourName(std::string); // declaration.
setYourName("pezy"); // just fine.
//it is very natural.
7.53:
class Debug
{
public:
constexpr Debug(bool b =true):hw(b),io(b),other(b){}
constexpr Debug(bool h,bool i, bool o): hw(h), io(i), other(o) {}
constexpr bool any()
{
return hw || io || other;
}
void set_io(bool b) { io = b; }
void set_hwo(bool b) { hw = b; }
void set_other(bool b) { other = b; }
private:
bool hw; //硬件错误
bool io; //io错误
bool other;
};
7.57:
class Account {
public:
void calculate() { amount += amount * interestRate; }
static double rate() { return interestRate; }
static void rate(double newRate) { interestRate = newRate; }
private:
std::string owner;
double amount;
static double interestRate;
static constexpr double todayRate = 42.42;
static double initRate() { return todayRate; }
};
double Account::interestRate = initRate();