Program2

任务1

任务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; //随机生成一个0到100的数字
		printf("20248367%04d\n",number); //格式符%04d:修饰符04意味着字段宽度是4,不足的地方由“0”补充
	}

	system("pause");
	return 0;
} //功能:随机输出五个学号(其中学号)

任务1截图
image

ans:
1).随机生成一个0到100的数字;
2).修饰符04代表着字段宽度是4,不足宽度由“0”在前面补充;
3).整个任务的目的是随机输出5个学号。

任务2

任务2代码

#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; //continue为结束本轮循环,开始新一轮循环;
		 }

		 switch(choice){
			 case 1:
			 case 2:
				 total_price=3*quantity;
				 break;
			 case 3:
				 total_price=5*quantity;
				 break;
			 case 4:
				 total_price= 2 * quantity;
				 break; //break为跳出switch结构,即跳出本层循环;
		 } //该switch不需要增加default子句,因其choice的取值没有其他可能性;

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

任务2截图
image

ans:
1).因为该while结构循环作为无限循环(如果不输入“0”),所以每轮循环结束前需要“清零”total_price值;

2).break为跳出switch结构,即跳出本层循环;
continue为结束本轮循环,开始新一轮循环;

3).该switch结构不需要default子句,因其choice的取值没有其他可能性。

任务3

任务3代码

#include<stdio.h>
#include<stdlib.h>

int main()
{
	int a;//getchar返回的是int类型;
	while((a=getchar())!=EOF)	
	{	switch(a)
		{
			case 'r':printf("stop!\n");break;
			case 'g':printf("go go go\n");break;
			case 'y':printf("wait a minute\n");break;
			case '\n':break;
			default:printf("something must be wrong...\n");
		}
	}
	system("pause");
	return 0;
}

任务3截图
image

任务4

任务4代码

#include<stdio.h>
#include<stdlib.h>

int main()
{

	float max,min,total,a;
	max=0.0;
	min=20000;
	total=0;
	while(1)
	{
		scanf("%f",&a);
		if(a!=-1)
			{total=total+a;
			if(a>=max)
				max=a;
			if(a<=min)
				min=a;}
		
		if(a==-1)
			break;

	}
	printf("今日累计消费总额:%.1f\n",total);
	printf("今日最高一笔开销:%.1f\n",max);
	printf("今日最低一笔开销:%.1f\n",min);

	system("pause");
	return 0;
}

任务4截图
image

任务5

任务5代码

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define N 5

int main()
{
	int num,w,i;
	i = 1;
	printf("猜猜2025年4月哪一天是你的lucky day");

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

	while(i<=3)
		{
			if(i==1)
			
				printf("开始咯,你有三次机会,猜吧:");
			else
				printf("再猜:");
				
			scanf("%d",&w);
			
			if(num==w)
				{printf("哇,猜中了:-)");break;}
			else if(w>num)
				printf("你猜的日期晚了,你的lucky day还没到呢\n");
			else
				printf("你猜的日期早了,你的lucky day还没到呢\n");

			i++;
			}
	if(num!=w)
		printf("次数用完了。偷偷告诉你,4月你的lucky day是%d\n",num);
	
	system("pause");
	return 0;
}

任务5截图
image

任务6

任务6代码

#include<stdio.h>
#include<stdlib.h>

int main()
{
	int i,n,j,t;
	int n_line=0;
	printf("请输入行数:");

	scanf("%d",&n);
	n_line=2*n-1;
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=n_line && n_line>=1;j++)
		{
			printf(" o \t");
		}
		printf("\n");
		
		for(t=1;t<i;t++)
			printf("\t");

		for(j=1;j<=n_line && n_line>=1;j++)
		{
			printf("<H>\t");
		}
		printf("\n");
		
		for(t=1;t<i;t++)
			printf("\t");

		for(j=1;j<=n_line && n_line>=1;j++)
		{
			printf("| |\t");
		}
		printf("\n");
		
		for(t=0;t<i;t++)
			printf("\t");
		
		n_line=n_line-2;
	}
	
	system("pause");
	return 0;
}

任务6截图
image

posted @ 2025-03-17 15:15  王皓惟  阅读(36)  评论(0)    收藏  举报