(四十四)编程练习

简介:

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

答:


#include<iostream>
int main()
{
	using namespace std;
	int height;
	cout << "请输入你的身高,单位是厘米(cm):___\b\b\b";
	cin >> height;
	cout << "你的身高为:" << height << "厘米。" << endl;
	cout << "因为1cm约等于0.39英寸,1英尺=12英寸。通过换算,你的身高如下:" << endl;
	cout << endl;
	const int yingchi = 12;	//一英尺=12英寸
	const double yingcun = 0.39;	//1厘米=0.39英寸
	int b = height * yingcun;	//将身高(cm)换算为身高(英寸)
	int c = b / yingchi;	//将身高(英寸)除以英尺英寸换算比例,得出英尺的部分
	int d = b%yingchi;	//将身高(英寸)求模英尺英寸换算比例,得出英寸部分
	cout << "你的身高为: " << c << " 英尺 " << d << " 英寸" << endl;
	system("pause");
	return 0;
}

输出:(假设身高填180)


请输入你的身高,单位是厘米(cm):180
你的身高为:180厘米。
因为1cm约等于0.39英寸,1英尺=12英寸。通过换算,你的身高如下:

你的身高为: 5 英尺 10 英寸

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

答:


#include <iostream>
int main()
{
	using namespace std;
	int yingchi, yingcun, bang;
	const double chi_cun = 12;//一英尺=12英寸,需要yingchi*chi_cun
	const double cun_m = 0.0254;	//1英寸=0.0254米,需要用身高的英寸值*cun_m
	const double bang_kg = 2.2;	//1kg=2.2磅,需要用bang/bang_kg
	cout << "请输入你的身高,按照__英尺__英寸的方式输入:" << endl;
	cout << "英尺:";
	yingcun = 0;
	cin >> yingchi;
	cout << "英寸:";
	cin >> yingcun;
	while(yingcun >= 12) 
	{
		cout << "你的英寸输入错误(不应大于等于12),请重新输入。" << endl;
		cout << "英寸:";
		cin >> yingcun;
	}
	double m = (yingchi*chi_cun + yingcun)*cun_m;
	cout << endl;
	cout << "现在请输入你的体重,单位为磅:";
	cin >> bang;
	float kg = bang / bang_kg;
	cout << endl;
	cout << "经过换算,你的身高为:" << m << "米。体重为:" << kg << "千克。" << endl;
	cout << "你的BMI指数为:" << kg / (m*m) << endl;
	system("pause");
	return 0;
}

输出结果为:


请输入你的身高,按照__英尺__英寸的方式输入:
英尺:5
英寸:10

现在请输入你的体重,单位为磅:180

经过换算,你的身高为:1.778米。体重为:81.8182千克。
你的BMI指数为:25.8813

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

Enter a latitude in degrees, minutes, and seconds;

First, enter the degrees: 37

Next, enter the minutes of arc: 51

Finally, enter the seconds of arc: 19

37 degrees, 51 minutes, 19 seconds = 37.8553 degrees

答:


#include<iostream>
int main()
{
	using namespace std;
	double du, fen, miao;
	const int du_fen = 60;	//1度=60分
	const int fen_miao = 60;	//1分=60秒
	double degrees;
	cout << "Enter a latitude in degrees, minutes, and seconds;" << endl;
	cout << "First, enter the degrees: " ;
	cin >> du;
	cout << "Next, enter the minutes of arc: ";
	cin >> fen;
	cout << "Finally, enter the seconds of arc: ";
	cin >> miao;
	degrees = (miao / fen_miao + fen) / du_fen + du;
	cout << du << " degrees " << fen << " minutes " << miao << " seconds = " << degrees << " degrees" << endl;
	system("pause");
	return 0;

}


5.编写一个程序,要求用户输入全球当前的人口和美国当前的人口(或者其他国家的人口)。将这些信息储存在long long变量中,并让程序显示美国(或其他国家)的人口占全球人口的百分比。改程序输出应与下面类似:

Enter the world's population: 6398758899

Enter the population of the US: 310763781

The population of the US is 4.50492% of the world population

答:


#include<iostream>
int main()
{
	using namespace std;
	long long world, US;
	cout << "Enter the world's population: ";
	cin >> world;
	cout << "Enter the population of the US: ";
	cin >> US;
	double a = double(US)/double(world)*100;
	cout << "The population of the US is " << a << "% of the world population" << endl;
	system("pause");
	return 0;
}

