C++Primer(第八章课后习题程序题源代码)

8.1

#include<iostream>
#include<stdexcept>

using namespace std;

istream & f(istream & in)
{
    int v;
    while (cin >> v, !in.eof())
    {
        if (in.bad())
            throw runtime_error("IO流错误");
        if (in.fail())
        {
            cerr << "数据错误,请重试:" << endl;
            in.clear();
            in.ignore(100, '\n');
            continue;
        }
        cout << v << endl;
    }
    in.clear();
    return in;
}

int main()
{
    cout << "请输入一些整数,按Ctrl+Z结束" << endl;
    f(cin);
    return 0;
}

8.4

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

using namespace std;

int main()
{
    ifstream in("data");
    if (!in)
    {
        cerr << "无法打开输入文件" << endl;
        return -1;
    }
    string line;
    vector<string> words;
    while (getline(in, line))
    {
        words.push_back(line);
    }
    in.close();
    vector<string>::const_iterator it = words.begin();
    while (it != words.end())
    {
        cout << *it << endl;
        ++it;
    }

    return 0;
}

8.6

#include<iostream>
#include<fstream>
#include"Sales_data.h"

using namespace std;

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        cerr << "请给出文件名" << endl;
        return -1;
    }
    ifstream in(argv[1]);
    if (!in)
    {
        cerr << "无法打开输入文件" << endl;
        return -1;
    }
    Sales_data total;
    if (read(in, total))
    {
        Sales_data trans;
        while (read(in, trans))
        {
            if (total.isbn() == trans.isbn())
                total.combine(trans);
            else
            {
                print(cout, total) << endl;
                total = trans;
            }
        }
        print(cout, total) << endl;
    }
    else
    {
        cerr << "没有数据" << endl;
    }
    return 0;
}

8.7

#include<iostream>
#include<fstream>
#include"Sales_data.h"

using namespace std;

int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        cerr << "请给出输入,输出文件名" << endl;
        return -1;
    }
    ifstream in(argv[1]);
    if (!in)
    {
        cerr << "无法打开输入文件" << endl;
        return -1;
    }
    ofstream out(argv[2]);
    if (!out)
    {
        cerr << "无法打开输出文件" << endl;
        return -1;
    }
    Sales_data total;
    if (read(in, total))
    {
        Sales_data trans;
        while (read(in, trans))
        {
            if (total.isbn() == trans.isbn())
                total.combine(trans);
            else
            {
                print(cout, total) << endl;
                total = trans;
            }
        }
        print(cout, total) << endl;
    }
    else
    {
        cerr << "没有数据" << endl;
    }
    return 0;
}

8.9

#include<iostream>
#include<sstream>
#include<string>
#include<stdexcept>

using namespace std;

istream& f(istream & in)
{
    string v;
    while (in >> v, !in.eof())
    {
        if (in.bad())
        {
            throw runtime_error("IO流错误");
        }
        if (in.fail())
        {
            cerr << "数据错误,请重试:" << endl;
            in.clear();
            in.ignore(100, '\n');
            continue;
        }
        cout << v << endl;
    }
    in.clear();
    return in;
}

int main()
{
    ostringstream msg;
    msg << "C++ Primer第五版" << endl;
    istringstream in(msg.str());
    f(in);
    return 0;
}

8.10

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

using namespace std;

int main()
{
    ifstream in("data");
    if (!in)
    {
        cerr << "无法打开输入文件" << endl;
        return -1;
    }
    string line;
    vector<string> words;
    while (getline(in, line))
    {
        words.push_back(line);
    }
    in.close();
    vector<string>::const_iterator it = words.begin();
    while (it != words.end())
    {
        istringstream line_str(*it);
        string word;
        while (line_str>>word)
        cout << word << " ";
        cout << endl;
        ++it;
    }
    return 0;
}

8.11

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

using namespace std;

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

int main()
{
    string line, word;
    vector<PersonInfo>people;
    stringstream 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);
    }
    return 0;
}

8.12

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

using namespace std;

struct PersonInfo
{
    string name;
    vector<string> phones;
};
string format(const string &s)
{
    return s;
}
bool valid(const string&s)
{
    return true;
}
int main(int argc,char *argv[])
{
    string line, word;
    vector<PersonInfo>people;
    stringstream record;

    if (argc != 2)
    {
        cerr << "请给出文件名" << endl;
        return -1;
    }
    ifstream in(argv[1]);
    if (!in)
    {
        if (!in){
            cerr << "无法打开输入文件" << endl;
        }
    }

    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);
    }
    ostringstream os;
    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())
            os << entry.name << " "
            << formatted.str() <<endl;
        else
            cerr << "input error:"<<entry.name
            << "invalid number(s)" << badNums.str() << endl;
    }
    cout << os.str() << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值