7.1
#include <iostream>
#include <string>
struct Sales_data
{
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
int main()
{
Sales_data total;
double totalPrice;
if (std::cin >> total.bookNo >> total.units_sold >> totalPrice)
{
total.revenue = total.units_sold * totalPrice;
Sales_data trans;
double transPrice;
while (std::cin >> trans.bookNo >> trans.units_sold >> transPrice)
{
trans.revenue = trans.units_sold * transPrice;
if (total.bookNo == trans.bookNo)
{
total.units_sold += trans.units_sold;
total.revenue += trans.revenue;
}
else
{
std::cout << total.bookNo << " " << total.units_sold << " " << total.revenue << " ";
if (total.units_sold != 0)
std::cout << total.revenue / total.units_sold << std::endl;
else
std::cout << "(no sales)" << std::endl;
total.bookNo = trans.bookNo;
total.units_sold = trans.units_sold;
total.revenue = trans.revenue;
}
}
std::cout << total.bookNo << " " << total.units_sold << " " << total.revenue << " ";
if (total.units_sold != 0)
std::cout << total.revenue / total.units_sold << std::endl;
else
std::cout << "(no sales)" << std::endl;
return 0;
}
else
{
std::cerr << "No data?!" << std::endl;
return -1; // indicate failure
}
}
7.2
#ifndef SALES_DATA_H
#define SALES_DATA_H
#include<string>
struct Sale_data
{
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
std::string isbn() const {
return bookNo};
Sales_data& combine (const Sales_data&);
};
Sales_data &Sales_data::combine(const Sales_data &rhs)
{
units_sold+=rhs.units_sold;
revenue+=rhs.revenue;
return *this;
}
#endif
7.3
#include <iostream>
#include <string>
#include "Sales_data.h"
int main()
{
Sales_data total;
double totalPrice;
if (std::cin >> total.bookNo >> total.units_sold >> totalPrice)
{
total.revenue = total.units_sold * totalPrice;
Sales_data trans;
double transPrice;
while (std::cin >> trans.bookNo >> trans.units_sold >> transPrice)
{
trans.revenue = trans.units_sold * transPrice;
if (total.isbn() == trans.isbn())
{
total.combine(trans);
}
else
{
std::cout << total.bookNo << " " << total.units_sold << " " << total.revenue << " ";
if (total.units_sold != 0)
std::cout << total.revenue / total.units_sold << std::endl;
else
std::cout << "(no sales)" << std::endl;
total = trans;
}
}
std::cout << total.bookNo << " " << total.units_sold << " " << total.revenue << " ";
if (total.units_sold != 0)
std::cout << total.revenue / total.units_sold << std::endl;
else
std::cout << "(no sales)" << std::endl;
return 0;
}
else
{
std::cerr << "No data?!" << std::endl;
return -1; // indicate failure
}
}
7.4
#ifndef PERSON_H_
#define PERSON_H_
#include <string>
struct Persson
{
std::string person;
std::string address;
};
#endif
7.5
应该是const,因为只需要读取成员对象,无需改变
ifndef PERSON_H_
#define PERSON_H_
#include <string>
struct Persson
{
std::string name;
std::string address;
std::string get_name() const{
return name};
std::string get_address() const{
return address};
};
#endif
7.7
#ifndef SALES_DATA_H_
#define SALES_DATA_H_
#include <string>
struct Sales_data
{
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
std::string isbn() const {
return bookNo; }
Sales_data& combine(const Sales_data&);
double avg_price()const;
};
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;
}
std::istream&read (std::istream&is,Sales_data &item)
{
double price=0;
is>>item.bookNo>>item.units_sold>>price;
item.revenue=price*item.units_sold;
retuen is;
}
std::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;
}
#endif
7.7
#include <iostream>
#include <string>
#include "Sales_data.h"
int main()
{
Sales_data total;
if (read(std::cin, total))
{
Sales_data trans;
while (read(std::cin, trans))
{
if (total.isbn() == trans.isbn())
{
total.combine(trans);
}
else
{
print(std::cout, total);
std::cout << std::endl;
total = trans;
}
}
print(std::cout, total);
std::cout << std::endl;
return 0;
}
else
{
std::cerr << "No data?!" << std::endl;
return -1; // indicate failure
}
}
7.8
read需要改变成员对象;
print只需要读取成员对象。
7.9
ifndef PERSON_H_
#define PERSON_H_
#include <string>
struct Persson
{
std::string name;
std::string address;
std::string get_name() const{
return name};
std::string get_address() const{
return address};
};
std::istream &read(istream &is, Person &item)
{
return is>>item.name>>" ">>item.address;
}
std::ostream &print(ostream &os, Person &item)
{
return os<<item.name<<" "<<item.address;
}
#endif
7.10
读入data1和data2并判断是否为真
7.11
Sales_data_ex11.h
#ifndef SALES_DATA_H_
#define SALES_DATA_H_
#include <string>
struct Sales_data
{
Sales_data()=default;
Sales_data(const std::string &s):bookNo(s){
}
Sales_data(const std::string &s, unsigned n, double p): bookNo(s),units_sold(n),revenue(p*n){
}
Sales_data(std::istream &);
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
std::string isbn() const {
return bookNo;}
Sales_data& combine(const Sales_data&);
double avg_price() const;
};
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;
}
std::istream &read(std::istream &is, Sales_data &item)
{
double price = 0;
is >> item.bookNo >> item.units_sold >> price;
item.revenue = price * item.units_sold;
return is;
}
std::ostream &print(std::ostream &os, const Sales_data &item)
{
os << item.isbn() << " "