実験任務1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define STUDENT_COUNT 5
int main() {
int id_value;
int counter;
srand(time(NULL));
for (counter = 0; counter < STUDENT_COUNT; counter++) {
id_value = rand() % 100 + 1;
printf("20240042%04d\n", id_value);
}
system("pause");
return 0;
}
質問回答:
- 13行目で1~100の整数乱数を生成しています。
- %04dフォーマット指定子は4桁整数を表示し、桁不足時は先頭を0で埋めます。
- 202400420001~202400420100の範囲で5つの学籍番号を生成します。
実験任務2
#include <stdio.h>
int main() {
int menu_option;
int order_quantity;
float total = 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", &menu_option);
if (menu_option == 0)
break;
if (menu_option < 1 || menu_option > 4) {
printf("無効な選択です。再度入力してください。\n");
continue;
}
printf("数量を入力: ");
scanf("%d", &order_quantity);
if (order_quantity < 0) {
printf("数量は0以上でなければなりません。\n");
continue;
}
switch (menu_option) {
case 1:
case 2:
total += 3.0f * order_quantity;
break;
case 3:
total += 5.0f * order_quantity;
break;
case 4:
total += 2.0f * order_quantity;
break;
}
printf("支払額を入力: ");
scanf("%f", &payment);
refund = payment - total;
printf("合計金額: %.2f円\n", total);
printf("お釣り: %.2f円\n", refund);
total = 0.0f;
}
printf("ご購入ありがとうございました。\n");
return 0;
}
質問回答:
- totalを初期化しないと累積計算が発生します。
- breakはループを完全終了、continueは現在のループ処理をスキップします。
- 入力検証は必須です。無効値を適切に処理する必要があります。
実験任務3
#include <stdio.h>
#include <stdlib.h>
int main() {
char input;
while (scanf("%c", &input) != EOF) {
getchar();
switch (input) {
case 'r':
printf("停止\n");
break;
case 'g':
printf("走行\n");
break;
case 'y':
printf("一時停止\n");
break;
default:
printf("エラー:無効な入力\n");
}
}
system("pause");
return 0;
}
実験任務4
#include <stdio.h>
#include <stdlib.h>
int main() {
double total = 0.0, min_val = 20000.0, max_val = 0.0, amount;
printf("今日の支出を入力(-1で終了)\n");
while (1) {
scanf("%lf", &amount);
if (amount == -1.0) break;
total += amount;
if (amount < min_val) min_val = amount;
if (amount > max_val) max_val = amount;
}
printf("合計支出: %.1f\n", total);
printf("最大支出: %.1f\n", max_val);
printf("最小支出: %.1f\n", min_val);
system("pause");
return 0;
}
実験任務5
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int lucky_day, attempt, guess;
srand(time(NULL));
lucky_day = rand() % 30 + 1;
printf("あなたのラッキーデーを当ててください\n");
printf("3回のチャンスがあります。予想してください: ");
for (attempt = 1; attempt <= 3; attempt++) {
scanf("%d", &guess);
if (guess < lucky_day) {
printf("予想が早すぎます。まだ日付が来ていません。\n");
if (attempt == 3)
printf("チャンス終了。正解は%dです。\n", lucky_day);
else
printf("もう一度予想してください: ");
continue;
}
if (guess > lucky_day) {
printf("予想が遅すぎます。日付は前のほうです。\n");
if (attempt == 3)
printf("チャンス終了。正解は%dです。\n", lucky_day);
else
printf("もう一度予想してください: ");
continue;
}
if (guess == lucky_day) {
printf("正解です!\n");
break;
}
}
system("pause");
return 0;
}
実験任務6
#include <stdio.h>
#include <stdlib.h>
int main() {
int rows, row_index, col_index, element_count;
printf("行数を入力: ");
scanf("%d", &rows);
for (row_index = 0; row_index < rows; row_index++) {
element_count = (rows - row_index) * 2 - 1;
for (col_index = 0; col_index < row_index; col_index++) {
printf(" \t");
}
for (col_index = 0; col_index < element_count; col_index++) {
printf(" O \t");
}
printf("\n");
for (col_index = 0; col_index < row_index; col_index++) {
printf(" \t");
}
for (col_index = 0; col_index < element_count; col_index++) {
printf("<H>\t");
}
printf("\n");
for (col_index = 0; col_index < row_index; col_index++) {
printf(" \t");
}
for (col_index = 0; col_index < element_count; col_index++) {
printf("I I\t");
}
printf("\n\n");
}
system("pause");
return 0;
}