【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 }

 

posted @ 2021-08-25 11:33  开心果壳好硬  阅读(112)  评论(0编辑  收藏  举报