《C++ Primer Plus》第四章 复合类型 4.12复习题答案

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/yujinlong312/article/details/101512646

1

char actor[30] = { 0 };
short bstsie[100] = { 0 };
float chuck[13] = { 0 };
long double dipsea[64] = { 0 };

2

array<char,30> actor={0};
array<short, 100> betsie = { 0 };
array<float, 13> chuck = { 0 };
array<long double, 64> dipsea = { 0 };

3

int a[5]={1,3,5,7,9};

4

int even=a[0]+a[4];

5

float ideas[5] = { 1,2,3,4,5 };
cout << "ideas[1]:" << ideas[1] << endl;

6

char a[20] = "cheeseburger";		

7

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string str = "Waldorf Salad";
	cout << str << endl;
	cin.get();
	return 0;
}

8

struct Fish
{
	char type[20];
	int weight;
	double length;
};

9

Fish s1 = { "美人鱼",2,1.2 };

10

enum Response
{
	No,
	Yes,
	Maybe
};

11

double ted = 1.234;
double *tedPtr = &ted;
cout << *tedPtr << endl;
cout << tedPtr <<endl<< &ted << endl;//tedPtr和&ted均为地址值

12

#include <iostream>
using namespace std;

int main()
{
//答案
	float treacle[10] = { 1,2,3,4,5,6,7,8,9,10 };
	float *treaclePtr = treacle;
	cout << treaclePtr[0] << " " << treaclePtr[9] << endl;
/***********************/
	cin.get();
	return 0;
}

13

#include <iostream>
#include <vector>
using namespace std;
int main()
{
	int n = 0;
	cin >> n;
	cin.get();
	int *a = new int[n];
	vector<int>b(n);
	for (int i = 0; i < n; i++)
	{
		a[i] = i;
		b[i] = i;
	}
	for (int i = 0; i < n; i++)
	{
		cout << a[i] << " " << b[i] << endl;
	}
	cin.get();
	return 0;
}

14
有效。他将打印出储存这个字符串常量的内存地址。

#include <iostream>
using namespace std;

int main()
{
    //E0144	"const char *" 类型的值不能用于初始化 "char *" 类型的实体
    //做法是强制类型转换
	char *b = { (char*)"Home of the jolly bytes" };
	int* a = (int *)b;
	//如果要显示的是字符串的地址,则必须将这种指针强制转换为另一种指针类型,如int *
	cout << a << " " << *a << endl;
	//如果指针的类型为char *,则cout将显示指向的字符串
	cout << b << " " << *b << endl;
	cout << (int *)"Home of the jolly bytes" << endl;//打印字符串地址
	cin.get();
	return 0;
}

15

#include <iostream>
using namespace std;
struct Fish
{
	char type[20];
	int weight;
	double length;
};
int main()
{
	Fish *fish = new Fish;
	cout << "依次输入鱼类的名字,重量,长度";
	cin >> fish->type;
	cin.get();
	cin >> fish->weight;
	cin.get();
	cin >> fish->length;
	cin.get();
	cout << fish->type<<" "<< fish->weight <<" "<< fish->length << endl;
    cin.get();
	return 0;
}

16

cin.getline(address,80)的效果是,读取用户输入的一行,读取的字符数为80个,包括空格、TAB等,以换行符为止,并将换行符舍弃。将读取的值赋给变量address。
而cin>>address是从第一个非空格、tab或者换行符读取,然后读取到空格、tab、换行符为止,但不舍弃这些。例如输入kunming street,cin只会读取kunming,不会读取空格及以后的字符。

17

#include<vector>
#include<array>
#include<string>
const int a=10;
std::vector<std::string>b(a);
std::array<std::string,a>c;

猜你喜欢

转载自blog.csdn.net/yujinlong312/article/details/101512646