《C++ Primer Plus》(第6版)第3章编程练习

  • 《C++ Primer Plus》(第6版)第3章编程练习
    • 1. 打印身高
    • 2. 打印BMI
    • 3. 打印纬度
    • 4. 打印时间
    • 5. 打印人口占比
    • 6. 打印耗油量
    • 7. 耗油量转换

《C++ Primer Plus》(第6版)第3章编程练习

1. 打印身高

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

代码:

#include <iostream>
using namespace std;
int main()
{const int Ft_to_In = 12;int height;cout << "Please enter your height(inch):______\b\b\b\b\b\b";cin >> height;cout << height << " inches is " << height / Ft_to_In << " foots and " << height % Ft_to_In << " inches.\n";system("pause");return 0;
}

2. 打印BMI

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

代码:

#include <iostream>
using namespace std;const int Ft_to_In = 12;
const double In_to_Meter = 0.0254;
const double Kg_to_Pound = 2.2;double calculate_BMI(double height, double weight);int main()
{int inch;int foot;double pound;cout << "Please enter your height(x foots y inches): ";cin >> foot >> inch;cout << "Please enter your weight(pounds): ";cin >> pound;cout << "your BMI is " << calculate_BMI(In_to_Meter * (foot * Ft_to_In + inch), pound / Kg_to_Pound) << endl;system("pause");return 0;
}
double calculate_BMI(double height, double weight)
{double bmi = weight / (height * height);return 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()
{const int D_to_M = 60;const int M_to_S = 60;int degree;int minute;int second;double result;string prompt = "Enter a latitude in degrees, minutes, and seconds:\n";cout << prompt;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;result = degree + double(minute) / M_to_S + double(second) / M_to_S / D_to_M;cout << degree << " degrees, " << minute << " minutes, " << second << " seconds = " << result << " degrees\n";system("pause");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;
int main()
{const int D_to_H = 24;const int H_to_M = 60;const int M_to_S = 60;long long second;int day;int hour;int minute;cout << "Enter the number of seconds: ";cin >> second;cout << second << " seconds = ";minute = second / M_to_S;second %= M_to_S;hour = minute / H_to_M;minute %= H_to_M;day = hour / D_to_H;hour %= D_to_H;cout << day << " days, " << hour << " hours, " << minute << " minutes, " << second << " seconds\n";system("pause");return 0;
}

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 world;long long country;double percentage;cout << "Enter the world's population: ";cin >> world;cout << "Enter the population of the US: ";cin >> country;percentage = double(country) / double(world);cout << "The population of the US is " << 100 * percentage << "%"<< " of the world population.\n";system("pause");return 0;
}

6. 打印耗油量

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

代码:

#include <iostream>
using namespace std;
int main()
{double mileage;double fuelConsumption;cout << "Enter the mileage(miles): ";cin >> mileage;cout << "Enter the fuel consumption(gallons): ";cin >> fuelConsumption;cout << mileage / fuelConsumption << " miles/gallon.\n";cout << "Enter the mileage(kilometers): ";cin >> mileage;cout << "Enter the fuel consumption(litres): ";cin >> fuelConsumption;cout << 100 * fuelConsumption / mileage << " L/100Km.\n";system("pause");return 0;
}

7. 耗油量转换

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

代码:

#include <iostream>
using namespace std;
int main()
{const double OneHundredKilometers_to_Miles = 62.14;const double Gallon_to_Litre = 3.875;double fuelConsumption;double mile;double gallon;double result;cout << "Enter the fuel consumption(L/100Km): ";cin >> fuelConsumption;mile = OneHundredKilometers_to_Miles;gallon = fuelConsumption / Gallon_to_Litre;result = mile / gallon;cout << fuelConsumption << " L/100Km = " << result << " mpg.\n";system("pause");return 0;
}

《C++ Primer Plus》(第6版)第3章编程练习相关推荐

  1. C++ Primer Plus第六版第六章编程练习 第4题, 加入Benevolent Order of Programmer后,在BOP大会上

    /*************************************************************************************************** ...

  2. c++ primer plus第六版第六章编程练习

    编写程序读取键盘输入,回显除数字外字符,同时大写转小写,小写转大写,遇'@'就停止. //练习6.1 读取键盘输入,回显输出(除数字),另外大写字母和小写字母互转,遇"@"则退出程 ...

  3. C Primer Plus第六版第七章编程题目与参考答案⭐

    1.编写一个程序读取输入,读到#字符停止,然后报告读取的空格数.换行符数和所有其他字符的数量. #include <stdio.h> #define STOP '#' #define SP ...

  4. C++primer plus第六版第四章编程题代码

    1. #include "stdafx.h" #include <iostream> #include <string> #include <vect ...

  5. C Primer Plus第六版第四章编程题目与参考答案⭐

    1.编写一个程序,提示用户输入名和姓,然后以"名,姓"的格式打印出来. #include <stdio.h>int main() {char firstname[20] ...

  6. C++ Primer Plus 第六版 所有章节课后编程练习答案

    我的独立博客地址:www.blog4jimmy.com,欢迎大家关注 下面的是C++ Primer Plus 第六版所有章节的课后编程练习的答案,都是博主自己写的,有不对的地方请大家留言指出讨论讨论 ...

  7. C Primer Plus第六版第五章运算符,表达式,语句源码

    源码 //5.1#include<stdio.h> #define ADJUST 7.31 int main(void) {const double SCALE = 0.333;doubl ...

  8. C Primer Plus(第六版)第三章 数据和C

    笔记记录 1.float 类型可以储存带小数的数字. 2.printf()中使用%f来处理浮点值.%.2f中的.2用于精确控制输出,指定输出的浮点数只显示小数点后面两位. 3.scanf()函数用于读 ...

  9. c++primer plus 第六版 第六章重点内容总结 以及编程题答案

    1.if中的trick 当char型字符使用cout输出时,并且在使用运算符之后再cout的时候,一定要强制转化回char类型.否则打印出的是数字(字符的编码),if else中的两种操作如果需要多条 ...

  10. C Primer Plus (第六版) 第七章 7.11编程练习 参考答案

    11.ABC 邮购杂货店出售的洋蓟售价为 2.05 美元/磅,甜菜售价为 1.15美元/磅,胡萝卜售价为 1.09美元/磅.在添加运费之前,100美元的订单有5%的打折优惠.少于或等于5磅的订单收取6 ...

最新文章

  1. 4 用python进行OpenCV实战之图像变换1(平移)
  2. python把坐标写入文本_Python实现将数据写入netCDF4中的方法示例
  3. 知道Python中的字符串是什么吗?
  4. 机器学习算法Python实现:tfidf 特征词提取及文本相似度分类
  5. Android Activity标签属性
  6. vc c语言坐标图,VC++6.0下C语言画图编程问题
  7. 【我想进大厂】Redis夺命连环11问
  8. 水利水电工程与计算机技术应用,水利水电施工中计算机的应用
  9. Jmeter-【JSON Extractor】-响应结果中数组多个相同key取值
  10. 海康摄像头直连电脑修改IP解决播放失败无画面无法连接问题
  11. Android 动画
  12. 小区广播背景音乐系统IP网络广播解决方案
  13. 计算机网络arp表作用,arp映射表是什么?有什么作用
  14. Android常用对话框
  15. excel锁定前几行,无法选择和编辑
  16. 语法分析——自下而上分析
  17. 亚马逊发布“不可变”量子账本数据库产品
  18. ye321片库_jQuery的简约幻灯片库
  19. 谈笑间学会数仓-分层架构
  20. 尚硅谷大数据技术之Kettle

热门文章

  1. PHP特性整合 php7特性
  2. KEIL安装新版本后编译出现无法打开 core_cm3.h 问题
  3. 怎么选择合适的机柜?网络机柜服务器机柜
  4. Go Http 解析 text/plain
  5. git 重置用户名和密码
  6. PHP socket 连接 Socket server
  7. Linux防火墙的关闭
  8. 如何采集人类高质量脑电波?| 脑电设备推荐
  9. 国产服务器Kylin(aarch64)安装mysql8.0.27
  10. 20180415字节跳动今日头条笔试题——后台研发方向