CH8 课后习题
8.1和8.2
#include <iostream> using namespace std; istream& f(istream& in)
{
int v;
in >> v;
if (in.bad())
throw runtime_error("IO is bad");
if (in.fail())
{
cerr << "bad data,try again" << endl;
in.clear();
in.ignore(, '\n');
//忽略的知识点continue只能用在循环中
}
cout << v << endl;
in.clear();
return in;
}
int main()
{
cout << "please enter numbers:(Ctrl+Z to end)" << endl;
f(cin);//cin遇到空格会结束,如果要读取整行,可以使用getline
system("pause");
return ;
}
8.3
遇到了文件结束符标志,或者是输入错误的流或无效数据
8.4
#include <iostream>
#include <vector>
#include <string>
#include <fstream> using namespace std; int main()
{
string fileName;
cout << "please enter the name of file:" << endl;
cin >> fileName;
ifstream infile(fileName);
if (!infile)
{
cerr << "can not open the file you entered,please check!" << endl;
return -;
}
string line;
vector<string> row;
while (getline(infile, line))
{
row.push_back(line);
}
for (auto it : row)
cout << it << endl; system("pause");
return ;
}
8.5
将8.4的第21行改为while(infile>>line)即可
8.9
#include <iostream>
//#include <vector>
#include <sstream>
#include <string> using namespace std; istream& f(istream& in)
{
string str; //in >> str;//遇到空格就结束,使用循环
while (in >> str, !in.eof())
{
if (in.bad())
throw runtime_error("IO IS bad!");
if (in.fail())
{
cerr << "bad data or not matched" << endl;
in.clear();
in.ignore(, '\n');
continue;
}
cout << str << endl;
} } int main()
{
ostringstream os;
os << "Hello C++" << endl;;
istringstream in(os.str());
f(in);
system("pause");
return ;
}
8.10
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <fstream> using namespace std; int main()
{
string fileName;
cout << "please enter the name of the file:" << endl;
cin >> fileName;
ifstream infile(fileName);
if (!infile)
{
cerr << "can not open the file" << endl;
return -;
} string line;
vector<string>txt_row;
while (getline(infile, line))
{
txt_row.push_back(line);
//istringstream record(line);//题目要求从vector中读取数据,不是从文件,所以这样不符合要求
}
infile.close();
for (auto it : txt_row)
{
istringstream line_str(it);
string word;
while (line_str >> word)
cout << word << " ";
cout << endl; }
system("pause");
return ;
}
8.11
istringstream 的对象record若定义在while循环外,则会被循环调用,重复使用流,需要恢复流状态,使用clear恢复
#include <iostream>
#include <vector>
#include <sstream> using namespace std;
struct PersonInfo {
string name;
vector<string>phone; }; int main()
{ string line;
string word;
vector<PersonInfo>person;
istringstream record;
while (getline(cin, line))
{
PersonInfo info;
record.clear();
record >> info.name;
while (record >> word)
{
info.phone.push_back(word); }
person.push_back(info);
}
system("pause");
return ;
}
8.12
因为每个人的电话号码数量不固定。
8.13
此题,我写的存在问题,测试时和我的文件分别是这样的


