C++ Primer(第五版)课后习题记录 —— 第八章

第八章 IO库

练习8.1

istream& input(istream& in) {
    std::string buff;
    while(in >> buff)
        std::cout << buff;
    in.clear();
    return in;
}

练习8.2

int main()
{
    istream& in = input(cin);
    cout << "----------" <<endl;
    cout << in.good() << endl;
    return 0;
}

练习8.3

badbit、failbit 和 eofbit 中的任意一个被置位,检测流状态的条件就会失败。

练习8.4

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
    vector<string> ivec;
    ifstream in("8_4");
    if(in) {
        string put;
        while (in) {
            getline(in, put);
            ivec.push_back(put);
        }
    }
    for (auto c : ivec)
        cout << c <<endl;
    return 0;
}

练习8.5

int main()
{
    vector<string> ivec;
    ifstream in("8_4");
    if(in) {
        string put;
        while (in) {
            in >> put;   //只改了这句就行
            ivec.push_back(put);
        }
    }
    for (auto c : ivec)
        cout << c <<endl;
    return 0;
}

练习8.6

#include <iostream>
#include <string>
#include <fstream>
#include "Sales_data.h"
using namespace std;
int main(int argc, char** argv)
{
    ifstream input(argv[1]);
    Sales_data total;
    if (read(input, total)) {
        Sales_data trans;
        while (read(input, trans)) {
            if (total.isbn() == trans.isbn())
                total.combine(trans);
            else {
                print(cout, total) << endl;
                total = trans;
            }
        }
        print(cout, total) << endl;
    }
    else {
        cerr << "No data?!" << std::endl;
        return -1;
    }
    return 0;
}

练习8.7

#include <iostream>
#include <fstream>
#include "Sales_data.h"
using namespace std;
int main(int argc, char** argv)
{
    ifstream input(argv[1]);
    ofstream output(argv[2], ofstream::out);
    Sales_data total;
    if (read(input, total)) {
        Sales_data trans;
        while (read(input, trans)) {
            if (total.isbn() == trans.isbn())
                total.combine(trans);
            else {
                print(output, total) << endl;
                total = trans;
            }
        }
        print(output, total) << endl;
    }
    else {
        cerr << "No data?!" << std::endl;
        return -1;
    }
    return 0;
}

练习8.8

将 output 打开的文件模式设定如下
ofstream output(argv[2], ofstream::out | ofstream::app);
即可。

练习8.9

#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
istream& input(istream& in) {
    std::string buff;
    while(in >> buff)
        std::cout << buff;
    in.clear();
    return in;
}

int main()
{
    istringstream ss("hola~11");
    input(ss);
    return 0;
}

练习8.10

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
int main()
{
    fstream input("8_4");
    vector<string> strs;
    string buf;
    while(getline(input, buf))
        strs.push_back(buf);
    for (auto c : strs) {
        istringstream buff(c);
        string bufff;
        while (buff >> bufff)
            cout << bufff << endl;
    }
}

练习8.11

//只录入改变部分
istringstream record;
while (getline(cin, line)) {
  PersonInfo info;
  record.str(line);
  record >> info.name;
  while (record >> word)
      info.phones.push_back(word);
  people.push_back(info);
  record.clear(); //重点是要重置流
}

练习8.12

如果类内初始化,最后生成的对象中就会有无意义的值。

练习8.13

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
struct PersonInfo {
    string name;
    vector<string> phones;
};
bool valid(string s) {
    return isdigit(s[0]);
}
string format(const string& str) {
    return "+" + str.substr(0,2) + " " + str.substr(2,3) + "-" + str.substr(5) ;
}
int main()
{
    fstream input("sstream");
    string line, word;
    vector<PersonInfo> people;
    while(getline(input, line)){
        PersonInfo info;
        istringstream record(line);
        record >> info.name;
        while (record >> word)
            info.phones.push_back(word);
        people.push_back(info);
    }
    for (const auto& entry : people) {
        ostringstream formatted, badNums;
        for (const auto& nums : entry.phones)
            if (!valid(nums))
                badNums << " " << nums;
            else
                formatted << " " << format(nums);
        if (badNums.str().empty())
            cout << entry.name << " " << formatted.str() << endl;
        else
            cerr << "input error: " << entry.name << " invalid number(s) "
                 << badNums.str() << endl;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值