• C++ Primer Plus第三章编程练习答案


    答案仅供参考,实际运行效果取决于运行平台和运行软件

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

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. const int INCH_PER_FEET = 12;
    6. int height;
    7. cout << "Please enter your height in inch: ___\b\b\b";
    8. cin >> height;
    9. cout << "Your height is " << height / INCH_PER_FEET << " feet and ";
    10. cout << height % INCH_PER_FEET << " inches." << endl;
    11. return 0;
    12. }

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

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. const int INCH_PER_FEET = 12; //1英尺为12英寸;
    6. const double POUND_PER_KG = 2.2; //1千克为2.2磅;
    7. const double METER_PER_INCH = 0.0254; //1英寸为0.0254米;
    8. double height_feet, height_inch, weight, meter;
    9. cout << "Please enter your height in feet and inch: ";
    10. cin >> height_feet >> height_inch;
    11. cout << "Please enter your weight in pound: ";
    12. cin >> weight;
    13. cout << "Your height is " << height_feet * INCH_PER_FEET + height_inch << " inches." << endl;
    14. meter = (height_feet * INCH_PER_FEET + height_inch) * METER_PER_INCH;
    15. cout << "Your BMI is " << (weight / POUND_PER_KG) / (meter * meter) << "." << endl;
    16. return 0;
    17. }

    3.编写一个程序,要求用户以度、分、秒的方式输入一个纬度,然后以度为单位显示该纬度。T度为60 分,1分等于60秒请以符号常量的方式表示这些值对个输入值,应使用一个独立的量存储它下面是该程序运行时的情况:
    Enter a latitude in degrees,minutes, and seconds:

    First,enterthe 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

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. const double MINUTE_PER_DEGREE = 60.0;
    6. const double SECOND_PER_MINUTE = 60.0;
    7. double degree, minute, second, total;
    8. cout << "Enter a latitude in degrees, minutes, and seconds:" << endl;
    9. cout << "First, enter the degrees: ";
    10. cin >> degree;
    11. cout << "Next, enter the minutes of arc: ";
    12. cin >> minute;
    13. cout << "Finally, enter the seconds of arc: ";
    14. cin >> second;
    15. total = degree + (minute / MINUTE_PER_DEGREE) + (second / (MINUTE_PER_DEGREE * SECOND_PER_MINUTE));
    16. cout << degree << " degrees, ";
    17. cout << minute << " minutes, ";
    18. cout << second << " seconds = ";
    19. cout << total << " degrees" << endl;
    20. return 0;
    21. }

    4.编写一个程序,要求用户以整数方式输入秒数(使用lng 或long long 变量存储)然后以天、小时、分钟和秒的方式显示这段时间。使用符号常最来表示每天存多少小时、每小时有多少分钟以及每分钟有多少秒。该程序的输出应与下面类似:
    Enter the number of seconds: 31600000

    31600000seconds = 365 days,17 hours,46 minutes,40-seconds

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. const int HOUR_PER_DAY = 24; //1天有24小时;
    6. const int MINUTE_PER_HOUR = 60; //1小时有60分钟;
    7. const int SECOND_PER_MINUTE = 60; //1分钟有60秒;
    8. long long second;
    9. cout << "Enter the number of seconds: ";
    10. cin >> second;
    11. cout << second << " seconds = ";
    12. cout << second / (HOUR_PER_DAY * MINUTE_PER_HOUR * SECOND_PER_MINUTE);
    13. cout << " days, ";
    14. cout << second / (SECOND_PER_MINUTE * MINUTE_PER_HOUR) % HOUR_PER_DAY;
    15. cout << " hours, ";
    16. cout << second % (SECOND_PER_MINUTE * MINUTE_PER_HOUR) / SECOND_PER_MINUTE;
    17. cout << " minutes, ";
    18. cout << second % SECOND_PER_MINUTE;
    19. cout << " seconds" << endl;
    20. return 0;
    21. }

    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 populatior,

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. long long world_population;
    6. long long china_population;
    7. cout << "Enter the world's population: ";
    8. cin >> world_population;
    9. cout << "Enter the population of the US: ";
    10. cin >> china_population;
    11. cout << "The population of the US is ";
    12. cout << double(china_population) / world_population * 100LL;
    13. cout << "% of the world population." << endl;
    14. return 0;
    15. }

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

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. double kilometer, liter;
    6. cout << "Please enter your driving distance(km): ";
    7. cin >> kilometer;
    8. cout << "Please enter your gas consumption(liter): ";
    9. cin >> liter;
    10. cout << "You consume " << liter / kilometer * 100;
    11. cout << " liter gas for driving per 100 km." << endl;
    12. return 0;
    13. }

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

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. const double MILE_PER_KM = 0.6214; //1公里是0.6214英里;
    6. const double LITER_PER_GALLON = 3.875; //1加仑是3.875升;
    7. double kilometer, liter;
    8. cout << "Please enter your driving distance(km): ";
    9. cin >> kilometer;
    10. cout << "Please enter your gas consumption(liter): ";
    11. cin >> liter;
    12. cout << "You consume " << liter / kilometer * 100;
    13. cout << " liter gas for driving per 100 km (European style)." << endl;
    14. cout << "You drive " << kilometer * MILE_PER_KM / liter * LITER_PER_GALLON;
    15. cout << " miles for per gallon (American style)." << endl;
    16. return 0;
    17. }

  • 相关阅读:
    Social login -- LittleLink
    Java 环境配置
    关于浮动元素,你还在自己计算位置吗?来看看 Floating UI 吧
    嵌入式 Linux 入门(四、Linux 下的编辑器 — 让人爱恨交加的 vi )
    Vue中Slot的使用指南
    Hbase底层原理简介(一)
    【带头学C++】----- 1.基础知识 ---- 1.24 逻辑控制语句
    C语言常用的字符串函数(含模拟实现)
    【校招VIP】产品深度理解之热点事件分析
    信号发送与处理
  • 原文地址:https://blog.csdn.net/sakura0908/article/details/132696815