2.6 复习题
1. C++程序的模块叫什么?
答案:C++程序设计中的模块的主要形式是函数。
2. 下面的预处理器编译指令是做什么用的?
#include <iostream>
答案:#include <iostream>表示将iostream头文件添加到当前源代码中,iostream头文件主要包含了系统的标准输入/输出函数以及数据的声明和定义。
3. 下面的语句是做什么用的?
using namespace std;
答案:using预编译器指令的主要功能是表明当前源代码文件使用名称空间std。让程序可以直接使用 std 命名空间中的内容,而无需在每次使用时都添加 std:: 前缀。C++语言为了解决多个厂商的独立代码在标识符命名发生冲突的问题,要求不同厂商的代码模块拥有自己的命名空间,在使用这个模块时需要明确标注自己使用的代码模块。
4. 什么语句可以用来打印短语"Hello, world",然后开始新的一行?
#include <iostream>
int main()
{using namespace std;cout << "Hello, world" << endl;return 0;
}
5. 什么语句可以用来创建名为cheese的整数变量?
int cheese;
6. 什么语句可以用来将值32赋给变量cheese?
cheese = 32;
7. 什么语句可以用来将从键盘输入的值读入变量cheese中?
cin >> cheese;
8. 什么语句可以用来打印“We have X varicties of chesse,",其中X为变量cheeses的当前值。
cout << "We have " << cheese << " varieties of cheese," << endl;
9. 下面的函数原型指出了关于函数的哪些信息?
int froop(double t);
void rattle(int n);
int prune(void);
答案: 函数原型主要包含函数名、参数表和返回值三方面的内容。
第1个函数原型函数名为froop,函数返回值是整型,函数有一个参数,参数数据类型是double。
第2个函数原型函数名为rattle,函数没有返回值,函数有一个参数,参数数据类型是int。
第1个函数原型函数名为prunt,函数返回值是整型,函数没有参数。
10. 定义函数时,在什么情况下不必使用关键字return?
答案:通常当函数返回值为空(void)时,可以不需要return语句。
11. 假设您编写的main()函数包含如下代码:
cout << "Please enter your PIN: ";
而编译器指出cout是一个未知标识符。导致这种问题的原因很可能是什么?指出3种修复这种问题的方法。
答案:这种问题通常是因为缺少对标准命名空间 std 的引用或声明,导致编译器无法识别 cout 标识符。
可以使用如下3种方法进行声明:
std::cout << "Please enter your PIN: ";
using namespace std;
cout << "Please enter your PIN: ";
using std::cout;
cout << "Please enter your PIN: ";
2.7 编程练习
1. 编写一个C++程序,它显示您的姓名和地址。
#include <iostream>
using namespace std;
int main()
{cout << "< C++ Primer Plus> author: Stephen Prata" << endl;return 0;
}
2. 编写一个C++程序,它要求用户输入一个以long为单位的距离,然后将它转换为码(一long等于220码)。
#include <iostream>
using namespace std;
#define LONGPERYERD 220
int main()
{double distance;cout << "Please enter the distance (IN LONG):";cin >> distance;cout << "The distance " << distance << " (long) is " << distance * LONGPERYERD << " (yard)." << endl;return 0;
}
3. 编写一个C++程序,它使用3个用户定义的函数(包括main()),并生成下面的输出:
Three blind mice
Three blind mice
See how they run
See how they run
其中一个函数要调用两次,该函数生成前两行;另一个函数也被调用两次,并生成其余的输出。
#include <iostream>
using namespace std;
void printMice();
void printRun();
int main()
{printMice();printMice();printRun();printRun();return 0;
}
void printMice()
{cout << "Three blind mice" << endl;
}
void printRun()
{cout << "See how they run" << endl;
}
4. 编写一个程序,让用户输入其年龄,然后显示该年龄包含多少个月,如下所示:
Enter your age: 29
Your age in months is 384.
#include <iostream>
using namespace std;
#define MONTHSPERYEAR 12
int main()
{int age;cout << "Enter your age: ";cin >> age;cout << "Your age in months is " << age * MONTHSPERYEAR << endl;return 0;
}
5. 编写一个程序,其中的main()调用一个用户定义的函数(以摄氏温度值为参数,并返回相应的华氏温度值)。该程序按下面的格式要求用户输入摄氏温度值,并显示结果:
Please enter a Celsius value: 20
20 degrees Celsius is 68 degrees Fahrenheit.
下面是转换公式:
华氏温度 = 1.8 X 摄氏温度 + 32.0;
#include <iostream>
using namespace std;
double convertCelToFah(double celsius);
int main()
{double celsius,fahrenheit;cout << "Please enter a Celsius value: ";cin >> celsius;fahrenheit = convertCelToFah(celsius);cout << celsius << " degrees Celsius is " << fahrenheit << " degrees fahrenheit." << endl;return 0;
}
double convertCelToFah(double celsius)
{double fahrenheit;fahrenheit = 1.8 * celsius + 32.0;return fahrenheit;
}
6. 编写一个程序,其main()调用一个用户定义的函数(以光年值为参数,并返回对应天文单位的值)。该程序按下面的格式要求用户输入光年值,并显示结果:
Enter the number of light years: 4.2
4.2 light years = 265608 astronomical units.
天文单位是从地球到太阳的平均距离(约150000000公里或93000000英里),光年是光一年走的距离(约10万亿公里或6万亿英里)(除太阳外,最近的恒星大约离地球4.2光年)。请使用double 类型(参见程序清单2.4),转换公司为:
1光年 = 63240天文单位
#include <iostream>
using namespace std;
#define ASTRO_PER_LIGHTYERA 63240
double convertLightyearsToAstros(double lightYears);
int main()
{double lightYears, astroVal;cout << "Enter the number of light years: ";cin >> lightYears;astroVal = convertLightyearsToAstros(lightYears);cout << lightYears << " light yers = " << astroVal << " astronomical units." << endl;return 0;
}
double convertLightyearsToAstros(double lightYears)
{return lightYears * ASTRO_PER_LIGHTYERA;
}
7. 编写一个程序,要求用户输入小时数和分钟数。在main()函数中,将这两个值传递给void函数,后者以下面这样的格式显示这两个值:
#include <iostream>
using namespace std;
void formatTime(int hours, int minutes);
int main()
{int hours, minutes;cout << "Enter the number of hours: ";cin >> hours;cout << "Enter the number of minutes: ";cin >> minutes;formatTime(hours, minutes);return 0;
}
void formatTime(int hours, int minutes)
{int minutesToHour = minutes / 60;int minutesLeft = minutes % 60;cout << "Time: " << hours + minutesToHour << " : " << minutesLeft;
}