#include <iostream>
#include <fstream>
#include <sstream>
#include <vector> using namespace std; struct PersonInfo {
string name;
vector<string>phone;
}; bool valid(const string& str)
{
cout << "check the string valid or not" << endl;
return true;
} //string format(string& str)
string format(const string& str)
{
cout << "format the phone numbers " << endl;
return str;
}
int main(int argc,char*argv[])
{
string line;
string word;
vector<PersonInfo> person;
istringstream record;
if (argc != )
{
cerr << "please enter the name of file." << endl;
return -;
}
ifstream infile(argv[]);
if (!infile)
{
cerr << "can not open the file" << endl;
return -;
}
while (getline(infile, line))
{
PersonInfo info;
infile.clear();
record.str(line);
record >> info.name; while (record >> word)
{
info.phone.push_back(word);
}
person.push_back(info);
} //实现将从文件读取的记录输出
ostringstream os;
for (const auto& entry : person)
{
ostringstream badNums;
ostringstream formatted;
for (auto &nums : entry.phone)
{
if (!valid(nums))
{
//badNums << nums << " is invalid" << endl;
badNums << " " << nums; }
else
//formatted << "after foramtted: " << format(nums) << endl;
formatted << " " << format(nums);
} if (badNums.str().empty())
os << entry.name << " " << formatted.str() << endl;
else
cerr << "input error,try again" << endl; }
cout << os.str() << endl;//
//system("pasue");
return ;
}
另外,我认为format函数的参数应该是非const 型的,但是若是非const型,则62行就不能是引用型,否则编译出错,我暂时还没想明白原因
8.14
如上,使用引用是因为person和phone的元素分别是struct和string对象,使用引用可避免拷贝,但是教材中定义为const,我就无法理解了,通常定义为const是避免改变这些项的值,name成员不能改变,可是phone成员需要格式化处理,需要改变
CH8 课后习题的更多相关文章
- 《python核心编》程课后习题——第三章
核心编程课后习题——第三章 3-1 由于Python是动态的,解释性的语言,对象的类型和内存都是运行时确定的,所以无需再使用之前对变量名和变量类型进行申明 3-2原因同上,Python的类型检查是在运 ...
- web实验指导书和课后习题参考答案
实验指导书 :http://course.baidu.com/view/daf55bd026fff705cc170add.html 课后习题参考答案:http://wenku.baidu.com/li ...
- 《Python核心编程》 第六章 序列 - 课后习题
课后习题 6–1.字符串.string 模块中是否有一种字符串方法或者函数可以帮我鉴定一下一个字符串是否是另一个大字符串的一部分? 答:成员关系操作符(in.not in) import string ...
- 《Python核心编程》 第五章 数字 - 课后习题
课后习题 5-1 整形. 讲讲 Python 普通整型和长整型的区别. 答:普通整型是绝大多数现代系统都能识别的. Python的长整型类型能表达的数值仅仅与你机器支持的(虚拟)内存大小有关. 5- ...
- 機器學習基石(Machine Learning Foundations) 机器学习基石 课后习题链接汇总
大家好,我是Mac Jiang,非常高兴您能在百忙之中阅读我的博客!这个专题我主要讲的是Coursera-台湾大学-機器學習基石(Machine Learning Foundations)的课后习题解 ...
- OpenCV学习笔记之课后习题练习3-5
OpenCV学习笔记之课后习题练习2-5 练习使用感兴趣区域(ROI).创建一个210*210的单通道图像并将其归0.在图像中使用ROI和cvSet()建立一个增长如金字塔状的数组. 参考博文:www ...
- OpenCV学习笔记之课后习题练习2-5
5.对练习4中的代码进行修改,参考例2-3,给程序加入滚动条,使得用户可以动态调节缩放比例,缩放比例的取值为2-8之间.可以跳过写入磁盘操作,但是必须将变换结果显示在窗口中. 参考博文:blog.cs ...
- OpenCV学习笔记之课后习题练习2-3
3.使用例2-10中的视频捕捉和存储方法,结合例2-5中的doPyrDown()创建一个程序,使其从摄像机读入视频数据并将缩放变换后的彩色图像存入磁盘. 例2-10中所用的方法虽然能正常运行,但却不能 ...
- OpenCV学习笔记之课后习题练习3-4
练习:创建一个大小为100*100的三通道RGB图像.将它的元素全部置0.使用指针算法以(20,5)与(40,20)为顶点绘制一个绿色平面. 参考博文:blog.csdn.net/qq_2077736 ...
随机推荐
- IDEA导入maven项目不自动识别
解决办法: 选中module的pom.xml,右键,选择" add as maven project",即可刷新为maven项目
- python-opencv:读取图片
代码 import cv2 # 读取一张图片 src = cv2.imread("../images/lena.jpg") # 命名一个窗口 cv2.namedWindow(&qu ...
- springmvc中整合mongodb副本集配置文件
配置文件jdbc.properties: mongo.hostport=192.168.100.100:28007,192.168.100.110:28008,192.168.100.120:2800 ...
- spring boot 整合mapreduce运行的ClassNotFoundException
问题 一个wordcount运行总是报错 java.lang.RuntimeException: java.lang.ClassNotFoundException: Class com.hadoop. ...
- 洛谷 P1339 [USACO09OCT]热浪Heat Wave(最短路)
嗯... 题目链接:https://www.luogu.org/problem/P1339 这道题是水的不能在水的裸最短路问题...这里用的dijkstra 但是自己进了一个坑—— 因为有些城市之间可 ...
- springboot后端时间到前端,相差8小时,时间格式不对
spring boot后台时间正确,返回给前台的时间不正确,和后台差8个小时 { "code": 1, "msg": "SUCCESS", ...
- CSS - 精灵Sprite
1. CSS精灵是一种处理网页背景图像的方式. 2. 它将一个页面涉及到的所有零星背景图像都集中到一张大图中去,然后将大图应用于网页,这样,当用户访问该页面时,只需向服务发送一次请求,网页中的背景图像 ...
- Vue系列(六)之常用指令v-model
v-model 基本使用 修饰符 .trim .number .lazy 前面讲到的插值,其实都是单向绑定,数据变-->视图变.有些元素可以与用户交互,比如input,select等,那么我们希 ...
- 关于sarima模型的描述,时间序列的理论与方法(第二版)(美 布洛克威尔)有一部分比较值得看
- 5-2 使用antDesign的Table 及 modal(对话情景框) 功能
1,进入antDesign官网,粘贴table代码 2,看代码各个部分实现什么功能,根据需求改代码 表格类似如下: 代码如下: const columns = [ { title: 'Name', d ...