《C++ Primer Plus(第六版)》(4)(第四章 复合类型 答案1)

第四章好长,分开一下。

4.12 复习题


1.

char actor[30] = { 0 };
short betsie[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, 2, 3, 4, 5 };


4.

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

5.

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


6.

char c[20] = "cheeseburger";
cout << c << endl;

7.
string str = "Waldorf Salad";
cout << str << endl;

8.

struct SFish
{
	char type[20];
	int weight;
	double lenght;
};

9.

SFish fish = { "big", 2, 3.4 };

10.

enum Response
{
	No,
	Yes,
	Maybe,
};

11.

double ted = 123.456;
double* tedPtr = &ted;
cout << *tedPtr << endl;

12.

float treacle[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
float* ptr = treacle;
cout << ptr[0] << " " << ptr[9] << endl;

13.

int n = 0;
cin >> n;
int* a = new int[n];
vector<int> b(n);
for (int i = 0; i < n; i++)
{
	a[i] = i;//原来调试的时候,是看不到a指向的数组的内存的。
	b[i] = i;
}
for (int i = 0; i < n; i++)
{
	cout << a[i] << " " << b[i] << endl;
}

14.语句没有问题。其实就是把字符串指针,强制转换成整形指针。

cout只对字符串指针进行特殊处理,其他类型的指针是输出该指针的内容,即地址。

char* b = "Home of the jolly bytes";
int* a = (int *)b;
cout << a << " " << *a << endl;
cout << b << " " << *b << endl;
cout << (int *)"Home of the jolly bytes" << endl;

15.

struct SFish
{
	char type[20];
	int weight;
	double lenght;
};
SFish* fish = new SFish{ "big", 2, 3.4 };
cout << "fish type:" << fish->type << " wight:" << fish->weight << " lenght:" << fish->lenght << endl;

16.只读单词,不能读整行

//还是要看看4.6清单的代码
int year;
cin >> year;//这里点了回车键
char address[80];
cin.getline(address, 80);//这里只读了一个回车键,没机会输入
cout << year << " " << address << endl;//address没有相应的内容
cin >> year;
cin >> address;
cout << year << " " << address << endl;//可以输入adress,不能读取空白字符,只能读一个单词

17.

const int num = 10;
vector<string> a(num);// #include <string>
array<string, num> b;// #include <array>












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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值