输出结果为:


Enter the world's population: 6398758899
Enter the population of the US: 310763781
The population of the US is 4.85663% of the world population

————为什么结果不一样啊?因为精度么?可是我最后无论是double long还是double,结果都是4.8%呀,用计算器算也是4.8%。奇怪!


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

答:


#include<iostream>
int main()
{
	using namespace std;
	char a;
	cout << "这里可以帮你计算你的汽车耗油量,请输入计算类型:" << endl;
	cout << "a.计算每加仑行驶里程。" << endl;
	cout << "b.计算每百公里耗油量(升)。" << endl;
	cin >> a;
	if(a=='a')
	{
		int yingli, jialun;
		cout << "输入你行驶的里程(单位:英里): ____\b\b\b\b";
		cin >> yingli;
		cout << "输入你的耗油量(单位:加仑):____\b\b\b\b";
		cin >> jialun;
		cout << "经过计算,你的汽车每加仑可以行驶 " << double(yingli) / double(jialun) << " 英里。" << endl;
	}
	else if (a == 'b')
	{
		int km, L;
		cout << "输入你行驶的里程(单位:公里): ____\b\b\b\b";
		cin >> km;
		cout << "输入你的耗油量(单位:升): ____\b\b\b\b";
		cin >> L;
		cout << "经过计算,你的汽车百公里耗油量为 " << double(L) / double(km) * 100 << " 升。" << endl;
	}
	else
	{
		cout << "输入错误,程序自动退出。" << endl;
	}
	system("pause");
	return 0;
}

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

答:


#include<iostream>

int main()
{
	using namespace std;
	float km, L;
	cout << "请输入你的汽车耗油量(欧洲风格),本程序将自动帮你转换为美国风格的耗油量。\n";
	cout << "请输入你已知耗油量的汽车行驶里程(单位:公里): ";
	cin >> km;
	cout << "请输入这段行驶里程的耗油量(单位:升): ";
	cin >> L;
	cout << "你的汽车行驶 " << km << " 公里的耗油量为 " << L << " 升。\n百公里耗油量为。" << double(L) / double(km) * 100 << " 升。\n\n";
	float yingli = double(km) / 100 * 62.14;
	float jialun = double(L) / 3.875;
	cout << "通过换算,你的汽车的美式风格耗油量为: " << yingli / jialun << " 英里每加仑。" << endl;
	system("pause");
	return 0;
}


目录
相关文章
|
3月前
C#WinForm基础编程(二)
C#WinForm基础编程
62 0
|
3月前
|
C# 数据安全/隐私保护
C#WinForm基础编程(一)
C#WinForm基础编程
63 0
|
3月前
|
SQL 数据库连接 应用服务中间件
C#WinForm基础编程(三)
C#WinForm基础编程
82 0
|
7月前
|
存储 测试技术 C#
C#之四十四 滑铁卢战役
C#之四十四 滑铁卢战役
69 0
|
5月前
|
前端开发 程序员
前端知识笔记(四十四)———为什么要学代码
前端知识笔记(四十四)———为什么要学代码
35 0
|
7月前
|
前端开发 定位技术 容器
webgis前端控件编程
webgis前端控件编程
46 0
|
8月前
|
SQL 关系型数据库 MySQL
C#三十四 常用开发的部分总结
C#三十四 常用开发的部分总结
24 0
|
10月前
|
存储 Kubernetes 开发工具
【k8s 系列】k8s 学习二十一,k8s 中的卷
前面的文章我们分享了 pod ,RC,RS,DaemonSet,CJ,Service 等各种资源
|
10月前
|
Kubernetes API 调度
【k8s 系列】k8s 学习二十七-2,k8s 自身原理 2
前面我们说到 K8S 的基本原理和涉及的四大组件,分享了前两个组件 etcd 和 ApiServer 这一次我们接着分享一波
|
10月前
|
Kubernetes 监控 Linux
【k8s 系列】k8s 学习二十七 - 5,k8s 自身原理 5
我们知道容器是通过 pod 来承载的,我们在 k8s 中,服务都是跑在 pod 里面的,pod 里面可以跑 1 个容器,或者跑多个容器,那么咱们 pod 里面跑 1 个服务容器,咱真的就以为里面就只有这样个容器吗