Yh-c-learning

导航

 

实验任务1:

源代码:

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

运行截图:

捕获

问题回答:

问题1:随机生成5个学号(从204900420001-204900420100)

问题2:随机生成1-100的数

问题3:生成一个占4宽度位置的数,不够4位左侧补0,统一格式

问题4:以当前系统时间作为随机种子,随时间变化随机数(去掉后多次运行均为同一组数)

 

实验任务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 
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 }
View Code

运行截图:

捕获

问题回答:

问题1:购买总价不会清除,会叠加到下一次购买总价里

问题2:立即终结本次循环的执行,跳过continue之后的所有剩余代码,直接回到while循环的条件判断处,开启下一轮循环

 

实验任务3

源代码:

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

运行截图:

屏幕截图 2026-04-04 194021

 

实验任务4

源代码:

 

 1 #include <stdio.h>
 2 
 3 int main() {
 4     double expense, total = 0.0;
 5    
 6     double max_exp = 0.0, min_exp = 20000.0;
 7     
 8     int has_valid_input = 0;
 9 
10     printf("输入今日开销,直到输入-1终止:\n");
11 
12     while (1) {
13         scanf("%lf", &expense);
14 
15         
16         if (expense == -1) {
17             break;
18         }
19 
20         
21         if (expense <= 0 || expense > 20000) {
22             printf("输入无效!消费金额必须满足 0 < 金额 <= 20000,请重新输入\n");
23             continue;
24         }
25 
26        
27         total += expense;
28         has_valid_input = 1;
29 
30 
31         if (expense > max_exp) {
32             max_exp = expense;
33         }
34         if (expense < min_exp) {
35             min_exp = expense;
36         }
37     }
38 
39     
40     if (has_valid_input) {
41         printf("今日累计消费总额: %.1f\n", total);
42         printf("今日最高一笔开销: %.1f\n", max_exp);
43         printf("今日最低一笔开销: %.1f\n", min_exp);
44     } else {
45         printf("今日无有效消费记录\n");
46     }
47 
48     return 0;
49 }

 

运行截图:

屏幕截图 2026-04-04 202937

 

实验任务5

源代码:

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

运行截图:

屏幕截图 2026-04-04 204102

 

实验任务6

源代码:

 

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

 

运行截图:

屏幕截图 2026-04-04 210809

 

posted on 2026-04-06 21:47  葛益豪  阅读(4)  评论(0)    收藏  举报