C++ primer plus 6 第四章习题答案

C++ primer plus 6 第四章习题答案

在这里插入图片描述在这里插入图片描述在这里插入图片描述

//4.4  使用string对象和头文件
#include<iostream>
#include<string>
using namespace std;
int main()
{
	string first_name, last_name, full_name;
	cout << "Enter your first name: ";
	getline(cin, first_name);
	cout << "Enter your last name: ";
	getline(cin, last_name);
	full_name = last_name + "." + first_name;
	cout << "Here's the information in a single string: ";
	cout << full_name << endl;
	return 0;
}
// 4.5 CandyBar 糖块
#include<iostream>
using namespace std;
struct CandyBar
{
	char brand[20];
	float weight;
	unsigned int calorie;
};
int main()
{
	CandyBar snack = { "Mocha Munch", 2.3, 350 };
	cout << "My favourite CandyBar is " << snack.brand << "." << endl;
	cout << "And its weight is " << snack.weight << ", calorie is " << snack.calorie;
	cout << "." << endl;
	return 0;

}
//4.6 Candy 结构相关
#include<iostream>
using namespace std;
struct CandyBar
{
	char brand[20];
	float weight;
	unsigned int calorie;
};
int main()
{
	CandyBar snack[3] = { {"Mocha Munch", 2.3, 350},{"Hershey bar", 4.2, 550},{"Musketeers", 2.6, 430} };
	cout << "My 1st CandyBar is " << snack[0].brand << "." << endl;
	cout << "And its weight is " << snack[0].weight << ", calorie is " << snack[0].calorie;
	cout << "." << endl;
	cout << "My 2st CandyBar is " << snack[1].brand << "." << endl;
	cout << "And its weight is " << snack[1].weight << ", calorie is " << snack[1].calorie;
	cout << "." << endl;
	cout << "My 3st CandyBar is " << snack[2].brand << "." << endl;
	cout << "And its weight is " << snack[2].weight << ", calorie is " << snack[2].calorie;
	cout << "." << endl;
	return 0;
}
//4.7 william wingate 披萨
#include<iostream>
using namespace std;
struct Pizza
{
	char company[40];
	float diameter;
	float weight;
};
int main()
{
	Pizza dinner;
	cout << "Enter the PIzza's information: " << endl;
	cout << "Pizza's company:";
	cin.getline(dinner.company, 40);
	cout << "Pizza's diameter(inchs): ";
	cin >> dinner.diameter;

	cout << "CandBar's weight(pounds):";
	cin >> dinner.weight;
	cout << "The lunch pizza is " << dinner.company << "." << endl;
	cout << "And its diameter is " << dinner.diameter << " inch,weight is " << dinner.weight;
	cout << "pounds." << endl;
	return 0;

}
//4.8 披萨公司相关
#include<iostream>
using namespace std;
struct Pizza
{
	char company[40];
	float diameter;
	float weight;
};
int main()
{
	Pizza* ppizza = new Pizza;
	cout << "Enter the pizza's information: " << endl;
	cout << "Pizza's diameter(inchs): ";
	cin >> ppizza->diameter;

	cout << "Pizza's company:";
	cin.getline(ppizza->company, 40);

	cout << "CandBar's weight(pounds): ";
	cin >> ppizza->weight;
	cout << "The lunch pizza is " << ppizza->company << "." << endl;
	cout << "And it's diameter is " << ppizza->diameter << " inch,weight is" << ppizza->weight;
	cout << "pounds. " << endl;
	delete ppizza;
	return 0;

}
//4.9 使用new动态分配数组
#include<iostream>
using namespace std;
struct CandyBar
{
	char brand[20];
	float weight;
	unsigned int calorie;
};
int main()
{
	CandyBar*pc = new CandyBar[3];
	strcpy_s(pc[0].brand, "Mocha Munch");
	pc[0].weight = 2.3;
	pc[0].calorie = 350;
	strcpy_s(pc[1].brand, "Hershey bar");
	pc [1].weight = 24.2;
	pc [1].calorie = 550;
	strcpy_s(pc[2].brand, "Musketeers");
	pc [2].weight = 2.6;
	pc [2].calorie = 430;

	cout << "My 1st CandyBar is " << pc->brand << "." << endl;
	cout << "And its weight is " << pc->weight << ", calorie is " << pc->calorie;
	cout << "." << endl;
	cout << "My 2st CandyBar is " << (pc+1)->brand << "." << endl;
	cout << "And its weight is " << (pc+1)->weight << ", calorie is " <<( pc+1)->calorie;
	cout << "." << endl;
	cout << "My 3st CandyBar is " << (pc+2)->brand << "." << endl;
	cout << "And its weight is " << (pc+2)->weight << ", calorie is " << (pc+2)->calorie;
	cout << "." << endl;
	delete[]pc;
	return 0;
}
// 4.10 跑步成绩
#include<iostream>
#include<array>
using namespace std;
int main()
{
	array<float, 3> record_list;
	float average;
	cout << "Please input three record of 40 miles.\n";
	cout << "First record: ";
	cin >> record_list[0];
	cout << "Second record: ";
	cin >> record_list[1];
	cout << "Third record: ";
	cin >> record_list[2];
	cout << "ok, you input:\n1." << record_list[0] << "\n2." << record_list[1] << "\n3." << record_list[2] << endl;
	average = (record_list[0] + record_list[1] + record_list[2]) / 3;
	cout << "Congratylate, your average performance is " << average << ".";
	return 0;
}

第四章终于敲完了
假期冲冲冲

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值