C++ Primer plus(第六版)中文版 第四章练习

1.

char actor[30];
short betsic[100];
float chuck[13];
long double  dipsea[64];

2.

array<char, 30>actor;
array<short, 100>betsic;
array<float, 13>chuck;
array<long double, 64>dipsea;

3.

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

4.

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

5.

float ideas[6] = { 0,1,2,3,4,5 };
cout << ideas[2];

6.

char s[] = "cheeseburger";

7.

string s = "Waldorf Salad";

8.

struct Fish {
	string name;
	int weight;//单位为盎司
	float longs;//单位为小数
};

9.

int main()
{
	Fish fish;
	fish.name = "clownfish";
}

10.

enum Response{No,Yes,Maybe};

11.在使用vs编写程序时,有C26429的警告无法解决

#include<iostream> 
using namespace std;

int main()
{
	int ted = 2;
	int *p = &ted;
	cout << *p << endl;
}

12.

float treacle[10];
	float* p = treacle;
	cout << treacle[0] << treacle[10];

13.

	int x;
	cout << "请输入一个正整数" << endl;
	cin >> x;
	int* p = new int[x];	
	vector<int>Vec(x);

14.

cout << (int*)"Home of the jolly bytes";

有效,他将打印字符串的地址

15.

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

int main()
{

	struct Fish { char Kind[30]; int Weight; float Length; }; 
	Fish* p = new Fish; 
	cout << "Enter the kind of fish: " << endl; 
	cin >> p->Kind;
}

16.程序清单 4.6 的执行流程可以大致解释为:当输入 year 的值并按下回车键后, year 的值被存储在 year 中,但换行符被保留在了输入队列中,因此下面的 getline() 函数一眼便看到了换行符,于是便以为读到了行尾,因此便把一个空字符赋值给了address。所以使用 cin>>address 时,它可以使得程序跳过空白继续读取直到再次遇到空白字符位置。因此它可以跳过数字输入后保留下来的换行符,可以避免这个问题。但是又会引发另一个问题,也就是它只能读取一个单词,而不是一整行。

17.

#include<string> 
#include<vector>
 #include<array> 
const int Size=20; 
vector<string> VecStr(Size); 
array<string,Size> ArrStr;

4.13编程练习 

1.

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


int main()
{   
	string str1,str2,grade;
	int age;
	cout << "What is your first name?"<<endl;
	cin >> str1;
	cout << "What is your last name?"<<endl;
	cin >> str2;
	cout << "What letter grade do you deserve?"<<endl;
	cin >> grade;
	cout << "What is your age?"<<endl;
	cin >> age;
	cout << "Name:" << str2 << " , " << str1<<endl;
	cout << "Grade:"<<grade<< endl;
	cout << "Age: " << age << endl;
	
}

2.

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


int main()
{   
	const int ArSize = 20;
	string name, dessert;
	cout << "Enter your name:\n"<<endl;
	getline(cin, name);
	cout << "Enter your favorite dessert:\n"<<endl;
	getline(cin, dessert);
	cout << "I have some delicious " << dessert;
	cout << " for you, " << name << ",\n";
	return 0;
}

3.在vs中无法运行

#include<iostream> 
#include<stdlib.h>
#include<cstring>

using namespace std;

int main()
{
	char Name[2][20];
	cout << "Enter your first name: ";
	cin >> Name[0];
	cout << "Enter your second name: ";
	cin >> Name[1];
	cout << "Here is the information in a single string: " << strcat(strcat(Name[0], ", "), Name[1]) << endl;
	system("pause");
	return 0;
}

4.

#include<iostream> 
#include<string>

using namespace std;

int main()
{
	string firstName, secondName;
	cout << "Enter your first name: " << endl;
	cin >> firstName;
	cout << "Enter your second name: " << endl;
	cin >> secondName;
	cout << "Here's the information in a single string : " << firstName + "," + secondName<<endl;
}

5.

#include<iostream> 
#include<string>

using namespace std;
struct CandyBar {
	string name;
	float weight;
	int content;
};

int main()
{
	CandyBar snack = {
		"Mocha Munch",
		2.3,
		350,
	};

	cout << snack.name << endl;
	cout << snack.content << endl;
	cout << snack.weight << endl;
}

6.

#include<iostream> 
#include<string>

using namespace std;
struct CandyBar {
	string name;
	float weight;
	int content;
};

int main()
{

	CandyBar snack[3] = {
		{"Mocha Munch",2.3,350,},
		{"AAAA",4.5,450},
		{"BBBB",6.5,550},

	};
	cout << snack[0].name << endl;
	cout << snack[0].content << endl;
	cout << snack[0].weight << endl;

	cout << snack[1].name << endl;
	cout << snack[1].content << endl;
	cout << snack[1].weight << endl;

	cout << snack[2].name << endl;
	cout << snack[2].content << endl;
	cout << snack[2].weight << endl;
}

7.

#include<iostream> 
#include<string>

using namespace std;
struct Wingate {
	string name;
	int diameter;
	int height;
};

int main()
{
	Wingate campany;
	cout << "披萨饼公司的名称,可以有多个单词组成" << endl;
	cin >>campany.name;
	cout << "披萨饼的直径" << endl;
	cin >> campany.diameter;
	cout << "披萨饼的重量" << endl;
	cin >> campany.height;

	cout << "披萨饼公司的名称,可以有多个单词组成: " <<campany.name<< endl;
	cout << "披萨饼的直径: " <<campany.diameter<< endl;
	cout << "披萨饼的重量:"<<campany.height << endl;
}

 8.使用vs来编译时,是有警告的,提示需要使用智能模板库

#include<iostream> 
#include<string>

using namespace std;
struct Wingate {
	string name;
	int diameter;
	int height;
};

int main()
{
	Wingate *campany=new Wingate ;
	cout << "披萨饼公司的名称,可以有多个单词组成" << endl;
	cin >>campany->name;
	cout << "披萨饼的直径" << endl;
	cin >> campany->diameter;
	cout << "披萨饼的重量" << endl;
	cin >> campany->height;

	cout << "披萨饼公司的名称,可以有多个单词组成: " <<campany->name<< endl;
	cout << "披萨饼的直径: " <<campany->diameter<< endl;
	cout << "披萨饼的重量:"<<campany->height << endl;
	delete campany;
	return 0;
}

9.与上述8类似

10.

#include<iostream> 
#include<array>

using namespace std;


int main()
{
	float average;
	array<float, 3>grade;
	cout << "请输入第一次成绩" << endl;
	cin >> grade.at(0);
	cout << "请输入第二次成绩" << endl;
	cin >> grade.at(1);
	cout << "请输入第三次成绩" << endl;
	cin >> grade.at(2);
	average =(grade.at(0)+ grade.at(1)+ grade.at(2)) / 3;
	cout << "三次平均成绩是: " << average ;
}

【C++】C++11的std::array的详细剖析_Yngz_Miao的博客-CSDN博客_c++ std::array

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值