实验2
任务一
源代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 #define N 5 5 int main(){ 6 int number; 7 int i; 8 srand(time(0)); 9 for(i=0;i<N;++i) 10 { 11 number=rand()%100+1; 12 printf("20490042%04d\n",number); 13 } 14 system("pause"); 15 return 0; 16 }
测试结果截图

问题回答
问题1:随机生成五个学员编号,类似于上课随机抽人系统。
问题2:生成一个1-100之间的随机整数。
问题3:使得输出的数至少占四位,不够前面补零。
问题4:在去掉这行,生成的随机数每次都一样,因此,这行代码的作用是给随机数生成提供种子,使得每次生成的随机数不一样。
任务二
源代码
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 16 scanf("%d",&choice); 17 if(choice==0) 18 break; 19 if(choice<1||choice>4) 20 { 21 printf("无效饮料编号,请重输。\n"); 22 continue; 23 } 24 printf("请输入购买的数量:"); 25 scanf("%d",&quantity); 26 if(quantity<0) 27 { 28 printf("购买数量不可为负,请重输。\n"); 29 continue; 30 } 31 if(choice==1||choice==2) 32 total_price+=3*quantity; 33 else if(choice==3) 34 total_price+=5*quantity; 35 else 36 total_price+=2*quantity; 37 38 printf("请投入金额:"); 39 scanf("%f",&amount_paid); 40 41 change=amount_paid-total_price; 42 printf("本次购买总价:%.2f元\n",total_price); 43 printf("找零: %.2f元\n",change); 44 45 total_price=0; 46 } 47 printf("感谢购买,欢迎下次光临!\n"); 48 system("pause"); 49 return 0; 50 } 51 52
测试结果截图

问题回答
问题1:
作用是在一次结算后将price清零,为下一次做准备。如果去掉,那么,在一次的循环中,如果不输入0终止,那么,总价将会不断累加,结果会不正确。
问题2:continue的作用是,跳过本次循环剩下的代码,进入下一次循环。在本任务中的语义为,这次的操作无效,需要从头重新操作。
任务三
源代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 int main(){ 4 double x; 5 char m; 6 7 while(scanf(" %c",&m)!=EOF) 8 { 9 switch(m) 10 { 11 case'r': 12 printf("stop\n"); 13 break; 14 case'g': 15 printf("go go go\n"); 16 break; 17 case'y': 18 printf("wait a minute\n"); 19 break; 20 default: 21 printf("something must be wrong...\n"); 22 break; 23 } 24 } 25 system("pause"); 26 return 0; 27 }
测试结果截图

任务四
源代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 int main(){ 4 double x,max,min; 5 double sum=0.0; 6 printf("请输入今日开销,直到输到-1终止:\n"); 7 scanf("%lf",&x); 8 max=x; min=x; 9 while(x!=-1) 10 { 11 sum=sum+x; 12 if(x>max) 13 max=x; 14 if(min>x) 15 min=x; 16 scanf("%lf",&x); 17 } 18 printf("\n今日累计消费总额:%.1f\n今日最高一笔开销:%.1f\n今日最低一笔开销:%.1f\n",sum,max,min); 19 system("pause"); 20 return 0; 21 }
测试结果截图

任务五
源代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 int main(){ 4 5 int a,b,c; 6 7 while(scanf("%d%d%d",&a,&b,&c)!=EOF) 8 { 9 if(a+b>c&&b+c>a&&a+c>b) 10 { 11 if(a==b&&b==c) 12 13 printf("等边三角形\n"); 14 15 16 else if(a*a+b*b==c*c||a*a+c*c==b*b||b*b+c*c==a*a) 17 18 printf("直角三角形\n"); 19 20 21 else if(((a==b)&&(b!=c))||((a==c)&&(c!=b))||((b==c)&&(c!=a))) 22 23 printf("等腰三角形\n"); 24 25 26 else 27 28 printf("普通三角形\n"); 29 30 31 } 32 else 33 printf("不能构成三角形\n"); 34 } 35 system("pause"); 36 return 0; 37 38 }
测试结果截图

任务六
源代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 int main() 5 { 6 int a; 7 int b=0; 8 9 srand(time(0)); 10 int number=rand()%30+1; 11 printf("猜猜2026年4月哪一天是你的lucky day:\n"); 12 printf("开始喽,你有三次机会,猜吧(1-30):\n"); 13 while(b<3) 14 { 15 scanf("%d",&a); 16 b++; 17 if(a>number) 18 printf("你猜的日期晚了,你的lucky day在前面哦。\n"); 19 else if(a<number) 20 printf("你猜的日期早了,你的lucky day在后面哦。\n"); 21 else 22 printf("哇,猜中了。\n"); 23 if(b<=3) 24 printf("再猜(1-30)\n"); 25 } 26 printf("次数用光了。4月你的lucky day 是%d哦\n",number); 27 system("pause"); 28 return 0; 29 }
测试结果截图

实验总结
这次实验,1,通过练习,明白了随机数的生成原理,知道了如何生成“伪随机数”和“真随机数”;
2,练习熟悉了continue与break的用法;
3,知道了多次循环(包括定次和自己终止)的写法,但是,不知道为什么,我的电脑上,终止循环只需要按^Z俩次或一次,还需要问老师;
4,对条件的写法更加熟练。
浙公网安备 33010602011771号