《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 };
int even = a[0] + a[4];
5.
float ideas[5] = { 1, 2, 3, 4, 5 };
cout << "ideas[1]:" << ideas[1] << endl;
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>
人生如戏,还是戏如人生?微信公众号:传说之路
csdn博客 http://blog.csdn.net/u012175089/article/list/2

浙公网安备 33010602011771号