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 &
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端