C++ primer plus编程练习(第三章)

3.7

1.编写一个程序,要求用户使用一个整数指出自己的身高(单位为英寸),然后将身高转换为英尺和英寸。该程序使用下划线字符来指示输入位置。另外,使用一个const符号常量来表示转换因子。

代码如下:

#include <iostream>
using namespace std;
int main() {
	const int x = 12;//1英尺=12英寸
	int height;
	cout << "请输入你的身高(英寸):__\b\b";
	cin >> height;
	cout << "你的身高为" << height / x << "英尺" << height % x << "英寸" << endl;//取整和取余
	return 0;
}

 运行效果:

2.编写一个程序,要求以几英尺几英寸的方式输入其身高,并以磅为单位输入其体重。(使用三个变量存储)。该程序报告其BMI。为了计算BMI,该程序以英寸的方式指出用户的身高,并将以英寸为单位的身高转换为以米为单位的身高(1英寸=0.0254米)。然后,将以磅为单位的体重转换为以千克为单位的体重(1千克=2.2磅)。最后,计算相应的BMI=体重(千克)/身高(米)的平方。用符号常量表示各种转换因子。

代码如下:

#include <iostream>
using namespace std;
//用全局变量表示好三个转换因子
const int a = 12;
const double b = 0.0254;
const float c = 2.2;
//计算身高
float Height(int f, int i) {
	return (f * 12 + i) * b;
}
//计算体重
float Weight(double w) {
	return w / c;
}

int main() {
	int feet, inches;
	float height, weight;
	cout << "请输入身高\n" << "英尺:";
	cin >> feet;
	cout << "英寸:";
	cin >> inches;
	cout << "你的身高为" << feet << "英尺" << inches << "英寸" << endl;
	cout << "请输入体重\n" << "磅:";
	cin >> weight;
	height = Height(feet, inches);
	weight = Weight(weight);
	cout << "转换后的身高为:" << height << "米;" << "体重为:" << weight << "千克" << endl;
	cout << "你的BMI指数为:" << weight / (height * height);
	return 0;
}

运行效果:

3.编写一个程序,要求用户以度,分,秒的方式输入一个纬度,然后以度为单位显示该纬度。1度为60分,1分等于60秒,请以符号常量的方式表示这些值。对于每个输入值,应使用一个独立的变量存储它。

代码如下:

#include <iostream>
using namespace std;
int main() {
	//转换因子为60
	const int x = 60;
	double degrees, minutes, seconds;
	cout << "Enter a latitude in degrees, minutes, seconds:" << endl;
	cout << "First, enter the degrees:";
	cin >> degrees;
	cout << "Next, enter the minutes of arc:";
	cin >> minutes;
	cout << "Finally, enter the seconds of arc:";
	cin >> seconds;
	cout << degrees << " degrees, " << minutes << " minutes, " << seconds << " seconds =" 
		 << degrees + minutes / x + seconds / (x * x) << " degrees";
	return 0;
}

运行效果:

4.编写一个程序,要求用户以整数方式输入秒数(使用long或long long变量存储),然后以天、小时、分钟和秒的方式显示这段时间。使用符号常量来表示每天有多少小时,每小时有多少分钟,以及每分钟有多少秒。

代码如下:

#include <iostream>
using namespace std;
int main() {
	long seconds;
	int h = 24;
	int m = 60;
	int s = 60;
	cout << "Enter the number of seconds: ";
	cin >> seconds;
	cout << seconds << " seconds = " << seconds / (h * m * s) << " days, " << (seconds % (h * m * s)) / (m * s) << " hours, "
		<< ((seconds % (h * m * s)) % (m * s)) / s << " minutes, " << ((seconds % (h * m * s)) % (m * s)) % s << " seconds";
	return 0;
}

运行效果:

5.编写一个程序,要求用户输入全球当前的人口和中国当前的人口。将这些信息存储在long long变量中,并让程序显示中国的人口占全球人口的百分比。

代码如下:

#include <iostream>
using namespace std;
int main() {
	long long w;
	long long cn;
	cout << "Enter the world's population: ";
	cin >> w;
	cout << "Enter the population of the china: ";
	cin >> cn;
	cout << "The population of the china is " << (double(cn) / double(w))*100 << "% of the world population.";
	return 0;
}

运行效果:

6.编写一个程序,要求用户输入驱车里程(英里)和使用汽油量(加仑),然后指出汽车耗油量为一加仑的里程。如果愿意,也可以让程序要求用户以公里为单位输入距离,并以升为单位输入汽油量,然后指出欧洲风格的结果-即每100公里的耗油量(升)。

代码如下:

#include <iostream>
using namespace std;

int main() {
    double d;
    double v;
    cout << "请输入驱车里程(英里):";
    cin >> d;
    cout << "请输入使用汽油量(加仑):";
    cin >> v;

    cout << "一加仑的里程为: " << d / v << "英里/加仑" << endl;
    return 0;
}

运行效果:

7.编写一个程序,要求每个用户按欧洲风格输入汽车的耗油量(每100公里消耗汽油量(升 )),然后将其转换为美国风格的耗油量-每加仑多少英里。注意,除了使用不同的单位计量外,美国方法(距离/燃料)与欧洲方法(燃料/距离)相反。100公里等于62.14英里,1加仑等于3.785升。因此,19mpg(1加仑能跑多少公里)大约合12.41/100km,27mpg大约合8.71/100km。

代码如下:

#include <iostream>
using namespace std;

int main()
{
	double o, m;
	cout << "请输入欧洲风格汽车的耗油量:";
	cin >> o;

	m = 1 / o * 62.14 * 3.785;
	cout << "美国风格的耗油量为:" << m << endl;
	return 0;
}

运行效果:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值