首页 > 试题广场 >

编写一个程序,其main()调用一个用户定义的函数(以光年值

[问答题]
编写一个程序,其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天文单位
发表于 2019-03-13 07:16:05 回复(0)
#include <iostream>

using namespace std;

int main()
{
    int tian;
    int light_trans(int light);
    int Light;
    cin>>Light;//用户输入光年
    cout<<"转换后的天文单位:"<<light_trans(Light)<<endl;//转换成天文单位
    return 0;
}

int light_trans(int light)
{
    tian=63240*light;
    return tian;
}

发表于 2020-06-24 14:41:49 回复(0)
#include <iostream>

using namespace std;

double function(double ly)
{
	return 63240 * ly;
}

int main()
{
	double ly;
	cout << "Enter the number of light year:";
	cin >> ly;
	cout << ly << " light years = " << function(ly) << " astronical units.";
	return 0;
}

发表于 2019-10-31 10:03:59 回复(0)
#include<iostream>
using namespace std;
double function(double ly)
{
 double au(63240*ly);
 return au;
}
int main()
{
 double ly;
 cout<<"Enter the number of light year:";
 cin>>ly;
 cout<<ly<<" light years = "<<function(ly)<<" astronical units.";
 return 0;
}

发表于 2019-01-23 19:35:43 回复(0)