【C++ Primer Plus】编程练习答案——第17章

 1 // chapter17.h
 2 
 3 #ifndef LEARN_CPP_CHAPTER17_H
 4 #define LEARN_CPP_CHAPTER17_H
 5 
 6 #include <iostream>
 7 #include <fstream>
 8 #include <vector>
 9 #include <algorithm>
10 #include <cstring>
11 
12 void ch17_1();
13 void ch17_2(const std::string filename);
14 void ch17_3(const std::string infilename, const std::string outfilename);
15 void ch17_4(const std::string infilename1, const std::string infilename2, const std::string outfilename);
16 void ch17_5(const std::string matfile, const std::string patfile, const std::string matnpatfile);
17 void ch17_6();
18 void ShowStr(const std::string & str);
19 void GetStrs(std::ifstream & fin, std::vector<std::string> & vistr);
20 void ch17_7();
21 
22 class Store {
23 private:
24     char * str;
25     std::ofstream * outf;
26 public:
27     Store(std::ofstream & fout) : outf(&fout) {
28         str = new char[1024];
29     }
30     Store(const Store & s) {
31         if (this == &s)
32             delete [] str;
33         str = new char[1024];
34         strcpy(str, s.str);
35         outf = s.outf;
36     }
37     bool operator()(const std::string & s) {
38         int len = s.size();
39         if (outf->is_open()) {
40             outf -> write((char *)&len, sizeof(int));
41             outf -> write(s.data(), len);
42             return true;
43         }
44         else {
45             exit(EXIT_FAILURE);
46             return false;
47         }
48     }
49 };
50 
51 
52 #endif //LEARN_CPP_CHAPTER17_H
  1 // chapter17.cpp
  2 
  3 #include "chapter17.h"
  4 
  5 void ch17_1() {
  6     using namespace std;
  7     int count = 0;
  8     char ch;
  9     cout << "input some sentences: " << endl;
 10     while (cin.get(ch)) {
 11         if (ch == '$')
 12             break;
 13         ++ count;
 14     }
 15     cout << count << " chars before '$'" << endl;
 16 }
 17 
 18 void ch17_2(const std::string filename) {
 19     using namespace std;
 20     ofstream outFile(filename, ios_base::out | ios_base::trunc);
 21     if (!outFile.is_open())
 22         cerr << "file not opened" << endl;
 23     char ch;
 24     while ((ch = cin.get()) != EOF) {
 25         outFile << ch;
 26     }
 27     outFile.close();
 28 }
 29 
 30 void ch17_3(const std::string infilename, const std::string outfilename) {
 31     using namespace std;
 32     ifstream inFile;
 33     ofstream outFile;
 34     //inFile.open(infilename, ios_base::in);
 35     //outFile.open(outfilename, ios_base::out | ios_base::trunc);
 36     inFile.open(infilename, ios_base::binary | ios_base::in);
 37     outFile.open(outfilename, ios_base::binary | ios_base::out | ios_base::trunc);
 38     if (!inFile.is_open() || !outFile.is_open()) {
 39         cerr << "file not opened" << endl;
 40         exit(1);
 41     }
 42     char ch;
 43 //    while (inFile.get(ch)) {
 44 //        outFile << ch;
 45 //        cout << ch;
 46 //    }
 47     while (!inFile.eof()) {
 48         inFile.read(&ch, sizeof(char));
 49         if (inFile.eof()) break; // 去掉多余的一行
 50         outFile.write(&ch, sizeof(char));
 51     }
 52     inFile.close();
 53     outFile.close();
 54     cout << "done" << endl;
 55 }
 56 
 57 void ch17_4(const std::string infilename1, const std::string infilename2, const std::string outfilename) {
 58     using namespace std;
 59     ifstream inFile1, inFile2;
 60     ofstream outFile;
 61     inFile1.open(infilename1, ios_base::in);
 62     inFile2.open(infilename2, ios_base::in);
 63     outFile.open(outfilename, ios_base::out | ios_base::trunc);
 64     if (!inFile1.is_open() || !inFile2.is_open() || !outFile.is_open()) {
 65         cerr << "file not opened" << endl;
 66         exit(1);
 67     }
 68     while (!inFile1.eof() && !inFile2.eof()) {
 69         char temp1[1024], temp2[1024];
 70         inFile1.getline(temp1, 1024);
 71         inFile2.getline(temp2, 1024);
 72         outFile << temp1 << ' ' << temp2 << endl;
 73     }
 74     char temp[1024];
 75     while (inFile1.getline(temp, 1024))
 76         outFile << temp;
 77     while (inFile2.getline(temp, 1024))
 78         outFile << temp;
 79     inFile1.close();
 80     inFile2.close();
 81     outFile.close();
 82     cout << "done" << endl;
 83 
 84 }
 85 
 86 void ch17_5(const std::string matfile, const std::string patfile, const std::string matnpatfile) {
 87     using namespace std;
 88     ifstream inMat, inPat;
 89     ofstream outMnP;
 90     inMat.open(matfile, ios_base::in);
 91     inPat.open(patfile, ios_base::in);
 92     outMnP.open(matnpatfile, ios_base::out | ios_base::trunc);
 93     if (!inMat.is_open() || !inPat.is_open() || !outMnP.is_open()) {
 94         cerr << "file not opened" << endl;
 95         exit(1);
 96     }
 97     vector<string> mat, pat;
 98     char temp[1024];
 99     while (inMat.getline(temp, 1024))
100         mat.push_back(temp);
101     while (inPat.getline(temp, 1024))
102         pat.push_back(temp);
103     sort(mat.begin(), mat.end());
104     sort(pat.begin(), pat.end());
105     vector<string> mer(mat.size() + pat.size());
106     merge(mat.begin(), mat.end(), pat.begin(), pat.end(), mer.begin());
107     for (auto i = mer.begin(); i < mer.end(); ++ i) {
108         if (i < mer.end() && *i == *(i + 1))
109             continue;
110         outMnP << *i << endl;
111     }
112     inMat.close();
113     inPat.close();
114     outMnP.close();
115     cout << "done" << endl;
116 }
117 
118 void ch17_6() {
119     using namespace std;
120   // 瑞了,不想写了
121 }
122 
123 void ShowStr(const std::string & str) {
124     using namespace std;
125     cout << str << endl;
126 }
127 
128 void GetStrs(std::ifstream & fin, std::vector<std::string> & vistr) {
129     int len;
130     while (fin.read((char *)&len, sizeof(int))) {
131         char * temp = new char[len];
132         fin.read(temp, len);
133         vistr.push_back(temp);
134         delete [] temp;
135     }
136 }
137 
138 void ch17_7() {
139     using namespace std;
140     vector<string> vostr;
141     string temp;
142     // acquire strings
143     cout << "Enter strings (empty line to quit) :" << endl;
144     while (getline(cin, temp) && temp[0] != '\0')
145         vostr.push_back(temp);
146     cout << "Here is your input." << endl;
147     for_each(vostr.begin(), vostr.end(), ShowStr);
148     // store in a file
149     ofstream fout("./C++PrimerPlus/testfiles/strings.dat", ios_base::out | ios_base::binary);
150     for_each(vostr.begin(), vostr.end(), Store(fout));
151     fout.close();
152     // recover file contents
153     vector<string> vistr;
154     ifstream fin("./C++PrimerPlus/testfiles/strings.dat", ios_base::in | ios_base::binary);
155     if (!fin.is_open()) {
156         cerr << "Could not open file for input." << endl;
157         exit(EXIT_FAILURE);
158     }
159     GetStrs(fin, vistr);
160     cout << endl << "Here are the strings read from the file:" << endl;
161     for_each(vistr.begin(), vistr.end(), ShowStr);
162 }

 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数 字数字数

上一篇:【Mybatis-Plus进阶学习(八)】SQL注入器


下一篇:uni-app实现uniPush推送功能(Android)