C++ Primer 第五版第六章习题答案(上)

书籍版本:2019年9月第一版;王刚 杨巨峰译;电子工业出版社

编译器 : win10  && VS2015

这一篇写到6.2节结束,也就是前29题

6.1

形参是定义在函数中的虚拟值,实参是形参的初始值。

6.2

a.  返回值是int类型,不能return一个string

b.  没有定义返回值类型

c.  两个参数名不能一样

d.  没有加{}

6.3

 

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int fact(int val)
{
	int sum = 1;
	for (int i = 1; i <= val; i++)
	{
		sum = sum * i;
	}
	return sum;
}

int main()
{
	int s = fact(5);
	cout << s << endl;

	system("pause");
}



6.4

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int fact(int val)
{
	int sum = 1;
	for (int i = 1; i <= val; i++)
	{
		sum = sum * i;
	}
	return sum;
}

int main()
{
	cout << "请输入一个数字:";
	int a;
	cin >> a;
	int s = fact(a);
	cout << a << "的阶乘值为:" << s << endl;

	system("pause");
}



6.5

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int numfab(int val)
{
	return fabs(val);
}

int main()
{
	cout << "请输入一个数字:";
	int a;
	cin >> a;
	int s = numfab(a);
	cout << a << "的绝对值为:" << s << endl;

	system("pause");
}



6.6
局部变量包括 形参和静态局部变量。 
形参属于自动对象,在函数开始时为形参申请存储空间,在函数终止时形参被销毁。 
局部静态变量,在程序第一次经过对象定义语句时初始化,直到程序终止时才被销毁。

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int numfab(int val)//形参
{
	static int count = 0;// 局部静态变量
	count++;
	int num = fabs(val);// 局部变量
	return num;
}

int main()
{
	cout << "请输入一个数字:";
	int a;
	cin >> a;
	int s = numfab(a);
	cout << a << "的绝对值为:" << s << endl;

	system("pause");
}



6.7

#include "stdafx.h"
#include <iostream>
using namespace std;

int num()
{
	static int count = 0;
/*	if (count<1)
	{
		return count++;
	}
	else
		return count;*/
        return count++;
}

int main()
{
	for (int i = 0; i < 10; i++)
	{
		cout << num() << endl;
	}

	system("pause");
}

6.8

//这是Chapter6.h文件的内容
#pragma once
class CChapter6
{
public:
	CChapter6();
	~CChapter6();

private:
	int f();
	int f2(int i);
	int calc(int v1, int v2);
	double square(double x);
};

 

// 这是Chapter6.cpp文件中的内容
#include "stdafx.h"
#include "Chapter6.h"
#include <iostream>
using namespace std;

CChapter6::CChapter6()
{
}


CChapter6::~CChapter6()
{
}

int CChapter6::f()
{
	string s;
	return 0;
}

int CChapter6::f2(int i)
{

}

int CChapter6::calc(int v1, int v2)
{

}

double CChapter6::square(double x)
{

}

6.9

如上题

6.10

#include "stdafx.h"
#include <iostream>
using namespace std;

void ExChangeNum(int* a, int* b)
{
	int tmp = *a;
	*a = *b;
	*b = tmp;
}

int main()
{
	int a = 111, b = 999;
	cout << "交换之前: a: " << a << ", b: " << b << endl;
	ExChangeNum(&a, &b);
	cout << "交换之后: a: " << a << ", b: " << b << endl;

	system("pause");
}

6.11

#include "stdafx.h"
#include <iostream>
using namespace std;

void ExChangeNum(int* a, int* b)
{
	int tmp = *a;
	*a = *b;
	*b = tmp;
}

void Reset(int& a, int& b)
{
	int tmp = a;
	a = b;
	b = tmp;
}

int main()
{
	int a = 111, b = 999;
	cout << "交换之前: a: " << a << ", b: " << b << endl;
	Reset(a, b);
	cout << "交换之后: a: " << a << ", b: " << b << endl;

	system("pause");
}

6.12

代码同上。个人觉得引用更好用。

6.13

void f(T)是传入一个T类型的变量

void f(&T)是传入一个T类型的引用

6.14

当形参的值不能改就不能传引用;当形参是一个很难拷贝的类型并且可能对其值修改时传引用;

