《C++Primer》第五版习题答案--第六章【学习笔记】
《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》第五版习题答案--第六章【学习笔记】的更多相关文章
- 《C++Primer》第五版习题答案--第三章【学习笔记】
[C++Primer]第五版[学习笔记]习题解答第三章 ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/10 第三章:字符串,向量和数组 ...
- C++Primer第五版——习题答案目录
目前正在刷<C++Primer>这本书,会在博客上记录课后习题答案,答案仅供参考. 因为水平有限,如有有误之处,希望大家不吝指教,谢谢! 目录地址 使用的系统为:win 10,编译器:VS ...
- C++Primer第五版——习题答案详解(一)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第1章 开始&&第2章 变量和基本类型 练习1.3 #include&l ...
- C++Primer第五版——习题答案详解(二)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第3章 字符串.向量和数组 练习3.2 一次读入一整行 #include<iost ...
- C++Primer第五版——习题答案详解(三)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第4章 表达式 练习4.10 while(cin>>i&&i ...
- C++Primer第五版——习题答案详解(四)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第5章 语句 练习5.9 #include<iostream> #inclu ...
- C++Primer第五版——习题答案详解(五)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第6章 函数 练习6.4 #include<iostream> using ...
- C++Primer第五版——习题答案详解(六)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第7章 类 练习7.1 class Sales_data { public: std:: ...
- C++Primer第五版——习题答案详解(七)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第8章 IO库 练习8.1 istream &iofunc(istream &a ...
随机推荐
- springIOC源码接口分析(三):ApplicationContext
一 新增方法 主要都是获取容器基本信息的一些接口,比如获取名称,id和启动时间戳,获取AutowireCapableBeanFactory等接口 二 继承接口 ApplicationContext继承 ...
- 解决git报错:error: RPC failed; curl 18 transfer closed with outstanding read data remaining 的方法
报错信息: error: RPC failed; curl 18 transfer closed with outstanding read data remainingfatal: the remo ...
- HanLP《自然语言处理入门》笔记--5.感知机模型与序列标注
笔记转载于GitHub项目:https://github.com/NLP-LOVE/Introduction-NLP 5. 感知机分类与序列标注 第4章我们利用隐马尔可夫模型实现了第一个基于序列标注的 ...
- jmeter性能测试2:基础功能介绍
对于英语不好的同学建议先改为简体中文再进行使用 1.添加->threads->线程组(控制总体并发) 线程数:虚拟用户数.一个虚拟用户占用一个进程或线程 ...
- qt5实现简单布局
layout.h #ifndef LAYOUT_H #define LAYOUT_H #include <QtWidgets/QDialog> #include <QLabel> ...
- CCF_ 201512-4_送货
一道拖了很久了题,用bool开的vis不会爆内存,dfs回溯的话会超时,于是有了一个很巧妙的dfs. #include<iostream> #include<cstring> ...
- ORACLE隐式类型转换
隐式类型转换简介 通常ORACLE数据库存在显式类型转换(Explicit Datatype Conversion)和隐式类型转换(Implicit Datatype Conversion)两 ...
- 最大流-前置push-relabel算法实现
Front Push-Relabel Algorithm 接口定义 Input:容量数组vector<vector<int>> capacity ,大小为n:源点int sou ...
- djiango目录文件
一.创建项目 命令:django-admin startproject mysite mysite ├── manage.py └── mysite ├── __init__.py ├ ...
- 给Hangfire的webjob增加callback和动态判断返回结果功能设计
背景介绍 通常业务中需要用到定时执行功能,我用hangfire搭建了一个调度服务,这个调度服务是独立于业务逻辑的,具体可以参考文章:https://github.com/yuzd/Hangfire.H ...