实验2

任务1

 1  #include <stdio.h>
 2  #include <stdlib.h>
 3  #include <time.h>
 4  #define N 5
 5  #define N1 80
 6  #define N2 35
 7  int main() {
 8     int cnt;
 9     int random_major, random_no;
10     //srand(time(NULL));      
11     cnt = 0;
12     while(cnt < N) {
13         random_major = rand() % 2;
14         if(random_major) {
15             random_no = rand() % N1 + 1;
16             printf("20256343%04d\n", random_no);
17                    }
18         else {
19             random_no = rand() % N2 + 1;
20             printf("20256136%04d\n", random_no);
21         }
22         cnt++;
23     }
24     return 0;
25  }

屏幕截图 2025-10-17 172856

屏幕截图 2025-10-17 172909

 

问题1:去掉srand(time(NULL))后每次生成的都是相同的学号。srand(time(NULL))可以以当前系统时间作为随机种子,生成随机数。

问题2:该程序可以随机生成5个学号。

 

任务2

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

屏幕截图 2025-10-17 173201

屏幕截图 2025-10-17 173306

 

问题1:去掉后第二次购买的总价加上了第一次的总价,结果有误。

问题2:跳过本次循环剩下的部分,直接继续下一次循环。

 

任务3

 1 #include <stdio.h>
 2 
 3 int main() {
 4     char color;
 5     
 6 
 7     while (1) {
 8         printf("交通信号灯颜色(输入r表示red,输入g表示green, 输入y表示yellow,):\n");
 9         scanf(" %c", &color);
10 
11         if (color== EOF) {
12             printf("close\n"); 
13             break;
14         }
15 
16         if (color == 'r') {
17             printf("stop!\n");
18         } else if (color == 'g') {
19             printf("go go go\n"); 
20         } else if (color == 'y') {
21             printf("wait a minute\n"); 
22         } else {
23             printf("something must be wrong...\n");
24         }
25     }
26 
27     return 0;
28 }

屏幕截图 2025-10-17 172519

任务4

 

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

屏幕截图 2025-10-17 183438

任务5

 

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

屏幕截图 2025-10-18 100101

任务6

 

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

屏幕截图 2025-10-18 103313

屏幕截图 2025-10-18 103434

 

posted @ 2025-10-18 10:35  郭语涵  阅读(3)  评论(0)    收藏  举报