C++Primer第五版——习题答案详解(五)



习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html

第6章 函数



练习6.4

#include<iostream>
using namespace std;
int fact(int x) {
    if (x == 1) return x;
    else return x * fact(x - 1);
}

int main() {
    int x;
    cout << "Please input a number:\n";
    while (cin >> x) {
        int ans = fact(x);
        cout << ans << endl;
    }
    system("pause");
    return 0;
}

练习6.5

#include<iostream>
using namespace std;
int abs(int x) {
    return x >= 0 ? x : (-x);
}

int main() {
    int x;
    cout << "Please input a number:\n";
    while (cin >> x) {
        cout << abs(x) << endl;
    }
    system("pause");
    return 0;
}

练习6.7

size_t count_calls(){
    static size_t ctr=0;
    return ctr++;
}

练习6.10

#include<iostream>
using namespace std;
void swap(int *x, int *y) {
    int temp = *x;
    *x = *y;
    *y = temp;
}

int main() {
    int x, y;
    cin >> x >> y;
    cout << x << y << endl;
    swap(x, y);
    cout << x << y << endl;
    system("pause");
    return 0;
}

练习6.12

void swap(int &x, int &y) {
    int temp = x;
    x = y;
    y = temp;
}

练习6.17
不相同,因为转大写需要修改传入的字符串需要加&

#include<iostream>
#include<cctype>
#include<string>
using namespace std;
bool is_upper(const string s) {
    for (auto i : s) {
        if (isupper(i)) return true;
    }
    return false;
}

void to_upper(string &s) {
    for (auto &i : s) {
        i = toupper(i);
    }
}

int main() {
    string s = "abdcedA";
    if (is_upper(s)) cout << "is upper" << endl;
    else cout << "Not upper" << endl;
    to_upper(s);
    cout << s << endl;
    system("pause");
    return 0;
}

练习6.18

bool compare(matrix &a, matrix &b);
vector<int>::iterator change_val(int a, vector<int>::iterator);

练习6.19
a.不合法
b.合法
c.合法
d.合法

练习6.21

#include<iostream>
#include<cctype>
#include<string>
#include<vector>
using namespace std;
int Big(int x, int *y) {
    return x > *y ? x : *y;
}
int main() {
    int x = 3, y = 2;
    cout << Big(x, &y) << endl;
    system("pause");
    return 0;
}

练习6.22
函数传参必须加引用,不加结果不正确。

#include<iostream>
#include<cctype>
#include<string>
#include<vector>
using namespace std;
void swap(int *&x, int *&y) {
    int *temp = x;
    x = y;
    y = temp;
}
int main() {
    int x = 3, y = 2;
    int *px = &x, *py = &y;
    swap(px, py);
    cout << x << ", " << y << endl;
    cout << *px << ", " << *py << endl;
    system("pause");
    return 0;
}

6.23

#include<iostream>
using namespace std;
void print(const int *pi) {
    cout << *pi << endl;
}
void print(const int *beg, const int *end) {
    while (beg != end) {
        cout << *beg++ << " ";
    }
    cout << endl;
}
void print(const int ia[], size_t size) {
    for (size_t i = 0;i != size;i++) {
        cout << ia[i] << " ";
    }
    cout << endl;
}
void print2(const int (&arr)[2]) {
    for (size_t i = 0;i != 2;i++) {
        cout << arr[i] << " ";
    }
    cout << endl;
}

int main() {
    int i = 0, j[2] = { 0,1 };
    print(&i);
    print(begin(j), end(j));
    print(j, 2);
    print2(j);
    system("pause");
    return 0;
}

练习6.24
如果数组大小小于10,会造成数组越界。除此以外数组不能复制成形参。改成指针或引用。

练习6.25
用命令行当做输入,输入的argv[0]保存程序的名字,后面的才是实参

#include<iostream>
#include<string>
using namespace std;

int main(int argc,char const **argv)
{
    cout << argv[1] << endl;
    cout << argv[2] << endl;
    cout << string(argv[1])+string(argv[2]) << endl;
    system("pause");
    return 0;
}

练习6.26

#include<iostream>
#include<string>
using namespace std;

int main(int argc,char const **argv)
{
    for (int i = 0;i < argc;i++) {
        cout << argv[i] << endl;
    }
    system("pause");
    return 0;
}

练习6.27

