C++ Primer Plus习题及答案

62 阅读4分钟

第二章

复习题

1.c++程序模块叫什么?

函数。

2.下面的预处理编译器指令是做什么用的?

#include <iostream>

该编译指令导致预处理器将iostream文件的内容添加到程序中。

3.下面语句是做什么用的?

using namespace std;

这个using编译指令使得std名称空间中所有名称都可用。

4.什么语句可以用来打印“Hello,world”,然后打印新的一行?

cout << "Hello World" << endl;

5.什么语句可以用来创建名为cheeses的整数变量?

int cheeses;

6.什么语句可以用来将32赋值给变量cheeses?

cheeses = 32;

7.什么语句可以用来将键盘输入的值读入变量cheeses中?

cin >> cheeses;

8.什么语句可以用来打印“We have X varieties of cheeses”,其中X为变量cheese的当前值。

cout << "We have " << cheese << " varieties of cheeses";

9.下面的函数原型指出关于函数的哪些信息?

int froop(double t);
void rattle(int n);
int prune(void);
  • int froop(double t);

    函数返回一个整数值,一个参数,类型为 double,参数名为 t

  • void rattle(int n);

    函数不返回任何值,一个参数,类型为 int,参数名为 n

  • int prune(void);

    函数返回一个整数值,函数不接受任何参数,

10.定义函数时,什么情况下不必使用关键字return?

当函数不返回值

11.假设你编写的main()函数包含如下代码:

cout << "Please enter you PIN: ";

而编译器指出cout是一个未知标识符。导致这种问题的原因很可能是什么?指出3种修复这种问题的方法。

可能的原因:

  1. 没有包含头文件。
  2. 包含了但没有使用std命名空间

三种修复方法:

  1. 包含<iostream>头文件,并使用std::cout显式限定。
  2. 包含<iostream>头文件,并在函数内部或全局使用using namespace std;
  3. 包含<iostream>头文件,并单独引入cout

编程练习

1.编写一个c++程序,它显示您的姓名和地址。

#include <iostream>
int main() {
    std::cout << "姓名:一行瑠璃" << endl << "地址:图书馆";
    return 0;
}

2.编写一个c++程序,它要求用户输入一个以long为单位的距离,然后将它转换为码(一long等于200码)。

#include <iostream>
int main() {
    double distanceInLong;
    std::cout << "请输入以long为单位的距离:";
    std::cin >> distanceInLong;
    std::cout << distanceInLong << "Long = " << distanceInLong * 220 << "码";
    return 0;
}

3.编写一个c++程序,他使用3个用户定义的函数(包括main(),并生成下面的输出:

Three blind mice

Three blind mice

See how they run

See how they run

其中一个函数要调用两次,该函数生成前两行;另外一个函数也调用两次,并生成其余的输出。

#include <iostream>
void function_1() {
    std::cout << "Three blind mice" << std::endl;
}
void function_2() {
    std::cout << "See how they run" << std::endl;
}
int main() {
    function_1();
    function_1();
    function_2();
    function_2();
    return 0;
}

4.编写一个程序,让用户输入其年龄,然后显示该年龄包含多少个月,如下图所示:

Enter your age:29

Your age in months is 348.

#include <iostream>
int main() {
    int age;
    std::cout << "Enter your age:";
    std::cin >> age;
    std::cout << "Your age in months is " << age * 12 << ".";
    return 0;
}

5.编写一个程序,其中的main()调用一个用户定义的函数(以摄氏温度为参数,并返回相应的华氏温度值)。该程序按下面的格式要求用户输入摄氏温度值,并显示结果:

Please enter a Celsius value: 20

20 degrees Celsius is 68 degrees Fahrenheit.

下面是转化公式:

华氏温度 = 1.8 * 摄氏温度 + 32.0

#include <iostream>
int main() {
    double celsius;
    std::cout << "Please enter a Celsius value: ";
    std::cin >> celsius;
    std::cout << celsius << " degrees Celsius is " << 1.8 * celsius + 32.0 << " degrees Fahrenheit.";
    return 0;
}
    

6.编写一个程序,其main()调用一个用户定义的函数(以光年值为参数,并返回对应天文单位的值)。该程序按下面的格式要求用户输入光年值,并显示结果:

Enter the number of light years: 4.2

4.2 light years = 265608 astronomical units.

天文单位是从地球到太阳的平均距离(约150000000公里或93000000英里),光年是光一年走的距离(约10万亿公里或6万亿英里)(除太阳外,最近的恒星离地球4.2光年)。请使用double类型,转换公式为:

1光年 = 63240天文单位

#include <iostream>
double ly2au(double ly) {
    return ly * 63240;
}
int main() {
    double ly;
    double au;
    std::cout << "Enter the number of light years: ";
    std::cin >> ly;
    au = ly2au(ly);
    std::cout << ly << " light years = " << au << " astronomical units.";
    return 0;
}

7.编写一个程序,要求用户输入小时数和分钟数。再main()函数中,将两个值传递给一个viod函数,后者以下面这样的格式显示这两个值:

Enter the number of hours:9

Enter the number of minutes: 28

Time: 9: 28

#include <iostream>
void print_time(int hour, int minute) {
    std::cout << "Time: " << hour << ": " << minute;
}
int main() {
    int hour;
    int minute;
    std::cout << "Enter the number of hours: ";
    std::cin >> hour;
    std::cout << "Enter the number of minutes: ";
    std::cin >> minute;
    print_time(hour, minute);
    return 0;
}