C 言語における基本的な制御構文の実践アプリケーション開発

乱数のシードとデータ累積の管理

ソフトウェア開発において、非一貫的なデータ生成や総和計算は頻出する要件です。以下に示す実装では、プログラム実行毎に異なる結果を得るためにシード値を使用し、かつループ内の累積ロジックを整理しています。変数を適切に初期化することで、予期せぬ挙動を防ぎます。

#include <stdio.h>
#include <stdlib.h>
int main() {
    int value;
    // シードを設定してランダム性を確保
    srand(time(NULL)); 
    
    printf("ランダム数列の生成例:\n");
    for(int i=0; i<5; i++) {
        value = rand();
        printf("%d ", value);
    }
    
    // 総和計算の例
    int total = 0;
    int count = 3;
    while(count--) {
        total += value; 
    }
    printf("\n合計値:%d\n", total);
    return 0;
}

ストリーミング入力に基づく状態遷移システム

交通信号などの簡易的なステートマシンを作成する際は、標準入力からの文字読み取りと条件分岐を組み合わせて実現します。ここでは一度の入力で現在の状態を判定し、適切なメッセージを出力する構造体を採用しています。

#include <stdio.h>
int main() {
    char status_char;
    printf("信号を入力してください (r/g/y /q 終了)");
    
    while(1) {
        scanf("%c", &status_char);
        
        if(status_char == 'q') break;
        
        switch(status_char) {
            case 'r':
                printf("停車してください\n");
                break;
            case 'g':
                printf("進行可能です\n");
                break;
            case 'y':
                printf("注意して待機\n");
                break;
            default:
                printf("不明な信号です\n");
                break;
        }
        // バッファクリアのため改行読み取りなどを実装する場合がある
        getchar(); 
    }
    return 0;
}

支出記録の最小・最大・合計計算機能

財務データの分析においては、入力された金額リストから特定のパラメータ(合計額、最高額、最低額)を抽出する必要があります。負の値が終了トリガーとして機能するため、入力処理の順序に注意しながらロジックを実装します。

#include <stdio.h>
int main() {
    double amount, accumulated_total = 0;
    double max_val = 0;
    double min_val = 100000; // 基準値設定
    
    printf("支出を入力 (-1 で終了):");
    
    while(1) {
        scanf("%lf", &amount);
        if(amount == -1) break;
        
        accumulated_total += amount;
        if(max_val < amount) max_val = amount;
        if(min_val > amount) min_val = amount;
    }
    
    printf("合計: %.1f\n", accumulated_total);
    printf("最大: %.1f\n", max_val);
    printf("最小: %.1f\n", min_val);
    return 0;
}

幾何学的形状の分類アルゴリズム

3 つの辺の長さが与えられた際、三角形が成立するか、そしてどのような種類であるかを判定するロジックです。三角不等式とピタゴラスの定理を組み合わせて包括的な分類を行います。

#include <stdio.h>
int main() {
    int s1, s2, s3;
    while(scanf("%d %d %d", &s1, &s2, &s3) == 3) {
        if((s1 + s2 <= s3) || (s1 + s3 <= s2) || (s2 + s3 <= s1)) {
            printf("三角形を形成できません\n");
        } else if(s1*s1 + s2*s2 == s3*s3 || s1*s1 + s3*s3 == s2*s2) {
            printf("直角三角形です\n");
        } else if(s1 == s2 && s2 == s3) {
            printf("正三角形です\n");
        } else if(s1 == s2 || s2 == s3 || s1 == s3) {
            printf("二等辺三角形です\n");
        } else {
            printf("一般的な三角形です\n");
        }
    }
    return 0;
}

確率に基づいた推測ゲームの実装

ユーザーとのインタラクティブな対話を行うため、乱数を用いたターゲット日付の設定と制限付きトライアル機能を提供します。リトライ回数と成否のフィードバックによってゲームバランスを整えます。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
    int target_day;
    int attempt = 0;
    const int MAX_TRIES = 3;
    int user_guess;
    
    srand((unsigned int)time(NULL));
    target_day = rand() % 30 + 1;
    
    printf("運命の日を当ててください (1〜30 の数字):\n");
    
    while(attempt < MAX_TRIES) {
        printf("残りのチャンス:%d 予測: ", MAX_TRIES - attempt);
        if(scanf("%d", &user_guess) != 1) break;
        
        attempt++;
        if(user_guess == target_day) {
            printf("的中しました!\n");
            return 0;
        } else if(user_guess > target_day) {
            printf("もっと低い数字を狙いましょう\n");
        } else {
            printf("もっと高い数字を予想してください\n");
        }
    }
    printf("ゲーム終了。正解は%dでした。\n", target_day);
    return 0;
}

タグ: C 言語,while ループ,if/else 分岐,数値計算,標準入力出力

5月24日 04:40 投稿