C++ PrimerPlus(第6版) P31 2.7 编程练习 题6
编写一个程序,其中的main()调用一个用户定义函数(以光年只为参数,并返回对应天文单位的值)。该程序按下面格式要求用户输入光年值,并显示结果:
Enter the number of light years: 4.2
4.2 light years = 252960 astronomical units.
天文单位是从地球到太阳的平均距离(约 150000000 km)。光年是一年走的距离(约10万亿公里)。请使用double类型,转换公式为:
1 光年=63240 天文单位
小编定义函数a()不严谨,也可根据喜好编辑(不要用到C++关键字)
#include<iostream>
using namespace std;
void a(int);
int main()
{
double area;
cout << "Enter the number of light years: ";
cin >> area;
cout << area << " light years = ";
a(area);
return 0;
}
void a(int n)
{
n = n * 63240;
cout << n << " astronomical units.";
}