实验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 system("pause");
25 return 0;
26 
27 }

运行截图

捕获

1

2

3

问题1

使运行结果随时间有不同变化,不是一个结果

问题2

根据系统时间挑选相应的学号

试验任务2

 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 int main() {
 4 int choice, quantity;
 5 float total_price = 0, amount_paid, change;
 6 while (1) {
 7 printf("\n自动饮料售卖机菜单:\n");
 8 printf("1. 可乐 - 3 元/瓶\n");
 9 printf("2. 雪碧 - 3 元/瓶\n");
10 printf("3. 橙汁 - 5 元/瓶\n");
11 printf("4. 矿泉水 - 2 元/瓶\n");
12 printf("0. 退出购买流程\n");
13 printf("请输入饮料编号: ");
14 scanf("%d", &choice);
15 if (choice == 0)
16 break;
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   system("pause");
43 return 0;
44 }

运行截图

4

 问题1

本次购买总价会累加

问题2

结束本次循环,进行下一次循环

试验任务3

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

3

试验任务4

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 int main(){
 4     double asumption,sum=0.0,a=0.0,b=20000.0;
 5         printf("输入今日开销,直到输入-1终止");
 6     while(1){
 7         scanf("%lf",&asumption);
 8         if(asumption==-1){
 9             break;
10         }
11         sum+=asumption;
12         if(asumption>a){
13             a=asumption;
14         }
15         if(asumption<b){
16             b=asumption;
17         }
18     }
19     printf("今日累计消费总额:%.1f\n", sum);
20     printf("今日日最高一笔开销:%.1f\n", a);
21     printf("今日最低一笔开销:%.1f\n", b);
22     system("pause");
23     return 0;
24 }

运行截图

4

试验任务5

 

 1 #include <stdio.h>
 2 #include<stdlib.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 && a + c > b && b + c > a)
 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)
15             {
16                 printf("等边三角形\n");
17             }
18         else if (a == b || a == c || b == c)
19             {
20                 printf("等腰三角形\n");
21             }
22         else
23             {
24                 printf("普通三角形\n");
25             }
26         }
27         else
28             printf("不能构成三角形\n");
29     }
30     system("pause");    
31     return 0;
32 }

运行截图

5

试验任务6

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

运行截图

屏幕截图 2025-10-16 105229

屏幕截图 2025-10-16 105247

 

posted @ 2025-10-16 10:56  沈春玲  阅读(13)  评论(0)    收藏  举报