实验2

实验2

 

task1

include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 5
int main() {
   int number;
   int i;
   srand(time(0)); ? ? // 以当前系统时间作为随机种子
   for(i = 0; i < N; ++i) {
   number = rand() % 100 + 1;
   printf("20490042%04d\n", number);
}
  return 0;
}

line13代码功能为生成一个1~100的随机整数

格式符%04d的作用是在随机生成的整数前补0使其变成四个数字

这个程序的功能是给学生生成随机生成学号

 

task2

#include <stdio.h>
int main() {
int choice, quantity;
float total_price = 0, amount_paid, change;
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", &choice);
if (choice == 0)
break;
if (choice < 1 || choice > 4) {
printf("无效的饮料编号,请重新输入。\n");
continue;
}
printf("请输入购买的数量: ");
scanf("%d", &quantity);
if (quantity < 0) {
printf("购买数量不能为负数,请重新输入。\n");
continue;
}
switch (choice) {
case 1:
case 2:
total_price += 3 * quantity;
break;
case 3:
total_price += 5 * quantity;
break;
case 4:
total_price += 2 * quantity;
break;
}
printf("请投入金额: ");
scanf("%f", &amount_paid);
change = amount_paid - total_price;
printf("本次购买总价: %.2f 元\n", total_price);
printf("找零: %.2f 元\n", change);
total_price = 0;
}
printf("感谢您的购买,欢迎下次光临!\n");
return 0;
}

line53代码的用途是重新将变量赋值为0,去掉后下一次运算总价会被前一次影响

break和continue的区别是break会立刻跳出循环且结束循环而continue是跳过当前循环进入下一次循环

没必要加default子句,因为前面若输入无效编号会提前告诉无效饮料编号

 

task3

#include <stdio.h>

int main() {
    char input;

    printf("请输入交通信号灯颜色 (r/g/y),按 CTRL+Z 结束:\n");

    while ((input = getchar()) != EOF) {
        // 忽略换行符
        if (input == '\n') continue;

        switch (input) {
            case 'r':
                printf("stop!\n");
                break;
            case 'g':
                printf("go go go\n");
                break;
            case 'y':
                printf("wait a minute\n");
                break;
            default:
                printf("something must be wrong...\n");
                break;
        }
    }

    printf("程序结束。\n");
    return 0;
}

task4

#include <stdio.h>

int main() {
    float expense, total = 0.0, max = 0.0, min = 20000.0;

    printf("输入今日开销,直到输入-1终止:\n");

    while (1) {
        scanf("%f", &expense);

        if (expense == -1) {
            break;
        }

        total += expense;

        if (expense > max) {
            max = expense;
        }

        if (expense < min) {
            min = expense;
        }
    }

    printf("今日累计消费总额:%.1f\n", total);
    printf("今日最高一笔开销:%.1f\n", max);
    printf("今日最低一笔开销:%.1f\n", min);

    return 0;
}

task5

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

int main() {
    int lucky_day, guess, attempts = 0;

    srand(time(0));
    lucky_day = rand() % 30 + 1;

    printf("猜猜2025年4月哪一天是你的lucky day\n");
    printf("开始喽,你有三次机会,猜吧(1~30):\n");

    while (attempts < 3) {
        scanf("%d", &guess);
        attempts++;

        if (guess == lucky_day) {
            printf("哇,猜中了:-)\n");
            return 0;
        } else if (guess < lucky_day) {
            printf("你猜的日期早了,你的lucky day还没到呢\n");
        } else {
            printf("你猜的日期晚了,你的lucky day在前面哦\n");
        }

        if (attempts < 3) {
            printf("再猜(1~30):\n");
        }
    }

    printf("次数用完啦。偷偷告诉你,4月你的lucky day是%d号\n", lucky_day);
    return 0;
}

task6 

 

posted @ 2025-03-23 15:42  donk129  阅读(12)  评论(0)    收藏  举报