100 cases of C language classic exercises 46-50

Posted by cheekychop on Tue, 23 Jun 2020 11:59:20 +0200

Article catalog

Contentment is always enough, but never disgraced.
When the moon is full and the water is full, things will suffer disaster when they reach the extreme. Only when you know how to be content, you will be rich.

Exercise 46

Macro define command exercise.

Implementation ideas:
Macros are defined by the define command, which can be divided into nonparametric and parametric macros, and can be tested separately. This is just a simple string substitution.

The code is as follows:

#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;
}

Print:

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

Exercises 47

Macro define command exercise, replacing a code block.

Implementation ideas:
The implementation uses macros in the code just like calling functions (which, of course, are not actually calling functions).

The code is as follows:

#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;
}

Print:

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

Exercises 48

Macro define command exercise, replacing operation symbols.

Implementation ideas:
Replace the original symbol with the defined macro during the comparison operation.

The code is as follows:

#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;
}

Print:

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

Exercise 49

#if,#ifdef and#Comprehensive application of ifndef.

Implementation ideas:
The preprocessor provides the function of conditional compilation, which can compile different program parts according to different conditions, thus producing different object code files.

The code is as follows:

#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;
}

Print:

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

Exercise 50

#include.

Implementation ideas:
The angle bracket is used for file inclusion to indicate to search in the inclusion file directory (the inclusion directory is set by the user when configuring the environment), rather than in the source file directory;
Using double quotation marks means to search in the current source file directory first. If not, you can search in the containing directory.

Create cp.h as follows:

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

The code is as follows:

#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;
}

Print:

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