CPrimer课后习题完整答案第四版

合集下载

C++Primer中文版_第4版_第七章_函数_习题解答_文字word版

C++Primer中文版_第4版_第七章_函数_习题解答_文字word版

第七章函数题目00What is the difference between a parameter and an argument?形参和实参有什么区别?【解答】形参是在函数定义的形参表中进行定义,是一个变量,其作用域为整个函数。

而实参出现在函数调用中,是一个表达式。

进行函数调用时,用传递给函数的实参对形参进行初始化。

题目01Indicate which of the following functions are in error and why. Suggesthow you might correct the problems.下列哪些函数是错误的?为什么?请给出修改意见。

(a) int f() {string s;// ...return s;}(b) f2(int i) { /* ... */ }(c) int calc(int v1, int v1) /* ... */ }(d) double square(double x) return x * x;【解答】(a)是错误的。

因为函数头中所定义的返回值类型为int,return语句世纪返回的表达式的类型为string,两个类型不同,而string类型又不能隐式转换为int类型。

可修改为:string f(){string s;//…Return s;}(b)是错误的。

因为该函数定义中没有指定返回类型,在标准C++中,定义函数时不指定返回类型是非法的。

可修改为:Int f2(int i){/*…*/}(c)是错误的。

缺少括住函数体在左花括号,而且两个形参不应该同名。

可修改为:Int caic(int v1,intv2){/*…*/}(d)是错误的。

缺少括住函数体的一对花括号。

可修改为:Double square(double x){return x*x;}题目02Write a program to take two int parameters and generate the result ofraising the first parameter to the power of the second. Write a programto call your function passing it two ints. Verify the result.编写一个带有两个int 型形参的函数,产生第一个参数的第二个参数次幂的值。

C++primer中文版第四版 习题答案word版本 第八章

C++primer中文版第四版 习题答案word版本 第八章

第八章标准IO库8.1 假设os是一个ofstream对象,下面程序做了什么?os << “Goodbye!” << endl;如果os 是ostringstream对象呢?或者,os 是ifstream对象呢?答:第一个,向文件中写入“Goodbye”,第二个向string对象中写入“Goodbye”,第三个,如果os是一个ifstream对象,则错误,因为ifstream类中没有定义操作符<< 。

8.2 下面的声明是错误的,指出其错误并改正之: ostream print(ostream os);答:标准库类型不允许做复制或赋值操作。

形参或返回类型不能为流类型,所以上句代码错误,因为它把流类型的对象当做了形参。

应改为传递指向该对象的指针或引用:ostream &print( ostream &os );8.3 编写一个函数,其唯一的形参和返回值都是istream&类型。

该函数应一直读取流直到到达文件的结束符为止,还应将读到的内容输出到标准输出中。

最后,重设流使其有效,并返回该流。

答:// 定义控制台¬应用程序的入口点。

//#include"stdafx.h"#include"stdafx.h"#include<iostream>using namespace std;istream & f( istream & in ){int ival;while ( in >> ival, !in.eof()) // 遇到文件结束符之前一直读入数据{if(in.bad()) // input stream is corrupted; bail out, 流是否已被破坏throw runtime_error("IO stream corrupted");if ( in.fail() ) // bad input{cerr << " bad date, try again:";in.clear( ); // reset the streamin.setstate(istream::eofbit); // 结束死循环continue;}// process inputcout << ival << endl;}in.clear(); // 将n中的所有状态值都设为有效状态return in;}int main(){cout << " Input some words ( ctrl + z to end ):\n";f( cin );system("pause");return0;}8.4 通过cin为实参实现调用来测试上题编写的函数。

C++第四版15.41章答案primer

C++第四版15.41章答案primer

// TextQuery.h#ifndef TEXTQUERY_H#define TEXTQUERY_H#include<string>#include<vector>#include<set>#include<map>#include<cctype>//if(!ispunct(*it)) ret+=tolower(*it);#include<iostream>#include<fstream>#include<cstring>#include<sstream>using namespace std;class TextQuery{public:typedef string::size_type str_size;//类型别名typedef vector<string>::size_type line_no;//接口://readfile建立给定文件的内部数据结构void read_file(ifstream &is){store_file(is);build_map();}//run_query 查询给定单词并返回该单词所在的行号集合set<line_no> run_query(const string &query_word)const{map<string,set<line_no>>::const_iterator loc=word_map.find(query_word);if(loc==word_map.end())return set<line_no>();//找不到返回空对象elsereturn loc->second;}//打印原文void print_article(){int i=0;for(vector<string>::iterator siter=lines_of_text.begin();siter!=lines_of_text.end();++siter) cout<<"("<<++i<<")"<<" "<<*siter<<endl;}//text_line返回输入文件中指定的行号对应的行string text_line(line_no line)const{if(line<lines_of_text.size())return lines_of_text[line];throw out_of_range("line number out of range");}line_no size()const//返回文本最后行号{return lines_of_text.size();}private://保存输入文本容器vector<string> lines_of_text;//read_file所用的辅助函数void store_file(ifstream&is)//存储输入文件{string textline;while(getline(is,textline))lines_of_text.push_back(textline);}void build_map()//将每个单词与一个行号关联{for(line_no line_num=0;line_num!=lines_of_text.size();++line_num){//一次读一个单词istringstream line(lines_of_text[line_num]);string word;while(line>>word){//若不在容器则加入该单词,若单词已经存在则在set<line_no>中插入行号word_map[cleanup_str(word)].insert(line_num);}}}//单词和行号相关联map<string,set<line_no>> word_map;//去掉标点并将字母变成小写static string cleanup_str(const string &word){string ret;for(string::const_iterator it=word.begin();it!=word.end();++it){if(!ispunct(*it))///*若不是字符标点符号或特殊符号*/ret+=tolower(*it);}return ret;}};#endif//Query.h#ifndef QUERY_H#define QUERY_H#include"TextQuery.h"#include<string>#include<set>#include<algorithm>#include<iostream>#include<fstream>using namespace std;//Query_base 抽象基类class Query_base{friend class Query;//定义句柄类为基类的友员,句柄类方便使用虚拟函数protected:typedef TextQuery::line_no line_no;//类型简化代换virtual ~Query_base(){ }private:virtual set<line_no>eval(const TextQuery&)const=0;//纯虚构函数返回行号集合virtual ostream&display(ostream&os=cout)const=0;//纯虚构函数打印查询};//管理继承层次的句柄类Queryclass Query{friend Query operator~(const Query&);//访问Query_base*的构造函数friend Query operator|(const Query&,const Query&);friend Query operator&(const Query&,const Query&);Query &operator=(const Query &rhs);public:Query(const string &);//构造WordQuery对象Query(const Query &c):q(c.q),use(e){++*use;}~Query(){decr_use();}//析构句柄类函数//接口函数:调用相应的Query_base操作set<TextQuery::line_no>eval(const TextQuery &t)const {return q->eval(t);}//动态绑定ostream &display(ostream&os)const {return q->display(os);}//动态绑定private:Query(Query_base *query):q(query),use(new size_t(1)){ }//初始化句柄对象Query_base *q;size_t *use;void decr_use()//检查是否是最后一个句柄管理对象{if(--*use==0){delete q;delete use;}}};//内联函数定义赋值inline Query &Query::operator=(const Query &rhs){++*e;decr_use();//检查本对象是否只管理一个Query_base对象q=rhs.q;use=e;return *this;}inline ostream&//Query重载输出操作符operator<<(ostream&os,const Query&q){return q.display(os);}class WordQuery:public Query_base{friend class Query;//Query使用WordQuery的构造函数WordQuery(const string &s):query_word(s){}//实现基类的虚函数set<line_no>eval(const TextQuery&t)const{return t.run_query(query_word);} ostream&display(ostream &os)const{return os<<query_word;}string query_word;//要查找的单词};//内联函数实现Query--WordQuery对象的简历inlineQuery::Query(const string &s):q(new WordQuery(s)),use(new size_t(1)){}class NotQuery:public Query_base{friend Query operator~(const Query&);//友员函数在句柄中声明NotQuery(Query q):query(q){}//构造函数//纯虚函数实现set<line_no> eval(const TextQuery &file)const{//计算操作数结果集set<TextQuery::line_no> has_val=query.eval(file);set<line_no> ret_lines;for(TextQuery::line_no n=0;n!=file.size();++n)if(has_val.find(n)==has_val.end())ret_lines.insert(n);return ret_lines;}ostream& display(ostream &os) const{return os<<"~("<<query<<")";}const Query query;};class BinaryQuery:public Query_base{protected:BinaryQuery(Query left,Query right,string op):lhs(left),rhs(right),oper(op){ }//构造函数//抽象类继续继承eval抽象函数ostream& display(ostream&os)const{return os<<"("<<lhs<<" "<<oper<<" "<<rhs<<")";}const Query lhs,rhs;const string oper;//操作符};class AndQuery:public BinaryQuery{friend Query operator&(const Query&,const Query&);AndQuery(Query left,Query right)://构造函数BinaryQuery(left,right,"&"){}set<line_no> eval(const TextQuery&file) const//虚构函数的实现{set<line_no> left=lhs.eval(file),right=rhs.eval(file);set<line_no> ret_lines;//保存运算结果set<line_no>::iterator ret_it=ret_lines.begin(),left_it=left.begin(),right_it=right.begin();while(left_it!=left.end()&&right_it!=right.end()){if(*left_it==*right_it){ret_lines.insert(*left_it);left_it++;right_it++;}else if(*left_it>*right_it){++right_it;}else ++left_it;}//set_intersection(left.begin(),left.end(),right.begin(),right.end(),inserter(ret_lines, ret_lines.begin()));return ret_lines;}};class OrQuery:public BinaryQuery{friend Query operator|(const Query&,const Query&);OrQuery(Query left,Query right)://构造函数BinaryQuery(left,right,"|"){}set<line_no> eval(const TextQuery&file) const//虚构函数的实现{set<line_no> right=rhs.eval(file);set<line_no> ret_lines=lhs.eval(file);//保存运算结果for(set<line_no>::const_iterator iter=right.begin();iter!=right.end();++iter)ret_lines.insert(*iter);return ret_lines;}};inline Query operator&(const Query &lhs,const Query &rhs){return new AndQuery(lhs,rhs);}inline Query operator |(const Query &lhs,const Query &rhs){return new OrQuery(lhs,rhs);}inline Query operator~(const Query &oper){return new NotQuery(oper);}#endif// 15_41.cpp : 定义控制台应用程序的入口点。

C++Primer(第4版)习题解答(非扫描版有目录)

C++Primer(第4版)习题解答(非扫描版有目录)

习题 2.19............................................11 习题 2.20............................................11 习题 2.21............................................11 习题 2.22............................................11 习题 2.23........................................... 12 习题 2.24........................................... 12 习题 2.25........................................... 12 习题 2.26........................................... 12 习题 2.27........................................... 12 习题 2.28........................................... 12 习题 2.29........................................... 13 习题 2.30........................................... 13 习题 2.31........................................... 13 习题 2.32........................................... 14 习题 2.33........................................... 14 习题 3.1 ............................................. 14 习题 3.2 ............................................. 14 习题 3.3 ............................................. 14 习题 3.4 ............................................. 15 习题 3.5 ............................................. 15 习题 3.6 ............................................. 15 习题 3.7 ............................................. 15 习题 3.8 ............................................. 16 习题 3.9 ............................................. 17 习题 3.10........................................... 17 习题 3.11........................................... 17 习题 3.12........................................... 17 习题 3.13........................................... 18 习题 3.14........................................... 19 习题 3.15........................................... 19 习题 3.16........................................... 19 习题 3.17........................................... 20 习题 3.18........................................... 21 习题 3.19........................................... 22 习题 3.20........................................... 22 习题 3.21........................................... 22 习题 3.23........................................... 22 习题 3.24........................................... 22 习题 4.1 ............................................. 23 习题 4.3 ............................................. 23 习题 4.4 ............................................. 23 习题 4.5 ............................................. 24 习题 4.6 ............................................. 24 习题 4.7 ............................................. 24

C++Primer(第4版)习题解答_第十一章

C++Primer(第4版)习题解答_第十一章

第十一章泛型算法1.algorithm头文件定义了一个名为count的函数,其功能类似于find。

这个函数使用一对迭代器和一个值做参数,返回这个值出现的次数的统计结果。

编写程序读取一系列int型数据,并将它们存储到vector对象中然后统计某个指定的值出现了多少次。

// 11.17_11.1_int_to_vector_count.cpp : 定义控制台应用程序的入口点。

//#include"stdafx.h"#include<vector>#include<iostream>#include<algorithm>using namespace std;int _tmain(int argc, _TCHAR* argv[]){cout << "\tInput some int numbers ( ctrl + z to end):\n\t ";vector<int> iVec;int iVal;while ( cin >> iVal )iVec.push_back( iVal );cout << "\n\tInput a num to search in the iVec: ";cin.clear();cin >> iVal;int iCnt = 0;if ( iCnt = count( iVec.begin(), iVec.end(), iVal )){cout << "\n\tThe value " << iVal << " occurs " << iCnt << " times." << endl;}system("pause");return 0;}2.重复前面的程序,但是,将读入的值存储到一个string类型的list对象中。

C++Primer(第4版)习题解答_十五章

C++Primer(第4版)习题解答_十五章

第十五章面向对象编程1。

什么是虚成员?在类中被声明为virtual的成员,基类希望这种成员在派生类中重定义。

除了构造函数外,任意非static成员都可以为虚成员。

2。

给出protected访问标号的定义。

它与private有何不同?protected为受保护的访问标号,protected成员可以被该类的成员、友元和派生类成员(非友元)访问,而不可以被该类型的普通用户访问。

而private成员,只能被基类的成员和友元访问,派生类不能访问。

3。

定义自己的Item_base类版本。

class Item_base{public:Item_base( const string &book = '', double sales_price = 0.0) :isbn( book ), price( sales_price ) { }string book( ) const{return isbn;}virtual double net_price( size_t n ) c onst{return price * n;}virtual ~Item_base() { }private:string isbn;protected:double price;};4。

图书馆可以借阅不同种类的资料—书、CD、DVD等等。

不同种类的借阅资料有不同的登记、检查和过期规则。

下面的类定义了这个应用程序可以使用的基类。

指出在所有借阅资料中,哪些函数可能定义为虚函数,如果有,哪些函数可能是公共的。

(注:假定LibMember 是表示图书馆读者的类,Date是表示特定年份的日历日期的类。

)class Library{bool check_out( const LibMember& );bool check_in( cosnt LibMember& );bool is_late( const Date& today );double apply_fine();ostream& print( ostream& = count );Date due_date() const;Date date_borrowed() const;string title() const;const LibMember& member() const;};因为有不同的登记、检查、和过期规则,所以bool check_out( const LibMember& );bool check_in( cosnt LibMember& );bool is_late( const Date& today );double apply_fine();ostream& print( ostream& = count );这几个函数应该被定义为虚函数,print函数可能用于打印不同的项目的内同,也定义为虚函数。

C++ Primer 4th 第五章答案

C++ Primer 4th 第五章答案

编写while 循环条件从标准输入设备读入整型(int)数据,当读入值为42 时循 环结束。 【解答】 int val; cin >> val; while (val != 42) 或者,while 循环条件也可以写成 while (cin >> ival && ival != 42) 习题5.8 编写表达式判断4 个值a、b、c 和d 是否满足a 大于b、b 大于c 而且c 大于d 的条件。 【解答】 表达式如下: a > b && b > c && c > d 习题5.9 假设有下面两个定义: unsigned long ul1 =3, ul2 = 7; 下列表达式的结果是什么? (a) ul1 & ul2 (b) ul1 && ul2 (c) ul1 | ul2 (d) ul1 || ul2 【解答】 各表达式的结果分别为3、true、7、true。 习题 5.10 重写bitset 表达式:使用下标操作符对测验结果进行置位(置1)和复位(置 0)。 【解答】 bitset<30> bitset_quiz1; bitset_quiz1[27] = 1; bitset_quiz1[27] = 0; 习题5.11 请问每次赋值操作完成后,i 和d 的值分别是多少? int i; double d; d = i = 3.5; i = d = 3.5; 【解答】 赋值语句d=i=3.5;完成后,i 和d 的值均为3。因为赋值操作具有右结合性,所以首先将3.5 赋给 i(此时发生隐式类型转换,将double 型字面值3.5 转换为int 型值3,赋给i),然后将表达式 i=3.5 的值(即赋值后i 所具有的值3)赋给d。赋值语句 i=d=3.5;完成后,d 的值为3.5,i 的 值为3。因为先将字面值3.5 赋给d,然后将表达式d=3.5 的值(即赋值后d 所具有的值3.5)赋给 i(这时也同样发生隐式类型转换)。 习题 5.12 解释每个if 条件判断产生什么结果? if ( 42 = i ) // ... if ( i = 42 ) // ... 【解答】 前者发生语法错误, 因为其条件表达式42=i 是一个赋值表达式, 赋值操作符的左操作数必须为一 个左值,而字面值42 不能作为左值使用。 后者代码合法,但其条件表达式i=42 是一个永真式(即其逻辑值在任何情况下都为true),因为

cprimer(第4版)习题解答.doc

cprimer(第4版)习题解答.doc

C++ Primer(第 4 版 )习题解答 .txt51 自信是永不枯竭的源泉,自信是奔腾不息的波涛,自信是急流奋进的渠道,自信是真正的成功之母。

书名:C++ Primer(第 4 版)习题解答梅晓勇作者:蒋爱军李师贤来源:人民邮电出版社出版时间: 2006 年 12 月ISBN:55108定价: 45元内容介绍:C++ Primer(第 4 版)是C++大师Stanley B. Lippman 丰富的实践经验和C++标准委员会原负责人JoséeLajoie 对 C++标准深入理解的完美结合,更加入了C++先驱Barbara E. Moo 在 C++教学方面的真知灼见,C++ Primer( 4 版)习题解答2是初学者的最佳 C++指南,而且对于中高级程序员,也是不可或缺的参考书。

本书正是这部久负盛名的C++经典教程的配套习题解答。

书中提供了C++ Primer(第 4 版)中所有习题的参考答案。

本书对使用C++ Primer(第 4 版)学习C++程序设计语言的读者是非常理想的参考书。

C++是一门非常实用的程序设计语言,既支持过程式程序设计,也支持面向对象程序设计,因而也是目前应用极为广泛的一门程序设计语言。

在层出不穷的介绍C++语言的书籍中,C++ Primer 是一本广受欢迎的权威之作。

强大的作者阵容、全面的内容介绍、新颖的组织方式,使之深受C++爱好者的青睐。

本书编者在翻译 C++ Primer(第 4 版)的过程中也深深地感受到了这一点。

在学习一门程序设计语言的过程中,亲自动手编写代码是一种极其有效的学习方式,可以对语言的理解和应用达到事半功倍的效果,因此,C++ Primer(第 4版)中提供了许多习题,以帮助读者加深对书中内容的理解。

本书试图成为 C++ Primer(第 4 版)的配套书籍,根据C++ Primer(第 4 版)中所介绍的内容提供配套习题的解答,书中所给出的“见xx 节”,均指参见 C++Primer (第 4 版)的相应章节。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
相关文档
最新文档