C言語による基本的な実験プログラムの実装例

実験タスク 1

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX_IDS 5

int main() {
    int generatedNumber;
    int index;

    srand(time(NULL));
    for (index = 0; index < MAX_IDS; index++) {
        generatedNumber = rand() % 100 + 1;
        printf("20240042%04d\n", generatedNumber);
    }
    return 0;
}

問題1: 202400420001 から 202400420100 の範囲内でランダムに5つの学生IDを生成する方法について説明してください。

問題2: 1 から 100 までの整数を生成する方法。

問題3: 学生IDの末尾を4桁の形式で表示するための処理。

問題4: プログラム実行ごとに異なる乱数シーケンスを生成し、ランダム性を確保するための仕組み。

実験タスク 2

#include <stdio.h>

int main() {
    int userChoice, itemQuantity;
    float totalPrice = 0.0f, payment, refund;

    while (1) {
        printf("\n自動販売機メニュー:\n");
        printf("1. コカコーラ - 3円\n");
        printf("2. スプライト - 3円\n");
        printf("3. オレンジジュース - 5円\n");
        printf("4. ミネラルウォーター - 2円\n");
        printf("0. 終了\n");
        printf("商品番号を入力してください: ");
        scanf("%d", &userChoice);

        if (userChoice == 0) {
            break;
        }

        if (userChoice < 1 || userChoice > 4) {
            printf("無効な番号です。再入力してください。\n");
            continue;
        }

        printf("購入数量を入力してください: ");
        scanf("%d", &itemQuantity);

        if (itemQuantity < 0) {
            printf("数量は0以上でなければなりません。\n");
            continue;
        }

        switch (userChoice) {
            case 1:
            case 2:
                totalPrice += 3.0f * itemQuantity;
                break;
            case 3:
                totalPrice += 5.0f * itemQuantity;
                break;
            case 4:
                totalPrice += 2.0f * itemQuantity;
                break;
        }

        printf("支払い金額を入力してください: ");
        scanf("%f", &payment);

        if (payment < totalPrice) {
            printf("支払い金額が不足しています。再度入力してください。\n");
            continue;
        }

        refund = payment - totalPrice;
        printf("合計金額: %.2f円\n", totalPrice);
        printf("お釣り: %.2f円\n", refund);
        totalPrice = 0.0f;
    }

    printf("ご購入ありがとうございました。またのお越しをお待ちしております。\n");
    return 0;
}

問題1: 支払い金額が不足した場合のエラーハンドリングを追加する必要がある。

問題2: 無効な商品番号の入力をフィルタリングし、次のループへ移行する処理。

実験タスク 3

#include <stdio.h>

int main() {
    char currentChar;
    while ((currentChar = getchar()) != EOF) {
        if (currentChar == '\n') continue;

        switch (currentChar) {
            case 'r':
                printf("stop!\n");
                break;
            case 'g':
                printf("go go go\n");
                break;
            case 'y':
                printf("wait a minute\n");
                break;
            default:
                printf("unknown command\n");
                break;
        }
    }
    return 0;
}

問題1: 入力文字の処理と標準入力のバッファクリア方法について説明してください。

問題2: シンプルな条件分岐を用いた状態管理の実装方法。

実験タスク 4

#include <stdio.h>

int main(void) {
    double currentExpense;
    double totalSpent = 0.0;
    double highestExpense = 0.0;
    double lowestExpense = 0.0;
    int transactionCount = 0;

    printf("本日の支出を入力してください(-1で終了):\n");

    while (1) {
        scanf("%lf", &currentExpense);

        if (currentExpense == -1.0) {
            break;
        }

        if (currentExpense < 0.0 || currentExpense > 20000.0) {
            printf("無効な入力です。再度入力してください。\n");
            continue;
        }

        totalSpent += currentExpense;
        transactionCount++;

        if (transactionCount == 1) {
            highestExpense = currentExpense;
            lowestExpense = currentExpense;
        } else {
            if (currentExpense > highestExpense) {
                highestExpense = currentExpense;
            }
            if (currentExpense < lowestExpense) {
                lowestExpense = currentExpense;
            }
        }
    }

    if (transactionCount == 0) {
        printf("入力がありません。再度実行してください。\n");
    } else {
        printf("今日の総支出: %.2f\n", totalSpent);
        printf("最高支出額: %.2f\n", highestExpense);
        printf("最低支出額: %.2f\n", lowestExpense);
    }

    return 0;
}

問題1: 費用の最大値と最小値を計算する際の初期化処理の重要性。

問題2: 入力値の検証とエラーハンドリングの方法。

実験タスク 5

#include <stdio.h>

int main() {
    int side1, side2, side3;

    while (scanf("%d %d %d", &side1, &side2, &side3) != EOF) {
        if (side1 + side2 <= side3 || side1 + side3 <= side2 || side2 + side3 <= side1) {
            printf("三角形を構成できません\n");
        } else {
            if (side1 == side2 && side2 == side3) {
                printf("正三角形\n");
            } else if (side1 == side2 || side1 == side3 || side2 == side3) {
                printf("二等辺三角形\n");
            } else if (side1 * side1 + side2 * side2 == side3 * side3 ||
                       side1 * side1 + side3 * side3 == side2 * side2 ||
                       side2 * side2 + side3 * side3 == side1 * side1) {
                printf("直角三角形\n");
            } else {
                printf("一般三角形\n");
            }
        }
    }

    return 0;
}

問題1: 三角形の条件判定における不等式の検証方法。

問題2: 直角三角形の判定におけるピタゴラスの定理の適用。

実験タスク 6

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void) {
    int userGuess, luckyDay;
    int remainingAttempts = 3;

    srand(time(NULL));
    luckyDay = rand() % 30 + 1;

    printf("2026年4月のラッキーデーを当ててください(1~30の範囲)\n");
    printf("チャンスは3回です。開始します。\n");

    while (remainingAttempts > 0) {
        scanf("%d", &userGuess);

        if (userGuess == luckyDay) {
            printf("正解!おめでとうございます!\n");
            break;
        } else if (userGuess < luckyDay) {
            printf("小さすぎます。もう一度試してください。\n");
        } else {
            printf("大きすぎます。もう一度試してください。\n");
        }

        remainingAttempts--;
        if (remainingAttempts > 0) {
            printf("残り %d 回のチャンス。再度入力してください: ", remainingAttempts);
        }
    }

    if (remainingAttempts == 0) {
        printf("チャンスがなくなりました。ラッキーデーは%d日でした。\n", luckyDay);
    }

    return 0;
}

問題1: 乱数生成とユーザー入力の比較処理の実装。

問題2: チャンス制限を含むループ処理の設計。

タグ: C random-number-generation input-output conditional-statements floating-point

5月17日 08:04 投稿