《C++Primer》第五版习题答案--第六章【学习笔记】

ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考。

作者:cosefy

Date: 2020/1/16

第六章:函数

练习6.2:

  • 返回类型错误
  • 无返回类型
  • 形参名字应该不同
  • 函数体需要用花括号包含起来

练习6.4:

实现:编写函数,使得用户输入一个整数,main函数调用函数得到阶乘。

#include<iostream>
using namespace std; int fact(int n)
{
int result = 1;
while (n > 1)
result *= n--;
return result;
}
int main()
{
int a;
cout << "请输入一个整数,求阶乘: " << endl;
cin >> a;
cout << "结果:"<<fact(a) << endl;
return 0;
}

练习6.5:

#include<iostream>
using namespace std;
int fuc(int num)
{
return num > 0 ? num : -num;
}
int main()
{
cout << "请输入一个整数: " << endl;
int a;
cin >> a;
cout << "绝对值: " << fuc(a) << endl;
return 0;
}

练习6.6:

  • 形参和函数体内部的变量统称为局部变量,每当函数执行完就会释放;而局部静态变量存储在静态存储区,生命周期贯穿函数调用及之后的时间。

练习6.7:

#include<iostream>
using namespace std; int test1()
{
static int a = 0;
return a++;
}
int main()
{
for (int i = 1; i < 5; i++)
cout << test1() << endl;
return 0;
}

练习6.10:

#include<iostream>
using namespace std; void traverse(int* p, int* q)
{
int tmp;
tmp = *p;
*p = *q;
*q = tmp;
} int main()
{
int a = 20, b = 10;
cout << "Before: (a,b)=" << a <<" "<< b << endl;
traverse(&a, &b);
cout << "After: (a,b)=" << a <<" "<< b << endl;
return 0;
}

练习6.13:

前者是值传递,后者是地址传递,前者无法改变实参,后者可以改变实参。

练习6.15:

  • s是字符串,我们希望它不被修改,保持稳定,所以定义为常量,而occurs在程序中值需要修改
  • c是一个临时变量,没有必要进行地址传递,否则没有意义
  • 那么,occurs的值将一直保持0,而s有可能会被修改,并且s不再能接受常量字符串的实例化。

练习6.17:

#include<iostream>
using namespace std; bool find_upperalpha(const string& s)
{
for (auto c : s)
if (isupper(c))
return true;
return false;
}
void trf_tolower(string& s)
{
for (auto &c : s)
if (isupper(c))
c = tolower(c);
}
int main()
{
cout << "请输出一个字符串: " << endl;
string s;
cin >> s;
cout << "是否有大写字母:" << find_upperalpha(s) << endl;
trf_tolower(s);
cout << "把大写字母转化为小写: " << s << endl;
return 0;
}

练习6.18:

  • bool compare(matrix &m1,matrix &m2);
  • vector<int>:: iterator change_val(int ,vector<int>::iterator);

练习6.22:

 #include<iostream>
using namespace std; void fuc(int* &p1, int* &p2)
{
int* q = NULL;
q = p1;
p1 = p2;
p2 = q;
}
int main()
{
int a1 = 100, a2 = 200;
int* p1 = &a1;
int* p2 = &a2;
cout << "Before: " << *p1 << " " << *p2 << endl;
fuc(p1, p2);
cout<< "After: " << *p1 << " " << *p2 << endl;
return 0;
}

练习6.24:

不可以用值来传递数组。

void print(const int (&a)[10]){/**/}

练习6.25:

#include<iostream>
#include<string>
using namespace std; int main(int argc, char** argv)
{
string str;
for (int i = 1; i != argc; i++)
{
str += argv[i];
str += ' ';
}
cout << str << endl;
return 0;
}

练习6.27:

#include<iostream>
using namespace std; int get_sum(initializer_list<int> i1)
{
int sum = 0;
for (auto elem : i1)
sum += elem;
return sum;
}
int main()
{
initializer_list<int> i2{ 1,2,3,4,5,6 };
cout << get_sum(i2) << endl;
return 0;
}

练习6.32:

合法,把0-9拷贝到大小为10的数组中。

练习6.34:

如果输入为负数,则将一直递归下去。

练习6.35:

val--传入的值是自减之前的值。

练习6.39:

错误,错误,正确。

《C++Primer》第五版习题答案--第六章【学习笔记】的更多相关文章

  1. 《C++Primer》第五版习题答案--第三章【学习笔记】

    [C++Primer]第五版[学习笔记]习题解答第三章 ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/10 第三章:字符串,向量和数组 ...

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

    目前正在刷<C++Primer>这本书,会在博客上记录课后习题答案,答案仅供参考. 因为水平有限,如有有误之处,希望大家不吝指教,谢谢! 目录地址 使用的系统为:win 10,编译器:VS ...

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

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第1章 开始&&第2章 变量和基本类型 练习1.3 #include&l ...

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

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第3章 字符串.向量和数组 练习3.2 一次读入一整行 #include<iost ...

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

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第4章 表达式 练习4.10 while(cin>>i&&i ...

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

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第5章 语句 练习5.9 #include<iostream> #inclu ...

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

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第6章 函数 练习6.4 #include<iostream> using ...

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

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第7章 类 练习7.1 class Sales_data { public: std:: ...

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

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第8章 IO库 练习8.1 istream &iofunc(istream &a ...

随机推荐

  1. linux 命令全名

    su:Swith user  切换用户,切换到root用户cat: Concatenate  串联uname: Unix name  系统名称df: Disk free  空余硬盘du: Disk u ...

  2. not,and,or

    sql语句中not and or的执行优先级从高到低依次为:not>and>or <> 不等于

  3. Spring-cloud微服务实战【八】:API网关zuul

      在前面的文章中,我们先后使用了eureka/ribbon/feign/hystrix搭建了一个看似完美的微服务了,那是否还有值得继续优化的地方呢?答案肯定是有的,如果从整个微服务内部来看,基本已经 ...

  4. Spring的aop思想

    1.AOP思想 (1)在解决中文乱码的应用: 一般情况下如果在Servlet中要解决中文乱码问题需要在每一个Servlet中书写解决乱码的代码,但是,在运用了过滤器之后,就不再需要每一个Servlet ...

  5. js笔记(2)--第一天记录

    ---恢复内容开始--- 模仿了网站的一个常见小功能,开关灯小功能. 代码: <!DOCTYPE html> <html lang="en"> <he ...

  6. https原理总结

    博客搬家: https原理总结 最近在公司项目的服务器上做一些内部接口,要求使用https,于是花时间研究了一波.我们熟知的http在传输时未对数据进行加密,在传输一些敏感信息时存在着不小的安全隐患. ...

  7. [python-docx]docx文档操作的库

    from docx import Document from docx.shared import Inches # 新建document对象 document = Document() # 添加段落 ...

  8. PTA 7-10 树的遍历(二叉树基础、层序遍历、STL初体验之queue)

    7-10 树的遍历(25 分) 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(≤30),是二叉树中结点的个数 ...

  9. kendo ui 好用的小部件--grid

    Kendo Ui Grid控件,继承至Widget. https://demos.telerik.com/kendo-ui/grid/index  快速上手教程  下面的代码来自本教程 做表格时非常方 ...

  10. Go语言实现:【剑指offer】表示数值的字符串

    该题目来源于牛客网<剑指offer>专题. 请实现一个函数用来判断字符串是否表示数值(包括整数和小数).例如,字符串"+100","5e2",&qu ...