第三章 处理数据
1,编写一个程序,要求用户使用一个整数指出自己的身高(单位英寸),然后将身高转换为英尺和英寸。该程序使用下划线字符来只是输出位置。另外,使用一个const符号常量来表示转换因子。
#include<iostream>
using namespace std;
const int foot = 12;
int main()
{
int myHeight;
cout<<"Enter you height:_____\b\b\b\b\b";
cin>>myHeight;
cout<<"Your height is "<<myHeight/foot
<<" foot "
<<myHeight-myHeight/foot*12<<" inches\n";
return 0;
}
2,编写一个小程序,要求以几英尺几英寸的方式输入其身高,并以榜为单位输入其体重(使用三个变量来存储这些信息)该程序报告其BMI。
#include<iostream>
using namespace std;
const int foot = 12;
const double meter = 0.0254;
const double kilo = 2.2; //用符号常量表示转换因子
int main()
{
int myFoot=0,myInch=0;
double myWeight=0,height_meter;
double num_BMI;
cout<<"Enter your height:"<<endl<<"Foot:____\b\b\b\b";
cin>>myFoot;
cout<<"Inch:____\b\b\b\b";
cin>>myInch; //输入身高
cout<<"Enter your weight:____\b\b\b\b"
<<myWeight;
cin>>myWeight; //输入体重
height_meter = (myFoot*foot + myInch)*meter; //换算
num_BMI = myWeight*kilo/height_meter; //计算BMI值
cout<<"Your BMI number is:_____\b\b\b\b\b"<<num_BMI<<endl;
return 0;
}
3,编写一个程序,要求用户以度、分、秒的方式输入一个纬度,然后以度为单位显示该纬度。1度为60分,1分等于60秒,请以符号常量的方式表示这些值。对于每个输入值,应使用一个独立的变量储存它。
Enter a latitude in degrees, minutes, and seconds:
First, enter the degrees: 37
Next, enter the minutes of arc: 51
Finally, enter the seconds of arc: 19
37 degrees, 51 minutes, 19 seconds = 37.8553 degrees
#include<iostream>
using namespace std;
int main()
{
int in_degree,in_minute,in_second;
double lati_degree;
const int degree = 60;
const int minute = 60;
cout<<"Enter a latitude in degrees, minutes, and seconds:"<<endl;
cout<<"First, enter the degrees: ";
cin>>in_degree;
cout<<"Next, enter the minutes of arc:";
cin>>in_minute;
cout<<"Finally, enter the seconds of arc:";
cin>>in_second;
lati_degree = in_degree + double(in_minute)/degree + double(in_second)/(degree*minute);
cout<<in_degree<<" degrees,"<<in_minute<<" minute,"<<in_second<<" seconds, "
<<"="<<lati_degree<<" degrees"<<endl;
return 0;
}
4,编写一个程序,要求用户以整数方式输入秒数(使用long或long long变量存储),然后以天、小时、分钟和秒的方式显示这段时间。使用符号常量来表示每天有多少小时、每小时有多少分钟以及每分钟有多少秒。