实验2 C语言分支与循环基础应用
*实验任务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 }
task1
问题1:解释line13代码的作用
line13代码作用:生成一个[1,100]随机整数;
问题2:解释line14使用格式%04d起到什么作用
%04d作用:输出生成的数,并且数字宽度为4位,不足用0代替;
问题3:这个程序的功能是什么?
随机生成五个学号的后四位数字。
*实验任务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 20 if (choice < 1 || choice > 4) { 21 printf("无效的饮料编号,请重新输入。\n"); 22 continue; 23 } 24 25 printf("请输入购买的数量: "); 26 scanf("%d", &quantity); 27 28 if (quantity < 0) { 29 printf("购买数量不能为负数,请重新输入。\n"); 30 continue; 31 } 32 33 switch (choice) { 34 case 1: 35 case 2: 36 total_price += 3 * quantity; 37 break; 38 case 3: 39 total_price += 5 * quantity; 40 break; 41 case 4: 42 total_price += 2 * quantity; 43 break; 44 } 45 46 printf("请投入金额: "); 47 scanf("%f", &amount_paid); 48 49 change = amount_paid - total_price; 50 printf("本次购买总价: %.2f 元\n", total_price); 51 printf("找零: %.2f 元\n", change); 52 53 total_price = 0; 54 } 55 56 printf("感谢您的购买,欢迎下次光临!\n"); 57 system("pause"); 58 return 0; 59 }
task2
问题1:line53代码的用途?如果去掉,会有什么影响?
使一次购买之后总价归0,去掉之后会使第二次购买时加上第一次的总价;
问题2:line17-18,使用了break语句;line20-23,line28-31,使用了continue语句,在循环中使用break和continue有什么区别?
break会使循环在此处终止,continue会使这一次循环结束,总循环继续;
问题3:line33-44使用了switch语句实现多分支,通常情况下,在switch语句中使用default子句有利于代码运行时错误排查。在这个程序中,是否有必要增加default子句?你的答案和原因。
有一定必要,虽然前面已经把所有可能的输入数字的情况考虑到了,但是若输入汉字或字母,字符这类的话,会进入死循环。
*实验任务3
1 #include<stdio.h> 2 #include<stdlib.h> 3 int main() 4 { 5 char r,g,y; 6 char n; 7 printf("输入字符表示交通信号灯的颜色,r表示red,g表示green,y表示yellow:\n"); 8 do 9 { scanf("%c",&n); 10 switch(n) 11 {case 'r':printf("stop!\n"); 12 getchar();break; 13 case 'g':printf("go go go\n"); 14 getchar();break; 15 case 'y':printf("wait a minute\n"); 16 getchar();break; 17 default:printf("something must be wrong...\n"); 18 getchar();break;} 19 20 } 21 while(n!=EOF); 22 system("pause"); 23 return 0; 24 25 }
*实验任务4
1 #include <stdio.h> 2 #include <stdlib.h> 3 int main() { 4 double sum = 0, max,min; 5 double expense; 6 int first=1; 7 printf("输入今日开销,直到输入-1终止:\n"); 8 do { 9 scanf("%lf", &expense); 10 getchar(); 11 if (expense==-1) 12 { break; } 13 if (expense>20000) 14 { printf("输入金额不超过2万元,不低于0元,请重新输入\n"); 15 continue; } 16 if (expense<=0) 17 { printf("输入金额不超过2万元,不低于0元,请重新输入\n"); 18 continue;} 19 sum+=expense; 20 while(first==1) 21 {max=expense; 22 min=expense; 23 first=0;} 24 if (expense>max) max = expense; 25 if (expense<min) min = expense; 26 } 27 while(1); 28 printf("今日累计消费总额:%.1f\n", sum); 29 printf("今日最高一笔开销:%.1f\n", max); 30 printf("今日最低一笔开销:%.1f\n", min); 31 system("pause"); 32 return 0; 33 }
*实验任务5
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 int main() 5 { 6 int n,date; 7 int count=1; 8 srand(time(0)); 9 n=rand()%30+1; 10 printf("猜猜2025年4月哪一天是你的lucky day\n"); 11 printf("开始咯,你有三次机会,猜吧(1-30):"); 12 if(count<=3) 13 { 14 do{ 15 scanf("%d",&date); 16 if(date<n) 17 {printf("你猜的日期早了,你的lucky day还没到呢\n"); 18 if(count<3){ 19 printf("再猜(1-30):");} 20 count+=1;continue;} 21 if(date>n) 22 {printf("你猜的日期晚了,你的lucky day在前面哦\n"); 23 if(count<3){ 24 printf("再猜(1-30):");} 25 count+=1;continue;} 26 else printf("哇,猜中了,运气爆棚!Lucky April!");break; 27 }while(count<=3);count+=1; 28 } 29 if(count>3&&date!=n) 30 printf("次数用完啦,偷偷告诉你,4月你的lucky day是%d号,你就偷着乐吧\n",n); 31 system("pause"); 32 return 0; 33 }
*实验任务6
1 #include<stdio.h> 2 #include<stdlib.h> 3 int main() 4 {int i,n,line,part,blank,count; 5 int j=0; 6 printf("告诉我你想要几行小人阵列:"); 7 scanf("%d", &n); 8 const int m=n; 9 if(n>0) 10 {for(line=1;line<=m;line++) 11 {i=2*(m-line+1)- 1; 12 for(part=0;part<3;part++) 13 { for(blank=0;blank<j;blank++) 14 {printf(" \t");} 15 for(count=0;count<i;count++) 16 { 17 if(part==0) 18 printf(" O \t"); 19 else if(part==1) 20 printf("<H>\t"); 21 else 22 printf("I I\t"); 23 } 24 printf("\n"); 25 } 26 printf("\n"); 27 j++; 28 } 29 } 30 system("pause"); 31 return 0; 32 }
总结:
1.最大最小值问题中,若为循环结构去比较,应该使max和min存放第一个输入的值,再依此与后面的结构比较可以用如while(first==1) {max=expense; 22 min=expense; 23 first=0;}此类的方式,先设置标记,存放初始值
2.多重循环嵌套时应该要注意设置好每个{}的位置,使填写起来更简便。
3.double类型对应的scanf输入为%lf
4.输入字符型变量后如有回车使用,记得要加一行代码getchar()来消除回车的影响。