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