实验2

1.实验任务1

源代码,及,运行结果截图

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 #define N 5
 5 int main() {
 6     int number;
 7     int i;
 8     srand(time(0));
 9     for(i = 0; i < N; ++i) {
10     number = rand() % 100 + 1;
11     printf("20490042%04d\n", number);
12     }
13     return 0;
14 }

 

{8916099B-9613-49D2-B842-9AFD27808810}

问题1:这个程序的功能是什么?答1:在20490042的固定前缀格式下输出1-100的随机数,并且随机数是固定四位,循环五次。

问题2:解释lin13代码的功能。答2:: rand() 函数为返回一个0~RAND_MAX之间的伪随机数,对100取余,将其范围控制在0-99内,再加1,则范围为1-100。

问题3:解释line14使用格式符%04d起到什么作用。答3:使生成的整数符合四位数格式,4表示总宽度为4位,0表示不足4位时用0补齐。

问题4:代码 srand(time(0)); 起到什么作用?(提示:去掉这行代码,多次运行程序,观察结果有什么特 点) 答4:多次运行程序后发现去掉代码 srand(time(0))后每次生成的“随机数”都一样,srand(unsigned int seed) 为rand()设置随机种子,通常以时间作为随机种子,是因为时间每时每刻都在变化,可以起到一个随机数的作用。

 

2.实验任务2

源代码,及,运行结果截图

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

 

{042CECAA-D01B-472C-BFF2-6DDE821D0028}

{684FA004-5C6E-4465-A930-20F73C890914}

{FF955D67-AF0B-4CEE-B301-F8A68034CC18}

问题1:line47代码 total_price = 0; 如果去掉,对程序有什么影响?答1:如果去掉,程序会重复计算,即第二个购买的人会重复付款上一个人的消费。

问题2:while循环中,有两处使用 continue 语句。解释在循环中使用 continue 语句,语义是什么?答2:使用 continue 语句会跳出本次循环,重新下一次本层循环。在本程序中,即如果输入错误的商品编号或者错误的购买数量,结束本次循环,等待下一次输入。

3.实验任务3

task3.c源代码,及,运行结果截图

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 
 4 int main()
 5 {
 6     char sign;
 7     printf("请输入交通信号灯颜色:\n");
 8     while (scanf("%c", &sign) != EOF) {
 9         /*输入r表示red,输入g表示green,输入y表示yellow*/
10         switch (sign) {
11         case 'r':
12             printf("stop!\n"); break;
13         case 'g':
14             printf("go go go\n"); break;
15         case 'y':
16             printf("wait a minute\n"); break;
17         default:
18             printf("something must be wrong...\n"); break;
19         }
20     }
21     return 0;
22 }

 

{02D01613-76F5-4309-AA4B-0CB49D78A218}

实验总结:case后面不是()而是用单引号引用字符,还有break别忘了。

 

4. 实验任务4

task4.c源代码,及,运行结果截图

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 
 4 int main()
 5 {
 6     printf("输入今日开销,直到输入-1终止:\n");
 7     double money, all_money = 0, top_money = -1, small_money = 20001;
 8     while (1)
 9     {
10         scanf("%lf", &money);
11         if (money == -1)
12             break;
13         if (money > 20000 || money < 0)
14         {
15             printf("输入金额非法,请重新输入\n");
16             continue;
17         }
18 
19         all_money = all_money + money;
20         if (money > top_money)
21             top_money = money;
22         if (money < small_money)
23             small_money = money;
24     }
25 
26     printf("今日累计消费总额:%.1f\n", all_money);
27     printf("今日最高一笔开销:%.1f\n", top_money);
28     printf("今日最低一笔开销:%.1f\n", small_money);
29 
30     return 0;
31 }

 

{50801311-21B6-4A8C-9DDC-0294E331683A}

实验总结:最大值和最小值最开始的赋值很关键,%lf和%.1f的使用需要注意

 

5. 实验任务5

task5.c源代码,及,运行结果截图

 1 #define CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 
 4 int main()
 5 {
 6     /*从键盘输入三角形三边边长a, b, c。边长为整数。
 7     输出要求:
 8     如果a, b, c不能构成三角形,打印输出“不能构成三角形”
 9     如果能构成三角形,根据边长编写条件式,判断三角形属于哪一类别:普通三角形、直角三角形、等边三角形、等腰三角形
10     要求支持多组输入,直到用户按下CTRL+Z,敲回车,终止程序。*/
11     int a, b, c;
12     printf("请输入三角形三边边长a b c:\n");
13     while (scanf("%d %d %d", &a, &b, &c) != EOF)
14     {
15         if (a + b <= c || a + c <= b || b + c <= a)
16         {
17             printf("不能构成三角形\n");
18             continue;
19         }
20         else if (a == b && b == c)
21             printf("等边三角形\n");
22         else if (a * a + b * b == c * c || b * b + c * c == a * a || a * a + c * c == b * b)
23             printf("直角三角形\n");
24         else if (a == b || b == c || c == a)
25             printf("等腰三角形\n");
26         else
27             printf("普通三角形\n");
28     }
29     return 0;
30 }

 

{5DC9B53C-2E08-4C89-AA70-3C5108747E07}

实验总结:直角三角形判断要在等腰三角形之前。

 

6. 实验任务6

task6.c源代码,及,运行结果截图

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

 

{3020D24C-A1B1-49D0-BE19-340982E953BE}

 

posted @ 2026-04-07 13:31  202283300498石欣源  阅读(7)  评论(0)    收藏  举报