• C和指针 第14章 预处理器 14.10 编程练习


    1. 你所在的公司向市场投放了一个程序,用于处理金融交易并打印它们的报表。为了扩展潜在的市场,这个程序以及个不同的版本进行销售,每个版本都有不同选项的组合---选项越多,价格就越高。你的任务是实现一个打印函数的代码,这样它可以很容易地进行编译,产生程序的不同版本。
    你的函数名为print_ledger。它接受一个int参数,没有返回值。它应该调用一个或多个下面的函数,具体依取决于该函数被编译时定义了哪个符号(如果有的话)。 
            如果这个符号被定义为...                那么就调用这个函数
            OPTION_LONG                            print_ledger_long
            OPTION_DETAILED                     print_ledger_detailed
            (无)                                                print_ledger_default
    每个函数都接受单个int参数。把接收到的值传递给应该调用的函数。 
    解析:
    这个问题唯一的棘手之处在于两个选项都有可能被选择。这种可能性使得无法使用#elif指令来确定哪一个未被定义。
    /*
    ** 打印风格有预定义符号指定的分类账户。 
    */ 
    void print_ledger( int x ){
        #ifdef OPTION_LONG
        #    define OK 1
            printf_ledger_long( x );
        #endif
        
        #ifdef OPTION_DETAILED
        #    define OK 1
            printf_ledger_detailed( x );
        #endif
        
        #ifndef OK
            printf_ledger_default( x );
        #endif

    2. 编写一个函数,返回一个值,提示运行这个函数的计算机的类型。这个函数将由一个能够运行于不同计算机的程序使用。
    我们将使用条件编译来实现。你的函数应该叫做cpu_type,它不接受任何参数。当函数被编译时,在下表“定义符号”列中
    的符号之一可能被定义。函数应该从“返回值”列中返回对应的符号。如果左边列中的所有符号均未定义,那么函数就返回
    CPU_UNKNOWN这个值。如果超过一个的符号被定义,那么其结果就是未定义的。
            定义符号                    返回值
            VAX                            CPU_VAX
            M68000                        CPU_68000
            M68020                        CPU_68020
            I80386                        CPU_80386
            X6809                        CPU_6809
            X6502                        CPU_6502
            U3B2                        CPU_3B2
            (无)                         CPU_UNKNOWN
    “返回值”列中的符号将被#define定义为各种不同的整型值,其内容位于头文件cpy_type.h中。 

    /*
    ** cpu_tye.h
    */
    #ifndef CPU_TYPE_H
    #define CPU_TYPE_H

    #define CPU_VAX  0
    #define CPU_68000 1
    #define CPU_68020 2
    #define CPU_80386 3
    #define CPU_6809 4
    #define CPU_6502 5
    #define CPU_3B2 6
    #define CPU_UNKNOWN 7

    #endif

    #include <stdio.h>
    #include <stdlib.h>
    #include "cpu_type.h"

    int cpu_type( void );

    int main( void ){
        int type = cpu_type();
        
        printf( "type = %d\n", type );
        
        return EXIT_SUCCESS;    
    }

    int cpu_type( void ){
        int counter = 0;
        int cpu_type;
        #if defined( VAX )
            cpu_type = CPU_VAX;
            counter++;
        #endif
        #if defined( M68000 )
            cpu_type = CPU_68000;
            counter++;
        #endif
        #if defined( M68020 )
            cpu_type = CPU_68020;
            counter++;
        #endif
        #if defined( I80386 )
            cpu_type = CPU_80386;
            counter++;
        #endif
        #if defined( X6809 )
            cpu_type = CPU_6809;
            counter++;
        #endif
        #if defined( X6502 )
            cpu_type = CPU_6502;
            counter++;
        #endif
        #if defined( U3B2 )
            cpu_type = CPU_3B2;
            counter++;
        #else
            cpu_type = CPU_UNKNOWN;
            counter++;
        #endif
        if( counter != 1 ){
            printf( "the result is undefined.\n" );
            return -1;
        } else{
            return cpu_type;
        }
    }
    /* 输出:

    */ 

  • 相关阅读:
    【算法题】只出现一次的数字 III
    华硕天选2刷了3060的bios后,c口失效,不能像之前那样外界显示器
    cmakelist中查找boost和eigen3
    [附源码]Python计算机毕业设计Django基于JEE平台springboot技术的订餐系统
    ARM汇编(gun-complier)
    浅谈计算机领域顶会与顶刊的那些事
    利用 Incredibuild 的智能兼容层,进一步节省云开支
    教育小程序的性能优化:从前端到后端的综合提升策略
    网络安全笔记1——Internet协议的安全性
    【单目标优化求解】粒子群混沌混合蝴蝶优化算法求解最优目标问题(HPSOBOA)【含Matlab源码 1538期】
  • 原文地址:https://blog.csdn.net/weixin_40186813/article/details/125615081