实验二

实验一

 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 }
View Code

 

屏幕截图 2026-04-07 150952

问题一:随机抽取5个学员编号

问题二:随机生成1~100的数并赋值给number

问题三:将number位数变为四位,不足四位在前面加0

问题四:使每次输出结果不同

实验二

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

屏幕截图 2026-04-07 152703

问题一:之前的金额不会重置

问题二:跳过循环重新输入

实验三

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

屏幕截图 2026-04-07 154124

实验四

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

屏幕截图 2026-04-07 160754

实验五

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

 

屏幕截图 2026-04-07 161532

实验六

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

屏幕截图 2026-04-07 162705

 

posted @ 2026-04-07 16:28  阿狸波澜  阅读(6)  评论(0)    收藏  举报