C++ Primer Plus P23 编程题三
编写一个程序,要求用户以度、分、秒的方式输入一个纬度,然后以度为单位显示该纬度。
1°等于60’,1’等于60",请以符号常量表示这些值。对于每个输入值,应使用一个独立的变量存储它。
1°等于60’,1’等于60"
下面是该程序运行时的输出:
Enter a latitude in degrees, minutes, and secounds:
First, enter the degree:37
Next, enter the minutes of arc:51
Finally, enter the secounds of arc:19
37 degrees, 51 minuts, 19 secounds = 37.8553 degress
/*
C++ Primer Plus P23 编程题三
编写一个程序,要求用户以度、分、秒的方式输入一个纬度,然后以度为单位显示该纬度。
1°等于60',1'等于60",请以符号常量表示这些值。对于每个输入值,应使用一个独立的变量存储它。
1°等于60',1'等于60"
下面是该程序运行时的输出:
Enter a latitude in degrees, minutes, and secounds:
First, enter the degree:37
Next, enter the minutes of arc:51
Finally, enter the secounds of arc:19
37 degrees, 51 minuts, 19 secounds = 37.8553 degress
*/
//头文件
#include<iostream>
//转换因子
const int DEGREE_TO_MINUTE = 60; //分转度的转换因子
const int MINUTE_TO_SECOND = 60; //秒转分的转换因子
//主函数
int main(void)
{
using namespace std; //编译指令
int degree, minute, second;
double degrees;
cout << "Enter a latitude in degrees, minutes, and secounds:" << endl;//显示Enter a latitude in degrees, minutes, and secounds:
cout << "First, enter the degree:"; //显示First, enter the degree:
cin >> degree; //用户输入度值
cout << "Next, enter the minutes of arc:"; //显示Next, enter the minutes of arc:
cin >> minute; //用户输入分值
cout << "Finally, enter the secounds of arc:"; //显示Finally, enter the secounds of arc:
cin >> second; //用户输入秒值
degrees = degree + (float(minute) / DEGREE_TO_MINUTE) + (float(second) / MINUTE_TO_SECOND) / DEGREE_TO_MINUTE; //计算度值
cout << degree << "degress, " << minute << " minutes, " << second << " seconds = " << degrees << " degrees." << endl; //输出度
return 0;
}
注意计算公式(分转度,秒转分转度):
degrees = degree + (float(minute) / DEGREE_TO_MINUTE) + (float(second) / MINUTE_TO_SECOND) / DEGREE_TO_MINUTE; //计算度值
结果:
Enter a latitude in degrees, minutes, and secounds:
First, enter the degree:37
Next, enter the minutes of arc:51
Finally, enter the secounds of arc:19
37degress, 51 minutes, 19 seconds = 37.8553 degrees.
感谢观看
再次感谢~