C++primer 8.2.1节练习

练习8.4

 1 #include <string>
 2 #include <iostream>
 3 #include <vector>
 4 #include <fstream>
 5 
 6 using namespace std;
 7 
 8 void read(string s);
 9 
10 int main()
11 {
12     read("test.txt");
13     system("pause");
14     return 0;
15 }
16 
17 void read(string s)
18 {
19     string line;
20     vector<string> text;
21     ifstream in(s);
22     while (!in.eof())
23     {
24         getline(in, line);
25         text.push_back(line);
26     }
27     for (auto c : text)
28         cout << c << endl;
29 }

练习8.5

 1 #include <string>
 2 #include <iostream>
 3 #include <vector>
 4 #include <fstream>
 5 
 6 using namespace std;
 7 
 8 void read(string s);
 9 
10 int main()
11 {
12     read("title.txt");
13     system("pause");
14     return 0;
15 }
16 
17 void read(string s)
18 {
19     string line;
20     vector<string> text;
21     ifstream in(s);
22     while (!in.eof())
23     {
24         in >> line;
25         text.push_back(line);
26     }
27     for (auto c : text)
28         cout << c << endl;
29 }

练习8.6

 1 int main()                                                                            //main函数的部分需要改变
 2 {
 3     ifstream in("sales_data.txt");
 4     //改动地方,新增一个Sales_data.txt 
 5     Sales_data total;
 6     if (read(in, total)) {
 7         //read(cin,total); 变为read(in,total); 
 8         Sales_data trans;
 9         while (read(in, trans)) {
10             //read(cin, total); 变为read(in,total); 
11             if (total.isbn() == trans.isbn())
12                 total.combine(trans);
13             else
14             {
15                 print(cout, total) << endl;
16                 total = trans;
17             }
18         }
19         print(cout, total) << endl;
20     }
21     else {
22         cerr << "No data?!" << endl;
23     }
24     system("pause");
25     return 0;
26 }

 

转载于:https://www.cnblogs.com/wuyinfenghappy/p/7308987.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值