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

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

1.

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

2.

arrayactor;
arraybetsic;
arraychuck;
arraydipsea;

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 
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];	
	vectorVec(x);

14.

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

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

15.

#include
using namespace std;
#include

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 
#include
 #include 
const int Size=20; 
vector VecStr(Size); 
array ArrStr;
4.13编程练习 

1.

#include
#include
using namespace std;


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

2.

#include
#include
using namespace std;


int main()
{   
	const int ArSize = 20;
	string name, dessert;
	cout << "Enter your name:\n"<

3.在vs中无法运行

#include 
#include
#include

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 
#include

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<

5.

#include 
#include

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 
#include

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 
#include

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 << "披萨饼公司的名称,可以有多个单词组成: " <

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

#include 
#include

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

9.与上述8类似

10.

#include 
#include

using namespace std;


int main()
{
	float average;
	arraygrade;
	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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1499175.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-25
下一篇 2022-06-25

发表评论

登录后才能评论

评论列表(0条)