实验2

试验任务1:

代码:

 1 #include<stdio.h>
 2 #include<stdlib.h> 
 3 #include<time.h>
 4 #define N 5
 5 #define N1 80
 6 #define N2 35
 7 int main(){
 8     int cnt;
 9     int random_major,random_no;
10     srand(time(NULL));
11     cnt=0;
12     while(cnt<N){
13         random_major=rand()%2;
14         if(random_major){
15             random_no=rand()%N1+1;
16             printf("20256343%04d\n",random_no);
17         }else{random_no=rand()%N2+1;
18             printf("20256136%04d\n",random_no);
19         }
20         cnt++;
21     }return 0;
22 }
View Code

图片:捕获23

1

问题1:根据时间不同使rand()生成不同数,如果去掉会使rand生成相同的数。

问题2:生成5组随机学号

试验任务2

代码

 1 #include<stdio.h>
 2 int main(){
 3     int choice,quantity;
 4     float total_price=0,amount_paid,change;
 5         while(1){
 6             printf("\n自动饮料售卖机菜单:\n");
 7             printf("1,可乐-3元/瓶\n");
 8             printf("2,雪碧-3元/瓶\n");
 9             printf("3,橙汁-5元/瓶\n");
10             printf("4,矿泉水-2元/瓶\n");
11             printf("0,退出购买流程\n");
12             printf("请输入饮料编号:");
13             scanf("%d",&choice);
14             if(choice==0)
15             break;
16             if(choice<1||choice>4){
17                 printf("无效饮料编号,请重新输入。\n");
18                 continue;}
19                 printf("请输入购买数量:");
20                 scanf("%d",&quantity);
21                 if(quantity<0){
22                     printf("购买数量不能为负数,请重新输入。\n");
23                     continue;
24                 }
25                 if(choice==1||choice==2)
26                 total_price+=3*quantity;
27                 else if(choice==3)
28                 total_price+=5*quantity;
29                 else
30                 total_price+=2*quantity;
31                 printf("请输入金额:");
32                 scanf("%f",&amount_paid);
33                 change=amount_paid-total_price;
34                 printf("本次购买总价:%.2f元\n",total_price);
35                 printf("找零:%.2f元\n",change);
36                 total_price=0;
37         }printf("感谢你的购买,欢迎下次光临!\n");
38         return 0;
39 }
View Code

图片:4

问题1:会是总金额无法归零,导致下一次总金额会加上上一次总金额

问题2:1处如果输入choice大于4或者小于1则停止本次循环直接进入下次循环  2处如果输入quantity小于0则停止本次循环直接进入下次循环  

试验任务3

源代码

 1 #include<stdio.h>
 2 int main()
 3 { char a;
 4    
 5    while(1){
 6    a=getchar();
 7    if(a=='y'){
 8    
 9    printf("wait a mintue\n");}
10     else{
11     
12     if(a=='g')
13    printf("go go go\n");
14   else if(a=='r')
15    printf("stop!\n");
16    else
17    printf("something must be wrong\n");}
18    getchar();}
19    return 0;
20 }
View Code

图片:5

试验任务4

源代码

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

图片屏幕截图 2025-10-13 211132

实验任务5

源代码

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

图片屏幕截图 2025-10-13 212839

实验任务6

源代码

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

图片屏幕截图 2025-10-13 215901屏幕截图 2025-10-13 215835

 

 

 

       

posted @ 2025-10-14 12:14  空元  阅读(11)  评论(1)    收藏  举报