第三章 处理数据
1. 编写一个程序,要求用户使用一个整数指出自己的身高(单位为英寸),然后将身高转换为英尺和英寸。该程序使用下划线字符来指示输入位置,另外,使用一个const符号常量来表示转换因子。(百度:1英尺=12英寸)
#include<iostream>
using namespace std;
const double Times=12.0;//倍数是12
int main()
{
double height;
cout<<"Enter your height int inches_" ;
cin>>height;
cout<<"Your height convert to "<<height/Times<<" feet."<<endl;
return 0;
}
2、编写一个小程序,要求以几英尺几英寸的方式输入其身高,并以磅为单位输入其体重。(使用3个变量来存储这些信息。)该程序报告其BMI。为了计算BMI,该程序以英寸的方式指出用户的身高,并将以英寸为单位的身高转换为以米为单位的身高(1英寸为0.0254米)。然后将以磅为单位的体重转换为以千克为单位的体重(1千克= 2.2磅)。最后,计算相应的BMI——体重(千克)除以身高(米)的平方。用符号常量表示各种转换因子。
#include<iostream>
using namespace std;
const int FtoI=12;
const double ItoM=0.0254;
const double KtoP=2.2;
int main()
{
int height_foot,height_inch;
double weight_pound,height,weight,BMI;
cout<<"Please enter your height feet:";
cin>>height_foot;
cout<<"Please enter your height inches:";
cin>>height_inch;
cout<<"Please enter your weight in pounds:";
cin>>weight_pound;
height=(height_foot*FtoI+height_inch)*ItoM;
weight=weight_pound/KtoP;
BMI=weight/(height*height);
cout<<"Your BMI is "<<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;
const int DtoM=60;
const int DtoS=3600;
int main()
{
int degree,minute,second;
double Degree;
cout<<"Enter a latitude in degrees,minutes ,and seconds:"<<endl;
cout<<"First ,enter the degrees:";
cin>>degree;
cout<<"Next ,enter the minutes of arc:";
cin>>minute;
cout<<"Finally ,enter the seconds of arc:";
cin>>second;
Degree=degree+(double)minute/DtoM+(double)second/DtoS;
cout<<degree<<"degrees, "<<minute<<"minutes, "<<second<<"seconds= "<<Degree;
return 0;
}
4、编写一个程序,要求用户以整数方式输入秒数(使用long或long long变量存储),然后以天、小时、分钟和秒的方式显示这段时间。使用符号常量来表示每天有多少小时、每小时有多少分钟以及每分钟有多少秒。该程序的输出应与下面类似:
Enter the number of seconds:31600000
31600000 seconds=365 days, 17 hours,46 minutes,40 seconds
#include<iostream>
using namespace std;
const int DtoH=24;
const int HtoM=60;
const int MtoS=60;
int main()
{
long long seconds;
long long Seconds;
int days,hours,minutes;
cout<<"Please enter the number of seconds:" ;
cin>>seconds;
Seconds=seconds;
days=seconds/(DtoH*HtoM*MtoS);
seconds=seconds%(DtoH*HtoM*MtoS);
hours=seconds/(HtoM*MtoS);
seconds=seconds%(HtoM*MtoS);
minutes=seconds/MtoS;
seconds=seconds%MtoS;
cout<<Seconds<<"seconds="<<days<<"days,"<<hours<<"hours,"<<minutes<<"minutes,"<<seconds<<"seconds"<<endl;
return 0;
}
5、编写一个程序,要求用户输入全球当前的人口和美国当前的人口(或其他国家的人口)。将这些信息存储在long long变量中,并让程序显示美国(或其他国家)的人口占全球人口的百分比。该程序的输出应与下面类似:
Enterthe word’s population :6898758899
Enterthe population of the US :310783781
Thepopulation of the US is 4.50492
#include<iostream>
using namespace std;
int main()
{
long long global_population,american_population;
double percent;
cout<<"Enter the word's population :";
cin>>global_population;
cout<<"Enter the population of the US :";
cin>>american_population;
percent=(double)american_population/(double)global_population;
cout<<"The population of the US is "<<percent*100<<"% of the world population."<<endl;
return 0;
}
6、编写一个程序,要求用户输入驱车里程(英里)和使用汽油量(加仑),然后指出汽车耗油量为一加仑的里程。如果愿意,也可以让程序要求用户以公里为单位输入距离,并以升为单位输入汽油量,然后指出欧洲风格的结果——即每100公里的耗油量(升)。
#include<iostream>
using namespace std;
int main()
{
double Mile,Gallon,fuel_consume,Kilometer,Liter;
cout<<"Enter the distance in miles:";
cin>>Mile;
cout<<"Enter the fuel consume in gallon:";
cin>>Gallon;
fuel_consume=Mile/Gallon;
cout<<"The fuel consume is "<<fuel_consume<<"miles/gallon."<<endl;
cout<<"Enter the distance in kilometer:";
cin>>Kilometer;
cout<<"Enter the fuel consume in liter:";
cin>>Liter;
fuel_consume=100*(Liter/Kilometer);
cout<<"The fuel consume is "<<fuel_consume<<"liter/100Km."<<endl;
return 0;
}
7、编写一个程序,要求用户按欧洲风格输入汽车的耗油量(每100公里消耗的汽油量(升)),然后将其转换为美国风格的耗油量——每加仑多少英里。注意,除了使用不同的单位计量外,美国方法(距离/燃料)与欧洲方法(燃料/距离)相反。100公里等于62.14英里,1加仑等于3.875升。因此19mpg大约合12.41/100Km,127mpg大约合8.71/100km。
#include<iostream>
using namespace std;
const double GtoL=3.875;
const double HtoM=62.14;
int main()
{
double fuel_consume_Eur,fuel_consume_US;
cout<<"Enter the fuel consume in Europestyle(L/100Km):";
cin>>fuel_consume_Eur;
fuel_consume_US=(GtoL*HtoM)/fuel_consume_Eur;
cout<<"The fuel consume is"<<fuel_consume_US<<"miles/gallon"<<endl;
return 0;
}
#