实验作业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   return 0;
25 }
View Code

运行1

运行1(2)

运行2

运行2(2)

作用:调节seed的值;

功能:随机生成不重复的学生编号。

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

捕获

去掉total_price = 0,会使程序报错,因为后续对total_price 进行运算但total_price没有定值;

continue的作用为跳过本循环剩余语句,重新输入编号和数量。

实验3

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

3

实验4

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 int main()
 4 {
 5     float money,max,min,sum;
 6     max = sum = 0.0;
 7     min = 20000.0;
 8     printf("请输入今日开销,知道输入-1终止:\n");
 9     while (scanf("%f", &money),money!=-1)
10     {
11         sum += money;
12         if (max < money)
13             max = money;
14         if (min > money)
15             min = money;
16 
17     }
18     printf("今日累计消费总额:%.1f\n",sum);
19     printf("今日最高一笔开销:%.1f\n",max);
20     printf("今日最低一笔开销:%.1f\n", min);
21     return 0;
22 }
View Code

 

 QQ20251014-151118

实验5

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

QQ20251014-153340

实验6

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

QQ20251014-161157

 

posted @ 2025-10-14 16:13  哈气的大猫  阅读(12)  评论(1)    收藏  举报