C/C++ 学习笔记(一)——编译预处理

in 编程技术分享 with 0 comment

这个系列其实是学校作业,没难度,大家看着玩儿吧 😂

//
// Created by imbytecat on 2020/5/18.
// 输入两个整数,求它们的余数。用带参数的宏来实现。
//

#include <cstdio>

#define mod(x, y) x%y

int main() {
    int x, y, r;
    printf("Input two numbers: ");
    scanf("%d%d", &x, &y);
    printf("Their reminder: ");
    r = mod(x, y);
    printf("%d", r);
    return 0;
}
//
// Created by imbytecat on 2020/5/18.
// 编写一个带参数的宏,实现从3个数中,找出最小的数。
//

#include <cstdio>

#define  max(a, b, c) (a>b?a:b)>z?(a>b?a:b):z

int main() {
    int x, y, z, r;
    printf("Input three numbers: ");
    scanf("%d%d%d", &x, &y, &z);
    printf("The max number: ");
    r = max(x, y, z);
    printf("%d", r);
    return 0;
}
//
// Created by imbytecat on 2020/5/18.
// 编写一个带参数的宏,输入圆的半径,求圆的面积。
//

#include <cstdio>

#define PI 3.14159f
#define  calcCircleArea(r) PI*r*r

int main() {
    double r, area;
    printf("Input r: ");
    scanf("%lf", &r);
    printf("The area of circle: ");
    area = calcCircleArea(r);
    printf("%f", area);
    return 0;
}
//
// Created by imbytecat on 2020/5/18.
// 编写一个程序,用条件编译方式实现以下功能:
// 输入一行文字,用 #define 命令控制英文字母的加密,将字母变成它后面的第4个字母,w、x、y、z,变成 a、b、c、d,其它字符不变。
//

#include <cstdio>
#include <cstring>

#define ENCRYPT 1

int main() {
    char str[32];
    printf("String: ");
    scanf("%s", str);
    printf("Processed: ");
#if ENCRYPT
    for (int i = 0; i < strlen(str); ++i) {
        if ((str[i] >= 'a' && str[i] <= 'v') || (str[i] >= 'A' && str[i] <= 'V')) {
            str[i] += 4;
        } else if ((str[i] >= 'w' && str[i] <= 'z') || (str[i] >= 'W' && str[i] <= 'Z')) {
            str[i] -= 22;
        }
    }
    printf("%s", str);
#endif
    return 0;
}
Responses