C语言基础习题50例(十)46-50

文章目录

    • 习题46
    • 习题47
    • 习题48
    • 习题49
    • 习题50

知足常足,终身不辱。
月圆缺,水满溢,事情到了极致一定会遭受祸患,只有懂得知足,才是富足。

习题46

宏#define命令练习。

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

代码如下:

#include 

#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 = %d\n", 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

#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=%d\n",x,y);
    EXCHANGE(x,y);
    printf("After Exchange:x=%d; y=%d\n",x,y);
    
    return 0;
}

打印:

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

习题48

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

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

代码如下:

#include 

#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("Error\n");
    return 0;
}

打印:

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

习题49

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

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

代码如下:

#include
#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 bigger\n", MAXIMUM(a,b));
#else
    printf("%d is smaller\n", MINIMUM(a,b));
#endif

#ifndef MIN
    printf("%d is smaller\n", MINIMUM(a,b));
#else
    printf("%d is bigger\n", MAXIMUM(a,b));
#endif

#undef MAX

#ifdef MAX
    printf("%d is bigger\n", MAXIMUM(a,b));
#else
    printf("%d is smaller\n", MINIMUM(a,b));
#endif

#define MIN 1

#ifndef MIN
    printf("%d is smaller\n", MINIMUM(a,b));
#else
    printf("%d is bigger\n", MAXIMUM(a,b));
#endif

#if(MIN)
    printf("%d is smaller\n", MINIMUM(a,b));
#else
    printf("%d is bigger\n", 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 
#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("Error\n");
    return 0;
}

打印:

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

你可能感兴趣的:(C语言实战)