【C++ Primer(5th Edition) Exercise】练习程序 - Chapter4(第四章)

以下程序由 Teddy van Jerry (我自己)编写并运行,基本保证正确性。(有时可能会为优化程序超前使用某些内容)

Before we comb through the codes

这一章内容普遍比较简单,基本只需要小学或初中的数学水平即可。其中要注意细节,不要想当然(比如将 ^ 认为是乘方,这就悲剧了)。其中略有理解难度的内容为:4.11.3(需要加以自己的理解,实在不能接受可以先放一放。)
Review:
【C++ Primer(5th Edition) Exercise】练习程序 - Chapter1(第一章)
【C++ Primer(5th Edition) Exercise】练习程序 - Chapter2(第二章)
【C++ Primer(5th Edition) Exercise】练习程序 - Chapter3(第三章)


TIP:标有(C++/11)者为C++/11标准下可以使用的题,若为老版本应加以修改。


Exercise 4.4

#include <iostream>
using namespace std;

int main()
{
    
    
	cout << 12 / 3 * 4 + 5 * 15 + 24 % 4 / 2 << endl;
	return 0;
}

Output:

91

Exercise 4.5

#include <iostream>
using namespace std;

int main()
{
    
    
	cout << -30 * 3 + 21 / 5 << endl;
	cout << -30 + 3 * 21 / 5 << endl;
	cout << 30 / 3 * 21 % 5 << endl;
	cout << -30 / 3 * 21 % 4 << endl;
	return 0;
}

Output:

-86
-18
0
-2

Exercise 4.6

Way I:

#include <iostream>
using namespace std;

int main()
{
    
    
	int n;
	cin >> n;
	if (n % 2 == 0)
		cout << n << " is an even number." << endl;
	else cout << n << " is an odd number." << endl;
	return 0;
}

Way II:

#include <iostream>
using namespace std;

int main()
{
    
    
	int n;
	cin >> n;
	if (n % 2) // 0:false ;otherwise: true
		cout << n << " is an odd number." << endl;
	else cout << n << " is an even number." << endl;
	return 0;
}

Exercise 4.9

#include <iostream>
using namespace std;

int main()
{
    
    
	const char* cp = "Hello World";
	//Test
	cout << " cp= " << cp << endl;
	cout << "*cp= " << *cp << endl;
	if (cp && *cp)
		cout << "true" << endl;
	else cout << "false" << endl;
	return 0;
}

Output:
Output

注:*cp是一个pointer,指向一个字符串,cp是一个字符串,而
二者均不为空,所以结果为true。

Exercise 4.10

#include <iostream>
using namespace std;

int main()
{
    
    
	int num = 0;
	while (num != 42)
		cin >> num;
	return 0;
}

Exercise 4.11

#include <iostream>
using namespace std;

int main()
{
    
    
	int a, b, c, d;
	cin >> a >> b >> c >> d;
	if (a > b && b > c && c > d)
		cout << "true" << endl;
	else cout << "false" << endl;
	return 0;
}

注:不能写成a>b>c>d,因为运算出来是个bool数,书(English Version)在 Page 143 有讲解。

Exercise 4.12

见我的博客【思考】

Exercise 4.13(a)

#include <iostream>
using namespace std;

int main()
{
    
    
	int i;
	double d;
	d = i = 3.5;
	cout << d << " " << i << endl;
	return 0;
}

Output:

3 3

Exercise 4.13(b)

#include <iostream>
using namespace std;

int main()
{
    
    
	int i;
	double d;
	i = d = 3.5;
	cout << i << " " << d << endl;
	return 0;
}

Output:

3.5 3

Exercise 4.14

if (42 = i) // error!
if (i = 42) // true

Exercise 4.15

dval = ival = *pi = 0;

Exercise 4.16

/* (a) */ if ((p = getPtr()) != 0)
/* (b) */ if (i == 1024)

Exercise 4.19(a)

好像挺对的,不用改。

#include <iostream>
using namespace std;

int main()
{
    
    
	int a[] = {
    
     -3,-2,-1,0,1,2,3,4,5,6,7,8,9 };
	int* ptr = a;
	while (ptr != 0 && *ptr++)
		cout << ptr << " " << *ptr << " true" << endl;
	return 0;
}

Output:

007FFC2C -2 true
007FFC30 -1 true
007FFC34 0 true

Exercise 4.19(b)

好像效果也挺好

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

int main()
{
    
    
	vector<int> vec{
    
     -3,-2,-1,0,1,2,3,4,5,6,7,8,9 };
	int ival = -3;
	while (ival++ && ival)
		cout << ival << " " << endl;
	return 0;
}

Output:

-2
-1

Exercise 4.19(c)

原句着实有毛病,输出结果混乱不堪。

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

int main()
{
    
    
	vector<int> vec{
    
     1,3,2,5,7,6,7,8,8,10 };
	int ival = 0;
	while(ival != vec.size() - 1)
	{
    
    
		if (vec[ival++] <= vec[ival])
			cout << "vec[" << ival << "] <= vec[" << ival - 1 << "]" << endl;
		else cout << "vec[" << ival << "] > vec[" << ival - 1 << "]" << endl;
	}
	return 0;
}