#include<iostream>
#include<string>
#include<initializer_list>
using namespace std;
int count_Sum(initializer_list<int> il) {
    int sum = 0;
    for (auto beg = il.begin();beg != il.end();beg++) {
        sum += *beg;
    }
    return sum;
}
int main(int argc,char const **argv)
{
    cout << count_Sum({ 1,2,3,4,5 }) << endl;
    system("pause");
    return 0;
}

练习6.28
string类型

练习6.29
如果实参个数多且占用容量大,用引用更好。

练习6.31
返回的引用是局部对象的引用时无效;返回的常量引用是局部常量对象的引用时。

练习6.32
合法,将数组重置为0-9

练习6.33

#include<iostream>
#include<string>
#include<vector>
using namespace std;
void print(vector<int>::iterator vec_begin, vector<int>::iterator vec_end) {
    if (vec_begin != vec_end) {
        cout << *vec_begin << " ";
        return print(++vec_begin, vec_end);
    }
}
int main(int argc,char const **argv)
{
    vector<int> vec = { 1,2,3,4,6 };
    print(vec.begin(), vec.end());
    system("pause");
    return 0;
}

练习6.34
如果传入的参数小于0,会一直调用不停止

练习6.35
会一直调用本身,无限循环

练习6.36

string (&fun(int i)[10];

练习6.39
a.合法
b.不合法
c.合法

练习6.40
a.正确
b.错误,一旦某个形参被赋予了默认值,它后面的所有形参都必须有默认值

练习6.41
a.非法,第一个形参没有默认值,必须给实参
b.合法
c.合法,‘*‘转换为整形

练习6.42

#include<iostream>
#include<string>
#include<vector>
using namespace std;
string make_plural(size_t ctr, const string &word, const string &ending = "s") {
    return (ctr > 1) ? word + ending : word;
}
int main()
{
    cout << make_plural(2, "success", "es") << endl;
    cout << make_plural(2, "failure") << endl;
    system("pause");
    return 0;
}

练习6.43
a.放在头文件中,内联函数在程序中多个定义必须完全一致。
b.放在头文件中,函数声明。

练习6.44

#include<iostream>
#include<string>
#include<vector>
using namespace std;
inline bool isShorter(const string &s1, const string &s2) {
    return s1.size() < s2.size();
}
int main()
{
    string s1 = "abc", s2 = "abcd";
    cout << isShorter(s1, s2) << endl;
    system("pause");
    return 0;
}

练习6.46
不能,返回值不是常量表达式

练习6.47
在项目属性中设置命令行/DNDEBUG就不会出现 #ifndef....#endif、assert中的内容。

#include<iostream>
#include<string>
#include<vector>
#include<cassert>
using namespace std;
void read_vi(vector<int>::const_iterator iterator_begin, vector<int>::const_iterator iterator_end) {
#ifndef NDEBUG
    cerr << iterator_end - iterator_begin << __func__ << " " << __FILE__ << " "
        << __LINE__ << " " << __TIME__ << " " << __DATE__ << endl;
#endif // !NDEBUG
    if (iterator_begin != iterator_end) {
        cout << *iterator_begin << " ";
        return read_vi(++iterator_begin, iterator_end);
    }
    else {
        cout << endl;
        return;
    }
}
int main()
{
    vector<int> v{ 1,2,3,4,5 };
    read_vi(v.begin(), v.end());
    system("pause");
    return 0;
}

练习6.48
合理,输入结束时终止程序

练习6.50
a.非法,二义性
b.匹配void f(int)
c.匹配void f(int,int)
d.匹配void f(double,double)

练习6.53
a.合法
b.合法
c.不合法,重复定义

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int calc(int&, int&);
int calc(const int&, const int&);
char calc2(char*, char*);
char calc2(const char*, const char*);
int calc3(char*, char*);
int calc3(char* const, char* const);
int main()
{
    calc(1, 2);
    system("pause");
    return 0;
}
int calc(int&a, int&b) {
    return a + b;
}
int calc(const int&a, const int&b) {
    return a + b;
}
char calc2(char *a, char *b) {
    return *a;
}
char calc2(const char *a, const char *b) {
    return *a;
}
int calc3(char *a, char *b) {
    return a - b;
}
int calc3(char* const a, char* const b) {
    return a - b;
}

练习6.54

vector<int(*)(int, int)> vf;

练习6.55

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int add(int a, int b) {
    return a + b;
}
int sub(int a, int b) {
    return a - b;
}
int multi(int a, int b) {
    return a * b;
}
int divide(int a, int b) {
    return a / b;
}
int main()
{
    vector<int(*)(int, int)> vf{ add,sub,multi,divide };
    for (const auto e : vf) cout << e(4, 2) << endl;
    system("pause");
    return 0;
}

原文地址:https://www.cnblogs.com/Mered1th/p/10502708.html

时间: 2024-08-29 07:48:38

C++Primer第五版——习题答案详解(五)的相关文章

C++Primer第五版——习题答案详解(七)

习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第8章 IO库 练习8.1 istream &iofunc(istream &is) { string s; while (is >> s) { cout << s << endl; } is.clear(); return is; } 练习8.2 #include<iostream> #include<string>

C++Primer第五版——习题答案详解

开始刷<C++Primer>这本书,会在博客上更新课后习题. 水平有限,如有有误之处,希望大家不吝指教! 不断学习中,欢迎交流! 第一二章课后题 练习1.3 #include<iostream> int main(){ std::cout<<"Hello world"<<std::endl; return 0; } 练习1.4 #include<iostream> int main(){ std::cout <<

C++Primer第五版——习题答案目录

目前正在刷<C++Primer>这本书,会在博客上记录课后习题答案,答案仅供参考. 因为水平有限,如有有误之处,希望大家不吝指教,谢谢! 目录地址 使用的系统为:win 10,编译器:VS2017,答案用markdown写的. 第1章 开始&&第2章 变量和基本类型 ? 第3章 字符串.向量和数组 ? 第4章 表达式 ? 第5章 语句 ? 第6章 函数 ? 第7章 类 ? 第8章 IO库 ? ? ? ? ? ? ? ? ? ? ? ? ? ? 不断学习中,欢迎交流! 原文地址:

c++ Primer 第五版习题答案第三章

3.2 编写程序,从标准输入中一次读入一整行,然后修改该程序使其一次读入一个词. void readByLine () {    string line; ?    while (getline (cin, line)) {        cout << line << endl;   }   } void readByWord () {    string word; ?    while (cin >> word) {        cout << wo

c++ Primer 第五版习题答案第二章

练习2.1 Q: 类型int.long.long long和short的区别是什么,无符号和带符号类型的区别是什么?float和double的区别是什么? A:int. long. long long和short尺寸不同,表示的数据范围不同.无符号只能表示0和正数,无符号还可以表示负数.float为单精度浮点数,double为双精度,一般来说,float占4字节,double占8字节. 练习2.2 Q: 计算按揭贷款时,对于利率.本金和付款分别应选择何种数据类型?说明你的理由. A: 利率应该用

《C++Primer》第五版习题详细答案--目录

作者:cosefy ps: 答案是个人学习过程的记录,仅作参考. <C++Primer>第五版习题答案目录 第一章:引用 第二章:变量和基本类型 第三章:字符串,向量和数组 第四章:表达式 原文地址:https://www.cnblogs.com/cosefy/p/12180771.html

C++Primer第五版习题解答---第一章

C++Primer第五版习题解答---第一章 ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2022/1/7 第一章:开始 练习1.3 #include<iostream> int main() { std::cout << "hello, world" << std::endl; return 0; } 练习1.4: #include<iostream> int main() { int

Python核心编程(第二版) 第五章习题答案

5-1.整型.讲讲Python普通整型和长整型的区别. 答:Python 的标准整数类型是最通用的数字类型.在大多数 32 位机器上,标准整数类型的取值范围是-2**31到 2**31-1,也就是-2,147,483,648 到 2,147,483,647.如果在 64 位机器上使用 64 位编译器编译 Python,那么在这个系统上的整数将是 64 位. Python 的长整数类型能表达的数值仅仅与你的机器支持的(虚拟)内存大小有关. 5-2.操作符.(a)写一个函数,计算并返回两个数的乘积.

C语言程序设计:现代方法第2版习题答案

C语言程序设计:现代方法第2版课后答案 时至今日, C语言仍然是计算机领域的通用语言之一,C语言程序设计:现代方法第2版习题答案但今天的 C语言已经和最初的时候大不相同了.C语言程序设计:现代方法第2版答案本书最主要的一个目的就是通过一种“现代方法”来介绍 C语言,书中强调标准 C,强调软件工程,不再强调“手工优化”.这一版中紧密结合了 C99标准,并与 C89标准进行对照,补充了 C99中的最新特性.本书分为 C语言的基础特性. C语言的高级特性. C语言标准库和参考资料 4个部分.每章末尾都