实验2

实验任务1

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 
 5 #define N 5
 6 #define N1 80
 7 #define N2 35
 8 
 9 int main(){
10     int cnt;
11     int random_major, random_no;
12     
13     srand(time(NULL)); // 以当前系统时间作为随机种子
14     
15     cnt = 0;
16     while(cnt < N) {
17         random_major = rand() % 2;
18         
19         if(random_major) {
20             random_no = rand() % N1 + 1;
21             printf("20256343%04d\n", random_no);
22        }
23        else{
24            random_no=rand()%N2+1;
25            printf("20256136%04d\n",random_no);
26        }
27        
28        cnt++;
29 }
30 
31 return 0;
32 }
View Code

 屏幕截图 2025-10-14 125834

 

问题1:代码起到抽取随机数的作用,如果删去运行的抽取结果一样

问题2:这个程序的作用分别在1到80个人里面抽取学号,和在1到35个人里抽取,即两个班里抽

 

实验任务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("请投入金额:\n");
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         
49         printf("感谢您的购买,欢迎下次光临!\n");
50         return 0;
51     
52 }
View Code

 屏幕截图 2025-10-14 124257

问题1:47行的作用是重新计算,即重新令total的值为0,如果删去将在前面total的基础上再次相加

问题2:continue指跳过这次运行,但仍会进行接下来的

 

实验任务3

#include<stdio.h>
int main(){
    char c;
    while(scanf("%c",&c)!=EOF){
    getchar();
    
    if(c=='r')
     printf("stop!\n");
    else if(c=='g')
     printf("go go go\n");
    else if(c=='y')
     printf("wait a minute\n");
    else
     printf("something must be wrong...\n");
}
    return 0;
}
View Code

 屏幕截图 2025-10-14 124457

 

实验任务4

 1 #include <stdio.h>
 2 
 3 int main() {
 4     double price,sum;
 5     double max,min;
 6     sum=0;
 7     max=0;
 8     min=20000;
 9     printf("输入今日开销,直到输入-1终止:\n");
10 
11     while(1) {
12         scanf("%lf",&price);
13         if(price==-1)
14             break;
15         if(price<=0||price>20000)
16             continue;
17         if(price>max)
18             max=price;
19         if(price<min)
20             min=price;
21 
22         sum+=price;
23     }
24     printf("今日累计消费总额%f\n",sum);
25     printf("今日最高一笔开销:%f\n",max);
26     printf("今日最低一笔开销%f\n",min);
27     return 0;
28 }
View Code

屏幕截图 2025-10-14 122834

实验任务5

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

屏幕截图 2025-10-14 123225

实验任务6

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

屏幕截图 2025-10-14 132144

屏幕截图 2025-10-14 132344

 

posted @ 2025-10-14 13:26  楊yyy  阅读(10)  评论(1)    收藏  举报