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

回答问题
问题1:这个程序的功能是什么?
程序以当前系统时间为随机种子,循环5次,每次生成1个1~100之间的随机整数,然后将该整数与固定前缀20490042拼接,按4位宽度(不足补前导0)格式化输出,最终生成5个格式统一的随机编号。
问题2:解释line13代码的功能
第13行代码:number = rand() % 100 + 1;
1. rand():调用标准库函数,生成一个0 ~ RAND_MAX(通常为32767)之间的伪随机整数。
2. rand() % 100:对随机数取模100,将结果范围压缩到0 ~ 99。
3. + 1:在取模结果基础上加1,将最终数值范围修正为1 ~ 100,符合题目中1~100的随机数需求。
4. 最终将计算结果赋值给number变量,存储生成的随机数。
问题3:解释第14行格式符%04d起到的作用
• %d:基础格式,用于输出十进制整数。
• 4:指定输出的最小宽度为4个字符。
• 0:表示当整数位数不足4位时,用前导0补齐空位,而非空格。
• 作用:保证编号后4位始终为4位数字,格式统一整齐。
问题4:代码srand(time(0))的作用
核心作用:为rand()函数设置随机种子,使每次运行程序生成的随机数序列不同,实现真正的“随机”效果。
1. time(0):调用<time.h>头文件的函数,获取当前系统的时间戳(从1970年1月1日到现在的秒数),作为随机种子。
2. srand(seed):调用<stdlib.h>头文件的函数,用传入的seed初始化rand()的伪随机数生成器。
3. · 保留该行代码:每次运行程序时,时间戳不同,种子不同,生成的随机数序列完全不同。
· 去掉该行代码:rand()默认使用固定种子1,每次运行程序都会生成完全相同的随机数序列,失去随机意义。
实验任务二
源代码task2.c
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() { 5 int choice, quantity; 6 float total_price = 0, amount_paid, change; 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 if (choice == 0) 17 break; 18 if (choice < 1 || choice > 4) { 19 printf("无效的饮料编号,请重新输入。\n"); 20 continue; 21 } 22 printf("请输入购买的数量: "); 23 scanf("%d", &quantity); 24 if (quantity < 0) { 25 printf("购买数量不能为负数,请重新输入。\n"); 26 continue; 27 } 28 if(choice == 1 || choice == 2) 29 total_price += 3 * quantity; 30 else if(choice == 3) 31 total_price += 5 * quantity; 32 else 33 total_price += 2 * quantity; 34 printf("请投入金额: "); 35 scanf("%f", &amount_paid); 36 change = amount_paid - total_price; 37 printf("本次购买总价: %.2f 元\n", total_price); 38 printf("找零: %.2f 元\n", change); 39 total_price = 0; 40 } 41 42 printf("感谢您的购买,欢迎下次光临!\n"); 43 system("pause"); 44 return 0; 45 }
运行结果截图

回答问题
问题1:line47代码 total_price = 0; 如果去掉,对程序有什么影响?
total_price 是用来累计单次购买总价的变量,去掉这行代码会导致:
1. 多次购买时总价累加错误:
第一次购买完成后,total_price 会保留本次的金额。当用户进行第二次购买时,新的金额会累加到上一次的总价上,导致第二次购买的总价计算错误(包含了上一次的消费)。
2. 找零计算完全错误:
找零 change = amount_paid - total_price 会基于错误的累加总价计算,导致找零金额严重偏差,甚至出现负数(当用户投入金额小于累加总价时)。
3. 程序逻辑完全失效:
原本设计的「单次购买独立结算」逻辑被破坏,程序无法正确完成每一笔交易的结算,失去自动售货机的功能。
问题2:while循环中,两处使用continue语句,解释在循环中使用continue语句的语义是什么?:
跳过本次循环中 continue 之后的所有代码,直接开始下一次循环的条件判断,不会终止整个循环。
实验任务三
源代码task3.c
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main () 5 { 6 char input; 7 printf("请输入交通信号灯颜色(r/g/y):\n"); 8 while (scanf("%c",&input)!= EOF) 9 { 10 if (input == '\n') 11 { 12 continue; 13 } 14 switch (input){ 15 case 'r': 16 printf("stop!\n"); 17 break; 18 case 'g': 19 printf("go go go!\n"); 20 break; 21 case 'y': 22 printf("wait a minute!\n"); 23 break; 24 default: 25 printf("something must be wrong..\n"); 26 break; 27 } 28 } 29 system("pause"); 30 return 0; 31 }
运行结果截图

实验任务四
源代码task4.c
1 #include <stdio.h> 2 #include <float.h> 3 #include <stdlib.h> 4 int main() { 5 double expense; 6 double total = 0.0; 7 double max_exp = -DBL_MAX; 8 double min_exp = DBL_MAX; 9 printf("输入今日开销,直到输入-1终止:\n"); 10 11 while (1) { 12 scanf("%lf", &expense); 13 if (expense == -1) { 14 break; 15 } 16 total += expense; 17 if (expense > max_exp) { 18 max_exp = expense; 19 } 20 if (expense < min_exp) { 21 min_exp = expense; 22 } 23 } 24 printf("今日累计消费总额: %.1f\n", total); 25 printf("今日最高一笔开销: %.1f\n", max_exp); 26 printf("今日最低一笔开销: %.1f\n", min_exp); 27 system("pause"); 28 return 0; 29 }
运行结果截图

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

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


浙公网安备 33010602011771号