实验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 }

 

运行截图

捕获

 

回答问题

1.给某专业学员生成5个随机编号;

2.从1到100间随机生成一个数,并把它赋值给number;

3.生成的随机数要有四位,不足四位的前面用0补齐;

4.为了让每一次生成随机编号也是随机的,如果去掉srand(time(0));会使每一次生成的随机编号都相同。

 

 

实验任务2

代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 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     printf("请输入购买的数量: ");
24     scanf("%d", &quantity);
25     if (quantity < 0) {
26       printf("购买数量不能为负数,请重新输入。\n");
27       continue;
28    }
29     if(choice == 1 || choice == 2)
30       total_price += 3 * quantity;
31     else if(choice == 3)
32       total_price += 5 * quantity;
33     else
34       total_price += 2 * quantity;
35     printf("请投入金额: ");
36     scanf("%f", &amount_paid);
37     change = amount_paid - total_price;
38     printf("本次购买总价: %.2f 元\n", total_price);
39     printf("找零: %.2f 元\n", change);
40     total_price = 0;
41  }
42   printf("感谢您的购买,欢迎下次光临!\n");
43   system("pause");
44   return 0;
45 }

 

运行截图

捕获

 

回答问题

1.去掉total_price = 0;之后在第二次购买时会加上第一次所需费用,不会重新归零。

2.continue是直接跳过接下来的语句,运行下一个循环。

 

 

实验任务3

代码

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

 

运行截图

屏幕截图 2026-04-04 102152

 

 

 

实验任务4

代码

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

 

运行截图

屏幕截图 2026-04-04 220728

 

 

 

实验任务5

代码

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

 

运行截图

屏幕截图 2026-04-04 222826

 

 

 

实验任务6

代码

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

 

 

 

 

运行截图

屏幕截图 2026-04-04 230133

 

posted @ 2026-04-04 23:05  Hhhm00  阅读(6)  评论(0)    收藏  举报