实验2

任务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 }

捕获

问题1:该程序的功能是生成5个1~100之间的随机整数,并以“2049004”为前缀、按4位宽度格式化输出这些随机数。

问题2:number = rand() % 100 + 1;是生成1~100范围内随机整数的核心语句。

问题3:%04d是printf的格式控制符,作用是按4位十进制整数格式化输出,不足4位时用0补前导。

问题4:设置随机数生成器的“种子”,确保每次运行程序生成的随机数序列不同。

任务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;if (choice < 1 || choice > 4) {
16       printf("无效的饮料编号,请重新输入。\n");
17       continue;
18    }
19     printf("请输入购买的数量: ");
20     scanf("%d", &quantity);
21     if (quantity < 0) {
22       printf("购买数量不能为负数,请重新输入。\n");
23       continue;
24    }
25     if(choice == 1 || choice == 2)
26       total_price += 3 * quantity;
27     else if(choice == 3)
28       total_price += 5 * quantity;
29     else
30       total_price += 2 * quantity;
31     printf("请投入金额: ");
32     scanf("%f", &amount_paid);
33     change = amount_paid - total_price;
34     printf("本次购买总价: %.2f 元\n", total_price);
35     printf("找零: %.2f 元\n", change);
36     total_price = 0;
37  }
38   printf("感谢您的购买,欢迎下次光临!\n");
39   return 0;
40 }

 

捕获

问题1:如果去掉这行代码,每次购买的总价会在上一次的基础上累加,导致单次交易的总价和找零计算错误。

问题2:continue会终止当前轮循环的剩余代码,直接进入下一轮循环,用于处理非法输入的情况,让用户重新输入。

任务3:

源代码:

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

捕获

任务4:

源代码:

 1 #include<stdio.h>
 2 int main()
 3 
 4 {
 5 
 6     printf("输入今日开销,直到输入-1为止\n");
 7     double cost,sum=0.0;
 8     double max_cost=0.0,min_cost=20000.0;
 9     int count=0;
10     while(1)
11     {
12         scanf("%lf",&cost);
13         if(cost==-1)
14         break;
15         if(cost>0&&cost<=20000)
16         {sum+=cost;
17         count++;
18         if(cost>max_cost)
19         max_cost=cost;
20         if(cost<min_cost)
21         min_cost=cost;}
22         else
23         printf("输入无效,请输入0~20000之间的金额,或-1退出\n");}
24         if(count==0)
25         {printf("今日没有有效消费记录\n");
26         }
27         else
28         {printf("今日消费总额:%.1f\n",sum);
29         printf("今日最高一笔开销:%.1f\n",max_cost);
30         printf("今日最低一笔开销:%.1f\n",min_cost);}
31 
32 
33              return 0;
34 
35 }

image

任务5

源代码:

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

image

任务6

源代码:

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

image

 

posted @ 2026-04-08 22:27  帕茹克  阅读(2)  评论(0)    收藏  举报