1、复习了一下昨天的内容 while(std::cin >> value) 扫描【标准输入】

2、在网上下载Sales_item.h 代码如下

#ifndef SALESITEM_H
#define SALESITEM_H
#include <iostream>
#include <string> class Sales_item
{
public:
Sales_item(const std::string &book):isbn(book),units_sold(),revenue(0.0){}
Sales_item(std::istream &is){ is >> *this;}
friend std::istream& operator>>(std::istream &,Sales_item &);
friend std::ostream& operator<<(std::ostream &,const Sales_item &);
public:
Sales_item & operator+=(const Sales_item&);
public:
double avg_price() const;
bool same_isbn(const Sales_item &rhs)const
{
return isbn == rhs.isbn;
}
Sales_item():units_sold(),revenue(0.0){}
public:
std::string isbn;
unsigned units_sold;
double revenue;
}; using std::istream;
using std::ostream;
Sales_item operator+(const Sales_item &,const Sales_item &);
inline bool operator==(const Sales_item &lhs,const Sales_item &rhs)
{
return lhs.units_sold == rhs.units_sold && lhs.revenue == rhs.revenue && lhs.same_isbn(rhs);
}
inline bool operator!=(const Sales_item &lhs,const Sales_item &rhs)
{
return !(lhs == rhs);
} inline Sales_item & Sales_item::operator +=(const Sales_item &rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
inline Sales_item operator+(const Sales_item &lhs,const Sales_item &rhs)
{
Sales_item ret(lhs);
ret += rhs;
return ret;
}
inline istream& operator>>(istream &in,Sales_item &s)
{
double price;
in >> s.isbn >> s.units_sold >> price;
if(in)
s.revenue = s.units_sold * price;
else
s = Sales_item();
return in;
}
inline ostream& operator<<(ostream &out,const Sales_item &s)
{
out << s.isbn << "\t" <<s.units_sold << "\t" << s.revenue << "\t" << s.avg_price();
return out;
}
inline double Sales_item::avg_price() const
{
if(units_sold)
return revenue/units_sold;
else
return ;
}
#endif

3、使用文件重定向, 例如:$ prog1<input.txt>output.txt

遇到的问题:

1、在c++primer e5里, 访问isbn用的是 item1.isbn(),

而在我下载的头文件里 isbn直接就被定义为public 而没有写isdn()这个函数。因此在下面的代码里就直接用item1.isdn访问对象的isdn。

2、另外, std::cerr << "xxx" 不受重定向影响,xxx 直接输出在 cmd 界面

练习1.19

#include<iostream>

int main()
{
std::cout << "Enter two numbers:" << std::endl;
int v1 = , v2 = ;
std::cin >> v1 >> v2;
int temp = ;
if(v1 > v2){
temp = v1;
v1 = v2;
v2 = temp;
}
for(;v1 <= v2; ++v1){
std::cout << v1 << std::endl;
}
return ;
}
05-11 13:51