C语言经典习题100例(十)46-50

时间:2022-07-22
本文章向大家介绍C语言经典习题100例(十)46-50,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

习题46

宏#define命令练习。

实现思路: 宏通过#define命令定义,分为无参宏和带参宏,可以分别进行测试。这只是一种简单的字符串代换。

代码如下:

#include <stdio.h>

#define TRUE 1
#define FALSE 0
#define SQR(x) (x)*(x)

int main(){
	int num, next = TRUE;
	while(next){
		printf("Please input a number:n");
		scanf("%d", &num);
		printf("Square = %dn", SQR(num));
		if(num > 10){
			next = FALSE;
		}

	}
	
	return 0;
}

打印:

Please input a number:
1
Square = 1
Please input a number:
5
Square = 25
Please input a number:
9
Square = 81
Please input a number:
13
Square = 169

习题47

宏#define命令练习,替换一个代码块。

实现思路: 实现在代码中使用宏就像调用函数一样(当然,实际上并不是调用函数)。

代码如下:

#include<stdio.h>

#define EXCHANGE(a,b) { int t;t=a;a=b;b=t;}

int main()
{
    int x = 12;
    int y = 20;
    printf("Before Exchange:x=%d; y=%dn",x,y);
    EXCHANGE(x,y);
    printf("After Exchange:x=%d; y=%dn",x,y);
    
    return 0;
}

打印:

Before Exchange:x=12; y=20
After Exchange:x=20; y=12

习题48

宏#define命令练习,替换运算符号。

实现思路: 在进行比较运算的时候用定义的宏替换掉原来的符号。

代码如下:

#include <stdio.h>

#define GT >
#define LT <
#define EQ ==

int main()
{
    int i, j;
    printf("Please input two numbers:n");
    scanf("%d %d", &i, &j);
    if(i GT j)
        printf("%d is greater than %d n", i, j);
    else if(i EQ j)
        printf("%d is equal to %d n", i, j);
    else if(i LT j)
        printf("%d is smaller than %d n", i, j);
    else
        printf("Errorn");
    return 0;
}

打印:

Please input two numbers:
13 45
13 is smaller than 45

习题49

#if、#ifdef和#ifndef的综合应用。

实现思路: 预处理程序提供了条件编译的功能,可以按不同的条件去编译不同的程序部分,因而产生不同的目标代码文件。

代码如下:

#include<stdio.h>
#define MAX
#define MAXIMUM(x,y) (x>y)?x:y
#define MINIMUM(x,y) (x>y)?y:x
int main(){
    int a=12, b=20;
    
#ifdef MAX
    printf("%d is biggern", MAXIMUM(a,b));
#else
    printf("%d is smallern", MINIMUM(a,b));
#endif

#ifndef MIN
    printf("%d is smallern", MINIMUM(a,b));
#else
    printf("%d is biggern", MAXIMUM(a,b));
#endif

#undef MAX

#ifdef MAX
    printf("%d is biggern", MAXIMUM(a,b));
#else
    printf("%d is smallern", MINIMUM(a,b));
#endif

#define MIN 1

#ifndef MIN
    printf("%d is smallern", MINIMUM(a,b));
#else
    printf("%d is biggern", MAXIMUM(a,b));
#endif

#if(MIN)
    printf("%d is smallern", MINIMUM(a,b));
#else
    printf("%d is biggern", MAXIMUM(a,b));
#endif	

    return 0;
}

打印:

20 is bigger
12 is smaller
12 is smaller
20 is bigger
20 is bigger

习题50

#include的应用练习。

实现思路: 文件包含使用尖括号表示在包含文件目录中去查找(包含目录是由用户在配置环境时设置的),而不在源文件目录去查找; 使用双引号则表示首先在当前的源文件目录中查找,若未找到才到包含目录中去查找。

创建cp.h如下:

#define GT >
#define LT <
#define EQ ==

代码如下:

#include <stdio.h>
#include "cp.h"


int main()
{
    int i, j;
    printf("Please input two numbers:n");
    scanf("%d %d", &i, &j);
    if(i GT j)
        printf("%d is greater than %d n", i, j);
    else if(i EQ j)
        printf("%d is equal to %d n", i, j);
    else if(i LT j)
        printf("%d is smaller than %d n", i, j);
    else
        printf("Errorn");
    return 0;
}

打印:

Please input two numbers:
12 20
12 is smaller than 20