实验2

实验任务1:

程序源代码:

 1 //task2_1.c
 2 #include <stdio.h>
 3  #include <stdlib.h>
 4  #include <time.h>
 5  #define N 5
 6  int main() {
 7     int number;
 8     int i;
 9     srand(time(0));     // 以当前系统时间作为随机种子
10     for(i = 0; i < N; ++i) {
11         number = rand() % 100 + 1;
12         printf("20490042%04d\n", number);
13     }
14     system("pause");
15     return 0;
16  }

运行结果截图:

问题1:生成一个1~100的随机数

问题2:输出number的数时占4个字符,若不足4个则补0

问题3:生成5个在20490042001~20490042100间的随机整数

 

 

实验任务2:

程序源代码:

 1 #include <stdio.h>
 2 int main() {
 3     int choice, quantity;
 4     float total_price = 0, amount_paid, change;
 5 
 6     while (1) {
 7         printf("\n自动饮料售卖机菜单:\n");
 8         printf("1. 可乐 - 3 元/瓶\n");
 9         printf("2. 雪碧 - 3 元/瓶\n");
10         printf("3. 橙汁 - 5 元/瓶\n");
11         printf("4. 矿泉水 - 2 元/瓶\n");
12         printf("0. 退出购买流程\n");
13         printf("请输入饮料编号: ");
14         scanf("%d", &choice);
15 
16         if (choice == 0)
17             break;
18 
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 
45         printf("请投入金额: ");
46         scanf("%f", &amount_paid);
47 
48         change = amount_paid - total_price;
49         printf("本次购买总价: %.2f 元\n", total_price);
50         printf("找零: %.2f 元\n", change);
51 
52         total_price = 0;
53     }
54 
55     printf("感谢您的购买,欢迎下次光临!\n");
56     return 0;
57 }
View Code

 

运行结果截图:

问题1:使每次购买时的初始价格都为0元。如果去掉,每次购买时初始价格都为上次的总价

问题2:break:break执行后退出整个循环,执行循环后的代码

continue:continue执行后跳过本次循环后续代码,执行下一次循环

问题3:没有必要增加default语句。代码中if (choice < 1 || choice > 4)使只有输入1~4时才能继续执行后续代码

 

 

 

 

实验任务3:

程序源代码:

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

运行结果截图:

实验任务4:

程序源代码:

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

运行结果截图:

 

 

 实验任务5:

程序源代码:

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

运行结果截图:

实验任务6:

程序源代码:

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

运行结果截图:

 

posted @ 2025-03-21 19:38  起司配咖啡  阅读(5)  评论(0)    收藏  举报