Chapter 8: The I/O Library

Exercises Section 8.1.2

Ex8.1 8.2

#include<iostream>
#include<string>

std::istream &read_Write(std::istream &is)
{
    std::string s;
    while (is >> s)
        std::cout << s << std::endl;
    is.clear();
    return is;
}

int main()
{
    read_write(std::cin);
    system("pause");
    return 0;
}

Ex8.3

输入了 ctrl + z 或者输入不符合类型的值

Exercises Section 8.2.1

Ex8.4

void read_File(const string &filename, vector<string> &vec)
{
    std::ifstream ifsm(filename);
    if (ifsm)
    {
        std::string s;
        while (std::getline(ifsm, s))
            vec.push_back(s);
    }
}

Ex8.5

void read_File(const string &filename, vector<string> &vec)
{
    std::ifstream ifsm(filename);
    if (ifsm)
    {
        std::string s;
        while (ifsm >> s)
            vec.push_back(s);
    }
}

Ex8.6

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

int main(int argc, char *argv[])
{
    std::ifstream input(argv[1]);
    Sales_data taotal;
    if (read(input, total))
    {
        Sales_data trans;
        while (read(input, trans))
        {
            if (total.isbn() == trans.isbn())
                total.combine(trans);
            else 
            {
                print(std::cout, total) << std::endl;
                total = trans;
            }
        }
        print(std::cout, total) << std::endl;
    }
    else 
        std::cerr << "No Data?!" << std::endl;
    system("pause");
    return 0;
}

Exercises Section 8.2.2

Ex8.7

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

int main(int argc, char *argv[])
{
    std::ifstream input(argv[1]);
    std::ofstream output(argv[2]);
    Sales_data taotal;
    if (read(input, total))
    {
        Sales_data trans;
        while (read(input, trans))
        {
            if (total.isbn() == trans.isbn())
                total.combine(trans);
            else 
            {
                print(output, total) << std::endl;
                total = trans;
            }
        }
        print(output, total) << std::endl;
    }
    else 
        std::cerr << "No Data?!" << std::endl;
    system("pause");
    return 0;
}

Ex8.8

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

int main(int argc, char *argv[])
{
    std::ifstream input(argv[1]);
    std::ofstream output(argv[2], std::ofstream::app);
    Sales_data taotal;
    if (read(input, total))
    {
        Sales_data trans;
        while (read(input, trans))
        {
            if (total.isbn() == trans.isbn())
                total.combine(trans);
            else 
            {
                print(output, total) << std::endl;
                total = trans;
            }
        }
        print(output, total) << std::endl;
    }
    else 
        std::cerr << "No Data?!" << std::endl;
    system("pause");
    return 0;
}

Exercises Section 8.3.1

Ex8.9

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

using namespace std;

istream &read_Write(istream &is)
{
    string s;
    while (is >> s)
        cout << s << endl;
    is.clear();
    return is;
}

int main()
{
    istringstream is("Hello World");
    read_Write(is);
    system("pause");
    return 0;
}

Ex8.10

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

using namespace std;

void read_File(const string &filename, vector<string> &vec)
{
    ifstream ifs(filename);
    string line;
    if (ifs)
    {
        while (getline(ifs, line))
            vec.push_back(line);
    }
}

int main()
{   
    vector<string> vec;
    read_File("data.txt", vec);
    for (auto &s : vec)
    {
        istringstream iss(s);
        string word;
        while (iss >> word)
            cout << word << endl;
    }
    system("pause");
    return 0;
}

Ex8.11

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

using namespace std;

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

int main()
{
	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);
	}
	for (auto& p : people)
	{
		std::cout << p.name << " ";
		for (auto& s : p.phones) std::cout << s << " ";
		std::cout << std::endl;
	}
    system("pause");
	return 0;
}

Ex8.12

有默认构造函数初始化

Exercises Section 8.3.2

Ex8.13

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

using namespace std;

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.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;
	}
    system("pause");
	return 0;
}

Ex8.14

只是使用该引用对象的值而不改变该引用的对象的值,故而声明为 const auto &
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值