实验2

任务1

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

image

 

问题1:这个程序的功能是什么?
回答1:生成5个1-100的随机整数(按四位数格式,未满四位数的前面用0补齐),以“20490042”为前缀输出
问题2:解释lin13代码的功能。
回答2:随机数对100取余得到0-99随机数,再加1得到1-100中的随机数
问题3:解释line14使用格式符%04d起到什么作用。
回答3:随机整数未满四位数的前面用0补齐
问题4:代码 srand(time(0)); 起到什么作用?
回答4:让程序每次运行从随机数表的不同位置开始,产生不同的随机数序列
任务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 
47 total_price = 0;
48 }
49 
50 printf("感谢您的购买,欢迎下次光临!\n");
51 return 0;
52 }

image

 

问题1:line47代码 total_price = 0; 如果去掉,对程序有什么影响?
回答1:本次购买总价不会被清零,会不断累加,导致程序第二次及后续程序运行时总价和找零出错
问题2:while循环中,有两处使用 continue 语句。解释在循环中使用 continue 语句,语义是什么?
回答2:出现错误输入时程序不继续执行,跳过计算和支付找零的步骤,进入下一个循环让顾客重新填写
任务3
 1 #include<stdio.h>
 2 
 3 int main(){
 4     char color;
 5     while(scanf(" %c",&color)!=EOF){
 6         if(color=='r'){
 7           printf("stop!\n");}
 8         else if(color=='g'){
 9           printf("go go go\n");}
10         else if(color=='y'){
11           printf("wait a minute\n");}
12         else{
13           printf("something must be wrong...\n");}
14     }
15     return 0;
16 }

image

 任务4

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

image

 任务5

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

image

 任务6

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

image

 

image

 

posted @ 2026-04-07 22:05  悲绪  阅读(8)  评论(0)    收藏  举报