1 #pragma once
2 #ifndef CHAPTER3_H_
3 #define CHAPTER3_H_
4 #include <iostream>
5 #include <string>
6
7 using namespace std;
8
9 class Exercise1
10 {
11 private:
12 const int factor = 12;
13 public:
14 float showInch(float foot);
15 };
16
17
18 class Exercise2
19 {
20 private:
21 const float factorInch = 12;
22 const float factorMeter = 0.0254f;
23 const float factorKg = 2.2;
24 public:
25 void count(float *p,float foot, float inch, float weight);
26 };
27
28 class Exercise3
29 {
30 private:
31 float d1;
32 float d2;
33 const float factorMin = 60;
34 const float factorSec = 60;
35 public:
36 float convertToDegrees(float degree, float minutes, float seconds);
37 };
38
39 class Exercise4
40 {
41 private:
42 const int factorHours = 24;
43 const int factorMinutes = 60;
44 const int factorSeconds = 60;
45 public:
46 void countSeconds(int *p ,int seconds);
47 };
48
49 class Exercise5
50 {
51 private:
52 float result;
53 public:
54 float populationRatio(long long worldPopulation,long long nationalPopulation);
55 };
56
57 class Exercise6
58 {
59 private:
60 float temp;
61 float fuelCalculation;
62 public:
63 float oilMeter(float kilometer,float fuel );
64 };
65
66 class Exercise7
67 {
68 private:
69 float usOil;
70 const float factorMile = 62.14f;
71 const float factorLitre = 3.875f;
72 public:
73 float convertToMPG(float fuelConsumption);
74 };
75 #endif // !CHAPTER3_H_
1 #include "stdafx.h"
2 #include "chapter3.h"
3
4
5 float Exercise1::showInch(float foot)
6 {
7 return foot * factor;
8 }
9
10 void Exercise2::count(float *p,float foot, float inch, float weight)
11 {
12 inch += foot * factorInch;
13 weight /= factorKg;
14 p[0] = inch * factorMeter;
15 p[1] = weight;
16
17 }
18
19 float Exercise3::convertToDegrees(float degree, float minutes, float seconds)
20 {
21 d1 = minutes / factorMin;
22 d2 = seconds / factorSec / factorMin;
23 degree += (d1 + d2);
24 return degree;
25 }
26
27 void Exercise4::countSeconds(int *p,int seconds)
28 {
29 p[3] = seconds % factorSeconds;
30 p[2] = seconds / factorSeconds % factorMinutes;
31 p[1] = seconds / factorSeconds / factorMinutes % factorHours;
32 p[0] = seconds / factorSeconds / factorMinutes / factorHours;
33 }
34
35 float Exercise5::populationRatio(long long worldPopulation, long long nationalPopulation)
36 {
37 result = float(nationalPopulation) / float(worldPopulation);
38 return result;
39 }
40
41 float Exercise6::oilMeter(float kilometer, float fuel)
42 {
43 temp = fuel / kilometer;
44 fuelCalculation = temp * 100;
45 return fuelCalculation;
46 }
47
48 float Exercise7::convertToMPG(float fuelConsumption)
49 {
50 usOil = fuelConsumption / factorLitre/factorMile;
51 return usOil;
52 }
1 // C++Primer Plus 习题_第三章.cpp: 定义控制台应用程序的入口点。
2 //-------------------------------复习题-----------------------------------------
3 //1.为什么C++有多种整形?
4 //答案:有多种整形烈性,可以根据特定需求选择最适合的类型。例如,可以用short来存储空格,使用long来确保存储容量,也可以提高特定计算机的速度的类型。
5
6 //2.声明与下述描述相符的变量。
7 //a.short 整数,值为80
8 //b.unsigned int 整数,值为 42110
9 //c.值为3000000000 的整数
10 //答案:short s = 80;
11 // unsigned int q = 42110;
12 // unsigned long ants = 3000000000;
13
14 //3.C++提供了什么措施来防止超出整型的范围?
15 //答案:C++没有提供自动防止超出整型限制的功能,可以使用头文件climits来确定限制情况。
16
17 //4.33L与33之间有什么区别?
18 //答案:常量33L的类型为long,常量33的类型为int。
19
20 //5.下面两条C++语句是否等价?
21 //char grade = 65;
22 //char grade = 'A';
23 //答案:这两条语句并不是真正的等价,虽然对于某些系统来说,它们是等效的。最重要的是,只有在使用ASCII码的系统上,第一条语句才将得分设置为字母A,而第二条语句还可以使用其他编码的系统。其次65是一个int常量,而'A'是一个char常量。
24
25 //6.如何使用C++来找出编码88表示的字符?指出至少两种方法。
26 //答案:cout << char(88) << endl;
27 // cout << (char)88 << endl;
28 // cout.put(char(88));
29 // cout << c << endl;
30
31 //7.将long值赋值给float变量会导致舍入误差,将long值赋给double呢?将long long值赋给double变量呢?
32 //答案:这个问题的答案取决于这两个类型的长度。如果long为4个字节,则没有损失。因为最大的long值将近20亿,即有10位数。由于double提供了至少13位有效数字,因而不需要进行任何舍入。long long类型可提供19位有效数字,超过double保证的13位有效数字。
33
34 //8.下列C++表达式的结果分别是多少?
35 //a.8*9+2
36 //b.6*3/4
37 //c.3/4*6
38 //d.6.0*3/4
39 //e.15%4
40 //答案:74 4 0 4.5 3
41
42 //9.假设x1和x2是两个double变量,您要将它们作为整数相加,再将结果赋给一个整形变量。请编写一条完成这项任务的C++语言。如果要将它们作为double值相加并转换为int呢?
43 //答案:int pos = int(x1) + int (x2); 或者 int pos = (int)x1 + (int)x2;
44 // double类型相加,再进行转换
45 // int pos = int (x1 + x2); 或者 int pos = (int)(x1 + x2);
46
47 //10.下面每条语句声明的变量都是什么类型?
48 //a.auto cars = 15
49 //b.auto iou = 150.37f
50 //c.auto level ='B'
51 //d.auto crat = u'/u0002155'
52 //e.auto fract = 8.25f/2.5
53 //答案:a.int b.float c.char d.char32_t e.double
54
55 //-------------------------------编程练习-----------------------------------------
56 //1.编写一个小程序,要求用户使用一个整数指出自己的身高(单位为英寸),然后将身高转换为英尺和英寸。该程序使用下划线字符来指示输入位置,
57 //另外,使用一个const符号常量来表示转换因子。1英尺等于12英寸
58
59 //2.编写一个小程序,要求以几英尺几英寸的方式输入其身高,并以磅为单位输入其体重。(使用3个变量来存储这些信息。)该程序报告其BMI(Body Mass Index,体重指数)。
60 //为了将以英寸为单位的身高转换为以米为单位的身高(1英寸=0.0254米)。然后,将以磅为单位的体重转换为以千克为单位的体重(1千克=2.2磅)。
61 //最后,计算相应的BMI--体重(千克)除以身高(米)的平方。用符号常量表示各种转换因子。
62
63 //3.编写一个程序,要求用户以度、分、秒的方式输入一个维度,然后以度为单位显示该维度。1度为60分,1分等于60秒,请以符号常量的方式表示这些值,应使用一个独立的变量存储它。
64 //下面是该程序运行时的情况:
65 //Enter a latitude in degree, minutes, and seconds:
66 //First, enter the degrees: 37
67 //Next, enter the minutes of arc :51
68 //Finally, enter the seconds of arc:19
69 //37 degrees, 51 minutes, 19 secconds = 37.8553 degrees
70
71 //4.编写一个程序,要求用户以整数方式输入秒数(使用long或long long变量存储),然后以天、小时、分钟和秒的方式显示这段时间,使用符号常量来表示每天有多少小时、每小时有多少分钟以及
72 //每分钟有多少秒,该程序的输出应与下面类似:
73 //Enter the number of seconds: 31600000
74 //31600000 seconds = 365 days, 17 hours, 46 minutes, 40 seconds.
75
76 //5.编写一个程序,要求用户输入全球当前的人口和美国当前的人口(或其他国家的人口)。将这些信息存储在long long变量中,,并让程序显示美国(或其他国家)的人口占全球人口的百分比。该程序的
77 //输出应与下面类似:
78 //Enter the world's population: 6898758899
79 //Enter the population of the US: 310783781
80 //The population of the US is 4.50492& of the world population
81
82 //6.编写一个程序,要求用户输入驱车里程(英里)和使用汽油量(加仑),然后指出汽油量为一加仑的里程。如果愿意,也可以让程序要求用户以公里为单位输入距离,并以升为单位输入汽油量,然后
83 //指出欧洲风格的结果---即每100公里的耗油量(升)。
84
85 //7.编写一个程序,要求用户按欧洲风格输入汽车的油耗量(每100公里消耗的汽油量(升))。然后将其转换为美国风格的油耗量---每加仑多少英里。注意,除了使用不同的单位计量外,美国方法(距离/燃料)
86 //与欧洲方法(燃料/距离)相反。100公里等于62.14英里,1加仑等于3.875升。因此,19mpg大约合12.41/100km,127mpg大约合8.71/100km。
87 //(MPG 是MILE PER GALLON = 每加伦汽油能距的英里数 )
88
89 #include "stdafx.h"
90 #include "chapter3.h"
91
92 int main()
93 {
94 //1.
95 cout << "第一题" << endl;
96 float height;
97 Exercise1 ex1;
98
99 cout << "请输入身高(英寸)_" ;
100 cin >> height;
101 cout << "英尺:" << ex1.showInch(height) << endl;
102 cout << "英寸:" << height<< endl;
103
104 //2.
105 //C/C++中不允许返回数组,只能返回数组的指针。一般应在主调函数中声明数组,把它的指针传给被调函数,在被调函数中通过这个指针直接操作
106 //主调函数中的数组。也可以在背调函数中动态声明数组,将它的指针返回给主调函数进行后续操作,但不能忘记在操作完毕后释放动态数组,
107 //不然会产生内存泄漏问题。
108 cout << "第二题" << endl;
109 float foot,inch,weight;
110 Exercise2 ex2;
111 float data[2] = {};
112 float *p2 = data;
113 cout << "第二题" << endl;
114 cout << "请输入身高(英尺):";
115 cin >> foot;
116 cout << "请输入身高(英寸):";
117 cin >> inch;
118 cout << "请输入体重(磅):";
119 cin >> weight;
120 ex2.count(p2,foot, inch, weight);
121 cout << "输出结果:" << endl;
122 cout << "身高为:" << p2[0] << endl;
123 cout << "体重为:" << p2[1] << endl;
124
125 //3.
126 float degree, minutes, seconds, result;
127 Exercise3 ex3;
128 cout << "第三题" << endl;
129 cout << "请输入角度,分钟,秒:" << endl;
130 cout << "首先输入角度:";
131 cin >> degree;
132 cout << "接着输入分钟:";
133 cin >> minutes;
134 cout << "最后输入秒:";
135 cin >> seconds;
136 result = ex3.convertToDegrees(degree, minutes, seconds);
137 cout << degree << "度" << minutes << "分" << seconds << "秒 = " << result << "度" << endl;
138
139 //4.
140 cout << "第四题" << endl;
141 int seconds2;
142 int *p;
143 int time[4] = {};
144 p = time;
145 Exercise4 ex4;
146 cout << "请输入秒";
147 cin >> seconds2;
148 ex4.countSeconds(p,seconds2);
149 cout << seconds2 << " = " << p[0] << "天," << p[1] << "小时," << p[2] << "分钟," << p[3] << "秒." << endl;
150
151 //5.
152 cout << "第五题" << endl;
153 long long worldPopulation, nationalPopulation;
154 float result3;
155 Exercise5 ex5;
156 cout << "请输入世界人口数:";
157 cin >> worldPopulation;
158 cout << "请输入国家人口是:";
159 cin >> nationalPopulation;
160 result3 = ex5.populationRatio(worldPopulation, nationalPopulation);
161 cout << "当前国家人口占世界人口的 " << result3 * 100 << "% .";
162
163 //6.
164 cout << "第六题" << endl;
165 Exercise6 ex6;
166 float kilometer;
167 float fuel;
168 cout << "请输入行驶了的距离(公里):";
169 cin >> kilometer;
170 cout << "请输入油耗量(升):";
171 cin >> fuel;
172 cout << "每100公里油耗量为 " << int(ex6.oilMeter(kilometer, fuel)) << "L" << endl;
173
174 //7.
175 cout << "第七题" << endl;
176 Exercise7 ex7;
177 float fuelConsumption;
178 cout << "请输入百公里油耗:";
179 cin>>fuelConsumption;
180 cout << "每加仑为:" << ex7.convertToMPG(fuelConsumption) << "英里。";
181
182
183 return 0;
184
185 }