《c++编程思想第1卷》第3章练习题答案

本文详细解答了《C++编程思想第1卷》第三章的多个练习,涉及函数声明与定义、质数查找、输入处理、switch语句、表达式计算、内存管理、枚举类型、数组、指针、结构体等多个C++核心概念。通过实践加深了对C++编程的理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

3-1

建立一个头文件(扩展名为’.h’)。在该文件中,声明一组函数,具有可变参数,返回值包括void、char、int和float类型。建立一个包含上述头文件的.cpp文件,创建所有这些函数的定义。每个定义应该简单地输出函数名,参数列表,并返回类型以便知道它已经被调用。创建另外一个.cpp文件,它包含头文件且定义int main(),在其中调用已经定义的所有函数。编译和运行这个程序。
创建头文件3-1.h:

#define FUNC(type) type type##Func()
#define CALL_FUNC(type) type##Func()
FUNC(void);
FUNC(char);
FUNC(int);
FUNC(float);

创建3-1.cpp文件:

#include "3-1.h"
#include <iostream>
using namespace std;

FUNC(void) {
   
   
    cout << "void voidFunc() 被调用" << endl;
}

FUNC(char) {
   
   
    cout << "char charFunc() 被调用" << endl;
    return 'a';
}

FUNC(int) {
   
   
    cout << "int intFunc() 被调用" << endl;
    return 3;
}

FUNC(float) {
   
   
    cout << "float floatFunc() 被调用" << endl;
    return 3.14;
}

创建含有main函数的.cpp文件:

#include "3-1.h"
#include <iostream>
using namespace std;

int main() {
   
   
    CALL_FUNC(void);
    char a = CALL_FUNC(char);
    cout << "返回值为" << a << endl;
    int b = CALL_FUNC(int);
    cout << "返回值为" << b << endl;
    float c = CALL_FUNC(float);
    cout << "返回值为" << c << endl;
}

输出:

void voidFunc() 被调用
char charFunc() 被调用
返回值为a
int intFunc() 被调用
返回值为3
float floatFunc() 被调用
返回值为3.14

3-2

编写一个程序使用两重for循环和模运算符(%)去寻找和输出质数(只能被1和它本身整除的整数)。

#include <iostream>
using namespace std;

int main() {
   
   
    cout << "请输入一个整数:";
    int a;
    cin >> a;
    cout << "在2到" << a << "之间有如下质数:" << endl;
    for (int i = 2; i <=a; i++) {
   
   
        bool isPrime = true;
        for (int j = 2; j < i; j++) {
   
   
            if (i % j == 0) {
   
   //能整除,说明不是质数
                isPrime = false;
                break;
            }
        }
        if (isPrime) {
   
   
            cout << i << " ";
        }
    }
    cout << endl;
}

输出:

请输入一个整数:20
在2到20之间有如下质数:
2 3 5 7 11 13 17 19

3-3

编写一个程序,使用一个while循环从标准输入(cin)中把单词读入到string中。这是一个“无穷”while循环,可以使用break语句中断(和退出程序)。对于读入的每个单词,先用一系列的if语句把该单词“映射”为一个整数值,然后用该整数值作为一个switch语句的选择条件(这些操作并不意味着良好的设计风格,这仅仅是为练习这些控制流程)。在每个case中,输出一些有意义的信息。判定哪些是“有趣”的单词以及这些单词的意义。同时判定哪个单词是程序结束的标志。用文件作为输入来测试该程序(如果想节省输入,这个文件将作为程序的源文件)。

#include <iostream>
#include <string>
using namespace std;

int main() {
   
   
    string word;
    while (cin >> word) {
   
   
        if (word == "quit") {
   
   
            cout << "退出程序";
            break;
        }

        int type = 0;
        if (word.size() == 5) {
   
   
            type = 1;
        } else if (word.size() > 5) {
   
   
            type = 2;
        }

        switch (type) {
   
   
            case 0:
                cout << word << ":这个单词长度小于5" << endl;
                break;
            case 1:
                cout << word << ":是个有趣的单词,长度等于5" << endl;
                break;
            case 2:
                cout << word << ":这个单词长度大于5" << endl;
                break;
        }
    }
}

输出:

what
what:这个单词长度小于5
are
are:这个单词长度小于5
you
you:这个单词长度小于5
doing
doing:是个有趣的单词,长度等于5
quit
退出程序

3-4

修改Menu.cpp程序,使用switch语句代替if语句。

#include <iostream>
using namespace std;

int main() {
   
   
  char c; // To hold response
  while(true) {
   
   
    cout << "主菜单:" << endl;
    cout << "l: 左, r: 右, q: 退出 -> ";
    cin >> c;
    if (c == 'q') break;
    switch (c) {
   
   
      case 'l': {
   
   
        cout << "左键菜单:" << endl;
        cout << "选择 a 或 b:";
        cin >> c;
        switch (c) {
   
   
          case 'a':
            cout << "你选择了'a'" << endl;
            continue; // Back to main menu
          case 'b':
            cout << "你选择了'b'" << endl;
            continue; // Back to main menu
          default:
            cout << "你没有选择 a 或 b!"
                << endl;
            continue; // Back to main menu
        }
        break;
      }
      case 'r': {
   
   
        cout << "右键菜单:" << endl;
        cout << "选择 c 或 d: ";
        cin >> c;
        switch (c) {
   
   
          case 'c':
            cout << "你选择了'c'" << endl;
            continue; // Back to main menu
          case 'd':
            cout << "你选择了'd'" << endl;
            continue; // Back to main menu
          default:
            cout << "你没有选择 c 或 d!"
                << endl;
            continue; // Back to main menu
        }
        break;
      }
    }
    cout << "你必须选择 l 或 r 或 q!" << endl;
  }
  cout << "退出菜单..." << endl;
} 

3-5

编写一个程序计算在“优先级”一节中的两个表达式的值。

#include <iostream>
using namespace std;

#define PRINT(X) \
    cout << #X " = " << X << endl;

int main() {
   
   
    int X = 1, Y = 2, Z = 3;
    PRINT(X + Y - 2/2 + Z);
    PRINT(X + (Y - 2)/(2 + Z));
}

输出:

X + Y - 2/2 + Z = 5
X + (Y - 2)/(2 + Z) = 1

3-6

修改YourPets2.cpp程序以使用不同的数据类型(char、int、float、double和这些类型的变型)。运行该程序并画出结果内存分布图。如果能在多种机器、操作系统或者编译器上运行该程序,用尽可能多的变化进行这个试验。

#include <iostream>
using namespace std;

#define PRINT(type) cout << #type " 大小:" << sizeof(type) << endl;
#define PRINT_ADDR(x, y, z) cout << &x <&l
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值