Homework
Test 8.5
int main(int argc, char *argv[])
{
vector<string> svec;
readword(argv[1], svec);
for (auto s : svec)
{
cout << s << endl;
}
}
Test 8.6 & 8.7
int main(int argc, char *argv[]) {
ifstream input(argv[1]);
ofstream output(argv[2]);
my_sales_data total;
if (read(input, total))
{
my_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 << "No data?" << endl;
return 0;
}
Test 8.8
在main.cpp中:
int main(int argc, char *argv[]) {
ifstream input(argv[1]);
ofstream output(argv[2], ofstream::app);
my_sales_data total;
if (read(input, total))
{
my_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 << "No data?" << endl;
return 0;
}
在cmd中:
>cl main.cpp my_sales_data.cpp
>main.exe infile.txt outfile.txt
Test 8.10
int main(int argc, char *argv[])
{
ifstream ifs(argv[1]);
vector<string> lvec;
string line;
while (getline(ifs, line))
{
lvec.push_back(line);
}
istringstream istr;
for (auto i : lvec)
istr >> i;
return 0;
}
Test 8.13
struct PersonInfo
{
string name;
vector<string> phones;
};
int main(int argc, char *argv[])
{
string line, word;
vector<PersonInfo> people;
ifstream ifs(argv[1]);
while (getline(ifs, line))
{
PersonInfo info;
istringstream record(line);
record >> info.name;
while (record >> word)
info.phones.push_back(word);
people.push_back(info);
}
for (const auto& p : people)
{
cout << p.name <<endl;
for (const auto& i : p.phones)
cout << i << endl;
cout << endl;
}
return 0;
}