6.15

s是常量引用是表明函数中不会改变其值;

s与occurs是引用是为了取到对应的对象;

s是普通引用可能会造成其值被改变;

occurs是常量引用则无法改变其值,就无法得到想要的结果。

6.16

只能接受非常量string,改参数为const string&

6.17

#include "stdafx.h"
#include <iostream>
using namespace std;

bool IsUpExist(const string& s)
{
	for (int i = 0; i < s.size(); i++)
	{
		if (isupper(s[i]))
		{
			return true;
		}
	}
	return false;
}

void StringToLower(string& s)
{
	for (int i = 0; i < s.size(); i++)
	{
		if ( (s[i]) )
		{
			s[i] = tolower(s[i]);
		}
	}
}

int main()
{
	string a = "aaaaaHaaaa";
	string b = "AHFBDIALFH";
	cout << "修改之前a: " << a << endl;
	cout << "修改之前b: " << b << endl;
	bool hasUp = IsUpExist(a);
	StringToLower(b);
	cout << hasUp << endl;
	cout << "修改之后b: " << b << endl;

	system("pause");
}

6.18

a.  bool Compare(matrix& m1, matrix& m2);

b.  vectoe<int> change_val(int a, vector<int> iter);

6.19

a不合法,只能传一个参数

b合法,但第二个参数最好不要传常量

c合法

d,这样写编译器是可以通过的,但是个人认为这样写还是应尽量避免;

6.20

传入参数的值不能被改变时传入常量;

本该是常量设成了普通引用,有可能会改变其值与原目的不符,且无法传入常量引用作为参数

6.21

#include "stdafx.h"
#include <iostream>
using namespace std;

int Max(const int* a,const int b)
{
	if ( *a  > b)
	{
		return *a;
	}
	else
	{
		return b;
	}
}

int main() 
{
	int a = 5;
	int* b = new int();
	*b = 6;
	int max = Max(b, a);
	cout << max << endl;

	system("pause");
}



6.22

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;

void ChangePoint( int*& a, int*& b)
{
	int* tmp = new int();
	tmp = a;
	a = b;
	b = tmp;
	delete tmp;
	tmp = NULL;
}

int main() 
{
	int* a = new int();
	*a = 5;
	int* b = new int();
	*b = 6;
	cout << "交换前:a = " << a << "; b = " << b << endl;
	ChangePoint(b, a);
	cout << "交换后:a = " << a << "; b = " << b << endl;
	delete a;
	delete b;
	a = NULL;
	b = NULL;

	system("pause");
}



6.23

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;

void print(const int i)
{
	cout << i << endl;
}

void print(const int (&a)[2])
{
	cout << a[0] << a[1] << endl;
}

int main() 
{
	int  i = 0;
	int j[2] = { 0,1 };
	print(i);
	print(j);

	system("pause");
}



6.24

输出一个长度为10的整形数组;

问题是我们规定默认输出十个值,但我们可能传进去的不足10,因为实参会自动转换成指向数组元素的指针。例如

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;


void print(const int ia[10])
{
	for (int i = 0; i != 10; ++i)
	{
		cout << ia[i] << endl;
	}
}

int main() 
{
	int j[1] = { 0};
	print(j);

	system("pause");
}

这段代码可以通过编译,但实际输出的结果是有问题的。

正确的写法应限定形参的范围。

6.25

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;


int main(int argc, char *argv[]) 
{
	string str1 = "";
	string str2 = "";
	str1 = argv[1];
	str2 = argv[2];
	str1 += str2;
	cout << str1;

	system("pause");
}

6.26

把刚才的argv扩展到5

6.27

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int CalcAllNum(initializer_list<int> il)
{
	int sum = 0;
	for (auto begin = il.begin();begin != il.end();++begin)
	{
		sum = sum + *begin;;
	}
	return sum;
}

int main(int argc, char *argv[]) 
{
	int sum = CalcAllNum({ 0,1,2,3,4,5,67,89 });
	cout << sum << endl;

	system("pause");
}

6.28

const string

6.29

当循环控制变量是基本类型时,可以不声明为引用,否则应该声明成引用,因为initializer_list对象可能是各种类型,有可能是自定义类型或者string类型。此时使用引用可以避免拷贝。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值