准备:

1:销售记录文件book_sales

2:书的描述类Sales_item.h

book_sales内容如下

0-201-70353-X 4 24.99
0-201-82470-1 4 45.39
0-201-88954-4 2 15.00
0-201-88954-4 5 12.00
0-201-88954-4 7 12.00
0-201-88954-4 2 12.00
0-399-82477-1 2 45.39
0-399-82477-1 3 45.39
0-201-78345-X 3 20.00
0-201-78345-X 2 25.00复制代码

Sales_item.h内容如下

#ifndef SALESITEM_H
// we're here only if SALESITEM_H has not yet been defined
#define SALESITEM_H// Definition of Sales_item class and related functions goes here
#include <iostream>
#include <string>class Sales_item {
// these declarations are explained section 7.2.1, p. 270
// and in chapter 14, pages 557, 558, 561
friend std::istream& operator>>(std::istream&, Sales_item&);
friend std::ostream& operator<<(std::ostream&, const Sales_item&);
friend bool operator<(const Sales_item&, const Sales_item&);
friend bool
operator==(const Sales_item&, const Sales_item&);
public:// constructors are explained in section 7.1.4, pages 262 - 265// default constructor needed to initialize members of built-in typeSales_item(): units_sold(0), revenue(0.0) { }Sales_item(const std::string &book): bookNo(book), units_sold(0), revenue(0.0) { }Sales_item(std::istream &is) { is >> *this; }
public:// operations on Sales_item objects// member binary operator: left-hand operand bound to implicit this pointerSales_item& operator+=(const Sales_item&);// operations on Sales_item objectsstd::string isbn() const { return bookNo; }double avg_price() const;
// private members as before
private:std::string bookNo;      // implicitly initialized to the empty stringunsigned units_sold;double revenue;
};// used in chapter 10
inline
bool compareIsbn(const Sales_item &lhs, const Sales_item &rhs)
{ return lhs.isbn() == rhs.isbn(); }// nonmember binary operator: must declare a parameter for each operand
Sales_item operator+(const Sales_item&, const Sales_item&);inline bool
operator==(const Sales_item &lhs, const Sales_item &rhs)
{// must be made a friend of Sales_itemreturn lhs.units_sold == rhs.units_sold &&lhs.revenue == rhs.revenue &&lhs.isbn() == rhs.isbn();
}inline bool
operator!=(const Sales_item &lhs, const Sales_item &rhs)
{return !(lhs == rhs); // != defined in terms of operator==
}// assumes that both objects refer to the same ISBN
Sales_item& Sales_item::operator+=(const Sales_item& rhs)
{units_sold += rhs.units_sold; revenue += rhs.revenue; return *this;
}// assumes that both objects refer to the same ISBN
Sales_item
operator+(const Sales_item& lhs, const Sales_item& rhs)
{Sales_item ret(lhs);  // copy (|lhs|) into a local object that we'll returnret += rhs;           // add in the contents of (|rhs|) return ret;           // return (|ret|) by value
}std::istream&
operator>>(std::istream& in, Sales_item& s)
{double price;in >> s.bookNo >> s.units_sold >> price;// check that the inputs succeededif (in)s.revenue = s.units_sold * price;else s = Sales_item();  // input failed: reset object to default statereturn in;
}std::ostream&
operator<<(std::ostream& out, const Sales_item& s)
{out << " bookNo "<< s.isbn() << " soldNum " << s.units_sold << " totalSoldMoney "<< s.revenue << " price " << s.avg_price();return out;
}double Sales_item::avg_price() const
{if (units_sold) return revenue/units_sold; else return 0;
}
#endif复制代码

下面开始编写代码:

hello.cc

