实验2

task1

  • 源代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define N 100
#define CNT 5

int main() {
    int number;
    int i;

    srand(time(0));     // 以当前系统时间作为随机种子
    for(i = 0; i < CNT; ++i) {
        number = rand() % N + 1;
        printf("20490042%04d\n", number);
    }

    return 0;
}
  • 屏幕截图
    image

  • 问题
    【1】程序的功能是模拟"随机点名"生成 5 个学号
    【2】把 rand() 产生的大范围随机数转化成 1~100 的随机整数
    【3】保证学号的位数固定为 12 位(20490042 + 4 位数字)
    【4】srand() 用来给 rand() 设置随机种子 time(0) 返回当前系统时间(从 1970-01-01 起算的秒数) 把它作为种子后 每次运行程序的时间不同 种子不同 rand() 产生的随机序列也就不同 于是每次运行得到的是不同的 5 个学号

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

        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;
}
  • 屏幕截图
    image

  • 问题
    【1】这一行的作用是在每次购买结束后把累计金额清零 使下一笔交易从 0 开始独立结算 如果去掉它 total_price 会一直保留前面所有交易累加的结果 于是第二笔及以后交易的"本次购买总价"变成多次消费之和找零 也会随之变小 甚至变成负数
    【2】立即结束本次循环 跳过循环体中 continue 之后的所有语句 直接回到循环条件判断处 开始下一次循环

task3

  • 源代码
#include <stdio.h>
int main()
{
    char light;
    int i;

    for (i = 0; i < 4; i++) {
        scanf(" %c", &light);  
        switch (light) {
        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;
        }
    }

    return 0;
}
  • 屏幕截图
    屏幕截图 2026-10-28 174349

task4

  • 源代码
#include <stdio.h>
int main()
{
    double expense;                 
    double total = 0;             
    double max = 0, min = 0.;    
    int count = 0;                 
    scanf("%lf", &expense);
    while ( expense != -1) {
        if (count == 0) {           
            max = expense;
            min = expense;
        } else {
            if (expense > max)
                max = expense;
            if (expense < min)
                min = expense;
        }
        total += expense;
        count++;
        scanf("%lf", &expense);
    }
    printf("今日累计消费总额:%.1f\n", total);
    printf("今日最高一笔开销:%.1f\n", max);
    printf("今日最低一笔开销:%.1f\n", min);

    return 0;
}
  • 屏幕截图
    image

task5

  • 源代码
#include <stdio.h>
int main()
{
    int a, b, c, t, i;

    for (i = 0; i < 5; i++) {
        scanf("%d%d%d", &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");
        else if (a == b && b == c)
            printf("等边三角形\n");            
        else if (a == b || b == c || a == c)
            printf("等腰三角形\n");
        else if (a * a + b * b == c * c)
            printf("直角三角形\n");
        else
            printf("普通三角形\n");
    }

    return 0;
}
  • 屏幕截图
    屏幕截图 2026-10-28 181354

task6

  • 源代码
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
    int lucky;
    int guess;
    int i;
    srand(time(0));
    lucky=rand()%30+1;
    printf("猜猜2026年11月哪一天是你的lucky day\n");
    printf("开始喽,你有3次机会,猜吧(1~30):");
    for (i=1;i<=3;i++){
        scanf("%d",&guess);
        if (guess == lucky) {
            printf("哇,猜中了:)\n");
            return 0;          
        } else if (guess < lucky) {
            printf("你猜的日期早了,你的lucky day还没到呢\n");
        } else {
            printf("你猜的日期晚了,你的lucky day在前面哦\n");
        }
        if(i<3)
            printf("再猜(1~30):");}
    printf("次数用光啦。11月你的lucky day是%d号\n", lucky);

    return 0;
}
  • 屏幕截图
    屏幕截图 2026-10-29 130556
posted @ 2026-10-29 13:10  莫言无泪  阅读(5)  评论(0)    收藏  举报