您的位置:首页 > 编程语言 > C语言/C++

C++ Primer Plus 第三章练习题

2018-01-17 19:46 330 查看
3.7.1编写一个小程序。要求用户使用一个整数指出自己的身高(单位为英寸),然后将身高转换为英尺和英寸。该程序使用下划线字符来指示输入位置。另外,使用一个const符号常量来表示转换因子。

#include <iostream>
using namespace std;

int main()
{
cout<<"Please input a height(in inches): ___\b\b\b";
int height;
cin>>height;
const int CHG = 12;
cout<<"Your height is: "<<height / 12<<" feet "<<height % 12<<" inches."<<endl;
return 0;
}


3.7.2编写一个小程序,要求以几英尺几英寸的方式输入其身高,并以磅为单位输入其体重。(使用3个变量来存储这些信息。)该程序报告其BMI(Body Mass Index,体重指数)。为了计算BMI,该程序以英寸的方式指出用户的身高(1英尺为12英寸),并将以英寸为单位的身高转换为以米为单位的身高(1英寸=0.0254米)。然后,将以磅为单位的体重转换为以千克为单位的体重(1千克=2.2磅)。最后,计算相应的BMI——体重(千克)除以身高(米)的平方。用符号常量表示各种转换因子。

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
cout<<"Please input the height(in feet and inches): ";
double foot_number, inch_number;
cin>>foot_number>>inch_number;
cout<<"Please input the weight(in pounds): ";
double weight;
cin>>weight;
const int FT_TO_IH = 12;
const double IH_TO_MR = 0.0254;
const double KG_TO_PD = 2.2;
double height = (foot_number * FT_TO_IH + inch_number) * IH_TO_MR;
weight = weight / KG_TO_PD;
cout<<"Your height is "<<height<<" meters,your weight is "<<weight<<" kg."<<endl;
double bmi = weight / pow(height, 2.0);
cout<<"Your BMI(Body Mass Index) is "<<bmi<<endl;
return 0;
}


3.7.3编写一个程序,要求用户以度、分、秒的方式输入一个纬度,然后以度为单位显示该纬度。1度为60分,1分等于60秒,请以符号常量的方式表示这些值。对于每个输入值,应使用一个独立的变量存储它。

#include <iostream>
using namespace std;

int main()
{
cout<<"Enter a latitude in degrees, minutes, and seconds: "<<endl;
cout<<"First, 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;
const double FACTOR = 60.0;
cout<<degrees<<" degrees, "<<minutes<<" minutes, "<<seconds<<" seconds "
<<"= "<<degrees + minutes / FACTOR + seconds / FACTOR /FACTOR<<" degrees."
<<endl;
return 0;
}


3.7.4编写一个程序,要求用户以整数方式输入秒数(使用long或long long变量存储),然后以天、小时、分钟和秒的方式显示这段时间。使用符号常量来表示每天有多少小时、每小时有多少分钟一集每分钟有多少秒。

#include <iostream>
using namespace std;

int main()
{
cout<<"Enter the number of seconds: ";
long long int time;
cin>>time;
const int FACTOR1 = 60;
const int FACTOR2 = 24;
cout<<time<<" seconds = "<<time / FACTOR1 / FACTOR1 / FACTOR2<<" days, "
<<time / FACTOR1 / FACTOR1 % FACTOR2<<" hours, "<<time / FACTOR1 % FACTOR1
<<" minutes, "<<time % FACTOR1<<" seconds."<<endl;
return 0;
}


3.7.5编写一个程序,要求用户输入全球当前的人口和美国当前的人口(或其他国家的人口)。将这些信息存储在long long变量中,并让程序显示美国(或其他国家)的人口占全球人口的百分比。

#include <iostream>
using nam
4000
espace std;

int main()
{
cout<<"Enter the world's population: ";
long long int world_popu;
cin>>world_popu;
cout<<"Enter the population of the US: ";
long long int us_popu;
cin>>us_popu;
double rate = (double)us_popu / (double)world_popu;
cout<<"The population of the US is "<<rate * 100<<"% of the world population."<<endl;
return 0;
}


3.7.6编写一个程序,要求用户输入驱车里程(英里)和使用汽油量(加仑),然后指出汽车耗油量为一加仑的里程。如果愿意,也可以让程序要求用户以公里为单位输入距离,并以升为单位输入汽油量,然后指出欧洲风格的结果——即每100公里的耗油量(升).

#include <iostream>
using namespace std;

int main()
{
cout<<"Please input the distance(km): ";
double km;
cin>>km;
cout<<"Please input the oil consumption(liter): ";
double liters;
cin>>liters;
const double FACTOR = 100.0;
cout<<"The oil consumption is "<<liters / km * FACTOR<<" each 100 km."<<endl;
return 0;
}


3.7.7编写一个程序,要求用户按欧洲风格输入汽车的耗油量(每100公里消耗的汽油量(升)),然后将其转换为美国风格的耗油量——每加仑多少英里。注意,除了使用不同的单位计量外,美国方法(距离/燃料)与欧洲方法(燃料/距离)相反。100公里等于62.14英里,1加仑等于3.875升。因此,19mpg大约合12.41/100km,127mpg大约合8.71/100km。

#include <iostream>
using namespace std;

int main()
{
cout<<"Please input oil consumption(Europe style): ";
double v = 0;
cin>>v;
const double Y_TO_G = 62.14;
const double C_TO_V = 3.875;
cout<<"The US style oil consumption is "<<Y_TO_G * C_TO_V / v<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: