实验2

实验任务1

源代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 
 5 #define N 5
 6 #define N1 30
 7 #define N2 35
 8 
 9 int main()
10 {
11     int cnt;
12     int random_major,random_no;
13 
14     srand(time(NULL));
15 
16     cnt=0;
17     while(cnt<N){
18         random_major = rand()%2;
19          
20         if(random_major){
21             random_no=rand()%N1+1;
22             printf("20256343%04d\n",random_no);
23         }
24         else{
25             random_no=rand()%N2+1;
26             printf("20256136%04d",random_no);
27         }
28 
29         cnt++;
30     }
31     system("pause");
32 
33     return 0;
34 }

运行结果

有l14

捕获

无l14

无l14

问题1

代码为随机生成一个数,若删去该代码,每次随机生成的数都相同

问题2

程序的功能为司机生成五个不同的学号

实验任务2

源代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main()
 5 {
 6     int choice,quantity;
 7     float total_price=0,amount_paid,change;
 8 
 9     while(1){
10         printf("\n自动饮料售卖机菜单:\n");
11         printf("1.可乐-3元/瓶\n");
12         printf("2.雪碧-3元/瓶\n");
13         printf("3.橙汁-5元/瓶\n");
14         printf("4.矿泉水-2元/瓶\n");
15         printf("0.退出购买流程\n");
16         printf("请输入饮料编号:");
17         scanf("%d",&choice);
18 
19         if(choice==0)
20             break;
21 
22         if(choice<1||choice>4){
23             printf("无效的饮料编号,请重新输入。\n");
24             continue;
25         }
26 
27         printf("请输入购买的数量:");
28         scanf("%d",&quantity);
29 
30         if(quantity<0){
31             printf("购买数量不能为负数,请重新输入。\n");
32             continue;
33         }
34         if(choice==1||choice==2)
35             total_price+=3*quantity;
36         else if(choice==3)
37             total_price+=5*quantity;
38         else
39             total_price+=2*quantity;
40 
41         printf("请输入金额:");
42         scanf("%f",&amount_paid);
43 
44         change=amount_paid-total_price;
45         printf("本次购买总价:%.2f元\n",total_price);
46         printf("找零:%.2f元\n",change);
47 
48          total_price=0;
49     }
50     printf("感谢您的购买,欢饮下次光临!\n");
51 
52     system("pause");
53 
54     return 0;
55 }

运行结果

aaa

问题1

 如果去掉,下一次运行不会重置总价

问题2

停止本次运行,开始下一次

实验任务3

源代码

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

运行结果

qaq

(搞不出来QAQ)

实验任务4

源代码

 

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

运行结果

111

实验任务5

源代码

 

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

 

运行结果

 222

实验任务6

源代码

 

 1 int main() {
 2     srand(time(NULL));
 3 
 4     int lucky_day = rand() % 30 + 1;
 5 
 6     printf("猜猜2025年11月那一天是你的lucky day:\n");
 7     printf("开始喽,你有三次机会,猜吧(1-30):\n");
 8 
 9     int a;
10     
11 
12     int cnt=0;
13 
14     while (cnt < 3)
15     {
16         scanf_s("%d", &a);
17 
18         if (a == lucky_day)
19         {
20             printf("哇,猜中了\n");
21             return 0;
22         }
23         else if (a < lucky_day)
24         {
25             printf("你猜的日子早了,你的lucky_day在后面哦\n");
26             cnt++;
27         }
28         else
29         {
30             printf("你猜的日子晚了,你的lucky_day在前面哦\n");
31             cnt++;
32         }
33         if (cnt == 3)
34         {
35             printf("次数用光了,悄悄告诉你,11月你的lucky day是%d号\n", lucky_day);
36         }
37     }
38    
39     return 0;
40 }

 

运行结果

 time

 

posted @ 2025-10-16 13:05  辣椒酱拌芥末  阅读(5)  评论(0)    收藏  举报