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