C++ primer习题记录——第八章

8.1:

#include <iostream>
using namespace std;

istream& func(istream& is)
{
	string str;
	while (is >> str)
	{
		cout << str << endl;
	}
	cout << is.good() << endl;
	is.clear();
	cout << is.good() << endl;
	return is;
}
int main()
{
	func(cin);
	system("pause");
	return 0;
}

8.2:

#include <iostream>
using namespace std;

istream& func(istream& is)
{
	string str;
	while (is >> str)
	{
		cout << str << endl;
	}
	cout <<"复位前:" << is.good() << endl;
	is.clear();
	cout << "复位后: "<<is.good() << endl;
	return is;
}
int main()
{
	istream& is = func(cin);
	cout << is.rdstate() << endl;
	system("pause");
	return 0;
}

8.4:

#include <iostream>
using namespace std;
#include<fstream>
#include<vector>
#include<string>

void ReadFileToVs(const string& fileName, vector<string>& v)
{
	ifstream ifs(fileName);
	if (ifs)
	{
		string buf;
		while (getline(ifs, buf))
		{
			v.push_back(buf);
		}
	}
	else
	{
		cout << "文件打开失败" << endl;
	}
}
int main()
{
	vector<string>vs;
	ReadFileToVs("1.txt", vs);
	for (const auto& str : vs)
	{
		cout << str << endl;
	}
	system("pause");
	return 0;
}

8.5:

#include <iostream>
using namespace std;
#include<fstream>
#include<vector>
#include<string>

void ReadFileToVs(const string& fileName, vector<string>& v)
{
	ifstream ifs(fileName);
	if (ifs)
	{
		string buf;
		while (ifs >> buf)
		{
			v.push_back(buf);
		}
	}
	else
	{
		cout << "文件打开失败" << endl;
	}
}
int main()
{
	vector<string>vs;
	ReadFileToVs("1.txt", vs);
	for (const auto& str : vs)
	{
		cout << str << endl;
	}
	system("pause");
	return 0;
}

8.7:

那个argv【1】不太理解,只能先用"1.txt""2.txt"代替了,不然程序运行不了

#include <iostream>
using namespace std;
#include<string>
#include<fstream>
class Sales_data
{
	friend istream& read(istream& is, Sales_data& item);
	friend ostream& print(ostream& os, const Sales_data& item);
	friend Sales_data add(const Sales_data& s1, const Sales_data& s2);
public:
	Sales_data() = default;
	Sales_data(const string& s) :bookNo(s) {};
	Sales_data(const string& s, unsigned n, double p) :bookNo(s), unit_sold(n), revenue(n* p) {};
	Sales_data(istream& is) { read(cin, *this); }

	Sales_data& combine(const Sales_data& rhs);
	string isbn()const
	{
		return this->bookNo;
	}
private:
	inline double avg_price()const;
	string bookNo;
	unsigned unit_sold = 0;
	double revenue = 0.0;
};
Sales_data& Sales_data::combine(const Sales_data& rhs)
{
	this->unit_sold += rhs.unit_sold;
	this->revenue += rhs.revenue;
	return *this;
}
Sales_data add(const Sales_data& s1, const Sales_data& s2)
{
	Sales_data temp = s1;
	temp.combine(s2);
	return temp;
}
istream& read(istream& is, Sales_data& item)
{
	double price = 0;
	is >> item.bookNo >> item.unit_sold >> price;
	item.revenue = item.unit_sold * price;
	return is;
}
ostream& print(ostream& os, const Sales_data& item)
{
	os << item.bookNo << " " << item.unit_sold << " " << item.revenue;
	return os;
}
inline double Sales_data::avg_price()const
{
	return unit_sold ? revenue / unit_sold : 0;
}
int main(int argc,char**argv)
{
	ifstream input("1.txt");
	ofstream output("2.txt");
	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 << "没有记录" << endl;
	}
	system("pause");
	return 0;
}

8.8:

#include <iostream>
using namespace std;
#include<string>
#include<fstream>
class Sales_data
{
	friend istream& read(istream& is, Sales_data& item);
	friend ostream& print(ostream& os, const Sales_data& item);
	friend Sales_data add(const Sales_data& s1, const Sales_data& s2);
public:
	Sales_data() = default;
	Sales_data(const string& s) :bookNo(s) {};
	Sales_data(const string& s, unsigned n, double p) :bookNo(s), unit_sold(n), revenue(n* p) {};
	Sales_data(istream& is) { read(cin, *this); }

	Sales_data& combine(const Sales_data& rhs);
	string isbn()const
	{
		return this->bookNo;
	}
private:
	inline double avg_price()const;
	string bookNo;
	unsigned unit_sold = 0;
	double revenue = 0.0;
};
Sales_data& Sales_data::combine(const Sales_data& rhs)
{
	this->unit_sold += rhs.unit_sold;
	this->revenue += rhs.revenue;
	return *this;
}
Sales_data add(const Sales_data& s1, const Sales_data& s2)
{
	Sales_data temp = s1;
	temp.combine(s2);
	return temp;
}
istream& read(istream& is, Sales_data& item)
{
	double price = 0;
	is >> item.bookNo >> item.unit_sold >> price;
	item.revenue = item.unit_sold * price;
	return is;
}
ostream& print(ostream& os, const Sales_data& item)
{
	os << item.bookNo << " " << item.unit_sold << " " << item.revenue;
	return os;
}
inline double Sales_data::avg_price()const
{
	return unit_sold ? revenue / unit_sold : 0;
}
int main(int argc,char**argv)
{
	ifstream input("1.txt");
	ofstream output("2.txt",ofstream::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 << "没有记录" << endl;
	}
	system("pause");
	return 0;
}

8.9:

不理解,存档:

#include <iostream>
using namespace std;
#include<sstream>
istream& func(istream& is)
{
	string str;
	while (is >> str)
	{
		cout << str << endl;
	}
	is.clear();
	return is;
}
int main()
{
	istringstream iss("hello");
	func(iss);
	system("pause");
	return 0;
}

8.10:

#include <iostream>
using namespace std;
#include<sstream>
#include<fstream>
#include<vector>
#include<string>


int main()
{
	ifstream ifs("1.txt");
	if (!ifs)
	{
		cerr << "no data?" << endl;
		return -1;
	}
	vector<string>vecLine;
	string line;
	while (getline(ifs,line))
	{
		vecLine.push_back(line);
	}
	for (auto& s : vecLine)
	{
		istringstream iss(s);
		string word;
		while (iss >> word)
		{
			cout << word << endl;
		}
	}

	system("pause");
	return 0;
}

8.11:

#include <iostream>
using namespace std;
#include<sstream>
#include<fstream>
#include<vector>
#include<string>

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)
	{
		cout << p.name << " ";
		for (auto& s : p.phones)
		{
			cout << s << " ";
		}
		cout << endl;
	}

	system("pause");
	return 0;
}

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;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值