0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

書籍 ダイテル C言語プログラミング 練習問題 解答 13章

0
Posted at
書籍

image.png

13.4: 体積を計算するマクロ
source
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

#define PI 3.14159

// 体積を求めるマクロ
#define VOLUME(r)  ((4.0 / 3) * PI * pow(r, 3))

// 和を求めるマクロ
#define SUM(x, y) ( x + y )

int main()
{
    int radius;

    for (radius = 1; radius <= 10; radius++)
    {
        printf("%lf\n", VOLUME(radius));
        printf("xとyの和は %d\n", SUM(radius, radius));
    }
    getch();
    return 0;
}
13.5: 2つの引数xとyの和を求めるマクロ
source
#include <stdio.h>

#define SUM(x, y) ((x) + (y))

int main()
{
    int x = 10;
    int y = 20;

    printf("xとyの和は%d\n", SUM(x, y));

    return 0;
}
13.6: 2つの値の小さいほうを決定するマクロ
source
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>

#define MINIMUM2(x, y) ((x < y) ? (x) : (y))

int main()
{
    int x, y;


    printf("2つの値を入力してください\n");
    scanf("%d", &x);
    scanf("%d", &y);

    printf("小さいほうの値は%dです\n", MINIMUM2(x, y));

    getch();
    return 0;
}
13.7: 3つの整数の最小値を決定するマクロ
source
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>

#define MINIMUM2(x, y) ((x < y) ? (x) : (y))
#define MINIMUM3(x, y, z) ((x < MINIMUM2(y, z)) ? (x) : (MINIMUM2(y, z)))

int main()
{
    int x, y, z;


    printf("3つ値を入力してください\n");
    scanf("%d", &x);
    scanf("%d", &y);
    scanf("%d", &z);

    printf("一番小さい値は%dです\n", MINIMUM3(x, y, z));
    getch();
    return 0;
}
13.8: 文字列をプリントするマクロ
source
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>

#define PRINT(s) { printf("%s", s); }
int main()
{
    PRINT("文字列をプリントします\n")
    getch();
    return 0;
}
13.9: 整数の配列をプリントするマクロ
source
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>

#define PRINT(s) { printf("%s", s); }
#define PRINTARRAY(array, size) { int i; for (i = 0; i < size; i++) {printf("%d\n", array[i]);} }
int main()
{
    int a[3];

    PRINT("文字列をプリントします\n")

    printf("%d\n", sizeof(a) / sizeof(a[0]));
    PRINTARRAY(a, sizeof(a) / sizeof(a[0]))
    getch();
    return 0;
}
13.10: 配列内の数値を合計するマクロ
source
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>

#define PRINT(s) { printf("%s", s); }
#define PRINTARRAY(array, size) { int i; for (i = 0; i < size; i++) {printf("%d\n", array[i]);} }

#define SUMARRAY(array, size) {int i; int total = 0; for (i = 0; i < size; i++) { total += array[i];} }

int main()
{
    int a[3];

    PRINT("文字列をプリントします\n")

    printf("%d\n", sizeof(a) / sizeof(a[0]));
    PRINTARRAY(a, sizeof(a) / sizeof(a[0]))

    printf("\n");
    SUMARRAY(a, sizeof(a) / sizeof(a[0]));
    getch();
    return 0;
}
ポータルサイト
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?