实验二

实验内容
任务一

  • 源代码
#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);
	}
	system("pause");
	return 0;
}
  • 结果截图
    image

  • 问题1
    随机输出5个专业学员编号

  • 问题2
    随机输出1~100的整数

  • 问题3
    将数字按4位宽度输出,不足的在数字前按0补齐

  • 问题4
    确保每次输出的随机数列都不同。如果删去则每次都输出相同的数列

任务二

  • 源代码
#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
    如果删去line47,total_price就会变成每次购买金额的总额,无法得出单次购买的金额

  • 问题2
    continue的语义是跳过当前循环的剩余代码,直接进入下次循环

任务三

  • 源代码
#include<stdio.h>
int main()
{
	char a;
	while(scanf_s("%c", &a,1)!=EOF)
	{
		if (a == '\n')
			continue;
		
		else if (a == 'r')
		{
			printf("red\n");
		}
		else if (a == 'g')
		{
			printf("green\n");
		}
		else if (a == 'y')
		{
			printf("wait a minute\n");
		}
		else
		{
			printf("somethings must be wrong\n");
		}
	}
	return 0;
}
  • 结果截图
    task3

任务四

  • 源代码
#include<stdio.h>
int main()
{
	double a, max=0, min=0,sum=0;
	while (1)
	{
		scanf_s("%lf", &a);
		if (a == -1)
			break;
		if (max < a)
			max = a;
		if(min>a || min == 0)
		
			min = a;
		sum = sum + a;
	}
	printf("sum=%.1lf\nmax=%.1lf\nmin=%.1lf", sum, max, min);
	return 0;
}
  • 结果截图
    task4

任务五

  • 源代码
#include<stdio.h>
int main()
{
	int a, b, c;
	while (scanf_s("%d%d%d", &a, &b, &c) != EOF)
	{
		if (a + b < c || a + c < b || b + c < a)
		{
			printf("不能构成三角形\n");
			continue;
		}
		else
		{
			printf("可以构成三角形\n");
		}
		
		{

			if (a == b && a == c)
			{
				printf("等边三角形\n");
			}
			else if (a == b || a == c || 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
				{
					printf("普通三角形\n");
				}
			}
			continue;
			
		}
		return 0;
	}
}

  • 结果截图
    task5

任务六

  • 源代码
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
	int number, n,count=0;

	srand(time(0));
	number = rand() % 30 + 1;

	printf("猜猜2026年四月哪一天是你的lucky day\n开始咯,你有3次机会,猜吧(0-30): ");

	while (1)
	{
		scanf_s("%d", &n);
		if(n == number)
		{
			printf("恭喜你,猜对了!\n");
			break;
		}
		else if (n > number)
		{
			printf("太大了\n再猜: ");
		}
		else
		{
			printf("太小了\n再猜: ");
		}
		count++;
		if (count == 3)
		{
			printf("很遗憾,你没有猜对,正确答案是%d\n", number);
			break;
		}
	}
	return 0;
}
  • 结果截图
    task6-1
    task6-2
posted @ 2026-04-06 14:59  草莓铣礼  阅读(7)  评论(0)    收藏  举报