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

问题回答
问题1:随机生成5个1~100之间的整数并将其放于“20490042”之后,以四位数格式输出,模拟生成该专业的5个学员编号。
问题2:生成一个1~100之间的随机整数,并将其赋值给“number”。
问题3:作用是使对应整数以四位数字形式输出。
问题4:作用是设置随机生成器,使得每次运行时程序生成不同的随机数序列。若去掉这行代码,每次运行程序生成的随机数序列都完全相同,无法达到随机效果。
任务2
源代码
1 #include <stdio.h> 2 3 int main() 4 { 5 int choice,quantity; 6 float total_price=0,amount_paid,change; 7 8 while(1){ 9 printf("\n自动饮料售卖机菜单:\n"); 10 printf("1.可乐 - 3元/瓶\n"); 11 printf("2.雪碧 - 3元/瓶\n"); 12 printf("3.橙汁 - 5元/瓶\n"); 13 printf("4.矿泉水 - 2元/瓶\n"); 14 printf("0.退出购买流程\n"); 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 if(choice==1 || choice==2) 34 total_price += 3*quantity; 35 else if(choice==3) 36 total_price += 5*quantity; 37 else 38 total_price += 2*quantity; 39 40 printf("请输入金额:"); 41 scanf("%f",&amount_paid); 42 43 change=amount_paid-total_price; 44 printf("本次购买总价:%.2f 元\n",total_price); 45 printf("找零:%.2f 元\n",change); 46 47 total_price=0; 48 } 49 50 printf("感谢您的购买,欢迎下次光临!\n"); 51 return 0; 52 }
运行结果截图

问题回答
问题1:若去掉,会导致多次购买总价累加,后续购买计算会包含之前的购买总价,不符合单次购买单次计算,出现计算错误。
问题2:第一处,当输入饮料编号无效时,执行该语句,直接跳回循环开头,重新打印菜单让用户输入正确饮料编号。
第二处,当输入的购买数量为负数时,执行该语句,直接跳回循环开头,重新打印菜单让用户重新进行购买。
任务3
源代码
1 #include <stdio.h> 2 3 int main() 4 { 5 char ch; 6 7 while((ch=getchar())!=EOF){ 8 if(ch=='\n'){ 9 continue; 10 } 11 12 switch(ch){ 13 case'r': 14 printf("stop!\n"); 15 break; 16 case'g': 17 printf("go go go\n"); 18 break; 19 case'y': 20 printf("wait a minute\n"); 21 break; 22 default: 23 printf("something must be wrong...\n"); 24 break; 25 } 26 27 while(getchar()!='\n'); 28 } 29 30 return 0; 31 }
运行结果截图

任务4
源代码
1 #include <stdio.h> 2 3 int main() { 4 double expense; 5 double total = 0.0; 6 double max = 0.0; 7 double min = 20000.0; 8 9 printf("输入今日开销,直到输入-1终止:\n"); 10 11 while (1) { 12 scanf("%lf", &expense); 13 if (expense == -1) { 14 break; 15 } 16 17 if (expense > 0 && expense <= 20000) { 18 total += expense; 19 20 if (expense > max) { 21 max = expense; 22 } 23 24 if (expense < min) { 25 min = expense; 26 } 27 } else { 28 printf("输入的消费金额不符合要求(0 < 每笔消费 <= 2万元),请重新输入!\n"); 29 } 30 } 31 32 printf("今日累计消费总额:%.1f\n", total); 33 printf("今日最高一笔开销:%.1f\n", max); 34 printf("今日最低一笔开销:%.1f\n", min); 35 36 return 0; 37 }
运行结果截图

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

任务6
源代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 int main() { 6 int guess; 7 int luckyDay; 8 int count = 0; 9 10 srand((unsigned int)time(NULL)); 11 luckyDay = rand() % 30 + 1; 12 13 printf("猜猜2026年4月哪一天是你的lucky day\n"); 14 printf("开始喽,你有3次机会,猜吧(1~30):"); 15 16 while (count < 3) { 17 scanf("%d", &guess); 18 count++; 19 20 if (guess == luckyDay) { 21 printf("哇,猜中了:)\n"); 22 break; 23 } else if (guess > luckyDay) { 24 printf("你猜的日期晚了,你的lucky day在前面哦\n"); 25 } else { 26 printf("你猜的日期早了,你的lucky day还没到呢\n"); 27 } 28 29 if (count < 3) { 30 printf("再猜(1~30):"); 31 } 32 } 33 34 if (count == 3 && guess != luckyDay) { 35 printf("次数用光啦。4月你的lucky day是%d号\n", luckyDay); 36 } 37 38 return 0; 39 }
运行结果截图

浙公网安备 33010602011771号