C++Primer Plus 编程练习_第二章

 1 #pragma once
 2 #ifndef CHAPTER2_H_
 3 #define CHAPTER2_H_
 4 #include <iostream>
 5 using namespace std;
 6 
 7 class Exercise1
 8 {
 9 private:
10     string name_;
11     string address_;
12 public:
13     void showAddress(const string n,const string a);
14      
15 };
16 
17 class Exercise2
18 {
19 private:
20     float unit_;
21 public:
22     float unitConvert(const float u);
23 };
24 
25 class Exercise3
26 {
27 private:
28     
29 public:
30     void print1();
31     void print2();
32 };
33 
34 class Exercise4
35 {
36 private:
37     int age_;
38 public:
39     int calculateAge(int a);
40 };
41 
42 class Exercise5
43 {
44 private:
45     
46 public:
47     float celsiusToFahrenheit(float c);
48 };
49 
50 class Exercise6
51 {
52 private:
53     const int size_ = 63240;
54 public:
55     double countUnits(float lightYear);
56 };
57 
58 class Exercise7
59 {
60 private:
61 
62 public:
63     void timeStatistic(int hours,int minutes);
64 };
65 #endif // !CHAPTER2_H_
 1 #include "stdafx.h"
 2 #include "chapter2.h"
 3 #include <string>
 4  
 5 void Exercise1::showAddress(const string n, const string a)
 6 {
 7     name_ = n;
 8     address_ = a;
 9     cout << "姓名:" << name_ << endl;
10     cout << "地址:" << address_ << endl;
11 
12 }
13 
14 float Exercise2::unitConvert(const float u)
15 {
16     return u*220;
17 }
18 
19 void Exercise3::print1()
20 {
21     cout << "Three blind mice"<<endl;
22 }
23 
24 void Exercise3::print2()
25 {
26     cout << "See how they run"<<endl;
27 }
28 
29 int Exercise4::calculateAge(int a)
30 {
31     age_ = a;
32     int month = age_ *12;
33     return month;
34 }
35 
36 float Exercise5::celsiusToFahrenheit(float c)
37 {
38     return c * 1.8 + 32.0;
39 }
40 
41 double Exercise6::countUnits(float lightYear)
42 { 
43     return lightYear * size_;
44 }
45 
46 void Exercise7::timeStatistic(int hours, int minutes)
47 {
48     cout << "Time:" << hours << ":" << minutes << endl;
49      
50 }
  1 // C++Primer Plus 习题_第二章.cpp: 定义控制台应用程序的入口点。
  2 
  3 //-------------------------------复习题-----------------------------------------                    
  4 //1.C++程序的模块叫什么?
  5 //答案:函数
  6 
  7 //2.下面的预处理编译指令是做什么用的?
  8 // #include <iostream>
  9 //答案:在编译之前将头文件iostream中的所有内容copy到该编译指令。iostream包含类istream和ostream。
 10 //cin()智能对象在istream中,cout << 智能对象在ostream中(使用了函数重载)。因此如果需要使用cin或者
 11 //cout指令,需先将iostream头文件引入。
 12 
 13 //3.下面的语句是做什么用的?
 14 //using namespace std;
 15 //答案:std是命名空间,不同的命名空间中可以有相同的类名被定义,如果使用了#include <iostream>就必须写
 16 //std命名空间。如果用#include<iostream.h>就不需要这句话(旧标准)。
 17 
 18 //4.什么语句可以用来打印短语"Hello,world",然后开始新的一行?
 19 //答案:cout<<"Hello,world"<<endl; 或者cout<<"Hello,world\n";
 20 
 21 //5.什么语句可以用来创建名为cheeses的整数变量?
 22 //答案:int cheeses;
 23 
 24 //6.什么语句可以用来将32赋值给变量cheeses?
 25 //答案:cheeses=32;
 26 
 27 //7.什么语句可以用来将从键盘输入的值读入变量cheeses中?
 28 //答案:cin>>cheeses;
 29 
 30 //8.什么语句可以用来打印"We have X varieties of cheese,",其中X为变量cheeses的当前值。
 31 //答案:cout<<"We have "<<cheeses<<"varieties of cheese,"<<endl;
 32 
 33 //9.下面的函数原型指出了关于函数的哪些信息?
 34 //int froop(doubel t);
 35 //void rattle(int n);
 36 //int prune(void);
 37 //答案:froop函数包含双精度浮点类型的参数和整形的返回值。
 38 //        rattle函数包含整形的参数,没有返回值。
 39 //        prune函数没有参数,有整型的返回值。
 40 
 41 //10.定义函数时,在什么情况下不必使用关键字retuen?
 42 //答案:当函数的返回类型为void时,不用再函数中使用return,如果不提供返回值,则可以使用它。
 43 
 44 //-------------------------------编程练习-----------------------------------------
 45 //1.编写一个C++程序,它显示您的姓名和地址。
 46 
 47 //2.编写一个C++程序,它要求用户输入一个以long为单位的距离,然后将它转换为码(一 long 等于 220 码)。
 48 
 49 //3.编写一个C++程序,它使用3个用户定义的函数(包括main()),并生成下面的输出:
 50 //Three blind mice
 51 //Three blind mice
 52 //See how they run
 53 //See how they run
 54 //其中一个函数调用两次,该函数生成前两行:另一个函数也被调用两次,并生成其余的输出。
 55 
 56 //4.编写一个程序,让用户输入其年龄,然后显示该年龄包含多少个月,如下所示:
 57 //Enter your age:29
 58 
 59 //5.编写一个程序,其中的main()调用一个用户定义的函数(以摄氏度值为参数,并返回相应的华氏温度)。
 60 //该程序下面的格式要求用户输入摄氏温度值,并显示结果:
 61 //Please enter a Celsius value:20
 62 //20 degrees Celsius is 68 degrees Fahrenheit
 63 //下面是转换公式:
 64 //华氏温度=1.8X摄氏温度+32.0
 65 
 66 //6.编写一个程序,其 main()调用一个用户定义的函数(以光年值为参数,并返回对应天文单位的值)。
 67 //该程序下面的格式要求用户输入光年值,并显示结果:
 68 //Enter the number of light years: 4.2
 69 //4.2 light year = 265608 astromical units.
 70 //天文单位是从地球到太阳的平均距离(约150000000 公里或93000000 英里),光年是光一年走的距离
 71 //(约10万亿公里或6万亿公里)(除太阳外,最近的恒星大约离地球4.2光年)。请使用double类型。
 72 //转换公式为:
 73 //1光年=63240天文单位
 74 
 75 //7.编写一个程序,要求用户输入小时数和分钟数。在main()函数中,将这两个值传递给一个void函数,
 76 //后者以下面这样的格式显示这两个数:
 77 //Enter the number of hours: 9
 78 //Enter the number of minuts:28
 79 //Time: 9:28
 80 
 81 #include "stdafx.h"
 82 #include "chapter2.h"
 83 #include <string>
 84 
 85 int main()
 86 {
 87     //1.
 88     cout << "《第一题》" << endl;
 89     string name, address;
 90     cout << "请输入姓名:";
 91     cin >> name;
 92     cout << "请输入地址:";
 93     cin >> address;
 94     Exercise1 ex1;
 95     ex1.showAddress(name,address);
 96 
 97     //2.
 98     cout << "《第二题》" << endl;
 99     float unit;
100     cout << "请输入距离(单位long):";
101     cin >> unit;
102     Exercise2 ex2;
103     cout<<"单位转化为:"<<ex2.unitConvert(unit)<<""<<endl;
104 
105     //3.
106     cout << "《第三题》" << endl;
107     cout << "请输入任意键输出内容";
108     cin.get(); //之所以用两个是因为第一个get()读取了上面输入内容后回车确定的键击。
109     cin.get();//第二个get()终于让程序停止,等待键击。
110     Exercise3 ex3;
111     ex3.print1();
112     ex3.print1();
113     ex3.print2();
114     ex3.print2();
115 
116     //4.
117     cout << "《第四题》" << endl;
118     int age;
119     Exercise4 ex4;
120     cout << "请输入您的年龄:";
121     cin >> age;
122     cout << "您的年龄共包含" << ex4.calculateAge(age) << "个月" << endl;
123    
124     //5.
125     cout << "《第五题》" << endl;
126     Exercise5 ex5;
127     float celsius;
128     cout << "请输入摄氏温度:";
129     cin >> celsius;
130     cout<<celsius<<"摄氏度等于"<<ex5.celsiusToFahrenheit(celsius)<<"华氏温度"<<endl;
131 
132     //6.
133     cout << "《第六题》" << endl;
134     Exercise6 ex6;
135     double lightYear;
136     cout << "请输入光年:";
137     cin >> lightYear;
138     cout << lightYear << "光年 = " << ex6.countUnits(lightYear) << " 天文单位";
139 
140     //7.
141     cout << "《第七题》" << endl;
142     Exercise7 ex7;
143     int h;
144     int m;
145     cout << "请输入小时:";
146     cin >> h;
147     cout << "请输入分钟:";
148     cin >> m;
149     ex7.timeStatistic(h, m);
150     return 0;
151 
152 }

posted on 2018-07-23 13:58  uimodel  阅读(624)  评论(0编辑  收藏  举报

导航