【C++ Primer Plus】编程练习答案——第3章
1 void ch3_1() {
2 using namespace std;
3 unsigned int factor = 12;
4 unsigned int inch, feet;
5 cout << "enter your height in inch:______\b\b\b\b\b\b";
6 cin >> inch;
7 cout << inch / 12 << " feet and " << inch % 12 << " inch" << endl;
8 }
9
10 void ch3_2() {
11 using namespace std;
12 unsigned int feet{0}, inch{0}, pound{0};
13 unsigned int feet2inch{12};
14 double kg2pound{2.2}, inch2meter{0.0254}, meter{0}, kilogram{0};
15 cout << "enter your height in feet&inch and weight in pound" << endl;
16 cout << "feet: "; cin >> feet;
17 cout << "inch: "; cin >> inch;
18 cout << "pound: "; cin >> pound;
19 meter = (feet * feet2inch + inch) * inch2meter;
20 kilogram = pound / kg2pound;
21 cout << "meter: " << meter << endl;
22 cout << "kg: " << kilogram << endl;
23 cout << "BMI: " << kilogram / (meter * meter) << endl;
24 }
25
26 void ch3_3() {
27 using namespace std;
28 const unsigned int factor = 60;
29 unsigned int degrees{0}, minutes{0}, seconds{0};
30 cout << "enter degrees: "; cin >> degrees;
31 cout << "enter minutes: "; cin >> minutes;
32 cout << "enter seconds: "; cin >> seconds;
33 cout << degrees << " degrees, " << minutes << " minutes, " << seconds << " seconds == "
34 << degrees + double(minutes) / factor + double(seconds) / factor / factor << " degrees" << endl;
35 }
36
37 void ch3_4() {
38 using namespace std;
39 unsigned long long secs{0}, secsremain{0};
40 unsigned int days{0}, hours{0}, minutes{0}, seconds{0};
41 cout << "enter seconds: "; cin >> secs;
42 secsremain = secs;
43 days = secsremain / (60 * 60 * 24);secsremain %= (60 * 60 * 24);
44 hours = secsremain / (60 * 60);secsremain %= (60 * 60);
45 minutes = secsremain / 60;secsremain %= 60;
46 cout << secs << " seconds == " << days << " days, "
47 << hours << " hours, " << minutes << " minutes, " << secsremain << " seconds" << endl;
48 }
49
50 void ch3_5() {
51 using namespace std;
52 unsigned long long world_population{0}, usa_population{0};
53 cout << "enter world_population: "; cin >> world_population;
54 cout << "enter usa_population: "; cin >> usa_population;
55 cout << double(usa_population) / double(world_population) * 100 << '%' << endl;
56 }
57
58 void ch3_6() {
59 using namespace std;
60 unsigned int km{0}, L{0};
61 cout << "enter km: "; cin >> km;
62 cout << "enter L: "; cin >> L;
63 cout << double(L) / km * 100 << "L/100km" << endl;
64 }
65
66 void ch3_7() {
67 using namespace std;
68 double Lp100km{0}, mpg{0};
69 cout << "eu L/100km: "; cin >> Lp100km;
70 cout << "to mpg: " << 1 / (Lp100km / 3.875 / 62.14) << endl;
71 }
【C++ Primer Plus】编程练习答案——第3章的更多相关文章
- 【C++ Primer Plus】编程练习答案——第12章
1 // chapter12_1_cow.h 2 3 4 #ifndef LEARN_CPP_CHAPTER12_1_COW_H 5 #define LEARN_CPP_CHAPTER12_1_COW ...
- 【C++ Primer Plus】编程练习答案——第11章 (待更新)
最近开学,事情较多,过两天更新...
- 【C++ Primer Plus】编程练习答案——第10章
1 // chapter10_1_account.h 2 3 #ifndef LEARN_CPP_CHAPTER10_1_ACCOUNT_H 4 #define LEARN_CPP_CHAPTER10 ...
- 【C++ Primer Plus】编程练习答案——第9章
1 // chapter09_golf.h 2 3 #ifndef LEARN_CPP_CHAPTER09_GOLF_H 4 #define LEARN_CPP_CHAPTER09_GOLF_H 5 ...
- 【C++ Primer Plus】编程练习答案——第8章
1 void ch8_1_print(const std::string & str, int n = 0 ) { 2 using namespace std; 3 static int fl ...
- 【C++ Primer Plus】编程练习答案——第7章
1 double ch7_1_harmonicaverage(double a, double b) { 2 return 2 / (1 / a + 1 / b); 3 } 4 5 void ch7_ ...
- 【C++ Primer Plus】编程练习答案——第6章
1 void ch6_1() { 2 using namespace std; 3 char ch; 4 while ((ch = cin.get()) != '@') { 5 if (isdigit ...
- 【C++ Primer Plus】编程练习答案——第5章
1 void ch5_1() { 2 using namespace std; 3 int small, big, sum{0}; 4 cout << "enter small ...
- 【C++ Primer Plus】编程练习答案——第4章
1 void ch4_1() { 2 using namespace std; 3 string fname, lname; 4 char grade; 5 unsigned int age; 6 c ...
随机推荐
- SpringMVC的拦截器和过滤器的区别
一 简介 (1)过滤器: 依赖于servlet容器.在实现上基于函数回调,可以对几乎所有请求进行过滤,但是缺点是一个过滤器实例只能在容器初始化时调用一次.使用过滤器的目的是用来做一些过滤操作,获取我们 ...
- WPF---数据绑定之RelativeSource(五)
一.概述 当Binding有明确的数据来源的时候,我们可以用Source或者ElementName赋值的办法让Binding与之关联. 但是,有时候当我们不能确定作为Source的对象叫什么名字的时候 ...
- C语言定义常量
/* #define 标识符 #define day main中day=10;仅一次赋值*/ 错误 #define 标识符 常量值 #define day 10;
- 1.3RDD的设计与运行原理
此文为个人学习笔记如需系统学习请访问http://dblab.xmu.edu.cn/blog/1709-2/ 提供一种通用的数据抽象 RDD典型的执行过程如下: RDD读入外部数据源(或者内存中的集合 ...
- Int 2e 与 Sysenter区别
参考:张银奎<软件调试>第八章 Int 2e: Windows将2e号向量专门用作系统调用,在启动早起初始化中断描述表时便注册好了适合的服务例程.因此当NtDll中的NtReadFile发 ...
- 寻找最短路径Dijkstra算法
1 /** 2 * 1.对于T中的每个顶点u,找到u的具有最小权重的连接边.所有到u的连接边都存储在queues.get(u)中.queues.get(u).peek()返回拥有最小权值 3 * 的连 ...
- 面试必备:排序算法汇总(c++实现)
排序算法主要考点: 7种排序 冒泡排序.选择排序.插入排序.shell排序.堆排序.快速排序.归并排序 以上排序算法是面试官经常会问到的算法,至于其他排序比如基数排序等等,这里不列举. 以下算法通过c ...
- noip模拟35
A. 玩游戏 考场做法用双指针向两侧更新,当左段点左移一位时,如果右端点不满足条件,则跳回肯定满足的位置.复杂度玄学 题解做法是类似最长子段和,如果有一个区间和为负,则维护的指针跳过去即可 B. 排列 ...
- Appium问题解决方案(9)- Original error: Failed to launch Appium Settings app: Condition unmet after 5090 ms
背景 执行代码报错 解决方法 该问题并不常见,主要是手机操作系统的问题 程序无法无法自动打开appiumsettings,那么我们可以手动打开appiumsettings服务(程序没有界面,会出现打开 ...
- go语言游戏服务端开发(一)——架构
五邑隐侠,本名关健昌,12年游戏生涯. 本教程以Go语言为例. 网络游戏程序分为客户端和服务端.客户端负责图形渲染.交互和一些简单校验处理,服务端负责业务逻辑处理.数据存储. 我们开发一个游戏de ...