实验2 C语言分支与循环基础应用编程

实验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-14 155126

屏幕截图 2025-10-14 155142

作用:将时间作为随机数种子,每次生成不同随机数

去掉后只能生成一组随机数

功能:随机生成学号

 

实验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-14 163430

去掉后每次运行的总金额会进行叠加

不进行剩下的 直接进行下一次循环

 

实验3

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

屏幕截图 2025-10-14 170204

 

实验4

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

屏幕截图 2025-10-14 170903

 

实验5

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

屏幕截图 2025-10-14 171351

 

实验6

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

屏幕截图 2025-10-14 171624

屏幕截图 2025-10-14 171706

 

posted @ 2025-10-14 17:18  cchheenn  阅读(5)  评论(0)    收藏  举报