实验二

Task1:

 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 {
 9     int cnt;
10     int random_major, random_no;
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 }

 

QQ20251013-220111

 

 

 

问题一:代码srand(time(NULL))可以生成随机数据种子

问题二:生成以20256343或20256136开头的五组学号

 

 

Task2:

 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 }

QQ20251013-220718

QQ20251013-221459

 

问题一:删除后运行时购买总价会把上一次的金额算入

问题二:continue的作用是立即跳过当前循环的剩余代码,直接进入下一循环

Task3:

 1 #include <stdio.h>
 2 int main() 
 3 {
 4     char colour;
 5     printf("红绿灯颜色\n");
 6     while (1)
 7     
 8     {
 9         colour = getchar ();
10         if (colour == EOF)
11         {
12             break;
13         }
14         if (colour == '\n')
15         {
16             continue;  
17         }
18     
19         if (colour == 'r')
20         {
21             printf("stop!\n");
22 
23         }
24         else if (colour == 'g')
25         {
26             printf("go go go\n");
27         }
28         else if (colour == 'y')
29         {
30             printf("wait a minute\n");
31         }
32         else 
33         {
34             printf("something must be wrong...\n");
35         }
36 
37     }
38     
39     return 0;
40 }

QQ20251013-230320

 Task4:

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

 

QQ20251014-172348

Task5:

 

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

 QQ20251015-131829

Task6:

 

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

QQ20251015-222912

 

 

 

 

 

 

posted @ 2025-10-14 23:08  超级累的长颈鹿  阅读(2)  评论(0)    收藏  举报