《Primer C++》第五版习题:第八章

 8.1

istream& read(istream& i) {
    string s;
    while (i >> s) {
        //...
    }
    i.clear();
    return i;
}

8.2略

8.3

cin流的状态处于错误,比如eofbit,badbit,failbit

 8.4

void ReadFileToVec(const string& fileName, vector<string>& vec)
{
    ifstream ifs(fileName);
    if (ifs)
    {
        string buf;
        while (std::getline(ifs, buf))//fstream也可以用getline
            vec.push_back(buf);
    }
}

8.5

void ReadFileToVec(const string& fileName, vector<string>& vec)
{
    ifstream ifs(fileName);
    if (ifs)
    {
        string buf;
        while (ifs >> buf);
            vec.push_back(buf);
    }
}

8.6

记得保存好自己的Sales_data,因为这本书可惦记这个类了。

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?!" << endl;
    }
    
    return 0;
}

8.7 

int main(int argc, char **argv)
{
    ifstream input(argv[1]);
    ofstream output(argv[2]);//多加了这一句
    
    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?!" << endl;
    }
    
    return 0;
}

8.8


int main(int argc, char **argv)
{
    ifstream input(argv[1]);
    ofstream output(argv[2],ofstream::app);//默认是out,所以只写了一个app
    
    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?!" << endl;
    }
    
    return 0;
}

8.9

istream& read(istream& i) {
    string s;
    while (i >> s) {
        cout << s << endl;
    }
    i.clear();
    return i;
}
int main()
{
    istringstream is("ds d s d");
    read(is);
    return 0;
}   

    结果如下

8.10

void read(string pathname) {
    vector<string> vec;
    fstream f(pathname, ifstream::in);
    if (!f) {
        return;
    }
    string s;
    while (getline(f,s))
        vec.push_back(s);
    for (const auto& str : vec) {
        istringstream ss(str);
        string word;
        while(ss >> word)
            cout << word << " ";
    }
}

8.11

string line, word;
    vector<PersonInfo> people;
    istringstream record;
    while (getline(cin, line))
    {
        PersonInfo info;
        record.clear();//每次都得清除
        record.str(line);
        record >> info.name;
        while (record >> word)
            info.phones.push_back(word);
        people.push_back(info);
    }

8.12

因为需要的是一个聚合类,所以不应该使用类内初始化。

 8.13

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>

using std::vector; using std::string; using std::cin; using std::istringstream;
using std::ostringstream; using std::ifstream; using std::cerr; using std::cout; using std::endl;
using std::isdigit;

struct PersonInfo {
    string name;
    vector<string> phones;
};

bool valid(const string& str)
{
    return isdigit(str[0]);
}

string format(const string& str)
{
    return str.substr(0,3) + "-" + str.substr(3,3) + "-" + str.substr(6);
}

int main()
{
    ifstream ifs("../data/phonenumbers.txt");
    if (!ifs)
    {
        cerr << "no phone numbers?" << endl;
        return -1;
    }

    string line, word;
    vector<PersonInfo> people;
    istringstream record;
    while (getline(ifs, line))
    {
        PersonInfo info;
        record.clear();
        record.str(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;
    }

    return 0;
}

8.14

因为我们不会修改它们,所以定义成const

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值