实验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     system("pause");
25     return 0;
26  }

捕获

 

1是随机数重新生成

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

捕获

1多次购买时总价和找零会错误

2执行if语句,继续循环

任务3

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

image

 任务4

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

image

 任务5

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

 

image

 任务6

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

 

image

 

posted @ 2025-10-14 20:50  张晨熙  阅读(10)  评论(1)    收藏  举报