实验2

任务1

代码

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

截图

捕获

回答(1~4)

  • 取五个随机数(204900420001~204900420100)
  • 取随机数并控制范围在1~100
  • 自动用0补全不足4位的数字使最后占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("%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;
}

截图

捕获1

回答(1~2)

  • 没有重置变量导致第2次起程序几乎总是算不对
  • 立即结束本次循环并根据循环条件决定是否下次循环

任务3

代码

#include <stdio.h>

int main() {
	
	for (int i = 0;i < 4;i++)
	{
		char a = getchar();
		getchar();
		switch (a)
		{
			case 'r':
				printf("%s\n", "stop!");
				break;
			case 'y':
				printf("%s\n", "wait a minute");
				break;
			case 'g':
				printf("%s\n", "go go go");
				break;
			default:
				printf("%s\n", "something must be wrong...");
				break;
		}
	}
    return 0;
}

截图

捕获2

任务4

代码

#include <stdio.h>
int main() 
{
	printf("%s\n", "输入今日开销,直到输入-1终止:");
	double max = 0;
	double min = 2e4;
	double sum = 0;
	while (1)
	{
		double f;
		scanf("%lf", &f);
		if (!(f + 1)) break;
		sum += f;
		max = max > f ? max : f;
		min = min < f ? min : f;
	}
	printf("今日累计消费总额:%.1f\n", sum);
	printf("今日最高一笔开销:%.1f\n", max);
	printf("今日最低一笔开销:%.1f\n", min);
	return 0;
}

截图

屏幕截图 2026-10-28 131057

任务5

代码

#include <stdio.h>
#include <math.h>
int main() 
{
	for (int i = 0; i < 5; i++)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		if (a + b > c && b + c > a && c + a > b)
		{
			int d = pow(a, 2) + pow(b, 2) - pow(c, 2);
			int e = pow(a, 2) - pow(b, 2) + pow(c, 2);
			int f = -pow(a, 2) + pow(b, 2) + pow(c, 2);
			if (a == b && b == c) printf("%s\n", "等边三角形");
			else if (a == b || b == c || a == c) printf("%s\n", "等腰三角形");
			else if (d * e * f == 0) printf("%s\n", "直角三角形");
			else printf("%s\n", "普通三角形");
		}
		else printf("%s\n", "不能构成三角形");
	}
	return 0;
}

截图

屏幕截图 2026-10-28 132734

任务6

代码

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

截图

屏幕截图 2026-10-28 134549

屏幕截图 2026-10-28 134639

posted @ 2026-10-28 13:48  worthF  阅读(7)  评论(0)    收藏  举报