7.1
#include <iostream>
#include<string>
#include<vector>
using namespace std;
struct Sales_data
{
string bookNo;
unsigned units_sold;
double revenue;
Sales_data()
{
units_sold = 0;
revenue = 0.0;
}
};
int main(int argc, char* argv[])
{
cout << "请输入一本书的 书序列号 销售量 销售额" << endl;
Sales_data currBook;
Sales_data nextBook;
if (cin >> currBook.bookNo >> currBook.units_sold >> currBook.revenue)
{
cout << "请输入另一本书的 书序列号 销售量 销售额" << endl;
while (cin >> nextBook.bookNo >> nextBook.units_sold >> nextBook.revenue)
{
if (currBook.bookNo == nextBook.bookNo)
{
currBook.units_sold += nextBook.units_sold;
currBook.revenue += nextBook.revenue;
}
else
{
cout << currBook.bookNo << " 销售量为 :" << currBook.units_sold << " " << "销售额为 :" << currBook.revenue << endl;
currBook = nextBook;
}
cout << currBook.bookNo << " 销售量为 :" << currBook.units_sold << " " << "销售额为 :" << currBook.revenue << endl;
}
}
else
{
cerr << "No data" << endl;
}
cin.get();
}
7.6 7.7
#include <iostream>
#include<string>
#include<vector>
using namespace std;
struct Sales_data
{
string bookNo;
unsigned units_sold;
double revenue;
double avg_price() const;
Sales_data()
{
}
Sales_data& combine(const Sales_data &rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
string isbn() const
{
return this->bookNo;
}
istream& read(istream& is, Sales_data& item)
{
double price = 0;
is >> item.bookNo >> item.units_sold >> price;
item.revenue = price * item.units_sold;
return is;
}
ostream& print(ostream& os, const Sales_data& item)
{
os << "书好 : " << item.isbn() << "销量 : " << item.units_sold <<
"销售额: " << item.revenue << "均价: " << item.avg_price();
return os;
}
Sales_data add(const Sales_data& lhs, const Sales_data& rhs)
{
Sales_data sum = lhs;
sum.combine(rhs);
return sum;
}
};
double Sales_data::avg_price() const {
if (units_sold) {
return revenue / units_sold;
}
else return 0;
}
class Person
{
private:
string name;
string address;
public:
Person()
{
}
void Show_info(string name , string address)
{
cout << name << " 的地址是 " << address << endl;
}
string getname() const
{
return this -> name;
}
string getaddress() const
{
return this -> address;
}
};
int main(int argc, char* argv[])
{
cout << "请输入一本书的 书序列号 销售量 销售额" << endl;
Sales_data currBook;
Sales_data nextBook;
if (currBook.read(cin , currBook))
{
cout << "请输入另一本书的 书序列号 销售量 销售额" << endl;
while (currBook.read(cin,nextBook))
{
if (currBook.isbn() == nextBook.isbn())
{
currBook.combine(nextBook);
//currBook.units_sold += nextBook.units_sold;
//currBook.revenue += nextBook.revenue;
}
else
{
currBook.print(cout, currBook);
cout << endl;
currBook = nextBook;
/*cout << currBook.bookNo << " 销售量为 :" << currBook.units_sold << " " << "销售额为 :" << currBook.revenue
<< "平均价格: " << currBook.avg_price() << endl;
currBook = nextBook;*/
}
currBook.print(cout, currBook);
cout << endl;
/*cout << currBook.bookNo << " 销售量为 :" << currBook.units_sold << " " << "销售额为 :" << currBook.revenue
<< "平均价格: " << currBook.avg_price() << endl;*/
}
}
else
{
cerr << "No data" << endl;
}
//Person My_person;
//My_person.Show_info("Mr.Lei", " aluba");
cin.get();
}
7.11
#include <iostream>
#include<string>
#include<vector>
using namespace std;
class Sales_data {
public:
Sales_data() = default ;
Sales_data(const std::string bN) :bookNo(bN), units_sold(0), revenue(0) {}
Sales_data(const std::string bN, unsigned sold) :bookNo(bN), units_sold(sold), revenue(0) {}
Sales_data(const std::string bN, unsigned sold, double reven) :bookNo(bN), units_sold(sold), revenue(reven) {}
std::string isbn() const { return bookNo; }
unsigned getUnits_sold() const { return units_sold; }
double getRevenue() const { return revenue; }
Sales_data& combine(const Sales_data&);
private:
std::string bookNo;
unsigned units_sold;
double revenue;
};
Sales_data& Sales_data::combine(const Sales_data& rhs) {
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
//double Sales_data::avg_price() const {
// if (units_sold) {
// return revenue / units_sold;
// }
// else return 0;
//}
int main(int argc, char* argv[])
{
Sales_data A;
Sales_data B("0-1-2222-3");
Sales_data C("0-1-2222-4", 20);
Sales_data D("0-1-33333-5", 24, 66);
cout << A.isbn() << " " << A.getUnits_sold() << " " << A.getRevenue() << " " << endl;
cout << B.isbn() << " " << B.getUnits_sold() << " " << B.getRevenue() << " " << endl;
cout << C.isbn() << " " << C.getUnits_sold() << " " << C.getRevenue() << " " << endl;
cout << D.isbn() << " " << D.getUnits_sold() << " " << D.getRevenue() << " " << endl;
//Person My_person;
//My_person.Show_info("Mr.Lei", " aluba");
cin.get();
}
7.27
#include <iostream>
#include<string>
#include<vector>
using namespace std;
class Screen
{
public:
using pos = string::size_type;
Screen() = default;
Screen(pos ht , pos wd) : height(ht) , width(wd) , contents(ht * wd , ' '){}
Screen(pos ht, pos wd, char c) :height(ht), width(wd), contents(ht* wd, c)
{
}
Screen& set(char);
Screen& set(pos, pos, char);
Screen& move(pos, pos);
char get() const { return contents[cursor]; }
char get(pos r, pos c) const { return contents[r * width + c]; }
Screen &display(ostream& os)
{
do_display(os);
return *this;
}
const Screen& display(ostream& os)const
{
do_display(os);
return *this;
}
void do_display(ostream& os) const { os << contents; }
private:
pos height = 0;
pos width = 0;
string contents;
pos cursor = 0;
};
inline Screen &Screen::set(char c)
{
contents[cursor] = c;
return *this;
}
inline Screen& Screen::set(pos r, pos col, char ch)
{
contents[r * width + col] = ch;
return *this;
}
inline Screen& Screen::move(pos r, pos c) {
cursor = r * width + c;
return *this;
}
int main(int argc, char* argv[])
{
Screen My_screen(5 , 5 , 'X');
My_screen.move(4, 0).set('#').display(cout);
cout << "\n";
My_screen.display(cout);
cout << "\n";
cin.get();
}