c语言实验2

 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 
17     return 0;
18 }

问题一:生成一个范围在1到100的随机数

问题二:控制生成的数的格式一样,都为四位,不足的前面加0

问题三:生成一组数字,前缀都为20490042,后面为1到100的随机数,且为四位,不足的前面加0

 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 }

问题一:将上一次购买的价格清0,如果去掉的话,后续输出的金额就是所哟购买的水的总额,应付金额就会错误

问题二:break,直接退出循环

continue,跳过本次循环中continue后面的语句,直接进入下一次循环

问题三:没有必要,本程序已经涵盖了所有可能

实验3

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

实验4

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 int main()
 5 {
 6     double M, m;
 7     double expense, sum;
 8     
 9     
10     sum=0;
11     M=0;
12     m=20000;
13     
14     while(1)
15     {
16         scanf("%lf", &expense);
17         if(expense == -1)
18         {
19             break;
20         }
21         sum +=expense;
22         
23         if(m>=expense)
24         {
25             m=expense;
26           } 
27         if(M<=expense)
28         {
29             M=expense;
30         } 
31     }
32     printf("今日累计消费总额:%.1f\n", sum);
33     printf("今日最高一笔开销:%.1f\n", M);
34     printf("今日最低一笔开销:%.1f\n", m);
35 
36     system("pause");
37     return 0;
38 }

实验5

 

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

 

实验6

 1 #include <stdio.h>
 2 int main()
 3 {
 4     int n, i, j;
 5     printf("input n:");
 6     scanf("%d", &n);
 7 
 8     for(i = 0; i < n; ++i) {
 9         int left = 2 * i;
10         int p = 2 * (n - i) - 1;
11 
12         // 打印第一部分
13         for(j = 0; j < left; ++j) {
14             putchar(' ');
15             putchar(' ');
16             putchar(' ');
17         }
18         for(j = 0; j < p; ++j) {
19             printf("  0   ");
20         }
21         putchar('\n');
22 
23         // 打印第二部分
24         for(j = 0; j < left; ++j) {
25             putchar(' ');
26             putchar(' ');
27             putchar(' ');
28         }
29         for(j = 0; j < p; ++j) {
30             printf(" <H>  ");
31         }
32         putchar('\n');
33 
34         // 打印第三部分
35         for(j = 0; j < left; ++j) {
36             putchar(' ');
37             putchar(' ');
38             putchar(' ');
39         }
40         for(j = 0; j < p; ++j) {
41             printf(" I I  ");
42         }
43         putchar('\n');
44     }  
45 
46     return 0;
47 }

 

posted @ 2025-03-24 06:34  林觉得冷  阅读(7)  评论(0)    收藏  举报