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 ...
随机推荐
- Python 输入与输出
Python2版本 raw_input raw_input("输入提示"),会把输入的内容当做字符串返回 input 会把用户输入的内容当做代码来处理,可以理解为 raw_inpu ...
- Hadoop _ 疑难杂症 解决1 - WARN util.NativeCodeLoader: Unable to load native-hadoop library for your plat
最近博主在进行Hive测试 压缩解压缩的时候 遇到了这个问题, 该问题也常出现在日常 hdfs 指令中, 在启动服务 与 hdfs dfs 执行指令的时候 : 都会显示该提示,下面描述下该问题应该如何 ...
- same-security-traffic
关于Inter-interface和intra-interface: 要允许具有相同安全级别的接口之间的通信,或允许流量进入和退出同一接口,请在全局配置模式下使用same-security-traff ...
- java 关于多层的异常捕获
从这两个源程序可以看出来,这里的逻辑其实很好理清楚. 可以看到,每次抛出了相同的错误,但因为catch后的捕捉类型不同,所以结果不同,其实可以看出多层的异常捕捉和正常的多层代码运行逻辑是基本一致的.
- ipfs 资料汇集
目录 js ipfs u can use wikipeida here js ipfs https://github.com/ipfs/js-ipfs u can use wikipeida here ...
- Codeforces Round #589 (Div. 2)D(思维,构造)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;vector<int>adj[10 ...
- OPENTSDB: Request failed: Internal Server Error net.opentsdb.core.IllegalDataException
今天Opentsdb补传历史数据的时候,出现了如下的错误: Request failed: Internal Server Error net.opentsdb.core.IllegalDataExc ...
- vector 踩过的坑
今天,做LeetCode上的一道题,198题:Rob House,在纸上画了画,发现了重复的结构,就使用了递归的方式实现的 #include<iostream> #include<v ...
- 《JavaScript高级程序设计》读书笔记(一)JavaScript简介
起于客户端数据验证特性----闭包----匿名函数----元编程等----等想要全面理解和掌握JavaScript----本质----历史----局限性 ECMAScript 脚本语言标准 JavaS ...
- js学习:基本语法结构
语句 JavaScript 程序的执行单位为行(line),也就是一行一行地执行.一般情况下,每一行就是一个语句. 语句(statement)是为了完成某种任务而进行的操作,比如下面就是一行赋值语句. ...