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

截图

捕获

问题

1.生成4个204900420001~204900420100之间的随机整数

2.得到一个1到100之间的整数

3.作用为一整数输出,并且占4个字符宽度

4.以当前系统时间为随机种子,为rand设置随机种子,即使代码拥有随机性。如果去掉这行代码,则会导致每次生成的随机数都一样

 

实验任务2

源代码

 1 #include <stdio.h>
 2 
 3 int main(){
 4     
 5     int choice, quantity;
 6     float total_price = 0, amount_paid, change;
 7     
 8     while(1){
 9         printf("\n自动饮料售卖菜单:\n");
10         printf("1. 可乐 - 3 元/瓶\n");
11         printf("2. 雪碧 - 3 元/瓶\n");
12         printf("3.橙汁 - 5 元/瓶\n");
13         printf("4. 矿泉水 - 2 元/瓶\n");
14         printf("0. 退出购买流程\n");
15         printf("请输入饮料编号:");
16         scanf("%d", &choice);
17         
18         if(choice == 0)
19             break;
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     
53 }

 

截图

2

问题

1.如果去掉line47,会让找零数字出现错误,即total_price的值是上一次计算的值加上这次计算的结果

2.两次都是为使购买人在输入无效数据后仍能继续输入,而不是直接结束,即结束本次循环开始下一次循环。

 

实验任务3

运行代码

 1 #include <stdio.h>
 2 
 3 int main() {
 4     char ch;
 5     
 6     while((ch = getchar()) != EOF){
 7     
 8     if(ch == '\n')
 9         continue;
10         
11     else{
12     switch(ch){
13         case 'y':printf("wait a minute\n");continue;
14         case 'r':printf("stop!\n");continue;
15         case 'g':printf("go go go\n");continue;
16         default:printf("something must be wrong...\n");
17     }
18 }
19 
20 }
21 
22     return 0;
23 }

 

运行截图

屏幕截图 2026-04-05 193316

 

 

 实验任务4

源代码

 1 #include <stdio.h>
 2 
 3 int main() {
 4     double x, y, z, t;
 5     t = 0;
 6     y = 0;
 7     z = 20000;
 8     printf("输入今日开销,直到输入-1为止:\n");
 9     
10     
11     while(1){
12         scanf("%lf", &x);
13 
14         if(x == -1)
15             break;
16         if(x > 0 && x < 20000){
17             t += x;
18         if(x > y)
19             y = x;
20         if(x < z)
21             z = x;}
22 }
23 
24         printf("今日累计消费:%.1f\n", t);
25         printf("今日累最高一笔开销:%.1f\n", y);
26         printf("今日累最低一笔开销:%.1f\n", z);
27 
28     return 0;
29 }

 

运行截图

屏幕截图 2026-04-05 230801

 

实验任务5

源代码

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

 

运行截图

屏幕截图 2026-04-06 104845

 

实验任务6

源代码

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

 

运行截图

屏幕截图 2026-04-06 125200

 

posted @ 2026-04-06 12:54  Lei_YY  阅读(11)  评论(0)    收藏  举报