实验2

#define _CRT_SECURE_NO_WARNINGS 1
#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("20240042%04d\n", number);
    }

    system("pause");
    return 0;
}

image

 

任务 1 问题答案

 
  1. 程序功能:随机生成 5 个 1~100 的整数,拼接成学员编号 20240042xxxx 并输出。
  2. line13 功能:生成 1~100 之间的随机整数。
  3. %04d 作用:输出占 4 位,不足 4 位前面补 0,保证编号格式统一。
  4. srand (time (0)) 作用:设置随机种子,让每次运行结果不同;去掉后每次运行随机数都一样。
 
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<stdlib.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");
    system("pause");
    return 0;
}

 

 
 
 
 

image

 问题 1 

去掉 total_price=0; 会使总价累加,多次购买金额计算错误。

 问题 2 
continue 结束本次循环,直接回到循环开头重新执行。
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<stdlib.h>
int main() {
    char ch;
    while (scanf("%c", &ch) != EOF) {
        if (ch == '\n') continue;
        if (ch == 'r' || ch == 'R')
            printf("stop!\n");
        else if (ch == 'g' || ch == 'G')
            printf("go go go\n");
        else if (ch == 'y' || ch == 'Y')
            printf("wait a minute\n");
        else
            printf("something must be wrong...\n");
    }
    system("pause");
    return 0;
}

image

 

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<stdlib.h>
int main() {
    double money, sum = 0.0;
    double max = 0.0, min = 99999.0;
    printf("输入今日开销,直到输入-1终止:\n");
    while (1) {
        scanf("%lf", &money);
        if (money == -1) break;
        if (money <= 0 || money > 20000) continue;
        sum += money;
        if (money > max) max = money;
        if (money < min) min = money;
    }
    printf("今日累计消费总额:%.1f\n", sum);
    printf("今日最高一笔开销:%.1f\n", max);
    printf("今日最低一笔开销:%.1f\n", min);
    system("pause");
    return 0;
}

 

image

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<stdlib.h>
int main() {
    int a, b, c;
    while (scanf("%d%d%d", &a, &b, &c) != EOF) {
        if (a + b <= c || a + c <= b || b + c <= a) {
            printf("不能构成三角形\n");
            continue;
        }
        if (a == b && b == 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 if (a == b || a == c || b == c)
            printf("等腰三角形\n");
        else
            printf("普通三角形\n");
    }
    system("pause");
    return 0;
}

 

image

 

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    srand(time(0));
    int lucky = rand() % 30 + 1;
    int guess, cnt = 0;
    int win = 0;
    printf("猜猜2026年4月哪一天是你的luckyday\n");
    printf("开始喽,你有3次机会,猜吧(1~30):");
    while (cnt < 3) {
        scanf("%d", &guess);
        cnt++;
        if (guess == lucky) {
            printf("哇,猜中了:)\n");
            win = 1;
            break;
        } else if (guess > lucky) {
            printf("你猜的日期晚了,你的lucky day在前面哦\n");
        } else {
            printf("你猜的日期早了,你的lucky day还没到呢\n");
        }
        if (cnt < 3)
            printf("再猜(1~30):");
    }
    if (!win) {
        printf("次数用光啦。4月你的luckyday是%d号\n", lucky);
    }
    system("pause");
    return 0;
}

 

image

 

posted @ 2026-04-07 22:30  刺客伍六柒(上学ing)  阅读(2)  评论(0)    收藏  举报