第三章 编程练习
注:以下程序中,main()函数里的内容一般情况尽量缩进两格。
1. 编写一个小程序,要求用户使用一个整数指出自己的身高(单位为英寸),然后将身高转换为英尺和英寸,该程序使用下划线字符来指示输入的位置。另外,使用一个const符号常量来表示转换因子。
注:1米等于39.37英寸(in),1英尺(ft)等于12英寸(in)
//用户使用一个整数指出自己的身高
#include<iostream>
int main()
{
using namespace std;
const int inch_per_foot = 12;
int inch;
cout << "Enter your height in inch:__\b\b";
cin >> inch;
int foot = inch / inch_per_foot;
int inchTwo= inch% inch_per_foot;
cout << inch<<" inch are "<<foot<<" foot,"<<inchTwo<< "inchTwo"<<endl;
cin.get();
cin.get();
return 0;
}
2.编写一个小程序要求以几英尺几英寸的方式输入其身高,并以磅为单位输入其体重。(使用3个变量来存储这些信息),该程序报告其BMI。
为了计算BMI,该程序以英寸的方式指出用户的身高,并将以英寸为单位的身高转换为以米为单位的身高;然后将以磅为单位的体重转换为以千克为单位的体重;最后计算相应的BMI。
注:1英尺为12英寸,1英寸为0.0254 米;1千克=2.2 磅;BMI=体重(kg)除以身高(m)的平方。
//用户输入身高和体重,来测量体脂
#include<iostream>
int main()
{
using namespace std;
const int inch_per_foot =12;
const double mi_per_inch =0.0254;
const double lbs_per_kg = 2.2;
cout<<"请输入您的身高(foot):";
int foot;
cin>>foot;
cout<<"请输入您的身高(inch):";
int inch;
cin>>inch;
int inchs = foot*inch_per_foot+inch;
cout <<"\n您的身高为 "<<inchs<<" 英寸"<<endl;
cout <<"请输入您的体重(磅):";
double lbs;
cin>>lbs;
cout <<"\n您的体重为:"<<lbs<<"磅"<<endl;
double mi =inchs*mi_per_inch;
cout <<"您的身高是:"<<mi<<"米."<<endl;
double BMI = lbs/lbs_per_kg/(mi*mi);
cout <<"您的体脂率为:"<<BMI<<endl;
cin.get();
cin.get();
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>
int main()
{
using namespace std;
cout <<"Enter a latitude in degrees,minutes,and seconds:";
cout<<"\nFirst,enter the degrees: ";
int degrees;
cin>>degrees;
cout<<"Next,enter the minutes of arc: ";
int minutes;
cin>>minutes;
cout<<"Finally,enter the seconds of arc: ";
int seconds;
cin>>seconds;
double degreess = degrees + double(minutes)/60+double(seconds)/3600;
cout<<degrees<<" degrees, "<<minutes<<" minutes, "<<seconds<<" seconds = "<<degreess<<"degrees"<<endl;
cin.get();
cin.get();
return 0;
}
4.编写一个程序,要求用户以整数的方式输入秒数(使用long或long long变量存储),然后以天,小时、分钟和秒的方式显示这段时间。使用符号常量来表示每条有多少小时,每小时有多少分钟以及每分钟有多少秒。该程序的输出应与下面类似:
Enter the number of second: 31600000
31600000 seconds = 365 days, 17 hours, 46 minutes, 40 seconds
//以整数方式输入秒,以天、小时、分钟、秒的方式显示这段时间。
#include<iostream>
int main()
{
using namespace std;
int hours_per_day=24;
int mins_per_hour=60;
int secs_per_min=60;
cout<<"Enter the number of seconds:";
long seconds;
cin>>seconds;
int days = seconds/(hours_per_day*mins_per_hour*secs_per_min);
int hours = seconds%(hours_per_day*mins_per_hour*secs_per_min)/(mins_per_hour*secs_per_min);
int minutes = seconds%(hours_per_day*mins_per_hour*secs_per_min)%(mins_per_hour*secs_per_min)/secs_per_min;
int secs = seconds%(hours_per_day*mins_per_hour*secs_per_min)%(mins_per_hour*secs_per_min)%secs_per_min;
cout<<seconds<<" seconds = "<<days<<" days, "<<hours<<" hours, "<<minutes<<" minutes, "<<secs<<" seconds"<<endl;
cin.get();
cin.get();
return 0;
}
5.编写一个程序,要求用户输入全球当前人口和美国当前的人口(或其他国家的人口),将这些信息存储在long long变量中,并让程序员显示美国(或其他国家)的人口占全球人口的百分比。该程序输出应与下面类似:
Enter the world’s population: 6898758899
Enter the population of the US: 310789781
The population of the US is 4.50492% of the world population.
//用户输入全球当前人口和美国当前人口。将这些信息存储再LL变量中,并显示美国的人口占全球人口的百分比。
#include<iostream>
int main()
{
using namespace std;
cout<<"Enter the world's population:";
long long world_population;
cin>>world_population;
cout<<"Enter the population of the US:";
long long US_population;
cin>>US_population;
double percentage= double(US_population)/world_population*100;
cout<<"The population of the US is "<<percentage<<"%"<<" of the world population.";
cin.get();
cin.get();
return 0;
}
6.编写一个程序,要求用户输入驱车里程(英里)和汽油使用量(加仑),然后指出汽车耗油量为一加仑的里程。如果愿意,也可以让程序要求用户以公里为单位输入距离,然后指出欧洲风格的结果——即每100公里的耗油量(升)。
//输入驱车里程和使用汽油量,算出汽车耗油量。
#include<iostream>
int main()
{
using namespace std;
cout<<"Enter mileages in mile are:";
double mile;
cin>>mile;
cout<<"Enter gasoline in gallon are:";
double gallon;
cin>>gallon;
double fuel_consumption = mile/gallon;
cout<<"Your car's fuel consumption is "<<fuel_consumption<<" mile/gallon.";
cin.get();
cin.get();
return 0;
}
7.编写一个程序,要求用户按欧洲风格输入汽车的耗油量(每100公里消耗的汽油量(升)), 然后将其转换为美国风格的耗油量——每加仑多少英里。注意,除了使用不同的单位外,美国方法(距离/燃料)与欧洲方法(燃料/距离)相反。 100 公里等于62.14 英里 ,1加仑等于3.875 升。因此,19mpg合12.4 L/100 km,大约合8.71 L/100 km。
//用户输入欧洲风格的耗油量,输出美国风格的耗油量。
#include<iostream>
int main()
{
using namespace std;
cout<<"输入汽车的耗油量(单位:升/百公里):";
double EUR_fuel_consumption;
cin>>EUR_fuel_consumption;
double mile_per_hkilometer=62.14;
double L_per_gallon = 3.875;
double US_fuel_consumption = 1/EUR_fuel_consumption*mile_per_hkilometer*L_per_gallon;
cout<<EUR_fuel_consumption<<" 升/百公里 = "<<US_fuel_consumption<<" 英里/加仑."<<endl;
cin.get();
cin.get();
return 0;
}