实验2

实验任务1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define N 5
#define N1 80
#define N2 35
int main(){
	int cnt;
	int random_major,random_no;

	srand(time(NULL));

	cnt=0;
	while(cnt<N){
		random_major=rand()%2;

		if(random_major){random_no=rand()%N1+1;
		printf("2025634%04d\n",random_no);
		}else{
			random_no=rand()%N2+1;
			printf("20256136%04d\n",random_no);
		}
		cnt++;
	}
	return 0;
}

  实验结论1:task1.c-

问题1:srand是随机选取数据的作用

问题2:程序的功能是生成随机数据

实验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(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;
}

  实验结论2:实验任务2task2.

问题1:会使接下来的价格连带着先前的一起进行计算。

问题2:结束当前循环,但不会结束整个循环。

实验3:

#include <stdio.h>
 int main() {
     char ch;
     while ((ch = getchar()) != EOF) {
         if (ch == '\n') {
             continue;
         }
         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 {
             printf("something must be wrong...\n");
         }
         while (getchar() != '\n');
     }
     return 0;
 }

  实验结论3:实验任务2task3.c

实验4:

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

  实验结论4:实验任务2task4.c

 

 实验任务5:

#include <stdio.h>
int main() {
    int a,b,c;
    while(scanf("%d %d %d",&a,&b,&c)!=EOF){
		if(a+b>c&&a+c>b&&b+c>a){
			if(a==b&&b==c)
			{printf("等边三角形\n");}
			else if(a==b||b==c||a==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");}
		}else
		    {printf("不能构成三角形\n");}
			
	
	}
    return 0;
}

  实验结论5:实验任务2task5.c

实验任务6:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
    srand((unsigned)time(NULL));
    int luckyday = rand() % 30 + 1;
    int guess, count = 0;
    printf("猜猜2025年11月哪一天是你的luckyday\n");
    printf("开始咯,你有三次机会,猜吧(1~30):");
    while (count < 3) {
    scanf("%d", &guess);
    count++;
    if (guess == luckyday) 
	{printf("哇,猜中了:)\n");
    break;}
	else if (guess > luckyday) 
	{printf("你猜的日期晚了,你的luckyday在前面哦\n");}
	else 
	{printf("你猜的日期早了,你的luckyday还没到哦\n");}
    if (count < 3) 
	{printf("再猜(1~30):");}
    }
    if (count == 3 && guess != luckyday)
	{printf("次数用光啦。偷偷告诉你,11月你的luckyday是%d号\n", luckyday);}
    return 0;
}

  实验结论:实验2task6.c1

实验总结:1.{}所界定的范围会影响while循环的实现

                  2.(红绿灯实验)输入r或者g时总会带着输出something must be wrong (?疑点)

posted @ 2025-10-19 00:53  Aaaaa叉少  阅读(6)  评论(0)    收藏  举报