C++PrimerPlus(第6版)中文版 第3章 处理数据 编程练习 参考答案

自己编写的参考答案,在VS2019中都可以编译通过,不是标准答案,也不是最优答案,仅供参考

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

#include < iostream >
using namespace std;
int main()
{
const int FtToIn = 12;
cout << “请输入你的身高(英寸):___\b\b\b”;
int in01;
cin >> in01;
int ft = in01 / FtToIn;
int in02= in01 % FtToIn;
cout << “你的身高为” << ft << “英尺” << in02 << “英寸”;
}

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

#include < iostream >
using namespace std;
int main()
{
cout << “请以几英尺几英寸的方式输入你的身高:”;
int foot;
int inch;
cin >> foot >> inch;
cout << “请以磅为单位输入你的体重:”;
double pound;
cin >> pound;
const int FootToInch = 12;
const double InchToMeter = 0.0254;
double meter = (foot * FootToInch + inch) * InchToMeter;
const double PoundToKilometer = 1 / 2.2;
double kilometer = pound * PoundToKilometer;
double BMI = kilometer / meter / meter;
cout << “你的BMI为:” << BMI;
}

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()
{
double degree;
double minute;
double second;
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;
const double SecondToDegree = 1.0 / 3600;
const double MinuteToDegree = 1.0 / 60;
double latitude = second * SecondToDegree + minute * MinuteToDegree+degree;
cout << degree << " degrees, " << minute << " minutes, " << second << “onds = " << latitude << " degrees”;
}

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;
int main()
{
long seconds;
cout << "Enter the number of seconds: “;
cin >> seconds;
const double SecondToMinute = 1.0 / 60;
const double MinuteToHour = 1.0 / 60;
const double HourToDay = 1.0 / 24;
int day = int(int(int(seconds * SecondToMinute) * MinuteToHour) * HourToDay);
long surplus = seconds - day * 24 * 60 * 60;
int hour = int(int(surplus * SecondToMinute) * MinuteToHour);
surplus = surplus - hour * 60 * 60;
int minute = int(surplus * SecondToMinute);
int second = surplus - minute * 60;
cout << seconds << " seconds = " << day << " days, " << hour << " hours, " << minute << " minutes, " << second << " seconds”;
}

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

Enter the world’s population: 6898758899

Enter the population of the US: 310783781

The population of the US is 4.50492% of the world population.

#include < iostream >
using namespace std;
int main()
{
long long wordpopulation;
long long USpopulation;
cout << "Enter the world’s population: ";
cin >> wordpopulation;
cout << "Enter the population of the US: ";
cin >> USpopulation;
double percenate = (double(USpopulation) / double(wordpopulation))*100;
cout<<“The population of the US is “<<percenate<<”% of the world population.”; //C++中cout默认精度为6位
}

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

#include < iostream >
using namespace std;
int main()
{
while (1)
{
cout << “请选择计量单位:1.英里/每加仑 2.升/每百公里:”;
int choice;
cin >> choice;
if (choice == 1) //mile、gallon、MilePerGallon是本个if中的形参
{
cout << “请输入驱车公里(英里):”;
double mile;
cin >> mile;
cout << “请输入使用汽油量(加仑):”;
double gallon;
cin >> gallon;
double MilePerGallon = mile / gallon;
cout << “每加仑可以行使的英里数为:” << MilePerGallon;
break;
}
else if (choice == 2)
{
cout << “请输入驱车公里(公里):”;
double km;
cin >> km;
cout << “请输入使用汽油量(升):”;
double litre;
cin >> litre;
double LitrePre100Km = litre / km * 100;
cout << “每100公里的耗油量为:” << LitrePre100Km;
break;
}
else
{
cout << “输入的选项有误,请重新输入。”<<endl;
continue;
}
}
}

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

#include < iostream >
using namespace std;
int main()
{
cout << “请输入汽车的耗油量(每100公里消耗的汽油量(升)):”;
double LitrePre100Km;
cin >> LitrePre100Km;
int MilePerGallon = int(1 / LitrePre100Km * 62.14 * 3.875);
cout << “该汽车的美国风格的油耗量——每加仑多少英里为:”<<MilePerGallon;
}

发布了4 篇原创文章 · 获赞 0 · 访问量 58

猜你喜欢

转载自blog.csdn.net/HuLaTangDuoLaZi/article/details/103744257