实验2

实验1

代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4   #define N 5
 5   int main() 
 6 {
 7   int number;
 8   int i;
 9   srand(time(0)); // 以当前系统时间作为随机种子
10   for(i = 0; i < N; ++i) {
11   number = rand() % 100 + 1;
12   printf("20490042%04d\n", number);
13 }
14 return 0;
15 }

 

运行结果截图

1

 

问题回答

1.生成随机5个学员的编号

2.使生成1~100以内的随机数

3.使生成的随机数按四位格式生成,不足补0

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         if (choice < 1 || choice > 4){
20             printf("无效的饮料编号,请重新输入。\n");
21             continue;
22         }
23         printf("请输入购买的数量");
24         scanf("%d",&quantity);
25 
26         if (quantity < 0){
27             printf("购买数量不能为负数");
28             continue;
29         }
30         if (choice == 1 || choice == 2)
31             total_price += 3 * quantity;
32         else if (choice == 3)
33             total_price += 5 * quantity;
34         else
35             total_price += 2 * quantity;
36         printf("请投入金额; ");
37         scanf("%f",&amount_paid);
38 
39         change = amount_paid - total_price;        
40         printf("本次购买总价: %2.f 元\n",total_price);
41         printf("找零:%.2f 元\n",change);
42  
43         total_price = 0;
44  
45       }
46      printf("感谢你的购买,欢迎下次光临!\n");
47      
48      return 0;
49  }

 

运行结果截图

去除前

屏幕截图 2026-04-01 134441

 

去除后

屏幕截图 2026-04-01 134501

 

问题回答

1.line47(手输导致行数对应不太一致,问题中line47对应我输入的代码中的line43)使代码计算一次后将总价清零,若去除会导致下一次的总价会在之前的总价上进行求和

2.跳出当前循环的剩余部分,并进行下一次循环

实验3

代码

 1 #include <stdio.h>
 2 
 3 int main()
 4 {
 5     char color;
 6     printf("请输入交通灯颜色(r/g/y),按 CTRL+Z 回车结束:\n");
 7     while (scanf(" %c", &color) != EOF)
 8     {
 9         switch (color)
10         {
11             case 'r':
12             case 'R':
13                 printf("stop!\n");
14                 break;
15             case 'g':
16             case 'G':
17                 printf("go go go\n");
18                 break;
19             case 'y':
20             case 'Y':
21                 printf("wait a minute\n");
22                 break;
23             default:
24                 printf("something must be wrong...\n");
25                 break;
26         }
27     }
28 
29     return 0;
30 }

 

运行结果截图

屏幕截图 2026-04-13 194943

 

实验4

代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 int main()
 4 {
 5     double n;
 6     double t = 0;
 7     double max = 0;
 8     double min = 0;
 9     int count = 0;
10 
11     printf("输入今日开销,直到输入-1终止\n");
12 
13     while (1)
14     {
15         scanf("%lf", &n);
16 
17         if (n == -1)
18             break;
19 
20         t += n;
21 
22         if (count == 0)
23         {
24             max = min = n;
25         }
26         else
27         {
28             if (n > max)
29                 max = n;
30             if (n < min)
31                 min = n;
32         }
33         count++;
34     }
35     printf("今日累计消费总额:%.1f\n", t);
36     printf("今日最高一笔开销:%.1f\n", max);
37     printf("今日最低一笔开销:%.1f\n", min);
38 
39     system("pause");
40     return 0;
41 }

 

运行结果截图

屏幕截图 2026-04-13 200712

 

实验5

代码

 1 #include <stdio.h>
 2 
 3 int main()
 4 {
 5     int a, b, c;
 6 
 7     printf("请输入三角形三边边长:\n");
 8 
 9     while (scanf("%d %d %d", &a, &b, &c) != EOF)
10     {
11         if (a + b > c && a + c > b && b + c > a)
12         {
13             if (a == b && b == c)
14             {
15                 printf("等边三角形\n");
16             }
17             else if (a == b || a == c || b == c)
18             {
19                 printf("等腰三角形\n");
20             }
21             else if (a * a + b * b == c * c ||a * a + c * c == b * b ||b * b + c * c == a * a)
22             {
23                 printf("直角三角形\n");
24             }
25             else
26             {
27                 printf("普通三角形\n");
28             }
29         }
30         else
31         {
32             printf("不能构成三角形\n");
33         }
34     }
35 
36     return 0;
37 }

 

运行结果截图

屏幕截图 2026-04-13 202229

 

实验6

代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 
 5 int main()
 6 {
 7     int d;
 8     int n;
 9     int i;
10 
11     srand((unsigned int)time(NULL));
12 
13     d = rand() % 30 + 1;
14 
15     printf("猜测2026年4月哪一天是你的lucky day\n");
16 
17     printf("开始咯,你有3次机会,猜吧(1~30):");
18 
19     for (i = 0; i < 3; i++)
20     {
21         scanf("%d", &n);
22 
23         if (n == d)
24         {
25             printf("哇,猜中了: )\n");
26             return 0;
27         }
28         else if (n > d)
29         {
30             printf("你猜的日期晚了,你的lucky day在前面哦\n");
31         }
32         else
33         {
34             printf("你猜的日期早了,你的lucky day还没到呢\n");
35         }
36         if (i < 2)
37         {
38             printf("再猜(1~30):");
39         }
40     }
41     printf("次数用光啦。4月你的lucky day是%d号\n", d);
42 
43     system("pause");
44     return 0;
45 }

 

运行结果截图

 屏幕截图 2026-04-13 204143

 

posted @ 2026-04-13 20:43  AmanoTook0  阅读(3)  评论(0)    收藏  举报