C++ Primer Plus P22 编程题一(编写程序,身高转换英寸变英尺)——中职

C++ Primer Plus P22 编程题一

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

1英寸 = 0.0254米
1英尺 = 0.3048米
1英尺为12英寸

/*
C++ Primer Plus P22 编程题一

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

1英寸 = 0.0254米
1英尺 = 0.3048米
1英尺为12英寸
*/

//头文件
#include<iostream>

//符号常量
const int transition = 12;											//转换因子,1英尺为12英寸

//工具
void inch(void)														//身高转换
{
    
    
	using namespace std;											//编译指令

	float height;
	cout << "输入你的身高(厘米):";								//提示用户输入常规身高
	cin >> height;													//输入

	height = height / 100;											//将厘米转换为米
	height = height / 0.0254f;										//米换算成英寸
	cout << "你的身高(英尺)是:" << height / transition << endl;	//英寸换算成英尺
}

//主函数
int main(void)
{
    
    
	using namespace std;											//编译指令
	int height;														//要求用户使用一个整数指出自己的身高(单位为英寸)

	inch();															//查看身高的英寸

	cout << "输入你的身高(整数,英尺):__\b\b";					//要求使用一个整数指出身高(单位为英寸),使用下划线字符来指示输入位置
	cin >> height;													//用户输入
													
	cout << "你的身高的英尺是:" << height << " 剩余的英寸:" << height % transition << endl;

	return 0;
}

这个是用于转换的工具(书中没有的):

//工具
void inch(void)														//身高转换
{
    
    
	using namespace std;											//编译指令

	float height;
	cout << "输入你的身高(厘米):";								//提示用户输入常规身高
	cin >> height;													//输入

	height = height / 100;											//将厘米转换为米
	height = height / 0.0254f;										//米换算成英寸
	cout << "你的身高(英尺)是:" << height / transition << endl;	//英寸换算成英尺
}

感谢观看

再次感谢~

猜你喜欢

转载自blog.csdn.net/qq_51212951/article/details/113506697