Output:

vec[1] <= vec[0]
vec[2] > vec[1]
vec[3] <= vec[2]
vec[4] <= vec[3]
vec[5] > vec[4]
vec[6] <= vec[5]
vec[7] <= vec[6]
vec[8] <= vec[7]
vec[9] <= vec[8]

修改为:

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

int main()
{
    
    
	vector<int> vec{
    
     1,3,2,5,7,6,7,8,8,10 };
	int ival = 0;
	while(ival != vec.size() - 1)
	{
    
    
		if (vec[ival + 1] <= vec[ival])
			cout << "vec[" << ival << "] <= vec[" << ival++ << "]" << endl;
		else cout << "vec[" << ival << "] > vec[" << ival++ << "]" << endl;
	}
	return 0;
}

Output:

vec[1] > vec[0]
vec[2] <= vec[1]
vec[3] > vec[2]
vec[4] > vec[3]
vec[5] <= vec[4]
vec[6] > vec[5]
vec[7] > vec[6]
vec[8] <= vec[7]
vec[9] > vec[8]

(注:cout顺序从右向左,右边那个++不作用到自己,但作用到了左边那个上,因而L12不要写成cout << "vec[" << ival + 1 << "] <= vec[" << ival++ << "]" << endl;,L13类似。)

Exercise 4.20

其他应该都好理解,就 (e) 有一些思维含量。虽然比member的运算优先级低,但仍然是先*再++,因而是illegal的。
error
稍作修改,把顺序改一下,把++移到后面,仍有问题:
bug
啊,iterator out of range 太容易发生了。再改一下:

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

int main()
{
    
    
	//(e)
	vector<string> vec{
    
     "","Teddy", "van", "Jerry", "loves", "Teddy", "Bears" };
	auto iter = vec.cbegin();
	while (iter != vec.cend() - 1)
		cout << *++iter << endl;
	return 0;
}

Output:

Teddy
van
Jerry
loves
Teddy
Bears

Exercise 4.21

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

int main()
{
    
    
	vector<int> vint;
	int i;
	while (cin >> i)
		vint.push_back(i);
	for (auto& c : vint)
		c = (c % 2) ? 2 * c : c;
	for (auto c : vint)
		cout << c << " ";
	return 0;
}

Exercise 4.22(a)

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

int main()
{
    
    
	int grade;
	string result;
	cin >> grade;
	result = (grade < 60) ? "fail"
		: (grade <= 75) ? "low pass"
		: (grade > 90) ? "high pass" : "pass";
	cout << result << endl;
	return 0;
}

Exercise 4.22(b)

#include <iostream>
using namespace std;

int main()
{
    
    
	int grade;
	cin >> grade;
	if (grade < 60)cout << "fail";
	else
	{
    
    
		if (grade <= 75)cout << "low pass";
		else
		{
    
    
			if (grade > 90)cout << "high pass";
			else cout << "pass";
		}
	}
	cout << endl;
	return 0;
}

Exercise 4.25

#include <iostream>
using namespace std;

int main()
{
    
    
	long s = (~'q' << 6);
	cout << s << endl;
	return 0;
}

Output:

-7296

Exercise 4.27

(a)(b):
Header File Binary.h 见我的博客【笔记】 C++中的进制进制的转换
Source File:

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

int main()
{
    
    
	unsigned long ul1 = 3, ul2 = 7;
	cout << decimal_to_binary(ul1) << endl;
	cout << decimal_to_binary(ul2) << endl;
	unsigned long result1 = 0, result2 = 0;
	result1 = ul1 & ul2;
	result2 = ul1 | ul2;
	cout << result1 << endl;
	cout << decimal_to_binary(result1) << endl;
	cout << result2 << endl;	
	cout << decimal_to_binary(result2) << endl;
	return 0;
}

Output:

11
111
3
11
7
111

(c)(d): true!

Exercise 4.28

见我的博客 关于 C++中 sizeof 的思考

Exercise 4.29

#include <iostream>
using namespace std;

int main()
{
    
    
	int x[10];
	int* p = x;
	cout << sizeof(x) << endl;
	cout << sizeof(*x) << endl;
	cout << sizeof(x) / sizeof(*x) << endl;
	cout << sizeof(p) << endl;
	cout << sizeof(*p) << endl;
	cout << sizeof(p) / sizeof(*p) << endl;
	return 0;
}

Output:

40
4
10
4
4
1

Exercise 4.31

好像改成 postfix 毫无影响。

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

int main()
{
    
    
	int i;
	vector<int> ivec;
	while (cin >> i)
		ivec.push_back(i);
	auto cnt = ivec.size();
	for (decltype(ivec.size())ix = 0; ix != ivec.size(); ix++, cnt--)
		ivec[ix] = cnt;
	for (auto c : ivec)
		cout << c << " ";
	cout << endl;
	return 0;
}

Exercise 4.36

i *= static_cast<int>(d);

Next Chapter

【C++ Primer(5th Edition) Exercise】练习程序 - Chapter5(第五章)

猜你喜欢

转载自blog.csdn.net/weixin_50012998/article/details/108202624