实验2

实验任务1

  • 源代码
     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     system("pause");
    17     return 0;
    18 }

     

  • 运行结果截图

  实验2-任务1

  • 回答问题
  1. 从编号为202400420001~202400420100的专业学员中随机抽取5个
  2. 将随机数对100取余后+1,得到1~100间的随机整数
  3. 输出4位整数,不足4位的话在前面补0
  4. 以时间作为随机种子,使每次运行时生成的数字不同

实验任务2

  • 源代码
     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         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         if(choice == 1 || choice == 2)
    33             total_price += 3 * quantity;
    34         else if(choice == 3)
    35             total_price += 5 * quantity;
    36         else
    37             total_price += 2 * quantity;
    38 
    39         printf("请投入金额: ");
    40         scanf("%f", &amount_paid);
    41 
    42         change = amount_paid - total_price;
    43         printf("本次购买总价: %.2f 元\n", total_price);
    44         printf("找零: %.2f 元\n", change);
    45 
    46         total_price = 0;
    47     }
    48     printf("感谢您的购买,欢迎下次光临!\n");
    49     return 0;
    50 }
    View Code

     

  • 运行结果截图

  任务2截图 2026-04-06 190208

 

  • 回答问题
  1. 总价会累加,无法连续多次购买
  2. 跳过continue后的代码,回到循环的条件判断处开始下一次循环

实验任务3

  • 源代码
     1 #include<stdio.h>
     2 int main(){
     3     char colour;
     4     while(scanf("%c",&colour) != EOF){
     5         if(colour=='\n')
     6             continue;
     7         switch(colour){
     8             case'r':printf("stop!\n");
     9                 break;
    10             case'g':printf("go go go\n");
    11                 break;
    12             case'y':printf("wait a minute\n");
    13                 break;
    14             default:printf("something must be wrong...\n");
    15                 break;
    16         }
    17     
    18     }
    19 
    20 return 0;
    21 }
  • 运行结果截图

  task3截图 2026-04-06 193348

 

 

实验任务4

  • 源代码
     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 int main(){
     4     float cost,total=0,max=0,min=20000;
     5     printf("输入今日开销,输入直到-1终止:\n");
     6     while(1){
     7         scanf("%f",&cost);
     8         if(cost==-1)
     9             break;
    10 
    11         total+=cost;
    12 
    13         if(cost>max)
    14             max=cost;
    15         if(cost<min)
    16             min=cost;
    17     }
    18     printf("今日累计总消费:%.1f\n",total);
    19     printf("今日最高一笔开销:%.1f\n",max);
    20     printf("今日最低一笔开销:%.1f\n",min);
    21     system("pause");
    22     return 0;
    23 }
  • 运行结果截图

  task4截图 2026-04-06 195556

 

实验任务5

  • 源代码
     1 #include<stdio.h>
     2 int main(){
     3     int a,b,c,t;
     4     while(scanf("%d%d%d",&a,&b,&c)!=EOF){
     5         if(a>b){t=a;a=b;b=t;}
     6         if(a>c){t=a;a=c;c=t;}
     7         if(b>c){t=b;b=c;c=t;}
     8 
     9         if(a+b<=c){
    10             printf("不能构成三角形\n");
    11                 continue;}
    12         if(a==b&&b==c)
    13             printf("等边三角形\n");
    14         else if(a==b||b==c||a==c)
    15             printf("等腰三角形\n");
    16         else if(a*a+b*b==c*c)
    17             printf("直角三角形\n");
    18         else
    19             printf("普通三角形\n");
    20     }
    21     return 0;
    22 }
  • 运行结果截图

  task5截图 2026-04-06 203119

 

实验任务6

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

     

  • 运行结果截图 

  task6截图 2026-04-06 211022

 

posted @ 2026-04-06 21:15  Miracle_rui  阅读(2)  评论(0)    收藏  举报