博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
实例:从一个文件中读取销售记录,并合并生成销售报告,实现销售册数,总销售额,平均售价...
阅读量:7209 次
发布时间:2019-06-29

本文共 4549 字,大约阅读时间需要 15 分钟。

准备:

1:销售记录文件book_sales

2:书的描述类Sales_item.h

book_sales内容如下

0-201-70353-X 4 24.990-201-82470-1 4 45.390-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.390-399-82477-1 3 45.390-201-78345-X 3 20.000-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 
#include
class Sales_item {// these declarations are explained section 7.2.1, p. 270 // and in chapter 14, pages 557, 558, 561friend 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 type Sales_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 pointer Sales_item& operator+=(const Sales_item&); // operations on Sales_item objects std::string isbn() const { return bookNo; } double avg_price() const;// private members as beforeprivate: std::string bookNo; // implicitly initialized to the empty string unsigned units_sold; double revenue;};// used in chapter 10inlinebool compareIsbn(const Sales_item &lhs, const Sales_item &rhs) { return lhs.isbn() == rhs.isbn(); }// nonmember binary operator: must declare a parameter for each operandSales_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_item return 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 ISBNSales_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 ISBNSales_item operator+(const Sales_item& lhs, const Sales_item& rhs) { Sales_item ret(lhs); // copy (|lhs|) into a local object that we'll return ret += 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 succeeded if (in) s.revenue = s.units_sold * price; else s = Sales_item(); // input failed: reset object to default state return 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 
#include "Sales_item.h"int main(){ Sales_item totalBook;//统计对象 if(std::cin>>totalBook){//第一次读取的值赋值给totalBook Sales_item currBook;//当前对象 while(std::cin>>currBook)//从第二次开始,读取的值赋给currBook { if(totalBook.isbn()==currBook.isbn())//上一次读取的对象与当前读取的对象书本编号相等,就叠加 { totalBook += currBook; }else{//否则就打印上次书本统计的结果,接着又把当前读取的对象赋值给统计对象 std::cout<
<

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

接下来就是编译阶段

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.99 bookNo 0-201-82470-1 soldNum 4 totalSoldMoney 181.56 price 45.39 bookNo 0-201-88954-4 soldNum 16 totalSoldMoney 198 price 12.375 bookNo 0-399-82477-1 soldNum 5 totalSoldMoney 226.95 price 45.39 bookNo 0-201-78345-X soldNum 5 totalSoldMoney 110 price 22复制代码

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

你可能感兴趣的文章
php 获取客户端的浏览器信息
查看>>
JavaScript------获取表单信息
查看>>
R语言︱文本挖掘——词云wordcloud2包
查看>>
Chapter 3 Phenomenon——18
查看>>
hibernate+spring整合增删改事务错误
查看>>
怎样理解阻塞与非阻塞、同步与异步的区别?
查看>>
C语言不定参数
查看>>
Python第十五天 datetime模块 time模块 thread模块 threading模块 Queue队列模块 multiprocessing模块 paramiko模块 fab...
查看>>
在JavaScript中对HTML进行反转义
查看>>
面向服务的架构SOA
查看>>
WinForm 窗体初始位置篇
查看>>
PowerShell发送邮件(587)
查看>>
Chapter 3 Phenomenon——23
查看>>
自动化测试如何解决验证码的问题
查看>>
利用位操作的几道题目
查看>>
导入时如何定制spring-boot依赖项的版本
查看>>
31:字符串p型编码
查看>>
C语言 · 拿糖果
查看>>
如何启动和关闭MySQL?(四)
查看>>
Python3.x和Python2.x的区别
查看>>