实验2

任务1

  • 源代码
#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;
}
  • 截图

task1
问题1:生成N(100)个1~100之间的随机整数,并且每个数字占4位,不足4位前面补0的格式并输出。
问题2:用来生成一个1到100的随机整数,并赋值给变量number.
问题3:输出一个整数,总宽度占4个字符位置;如果不足4位,左边补0
问题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;
}
  • 运行截图

task2
问题1:如果去掉,下次运行程序时会保留上次订单的结果,导致出错。
问题2:结束本轮循环剩下的语句,直接跳转到while的条件判断,开启下一轮循环。

任务3

  • 源代码
#include <stdio.h>

int main() {
   char color;
   int i;
   for(i=1;i<=4;++i)
   {
   	scanf("%c",&color);
   	getchar();
   	switch(color)
    {case 'r':
    	printf("stop!\n");
    	break;
    case 'g':
    	printf("go go go\n");
    	break;
    case 'y':
    	printf("wait a minut\n");
    	break;
    	default:
    		printf("something must be wrong...\n");
}
   }
    return 0;
}
  • 运行截图

task3

任务4

  • 源代码
#include <stdio.h>

int main() {
    float  current;
    float max = 0, min = 20000;
    float amount = 0;
    while (1)
    {
        scanf("%f", &current);
        if (current == -1)

        {
            break;
        }
        amount += current;
        if(current>=max)
        {
            max = current;
        }
        if(current<=min)
        {
            min = current;
        }
        
    }
    printf("今日累计消费总额:%.1f\n", amount);
    printf("今日最高一笔开销:%.1f\n", max);
    printf("今日最低一笔开销:%.1f\n", min);

    return 0;
}
  • 运行截图

task4

任务5

  • 源代码
#include <stdio.h>

int main(void)
{
    int a, b, c;
    for (int i = 0; i < 5; i++)
    {
        printf("请输入第%d组三角形三边长:", i + 1);
        scanf("%d %d %d", &a, &b, &c);

        if (a <= 0 || b <= 0 || c <= 0 || a + b <= c || a + c <= b || b + c <= a)
        {
            printf("不能构成三角形\n");
        }
        else
        {
            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");
            }
        }
    }
    return 0;
}

  • 运行截图

task5

任务6

  • 源代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
    int lucky_day;
    int guess;
    int chance;

    srand((unsigned)time(NULL));
    lucky_day = rand() % 30 + 1;

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

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

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

    printf("次数用光啦。11月你的lucky day是%d号\n", lucky_day);

    return 0;
}
  • 运行截图

task6

posted @ 2026-10-28 15:30  冬天不吃橘  阅读(6)  评论(0)    收藏  举报