C++ Primer Plus第二章编程练习题6

6.编写一个程序,其 main()调用一个用户定义的函数(以光年值为参数,并返回对应天文单位的值)。该程序按下面的格式要求用户输入光年值,并显示结果:

Enter the number of light years:4.2
4.2 light years =265608 astronomical units.
天文单位是从地球到太阳的平均距离(约150000000公里或93000000英里),光年是光一年走的距离(约10万亿公里或6万亿英里)(除太阳外,最近的恒星大约离地球4.2光年)。请使用double 类型(参见程序清单 2.4),转换公式为:
1光年-63240天文单位

#pragma region 编程练习6.cpp
	/*
		6.编写一个程序,其 main()调用一个用户定义的函数(以光年值为参数,并返回对应天文单位的值)。该程序按下面的格式要求用户输入光年值,并显示结果:
		Enter the number of light years : 4.2
		4.2 light years = 265608 astronomical units.
		天文单位是从地球到太阳的平均距离(约150000000公里或93000000英里),光年是光一年走的距离(约10万亿公里或6万亿英里)(除太阳外,最近的恒星大约离地球4.2光年)。请使用double 类型(参见程序清单 2.4),转换公式为 :
		1光年 - 63240天文单位
	*/
#if 1
#include <iostream>
	double LightYearsToAstronomicalUnits(double);
int main()
{
	using namespace std;
	cout << "Enter the number of light years :";
	double light_years;
	double astronomical_units;
	cin >> light_years;
	astronomical_units = LightYearsToAstronomicalUnits(light_years);
	cout << light_years << " light years = " << astronomical_units << " astronomical units.";
	return 0;
}
double LightYearsToAstronomicalUnits(double light_years)
{
	using namespace std;
	double astronomical_units;
	//这里没有考虑溢出的情况
	astronomical_units = 63240 * light_years;
	return astronomical_units;
}
#endif
#pragma endregion
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值