实验2

实验2

task1

程序:

 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 system("pause");
14 return 0;
15 }

运行结果:

ans1:功能是生成一个1到100(包含1和100)之间的随机整数,并将其赋值给变量number。

ans2:作用是将整数输出为至少4位宽度,不足4位时在前用零填充。

ans3:功能是生成5个特定格式的学号。每个学号以固定前缀20490042开头,后接4位随机数字(范围1到100)。这4位数字通过%04d格式化为固定4位宽度,不足时补零。

task2

程序:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 int main()
 4 {
 5     int choice,quantity;
 6     float total_price=0,amount_paid,change;
 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         if(choice<1||choice>4){
20             printf("无效的饮料编号, 请重新输入。 \n");
21             continue;
22         }
23 
24         printf("请输入购买的数量: ");
25         scanf("%d",&quantity);
26 
27         if(quantity<0){
28             printf("购买数量不能为负数, 请重新输入。 \n");
29             continue;
30         }
31 
32         switch(choice){
33             case 1:
34             case 2:
35                 total_price+=3*quantity;
36                 break;
37             case 3:
38                 total_price+=5*quantity;
39                 break;
40             case 4:
41                 total_price+=2*quantity;
42                 break;
43         }
44         printf("请投入金额");
45         scanf("%f",&amount_paid);
46 
47         change=amount_paid-total_price;
48         printf("本次购买总价: %.2f 元\n", total_price);
49         printf("找零: %.2f 元\n", change);
50 
51         total_price=0;
52     }
53 
54     printf("感谢您的购买, 欢迎下次光临! \n");
55     system("pause");
56     return 0;
57 
58 }

运行结果:

ans1:用途:在每次购买结束后,将总金额 total_price 重置为 0。影响:如果去掉,total_price 会不断累加多次购买总金额,导致后续总价计算错误。

ans2:break 终止整个循环,彻底退出;continue 仅跳过当前的这小部分,忽略当前错误输入并重试。

ans3:不需要添加 default 子句。原因:在进入 switch 之前,程序已经通过 if (choice < 1 || choice > 4) 检查了 choice 的有效性,确保 choice 只能是 1-4 或 0(退出)。当 choice 为 1-4 时,switch 的 case 分支已覆盖所有可能的值,没有遗漏。

 

task3

 程序:

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

运行结果:

 

 

task4

程序:

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

运行结果:

 

 

task5

 程序:

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

运行结果:

 

task6

 程序:

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

运行结果:

 

posted @ 2025-03-23 15:59  OasisShining  阅读(29)  评论(0)    收藏  举报