前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C语言经典习题100例(十)46-50

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

作者头像
cutercorley
发布2020-07-23 16:53:25
2340
发布2020-07-23 16:53:25
举报

习题46

宏#define命令练习。

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

代码如下:

代码语言:javascript
复制
#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 = %d\n", SQR(num));
		if(num > 10){
			next = FALSE;
		}

	}
	
	return 0;
}

打印:

代码语言:javascript
复制
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命令练习,替换一个代码块。

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

代码如下:

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

打印:

代码语言:javascript
复制
Before Exchange:x=12; y=20
After Exchange:x=20; y=12

习题48

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

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

代码如下:

代码语言:javascript
复制
#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("Error\n");
    return 0;
}

打印:

代码语言:javascript
复制
Please input two numbers:
13 45
13 is smaller than 45

习题49

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

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

代码如下:

代码语言:javascript
复制
#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 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;
}

打印:

代码语言:javascript
复制
20 is bigger
12 is smaller
12 is smaller
20 is bigger
20 is bigger

习题50

#include的应用练习。

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

创建cp.h如下:

代码语言:javascript
复制
#define GT >
#define LT <
#define EQ ==

代码如下:

代码语言:javascript
复制
#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("Error\n");
    return 0;
}

打印:

代码语言:javascript
复制
Please input two numbers:
12 20
12 is smaller than 20
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-06-23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 习题46
  • 习题47
  • 习题48
  • 习题49
  • 习题50
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档