实验二

任务一

#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);
}
system("pause");
return 0;
}

屏幕截图 2026-04-07 205159

 

1功能:生成5个1-100的随机数,按20490042xxxx格式输出学号。
2第13行:生成1到100之间的随机整数。
3%04d:输出4位数字,不足4位前面补0。
4srand(time(0)):设置随机种子,让每次运行结果不一样。

任务二

#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;
        }

        if(choice == 1 || choice == 2)
            total_price += 3 * quantity;
        else if(choice == 3)
            total_price += 5 * quantity;
        else
            total_price += 2 * quantity;

        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;
}

屏幕截图 2026-04-07 210515

 

1:line47 total_price = 0; 去掉的影响
total_price 用来累计本次购买的总价。如果去掉这行,下一次购买时,总价会在上一次的基础上继续累加,导致金额计算错误。
2:continue 语句的语义
在 while 循环中,continue 的作用是:跳过本次循环剩余的代码,直接回到循环开头,开始下一次循环。

任务三

#include <stdio.h>

int main() {
    // 定义变量存储颜色字符
    char color;

    // 循环输入判断
    while (1) {
        printf("请输入红绿灯颜色(r/g/y): ");
        scanf("%c", &color);
        // 吸收缓冲区多余的回车
        getchar();

        if (color == 'r') {
            printf("stop!\n");
        } else if (color == 'g') {
            printf("go go go\n");
        } else if (color == 'y') {
            printf("wait a minute\n");
        } else {
            printf("input error\n");
        }
    }

    return 0;
}

屏幕截图 2026-04-07 211557

任务四

#include <stdio.h>

int main() {
    double cost, total = 0, max_cost, min_cost;
    int count = 0;

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

    while (1) {
        scanf("%lf", &cost);
        if (cost == -1)
            break;

        if (count == 0) {
            max_cost = cost;
            min_cost = cost;
        }

        total += cost;
        if (cost > max_cost)
            max_cost = cost;
        if (cost < min_cost)
            min_cost = cost;

        count++;
    }

    printf("今日累计消费总额: %.1f\n", total);
    printf("今日最高一笔开销: %.1f\n", max_cost);
    printf("今日最低一笔开销: %.1f\n", min_cost);
    system("pause");
    return 0;
}

屏幕截图 2026-04-07 212045

 

任务五

#include <stdio.h>

int main() {
    int a, b, c, t;
    // 循环读取多组输入,直到Ctrl+Z结束
    while (scanf("%d %d %d", &a, &b, &c) != EOF) {
        // 先排序,保证a<=b<=c,方便后续判断
        if (a > b) { t = a; a = b; b = t; }
        if (b > c) { t = b; b = c; c = t; }
        if (a > b) { t = a; a = b; b = t; }

        // 判断能否构成三角形:两边之和大于第三边
        if (a + b <= c) {
            printf("不能构成三角形\n");
            continue;
        }

        // 判断等边三角形
        if (a == b && b == c) {
            printf("等边三角形\n");
        }
        // 判断等腰三角形
        else if (a == b || b == c) {
            printf("等腰三角形\n");
        }
        // 判断直角三角形:勾股定理
        else if (a * a + b * b == c * c) {
            printf("直角三角形\n");
        }
        // 其余为普通三角形
        else {
            printf("普通三角形\n");
        }
    }
    system("pause");
    return 0;
}

屏幕截图 2026-04-07 212402

 

任务六

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

int main() {
    int lucky_day, guess, chance = 3;

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

    printf("猜猜2026年4月哪一天是你的lucky day\n");

    while (chance > 0) {
        printf("开始喽,你有%d次机会,猜吧(1~30): ", chance);
        scanf("%d", &guess);

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

        chance--;
    }

    printf("次数用光啦。4月你的lucky day是%d号\n", lucky_day);
    system("pause");
    return 0;
}

屏幕截图 2026-04-07 212706

 

屏幕截图 2026-04-07 212743

 

posted @ 2026-04-07 21:28  王前锦  阅读(2)  评论(0)    收藏  举报