实验2

实验任务1

源代码

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

 

实验截图

 问题1:在1到100中取一个随机数

问题2:把1到100中取的随机数以四位数的形式输入到学号中

问题3:随机取五个人的学号

实验任务2

源代码

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

 

实验截图

 问题1:使算出的价格重新赋值为0 ,如果去掉的话下次循环中价格会一直累加上去

问题2:break是跳出整个循环,continue只是跳出这一次循环后面继续循环

问题3:没有必要,因为在switch语句前面已经用if把除了1234以外的结果排除在外了

实验任务3

源代码

 1 #include<stdio.h>
 2 int main()
 3 { char n;
 4   while(1)
 5 {
 6       scanf("%c",&n);
 7       getchar();
 8       switch(n)
 9   {   
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 just be wrong...\n");
14   }
15   }
16       return 0;
17 }

 

实验截图

 

实验任务4

源代码

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

 

实验截图

 

实验任务5

源代码

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

 

实验截图

 

实验任务6

源代码

 1 #include <stdio.h>
 2 
 3 int main() {
 4     int rows, i, j;
 5 
 6 
 7     printf("请输入倒三角的行数: ");
 8     scanf("%d", &rows);
 9 
10 
11     for (i = rows; i >= 1; --i) 
12     {
13 
14         for (j = 0; j < rows - i; ++j) {
15             printf("      ");
16         }
17 
18         for (j = 0; j < 2 * i - 1; ++j) {
19             printf(" 0 ");
20             printf("   ");
21         }
22         printf("\n");
23         for (j = 0; j < rows - i; ++j) {
24             printf("      ");
25         }
26          for (j = 0; j < 2 * i - 1; ++j) {
27                     printf("<H>");
28                     printf("   ");
29          }
30          
31          printf("\n"); 
32          for (j = 0; j < rows - i; ++j) {
33              printf("      ");
34          }
35          for (j = 0; j < 2 * i - 1; ++j) {
36                     printf("I I");
37                     printf("   ");
38 
39          }
40          printf("\n");
41          for (j = 0; j < rows - i; ++j) {
42              printf(" ");
43          }
44           printf("\n");
45     }
46 
47             return 0;
48         }

 

实验截图

 

 

 

posted @ 2025-03-17 22:22  孙姚奕  阅读(23)  评论(0)    收藏  举报