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 ...
随机推荐
- ThinkPHP3.2.2的函数扩展
ThinkPHP的函数扩展:为了更好的在前台模板中显示变量,例如,商品分类中,分类名称之间的缩进.此时,在APP/Common/Common文件夹下(APP为新建的应用目录),新建一个php文件,如: ...
- RTU:EvalRightToUse License for feature adventerprise 1.0 will transition to RightToUse in 10 days. UDI ASR1002-X:JAE2100XXXX
关于这个log:[Hostname] EvalRightToUse License for feature adventerprise 1.0 will transition to RightToUs ...
- git 使用点
git checkout HEAD 只是缓存恢复到head 头版本,文件还没恢复,最后要执行 git checkout . (点) 才能恢复文件内容 git checkout 只是 ...
- row_number over ()排序函数
语法: row_number () over (排序规则) 计算每一行数据在结果集的行数 select ROW_NUMBER() over (order by FSalary) as 排序 ,* ...
- Linux 命令中 which、whereis、locate 命令的用法
which 命令 which 命令的作用是,在 PATH 变量指定的路径中搜索可执行文件的所在位置.它一般用来确认系统中是否安装了指定的软件. (1)命令格式 which 可执行文件名称 wherei ...
- oracle 高级函数
原 oracle 高级函数 2017年08月17日 16:44:19 阅读数:1731 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u013278 ...
- Java基础知识笔记第七章:内部类和异常类
内部类 /* *Java支持在一个类中定义另一个类,这样的类称为内部类,而包含内部类的类称为内部类的外嵌类 */ 重要关系: /* *1.内部类的外嵌类在内部类中仍然有效,内部类的方法也可以外嵌类的方 ...
- IDEA下的SVN设置以及TortoiseSVN安装后bin目录下没有svn.exe如何解决?
1.首先,我们要确保电脑上已经安装了TortoiseSVN. 2.打开IDEA-File-Settings-Version Control-Subversion,在右边的界面上选择TortoiseSv ...
- C语言入门---第九章 C语言指针
没学指针就是没学C语言! 指针是C语言的精华,也是C语言的难点. 所谓指针,也就是内存的地址,所谓指针变量,也就是保存了内存地址的变量.不过人们往往不会区分两者的概念,而是混淆在一起使用. ===== ...
- Python学习笔记005
if if == : xxxx elif : xxxx else: xxxx 输入字符串 input() 字符串转数值 int() 数值转字符串 str() 输出 print() ...