#include <iostream>
#include "Sales_item.h"
int main()
{Sales_item totalBook;//统计对象if(std::cin>>totalBook){//第一次读取的值赋值给totalBookSales_item  currBook;//当前对象while(std::cin>>currBook)//从第二次开始,读取的值赋给currBook{if(totalBook.isbn()==currBook.isbn())//上一次读取的对象与当前读取的对象书本编号相等,就叠加{totalBook += currBook;}else{//否则就打印上次书本统计的结果,接着又把当前读取的对象赋值给统计对象std::cout<<totalBook<<std::endl;totalBook = currBook;}}std::cout<<totalBook<<std::endl;//打印最后统计的值}else{std::cerr<<"no data!!!"<<std::endl;return -1;}return 0;}复制代码

逻辑类似于上一个练习,就不一一分析了。

接下来就是编译阶段

1:生成可执行程序

window下会生成book.exe可执行程序

ps:上述的所有文件都在统一目录下,这点很重要,不然会报错

2:读取销售记录文件以及程序输出到文件中

这里用到文件重定向的机制,这种机制允许我们将标准输入标准输出命名文件关联起来

book <book_sales >print_file

bookbook.exe执行程序,<book_sales代表从book_sales读取输入,>print_file代表程序标准输出到print_file文件中

经过这一系列,在同级目录下,你会看到生成的print_file,内容如下:

 bookNo 0-201-70353-X soldNum 4 totalSoldMoney 99.96 price 24.99bookNo 0-201-82470-1 soldNum 4 totalSoldMoney 181.56 price 45.39bookNo 0-201-88954-4 soldNum 16 totalSoldMoney 198 price 12.375bookNo 0-399-82477-1 soldNum 5 totalSoldMoney 226.95 price 45.39bookNo 0-201-78345-X soldNum 5 totalSoldMoney 110 price 22复制代码

转载于:https://juejin.im/post/5a6dd6e6f265da3e317e6805

实例:从一个文件中读取销售记录,并合并生成销售报告,实现销售册数,总销售额,平均售价...相关推荐

  1. MATLAB中从一个文件中读取指定的行

    function readData_write() %从指定行开始读取数据 readFilePath='C:\Second_teeth\data\di_tance\Diff_distance\sand ...

  2. python读取一个图像_从图像处理python的文件中读取多个图像

    嗨,我有一个脚本来运行图像处理.但是我想用一个循环或者其他方法从一个文件中读取多个图像 例如C:\Users\student\Desktop\Don\program (opencv version)\ ...

  3. c语言fscanf读取csv文件,使用fscanf函数从.csv文件中读取int

    我试图从.csv文件中读取一些整数.我不知道到底有多少整数是每行的文件中,我只知道,所有的csv文件是一样的东西:使用fscanf函数从.csv文件中读取int 1,2,3,9,6,3,4 4,5,6 ...

  4. java基础IO流使用读取一个文件中的文字输出到控制台上

    读取一个文件中的文字输出到控制台上 import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IO ...

  5. 从文件中读取一个long型数_Python 从文件中读取数据

    问题:在python中如何从文件中读取数据,比如有一个mydata.txt文件包含10000行,50列的数据,想提取某几列出来,比如1, 3,5列. 方法一,编一个读取数据的函数. import js ...

  6. Excel VBA 循环读取一个目录下面多个文件的内容,放到另外一个文件中

    循环读取一个目录下面多个文件的内容,放到另外一个文件中 Public Sub getInputInfo()On Error GoTo errl'OUT対象ファイル開くDim wbOut As Work ...

  7. c语言把一个字符从指定文件中读取的函数,C语言中对文件最基本的读取和写入函数...

    C语言中对文件最基本的读取和写入函数 C语言read()函数:读文件函数(由已打开的文件读取数据)头文件: #include 定义函数: ssize_t read(int fd, void * buf ...

  8. 编写图形界面程序,接受用户输入的5个浮点数据和一个文件目录名,将这五个数据保存在该文件中,再从文件中读取出来并且进行从大到小排序,然后再一次追加保存在该文件中。

    File(文件特征与管理):用于文件或者目录的描述信息,例如生成新目录,修改文件名,删除文件,判断文件所在路径等. InputStream(二进制格式操作):抽象类,基于字节的输入操作,是所有输入流的 ...

  9. 从文件中读取数据,排序之后输出到另一个文件中

    文件中有一组数据,要求排序后输出到另一个文件中去 主要有两个知识点: 排序.文件操作 C++/C代码如下: [cpp] view plaincopy #include<iostream> ...

最新文章

  1. asp python 定时任务_Ubuntu使用crontab定时执行python脚本
  2. 快手诉“短视频人气助手”软件不正当竞争:索赔100万元
  3. react-redux草稿
  4. 汽车上的一些名词缩写解释
  5. background-image 与 img 动画性能对比
  6. MQTT工作笔记0007---剩余长度
  7. 43. Element hasAttributes() 方法
  8. 新概念英语2电子版_新概念英语读100遍,英语能超神
  9. c语言 turbo,C语言Turbo C 2.0内容介绍
  10. day55-负载均衡之lvs
  11. 计算机网络教程第五版|微课版 - 第四章 网络层 - 重点概念【补充】
  12. win11使用excel卡顿怎么办 Windows11使用excel卡死的解决方法
  13. 计算机房无管网消防中七氟丙烷的药剂用量
  14. Leetcode每日一题2020.11.13第328题:奇偶链表
  15. 对抗生成网络学习(十六)——stackGAN++利用文字生成鸟类图片(tensorflow实现)(未完待续)
  16. 穷人python入门教程视频_《穷》字意思读音、组词解释及笔画数 - 新华字典 - 911查询...
  17. 河大计算机学院足球队,我校第二十九届“河大杯”足球赛落幕
  18. 用java实现表白二维码(附源码)
  19. DC初级摄友必学摄影技巧
  20. Python中使用matplotlib画图时各种大小设置

热门文章

  1. vue根据指令动态改变title名字
  2. svn移动目录时如何保留原来的日志
  3. 小甲鱼OD学习第18讲
  4. 一行或多行文本内容溢出显示省略号
  5. LintCode: Median of two Sorted Arrays
  6. 2015 多校赛 第三场 1002 (hdu 5317)
  7. 加速Java应用开发速度3——单元/集成测试+CI
  8. ios 6.1中 Release问题
  9. Flex整合Spring
  10. AttributeError: module 'pymysql' has no attribute 'escape' 错误的出现以及解决