C++ primer plus 第六版编程练习题参考代码(第三章)

练习一:

#include "pch.h"
#include <iostream>
int main()
{
	using namespace std;
	const int foot = 12;
	int hight;
	cout << "Enter your hight(inches):___ ";
	cin >> hight;

	cout << "Your hight is " << hight / foot << " foot and " << hight % foot << " inches. " << endl;
	return 0;
}

练习二:

#include "pch.h"
#include <iostream>
int main()
{
	using namespace std;
	//cout.setf(ios_base::fixed, ios_base::floatfield);

	const int foot = 12;
	const float meter = 0.0254;
	const float pound = 2.2;
	//要求用户输入身高信息(分别输入英尺英寸)
	int foots;
	cout << "*********BMI(Body Mass Index) Caculator*********" << endl;
	cout << "Please enter your height (foot and inches):\nFoots is: ";
	cin >> foots;
	int inches;
	cout << "Inches is: ";
	cin >> inches;
	cout << "All right!Your height is " << foots*foot+inches<<" inch." << endl;
	//输入用户体重信息(英镑)
	int pounds;
	cout << "Enter your weight: ";
	cin >> pounds;
	cout << "Your weight is " << pounds << " pound.\n";
	//转换各种单位
	int total_inch = foots * foot + inches;
	float height_meter = total_inch*meter;
	float weight_kg = pounds / pound;
	//输出BMI值
	cout << "Your BMI is " << weight_kg / height_meter << " !\n";

	return 0;
}

练习三:

#include "pch.h"
#include <iostream>
int main()
{
	using namespace std;
	const int hour = 60;
	const int second = 60;

	cout << "Enter a latitude in degrees, minutes, and seconds:\n";
	int degrees;
	cout << "First, enter the degrees: ";
	cin >> degrees;
	int minutes;
	cout << "Next, enter the minutes of arc: ";
	cin >> minutes;
	int seconds;
	cout << "Finally, enter the seconds of arc: ";
	cin >> seconds;
	
	cout << degrees << " degrees, " << minutes << " minutes, " << seconds << " seconds = ";
	cout << degrees + minutes / double(hour) + second / double(second) / double(hour);
	return 0;
}

练习四:

#include "pch.h"
#include <iostream>

using namespace std;
int main()
{
	const int minute = 60;
	const int hour = 3600;
	const int day = 86400;
	long seconds = 0;
	cout << "Enter the number of seconds: ";
	cin >> seconds;

	cout << seconds << " seconds = " << seconds / day << " days, "
		<< seconds % day / hour << " hours, "
		<< seconds % hour / minute << " minutes, "
		<< seconds % minute << " seconds." << endl;
	return 0;
}

练习五:

#include "pch.h"
#include <iostream>

using namespace std;
int main()
{
	long long world_popu = 0;
	cout << "Enter the world's population: ";
	cin >> world_popu;
	long long us_popu = 0;
	cout << "Enter the population of the US: ";
	cin >> us_popu;

	cout << "The population of the US is " << us_popu*100.0 / world_popu
		<< "% of the world population.";
	return 0;
}

练习六:

#include "pch.h"
#include <iostream>

using namespace std;
int main()
{
	float miles = 0;
	cout << "Enter the miles of your car driveing: ";
	cin >> miles;
	float gallon = 0;
	cout << "Enter the gallon of the oil: ";
	cin >> gallon;

	cout << "Your car ride " << miles / gallon
		<< " miles in 1 gallons.";
	return 0;
}

练习七:

#include "pch.h"
#include <iostream>

using namespace std;
int main()
{
	cout << "*******EU STYLE*******\n";
	float kilemeter = 0;
	cout << "Enter the distance(km) your car ride: ";
	cin >> kilemeter;
	float litres = 0;
	cout << "Enter the litres of the oil: ";
	cin >> litres;
	cout << "Your car using " << litres / kilemeter*100.0
		<< " L oil in 100km.\n";

	cout << "*******USA STYLE*******\n";
	float miles = 0;
	float gallon = 0;
	miles = 0.6214*kilemeter;
	gallon = litres / 3.875;
	cout << "Your car ride " << miles / gallon << " miles in 1 gallon." << endl;

	return 0;
}

本人采用的编译器是Visual Stdio 2017!

最后贴上个人的公众号,专门分享各种千奇百怪,好玩的软件!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值