实验2 C语言分支与循环基础应用编程

实验任务1

task1.c源代码:

 

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

 

运行结果截图:

 

回答问题:

问题1:line14代码的功能取一个1~100的随机数赋值给number。

问题2:line15使用%04d的作用是输出一个占四位的数,左边不足的部分用0补全。

问题3:该程序功能是生成一个202400420001~202400420100之间的随机数

实验任务2

task2.c源代码:

 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         switch (choice) {
34             case 1:
35             case 2:
36                 total_price += 3 * quantity;
37                 break;
38             case 3:
39                 total_price += 5 * quantity;
40                 break;
41             case 4:
42                 total_price += 2 * quantity;
43                 break;
44         }
45 
46         printf("请投入金额: ");
47         scanf("%f", &amount_paid);
48 
49         change = amount_paid - total_price;
50         printf("本次购买总价: %.2f 元\n", total_price);
51         printf("找零: %.2f 元\n", change);
52 
53         total_price = 0;
54     }
55 
56     printf("感谢您的购买,欢迎下次光临!\n");
57     return 0;
58 }

运行结果截图:

 回答问题:

问题1:line53的用途是初始化总价使其每次都等于0,下次循环时防止上次输入的数据影响下次购买时的总价。

问题2:break的作用是跳出本层循环,去执行本层循环后的语句,在本题中即为跳出while这个循环,直接去执行line56的输出操作;而continue语句的作用是提前结束本次循环并开启新一轮循环,在本题中的作用即为当输入的数据错误时重新输入。

问题3:有必要,因为choice输入的可能值不只是数字,虽然源程序中有如果choice小于1或者大于4时会要求重新输入,但是本程序中没有考虑到其他情况,如果我choice输入的是一个字母,本程序结果就会发生错乱。

实验任务3

 task3.c源代码

 

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

 

 

 

运行结果截图

 

 

 

实验任务4

 task4.c源代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 int main()
 4 {
 5     double total,x,max,min;
 6     total=0;
 7     printf("输入今日开销,直至输入-1终止\n");
 8     scanf("%lf",&x);
 9         
10     max=0;
11     min=20000;
12     while(x!=-1)
13     {
14         if(x>20000||x<0)
15         {
16             printf("error input");
17             break;
18         }
19         total+=x;
20         if(x>max)
21             max=x;
22         if(x<min)
23             min=x;
24         scanf("%lf",&x);
25     }
26     printf("今日累计消费总额:%.1lf\n",total);
27     printf("今日最高一笔开销:%.1lf\n",max);
28     printf("今日最低一笔开销:%.1lf\n",min);
29     system("pause");
30     return 0;
31 }

运行结果截图

 

实验任务5

task5.c源代码

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

运行结果截图

实验任务6

 task6.c源代码

 

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 int main()
 4 {
 5     int n,i,j,k; 
 6     printf("input n: ");
 7     scanf("%d",&n);
 8     for(i=n;i>0;i--){
 9         for(k=0;k<=n-i;k++)
10             printf("\t");
11             
12         for(j=2*i-1;j>=1;j--)
13             printf(" O\t");
14             
15             printf("\n");
16         for(k=0;k<=n-i;k++)
17             printf("\t");
18         for(j=2*i-1;j>=1;j=j-1)
19             
20             printf("<H>\t");
21             
22             printf("\n");
23 
24         for(k=0;k<=n-i;k++)
25             printf("\t");
26         for(j=2*i-1;j>=1;j=j-1)
27             
28             printf("I I\t");
29             
30             printf("\n");
31             printf("\n");
32     }
33     system("pause");
34     return 0;
35 }

 

运行结果截图

 

实验总结
本次实验加深了我的循环语句的理解,使用for while循环语句的熟练度大大增加,以及解决了一些编程·上的问题。实验3让我理解了EOF按crtlz结束循环的原因,实验六让我熟悉了嵌套循环语句的编写,这次实验对我是一次挑战,我从而从中进步了许多,我独立编程序的能力得到了很大提升。
posted @ 2025-03-20 20:20  High_Auditorium  阅读(59)  评论(0)    收藏  举报