实验2

实验任务1

task1.c

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

#define N 5
#define N1 80
#define N2 35
int main(){
    int cnt;
    int random_major, random_no;
     
    srand(time(NULL));
    cnt = 0;
    while(cnt < N) {
        random_major = rand() % 2;

        if(random_major){
            random_no = rand() % N1 + 1;
            printf("20256343%04d\n", random_no);
        } else {
            random_no = rand() % N2 + 1;
            printf("20256136%04d\n", random_no);
        }
        cnt++;
    }
    return 0;
}

运行结果截图

有srand(time(NULL));

屏幕截图 2025-10-13 213256

无srand(time(NULL));

屏幕截图 2025-10-13 213528

回答问题

问题1:srand 用来初始化伪随机数生成器的种子(seed),time(NULL) 返回当前时间(以秒为单位),把它当种子传给 srand,每次运行程序若时间不同就会产生不同的随机数序列 所以每次输入的时候处于不同时间 生成不同的随机数 而去掉的话 rand(0就会认为每一次都从同一个种子开始 于是反复运行生成同一组随机数

问题2:按两种专业学号前缀随机生成 5 个学号,每个学号的末尾序号在各自规定范围内并且补零到4位

实验任务2

task2.c

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

运行结果截图

屏幕截图 2025-10-13 213809

回答问题

问题1:total_price=0是在处理完每一次购买时清零total_price 不然每购买一次会一直累加total_price 像第一次花了1 第二次花了4 但程序任务第二次花了5 累加的 会导致总价的累积错误 影响每次购买流程的正确性,最终的找零也会出现错误

问题2:第一个的continue是当输入错误无效的饮料编号时 直接结束当此循环 进行下一次循环 第二个continue是当用户输入了错误的数量时 负数 就结束本次循环 进行下一次

实验任务3

task3.c

#include <stdio.h>
int main() {
    char x;
    while ((x = getchar()) != EOF) {
        if (x == '\n') continue;  
        
        if (x == 'r')
            printf("stop\n");
        else if (x == 'g')
            printf("go go go\n");
        else if (x == 'y')
            printf("wait a minute\n");
        else
            printf("something must be wrong\n");
    }
    return 0;
}

运行结果截图

屏幕截图 2025-10-13 213915

实验任务4

task4.c

#include <stdio.h>

int main() {
    double expense;      
    double max = -1;
    double min = 20001;  
    double total = 0;    
    int count = 0;       

    printf("输入今日开销,直到输入-1终止:\n");
    while (1) {
        scanf("%lf", &expense);
        
        if (expense == -1) {
            break;
        }
        
        if (expense <= 0 || expense > 20000) {
            printf("消费应在0元到2万元之间,请重新输入。\n");
            continue;
        }
        
        total += expense;
        
        if (expense > max) {
            max = expense;
        }
        
        if (expense < min) {
            min = expense;
        }
        
        count++;
    }
    
    

    if (count > 0) {
        printf("今天累计消费总额:%.1f元\n", total);
        printf("今日最高一笔开销:%.1f元\n", max);
        printf("今日最低一笔开销:%.1f元\n", min);
        
    } else {
        printf("今天没有任何开销记录。\n");
    }
    
    
    return 0;
}

运行结果截图

屏幕截图 2025-10-13 214115

实验任务5

task5.c

#include <stdio.h>
int main() {
    int a, b, c;
    while (scanf("%d %d %d", &a, &b, &c) == 3) {
        if (a + b > c && a + c > b && b + c > a)
            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 || a * a + c * c == b * b || b * b + c * c == a * a)
                printf("直角三角形\n");
            else
            printf("普通三角形\n");
        else
            printf("不能构成三角形\n");
    }
    return 0;
}

运行结果截图

屏幕截图 2025-10-13 214301

实验任务6

task6.c

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

int main() {
    srand(time(NULL));
    
    int lucky_day = rand() % 30 + 1;
    
    int guess;
    int attempts = 3;
    int guessed = 0;
    int i = 0;

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

    while (i < attempts) {
        if (i == 0) {
            printf("开始喽,你有三次机会,猜吧(1~30):");
        } else {
            printf("再猜(1~30):");
        }
        
        scanf("%d", &guess);
        
        if (guess == lucky_day) {
            printf("哇,猜中了:)\n");
            guessed = 1;
            break;
        } else if (guess < lucky_day) {
            printf("你猜的日期早了,你的luckyday还没到呢\n");
        } else {
            printf("你猜的日期晚了,你的luckyday在前面哦\n");
        }
        
        i++;
    }
    
    if (!guessed) {
        printf("次数用光啦。偷偷告诉你,11月你的lucky day是%d号\n", lucky_day);
    }
    
    return 0;
}


运行结果截图

屏幕截图 2025-10-13 214410

posted @ 2025-10-14 22:32  uggbond  阅读(6)  评论(1)    收藏  举报