C++Primer第五版 习题答案 第八章 IO库

终于到第二部分,给爷冲,干就完了!

8.1

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

8.2

#include<iostream>
#include<string>

std::istream& iofunction(std::istream& is)
{
	std::string s;
	while (is>>s)
	{
		std::cout << s << std::endl;
	}
	is.clear();
	return is;
}
int main() 
{
	iofunction(std::cin);
	std::string s1;
	while (std::cin>>s1)
	{
		std::cout << s1 << std::endl;
	}
	return 0;

}

8.3

如果badbit、failbit、eofbit任何一个被置位,则检测流状态的条件会失败;

8.4

“using namespace std” is YYDS!

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

using namespace std;

int main() 
{
	ifstream ifs("data");
	vector<string> v1;
	string buf;
	if (ifs)
	{
		while (getline(ifs,buf))
		{
			v1.push_back(buf);
		}
	}
	for (auto& s : v1)
		cout << s << " ";
	cout << endl;
	return 0;
}

8.5

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

using namespace std;

int main() 
{
	ifstream ifs("data");
	vector<string> v1;
	string buf;
	if (ifs)
	{
		while (getline(ifs,buf))
		{
			v1.push_back(buf);
		}
	}
	for ( const auto& s : v1)
		cout << s << " ";
	cout << endl;
	return 0;
}

8.6

#include <iostream>
#include <string>
#include <fstream>
#include "../ch07_Classes/Sales_data_ex26.h"

int main(int argc, char **argv)
{
    std::ifstream ifs(argv[1]);

    if(!ifs) return 1;

    Sales_data total(ifs);

    if (!total.isbn().empty())
    {
        Sales_data trans;

        while (read(ifs, trans))
        {
            if (total.isbn() == trans.isbn())
            {
                total.combine(trans);
            }
            else
            {
                print(std::cout, total);
                std::cout << std::endl;
                total = trans;
            }
        }
        print(std::cout, total);
        std::cout << std::endl;

        return 0;
    }
    else
    {
        std::cerr << "No data?!" << std::endl;
        return -1;  // indicate failure
    }
}

8.7

#include <iostream>
#include <string>
#include <fstream>
#include "../ch07_Classes/Sales_data_ex26.h"

int main(int argc, char **argv)
{
    std::ifstream ifs(argv[1]);
    std::ofstream ofs(argv[2]);

    if(!ifs) return 1;

    Sales_data total(ifs);

    if (!total.isbn().empty())
    {
        Sales_data trans;

        while (read(ifs, trans))
        {
            if (total.isbn() == trans.isbn())
            {
                total.combine(trans);
            }
            else
            {
                print(ofs, total);
                ofs << std::endl;
                total = trans;
            }
        }
        print(ofs, total);
        ofs << std::endl;

        return 0;
    }
    else
    {
        std::cerr << "No data?!" << std::endl;
        return -1;  // indicate failure
    }
}

8.8

#include <iostream>
#include <string>
#include <fstream>
#include "../ch07_Classes/Sales_data_ex26.h"

int main(int argc, char **argv)
{
    std::ifstream ifs(argv[1]);
    std::ofstream ofs(argv[2], std::ofstream::app);

    if(!ifs) return 1;

    Sales_data total(ifs);

    if (!total.isbn().empty())
    {
        Sales_data trans;

        while (read(ifs, trans))
        {
            if (total.isbn() == trans.isbn())
            {
                total.combine(trans);
            }
            else
            {
                print(ofs, total);
                ofs << std::endl;
                total = trans;
            }
        }
        print(ofs, total);
        ofs << std::endl;

        return 0;
    }
    else
    {
        std::cerr << "No data?!" << std::endl;
        return -1;  // indicate failure
    }
}

8.9

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

std::istream& iofunction(std::istream& is)
{
	std::string s;
	while (is >> s)
	{
		std::cout << s << std::endl;
	}
	is.clear();
	return is;
}
int main()
{
	std::istringstream istrs("aa\nbb\ncc\ndd");
	iofunction(istrs);
	std::string s1;
	while (std::cin >> s1)
	{
		std::cout << s1 << std::endl;
	}
	return 0;

}

8.10

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

using namespace std;

int main()
{
	vector<string>v1;
	ifstream is("book_sales");

	string buf;
	if (!is)
	{
		cerr << "open error" << endl;
		return -1;
	}
	while (getline(is,buf))
	{
		v1.push_back(buf);
	}

	for (const auto& s : v1)
	{
		istringstream iss(s);
		while (iss >> buf)
			cout << buf << endl;
	}
	return 0;
}

8.11

#include<iostream>
#include<sstream>
#include<fstream>
#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))
	{
		record.str(line);
		PersonInfo info;
		record >> info.name;
		while (record>>word)
		{
			info.phones.push_back(word);
		}
		record.clear();
		people.push_back(info);

		for (const auto& person : people)
		{
			cout << person.name << endl;

			for (const auto& ph : person.phones)
				cout << ph << " ";
			cout << endl;
		}
	}

	return 0;
}

8.12

string和vector可以自己初始化,并不会出现未定义的情况;

8.13

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

using namespace std;

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

bool valid(const string& s)
{
	for (const auto c : s)
		if (!isdigit(c)) return false;
	return true;
}

string format(const string& s)
{
	return s;
}

int main()
{
	string line, word;
	vector<PersonInfo> people;
	istringstream record;
	ifstream ifs("personinfo");
	ofstream ofs("personinfo_new");

	while (getline(ifs, line))
	{
		record.str(line);
		PersonInfo info;
		record >> info.name;
		while (record >> word)
			info.phones.push_back(word);
		record.clear();
		people.push_back(info);
	}

	for (const auto& person : people)
	{
		ostringstream formatted, badNums;
		for (const auto& ph : person.phones)
		{
			if (!valid(ph))
			{
				badNums << " " << ph;
			}
			else
				formatted << " " << format(ph);
		}
		if (badNums.str().empty())
			ofs << person.name << " " << formatted.str() << endl;
		else
			cerr << " input error: " << person.name << " invalid number(s)" << badNums.str() << endl;
	}

	return 0;
}

8.14

它们都是类类型,因此使用引用可以避免拷贝,提高效率;
在循环语句中不改变它们的值,因此用const;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值