实验2

  • 任务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("20490042%04d\n", number);
	}

	return 0;

}

屏幕截图 2026-04-07 232344

  • 问题1
    答:随机抽取4个学号。

  • 问题2
    答:抽取一个1到100的随机整数。

  • 问题3
    答:不足四位数的前面补零,使输出的number格式一致,让学号位数正确。

  • 问题4
    答:让每一次运行结果随机,避免和上一次结果一致。

  • 任务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;
}

屏幕截图 2026-04-07 232609

  • 问题1
    答:如果去掉,total_price不会归零,下个顾客购买的时候会在价格上加上上一次购买记录中的未归零的total_price,则机器上显示的需付金额错误;

  • 问题2
    答:使目前的程序继续重新运行。

  • 任务3

点击查看代码
#include <stdio.h>
int main() {
	char ch;
	while (1) {
		ch = getchar();
		if (ch == 'r')
			printf("stop!\n");
		else if (ch == 'g')
			printf("go go go\n");
		else if (ch == 'y')
			printf("wait a minute\n");
		else if (ch == '\n')
			continue;
		else
			printf("something must be wrong...\n");
		
	}

	return 0;
}
运行结果

屏幕截图 2026-04-07 201742

  • 任务4
点击查看代码
#include <stdio.h>
int main() {
	double exp, total = 0.0;
	double max, min;
	int first = 1;
	printf("输入今日开销,直到输入-1终止:\n");
	while (1) {
		scanf_s("%lf", &exp);//不用加\n
		if (exp == -1)
			break;
		if (exp <= 0 || exp > 20000) {
			printf("输入无效,请输入0 < 金额 <= 20000\n");
			continue;
		}
		if (first) {
			max = exp;
			min = exp;
			first = 0;
		}
		else {    
			if (exp > max) {
				max= exp;
			}
			if (exp < min) {
				min = exp;
			}
		}
		total += exp;
	}
	printf("今日累计消费总额:%.1f\n", total);
	printf("今日最高一笔开销:%.1f\n", max);
	printf("今日最低一笔开销:%.1f\n", min);
	return 0;
}

屏幕截图 2026-04-07 231019

  • 任务5
点击查看代码
#include <stdio.h>

int main() {
    int a, b, c;
    while (scanf_s("%d %d %d", &a, &b, &c) != EOF) {
        int temp;
        if (a > b) { temp = a; a = b; b = temp; }
        if (b > c) { temp = b; b = c; c = temp; }
        if (a > b) { temp = a; a = b; b = temp; }

        if (a + b <= c || a <= 0 || b <= 0 || c <= 0) {
            printf("不能构成三角形\n");
        }
        else {
            if (a == b && b == c) {
                printf("等边三角形\n");
            }
            else if (a == b || b == c || a == c) {
                if (a * a + b * b == c * c) {
                    printf("直角三角形\n");
                }
                else {
                    printf("等腰三角形\n");
                }
            }
            else if (a * a + b * b == c * c) {
                printf("直角三角形\n");
            }
            else {
                printf("普通三角形\n");
            }
        }
    }
    return 0;
}

屏幕截图 2026-04-07 231434

  • 任务6
点击查看代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    srand((unsigned int)time(NULL));
    int lucky = rand() % 30 + 1;
    int guess, i;

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

    for (i = 0; i < 3; i++) {
        if (i == 0)
            printf("开始喽,你有3次机会,猜吧(1~30):");
        else
            printf("再猜(1~30):");
        scanf_s("%d", &guess);

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

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

屏幕截图 2026-04-07 232025

image

posted @ 2026-04-07 23:28  大咸鱼半条  阅读(1)  评论(0)    收藏  举报