『新・明解C言語 入門編 第2版』演習問題解答例 2ー4

演習2ー1

問:整数定数、浮動小数定数、int型の変数、doubule型の変数を、掛けたり割ったりするなど、いろいろな演算を行うプログラムを作成し、本文に示した規則を確認せよ。

// Ex0204.c

#include <stdio.h>

int main(void)
{
    printf("#1は、printf内に直接、整数定数と浮動小数点定数を書き込んで計算した場合の確認\n");
    printf("#2は、変数「int x」と変数「double y」に、整数定数と浮動小数点定数を代入して場合の確認\n");
    printf("#3は、変数に整数定数と浮動小数点定数を用いた計算式を代入して場合の確認\n");
    printf("#4は、変数に整数定数と浮動小数点定数を代入した変数を用いた計算式を代入して場合の確認\n");

    printf("値は例として「5」と「2.5」を用いる。\n");

    putchar('\n');

    int x = 5;
    printf("int x = 5\n");

    double y = 2.5;
    printf("double y = 2.5;\n");
    
    int no1 = 5 * 5;
    int no2 = x * x;
    int no3 = 5 * 2.5;
    int no4 = x * y;
    int no5 = 2.5 * 5;
    int no6 = y * x;
    int no7 = 2.5 * 2.5;
    int no8 = y * y;

    int no11 = 5 / 5;
    int no12 = x / x;
    int no13 = 5 / 2.5;
    int no14 = x / y;
    int no15 = 2.5 / 5;
    int no16 = y / x;
    int no17 = 2.5 / 2.5;
    int no18 = y / y;
    
    putchar('\n');
    printf("掛け算(%dで表示)\n");
    printf("#1 (5 * 5 = %%d)            = %d\n", 5 * 5);
    printf("#2 (x * x = %%d)            = %d\n", x * x);
    printf("#3 (int no1 = 5 * 5;)      = %d\n", no1);
    printf("#4 (int no2 = x * x;)      = %d\n", no2);
    putchar('\n');
    printf("#1 (5 * 2.5 = %%d)          = %d\n", 5 * 2.5);
    printf("#2 (x * y = %%d)            = %d\n", x * y);
    printf("#3 (int no3 = 5 * 2.5;)    = %d\n", no3);
    printf("#4 (int no4 = x * y;)      = %d\n", no4);
    putchar('\n');
    printf("#1 (2.5 * 5 = %%d)          = %d\n", 2.5 * 5);
    printf("#2 (y * x = %%d)            = %d\n", y * x);
    printf("#3 (int no5 = 2.5 * 5;)    = %d\n", no5);
    printf("#4 (int no6 = y * x;)      = %d\n", no6);
    putchar('\n');
    printf("#1 (2.5 * 2.5 = %%d)        = %d\n", 2.5 * 2.5);
    printf("#2 (y * y = %%d)            = %d\n", y * y);
    printf("#3 (int no7 = 2.5 * 2.5;)  = %d\n", no7);
    printf("#4 (int no8 = y * y;)      = %d\n", no8);
    putchar('\n');
    putchar('\n');
    printf("掛け算(%fで表示)\n");
    printf("#1 (5 * 5 = %%f)            = %f\n", 5 * 5);
    printf("#2 (x * x = %%f)            = %f\n", x * x);
    printf("#3 (int no1 = 5 * 5;)      = %f\n", no1);
    printf("#4 (int no2 = x * x;)      = %f\n", no2);
    putchar('\n');
    printf("#1 (5 * 2.5 = %%f)          = %f\n", 5 * 2.5);
    printf("#2 (x * y = %%f)            = %f\n", x * y);
    printf("#3 (int no3 = 5 * 2.5;)    = %f\n", no3);
    printf("#4 (int no4 = x * y;)      = %f\n", no4);
    putchar('\n');
    printf("#1 (2.5 * 5 = %%f)          = %f\n", 2.5 * 5);
    printf("#2 (y * x = %%f)            = %f\n", y * x);
    printf("#3 (int no5 = 2.5 * 5;)    = %f\n", no5);
    printf("#4 (int no6 = y * x;)      = %f\n", no6);
    putchar('\n');
    printf("#1 (2.5 * 2.5 = %%f)        = %f\n", 2.5 * 2.5);
    printf("#2 (y * y = %%f)            = %f\n", y * y);
    printf("#3 (int no7 = 2.5 * 2.5;)  = %f\n", no7);
    printf("#4 (int no8 = y * y;)      = %f\n", no8);
    putchar('\n');
    putchar('\n');
    printf("割り算(%dで表示)\n");
    printf("#1 (5 / 5 = %%d)            = %d\n", 5 / 5);
    printf("#2 (x / x = %%d)            = %d\n", x / x);
    printf("#3 (int no11 = 5 / 5;)     = %d\n", no11);
    printf("#4 (int no12 = x / x;)     = %d\n", no12);
    putchar('\n');
    printf("#1 (5 / 2.5 = %%d)          = %d\n", 5 / 2.5);
    printf("#2 (x / y = %%d)            = %d\n", x / y);
    printf("#3 (int no13 = 5 / 2.5;)   = %d\n", no13);
    printf("#4 (int no14 = x / y;)     = %d\n", no14);
    putchar('\n');
    printf("#1 (2.5 / 5 = %%d)          = %d\n", 2.5 / 5);
    printf("#2 (y / x = %%d)            = %d\n", y / x);
    printf("#3 (int no15 = 2.5 / 5;)   = %d\n", no15);
    printf("#4 (int no16 = y / x;)     = %d\n", no16);
    putchar('\n');
    printf("#1 (2.5 / 2.5 = %%d)        = %d\n", 2.5 / 2.5);
    printf("#2 (y / y = %%d)            = %d\n", y / y);
    printf("#3 (int no17 = 2.5 / 2.5;) = %d\n", no17);
    printf("#4 (int no18 = y / y;)     = %d\n", no18);
    putchar('\n');
    putchar('\n');
    printf("割り算(%f)\n");
    printf("#1 (5 / 5 = %%f)            = %f\n", 5 / 5);
    printf("#2 (x / x = %%f)            = %f\n", x / x);
    printf("#3 (int no11 = 5 / 5;)     = %f\n", no11);
    printf("#4 (int no12 = x / x;)     = %f\n", no12);
    putchar('\n');
    printf("#1 (5 / 2.5 = %%f)          = %f\n", 5 / 2.5);
    printf("#2 (x / y = %%f)            = %f\n", x / y);
    printf("#3 (int no13 = 5 / 2.5;)   = %f\n", no13);
    printf("#4 (int no14 = x / y;)     = %f\n", no14);
    putchar('\n');
    printf("#1 (2.5 / 5 = %%f)          = %f\n", 2.5 / 5);
    printf("#2 (y / x = %%f)            = %f\n", y / x);
    printf("#3 (int no15 = 2.5 / 5;)   = %f\n", no15);
    printf("#4 (int no16 = y / x;)     = %f\n", no16);
    putchar('\n');
    printf("#1 (2.5 / 2.5 = %%f)        = %f\n", 2.5 / 2.5);
    printf("#2 (y / y = %%f)            = %f\n", y / y);
    printf("#3 (int no17 = 2.5 / 2.5;) = %f\n", no17);
    printf("#4 (int no18 = y / y;)     = %f\n", no18);

    return 0;
}

コメント

 いろんなパターンを入れてみました。

 


書籍情報


コメント

タイトルとURLをコピーしました