实验2

实验任务1

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

屏幕截图 2025-10-19 152217

问题1:用处是让代码随机生成学号

问题2:程序功能是随机生成5个学号

实验任务2

#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_s("%d", &choice);
        if (choice == 0)
            break;
        if (choice < 1 || choice > 4) {
            printf("无效的饮料编号,请重新输入。\n");
            continue;
        }
        printf("请输入购买的数量: ");
        scanf_s("%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_s("%f", &amount_paid);
        change = amount_paid - total_price;
        printf("本次购买总价: %.2f 元\n", total_price);
        printf("找零: %.2f 元\n", change);
        total_price = 0;
    }
    printf("感谢您的购买,欢迎下次光临!\n");
    return 0;
}
View Code

屏幕截图 2025-10-19 153452

问题1:如果去掉程序无法运行,下面的total_price未被声明

问题2:语义是跳过当前循环的剩余部分,执行下个循环

实验任务3

#include <stdio.h>
int main()
{
    char lig;
    while (scanf_s("%c", &lig) == 1, getchar) {
        if (lig == 'r') {
            printf("stoop!\n");
        }
        else if (lig == 'y') {
            printf("wait a minute\n");
        }
        else if (lig == 'g') {
            printf("go go go\n");
        }
        else {
            printf("something must be wrong...\n");
        }
        while (getchar() != '\n');
    }
    return 0;
}
View Code

屏幕截图 2025-10-19 160104

实验任务4

#include <stdio.h>

int main() {
    double n, t = 0, max = 0, min = 20000;
    printf("输入今日开销,直到输入-1为止:\n");
    scanf_s("%lf", &n);
    while (n != -1) {
        getchar();
        t += n;
        if (max < n) max = n;
        if (min > n) min = n;
        scanf_s("%lf", &n);
    }
    printf("今日累计消费总额:%lf\n", t);
    printf("今日最高一笔开销:%lf\n", max);
    printf("今日最低一笔开销:%lf\n", min);
    return 0;
}
View Code

屏幕截图 2025-10-19 163440

实验任务5

#include <stdio.h>

int main() {
    int a, b, c;
    while (scanf_s("%d%d%d", &a, &b, &c) != EOF)
    {
        if (a >= b + c || b >= a + c || c >= a + b)
            printf("不能构成三角形\n");
        else if (a == b == c)
            printf("等边三角形\n");
        else if (a == b || b == c | c == a)
            printf("等腰三角形\n");
        else if (a * a + b * b == c * c || b * b + c * c == a * a || a * a + c * c == b * b)
            printf("直角三角形\n");
        else
            printf("普通三角形\n");
    }
    return 0;
}
View Code

屏幕截图 2025-10-19 165142

实验任务6

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
    srand(time(NULL));
    printf("猜猜2025年11月那一天使你的lucky day\n开始咯,你有三次机会,猜吧(1-30):");
    int x = 1 + rand() % 30, i = 0, a;
    while (1)
    {
        if (i == 2)
            break;
        scanf_s("%d", &a);
        getchar();
        if (a == x)
            printf("哇,猜中了\n");
        else if (a < x)
            printf("你猜的日期早了,你的lucky day还没到呢\n再猜(1-30):");
        else if (a > x)
            printf("你猜的日期晚了,你的lucky day还在前面哦\n再猜(1-30):");
        i++;
    }
    getchar();
    printf("次数用光了。偷偷告诉你,11月你的lucky day是%d号", x);
    return 0;
}
View Code

屏幕截图 2025-10-19 170936

 

posted @ 2025-10-19 17:10  ydd-  阅读(0)  评论(0)    收藏  举报