实验2

任务1

源代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 
 5 #define N 5
 6 
 7 int main(){
 8     int number;
 9     int i;
10     
11     srand(time(0));
12     for(i=0;i<N;++i){
13         number = rand()%100+1;
14         printf("20490042%04d\n",number);
15     }
16     
17     return 0;
18 }
View Code

运行结果

屏幕截图 2026-04-01 204654

回答问题

1.生成5个从204900420001到204900420100的随机数

2.限定生成的随机数的后三位数字从1到100

3.确保生成的随机数的位数一致,即均在20490042的后面添上4位数字

4.确保每次运行结果的随机性,如果去掉这行代码,只有第一次运行结果是随机的,后面每一次运行的结果都与第一次一样

实验2

源代码

 1 #include<stdio.h>
 2 
 3 int main(){
 4     int choice,quantity;
 5     float total_price=0,amount_paid,change;
 6     
 7     while(1){
 8         printf("\n自动饮料贩卖机菜单:\n");
 9         printf("1.可乐 —3元/瓶\n");
10         printf("2.雪碧 —3元/瓶\n");
11         printf("3.橙汁 —5元/瓶\n");
12         printf("4.矿泉水 —2元/瓶\n");
13         printf("0.退出购买流程\n");
14         printf("请输入饮料编号:");
15         scanf("%d",&choice);
16         
17         if(choice==0) 
18         break;
19         
20         if(choice<1||choice>4){
21             printf("无效的饮料编号,请重新输入。\n");
22             continue;
23         }
24         
25         printf("请输入购买数量:");
26         scanf("%d",&quantity);
27         
28         if(quantity<0){
29             printf("购买数量不能为负数,请重新输入。\n");
30             continue;
31         }
32         
33         if(choice==1||choice==2)
34         total_price+=3*quantity;
35         else if(choice==3)
36            total_price+=5*quantity;
37         else
38         total_price+=2*quantity;
39         
40         printf("请投入金额:") ;
41         scanf("%f",&amount_paid);
42         
43         change = amount_paid-total_price;
44         printf("本次购买总价;%.2f元\n",total_price);
45         printf("找零:%.2f元\n",change);
46         
47         total_price=0;
48     }
49     
50     printf("感谢您的购买,欢迎下次光临!\n");
51     return 0;
52 }
View Code

运行结果

屏幕截图 2026-04-01 205934

回答问题

1.完成一次购买后line47的代码把总金额重置为0,如果去掉这行代码第二次购买时显示的金额是第一次总额和第二次总额的累加

2.line22的continue:当用户输入的choice不在1~4的范围内时,跳过本次循环中的余下代码,直接进入下一次循环的条件判断

   line30地continue:当用户输入的购买数量为负数时,跳过本次循环的余下代码,重新显示菜单

任务3

源代码

 1 #include<stdio.h> 
 2 
 3 int main(){
 4     char ch;
 5     while(scanf("%c",&ch)!=EOF){
 6         if(ch=='\n')continue;
 7         switch(ch){
 8         case'r':printf("stop!\n");break;
 9         case'g':printf("go go go\n");break;
10         case'y':printf("wait a minute\n");break;
11         default:printf("something must be wrong\n");break;
12     }
13     }
14     return 0;
15 }
View Code

运行结果

屏幕截图 2026-04-02 193754

任务4

源代码

 1 #include<stdio.h>
 2 
 3 int main(){
 4     float x,sum=0.0,max,min;
 5     printf("输入今日开销,直到输入-1终止\n");
 6     scanf("%f",&x);
 7     max=x;
 8     min=x;
 9     
10     while(x!=-1){
11         if(x>0&&x<20000){
12             sum+=x;
13             
14             if(x>max)
15             max=x;
16             if(x<min)
17             min=x;
18         }
19         scanf("%f",&x);
20     }
21     printf("今日累计消费总额:%.1f\n",sum);
22     printf("今日最高一笔开销:%.1f\n",max);
23     printf("今日最低一笔开销:%.1f\n",min);
24     
25     return 0;
26 }
View Code

运行结果

屏幕截图 2026-04-02 200730

任务5

源代码

 1 #include<stdio.h>
 2 
 3 int main(){
 4     int a,b,c;
 5     while(scanf("%d%d%d",&a,&b,&c)==3){
 6             if(a+b>c&&a+c>b&&b+c>a)
 7     {if(a==b&b==c)
 8       printf("等边三角形\n");
 9      else if(a==b||a==c||b==c)
10          printf("等腰三角形\n");
11        else if(a*a+b*b==c*c||b*b+c*c==a*a||a*a+c*c==b*b)
12      printf("直角三角形\n");
13       else printf("普通三角形\n");
14     }
15    else printf("不能构成三角形\n");
16   }
17   return 0;
18 }
View Code

 

运行结果

屏幕截图 2026-04-02 203732

实验6

源代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 
 5 int main(){
 6     int lucky_day,guess;
 7     int chance=3;
 8     
 9     srand(time(0));
10     lucky_day=rand()%30+1;
11     
12     printf("猜猜2026年4月哪一天是你的lucky_day\n");
13     printf("开始喽,你有3次机会,猜吧(1~30):");
14     
15     while(chance>0){
16         scanf("%d",&guess);
17         
18         if(guess==lucky_day){
19             printf("哇,猜中了:)\n");
20             return 0;
21         }else if(guess>lucky_day){
22             printf("你猜的日期晚了,你的lucky_day在前面哦\n");
23         }else{
24             if(guess<lucky_day)
25             {printf("你猜的日期早了,你的lucky_day还没到呢\n");}
26         }
27             chance--;
28             
29             if(chance>0){printf("再猜(1~30):");}
30         
31     }
32     printf("次数用光啦。四月你的lucky_day是%d号\n",lucky_day);
33     return 0;
34 } 
View Code

运行结果

屏幕截图 2026-04-02 214817

屏幕截图 2026-04-02 214737

问题

实验3源代码中的line6是我和AI交互后的结果,但我依旧不是很能理解这行代码的作用,望老师解惑。

屏幕截图 2026-04-02 215234

 

posted @ 2026-04-02 22:00  thinkbout  阅读(3)  评论(0)    收藏  举报