实验2 C语言分支与循环基础应用编程
实验任务1:
源代码:
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 number = rand() % 100 + 1; 11 printf("20240042%04d\n", number); 12 } 13 return 0; 14 }
运行截图:

问题1答案:生成并打印五个1~100之间的随机整数
问题2答案:生成五个1~100之间的随机整数并赋值给number
问题3答案:让数字以4位宽度、不足4位时前面补0的格式输出,输出符合要求的学员编号
问题4答案:以当前系统时间为随机数种子,让每次运行程序生成不同的随机数序列
试验任务2:
源代码:
1 #include <stdio.h> 2 int main() { 3 int choice, quantity; 4 float total_price = 0, amount_paid, change; 5 while (1) { 6 printf("\n自动饮料售卖机菜单:\n"); 7 printf("1. 可乐 - 3 元/瓶\n"); 8 printf("2. 雪碧 - 3 元/瓶\n"); 9 printf("3. 橙汁 - 5 元/瓶\n"); 10 printf("4. 矿泉水 - 2 元/瓶\n"); 11 printf("0. 退出购买流程\n"); 12 printf("请输入饮料编号: "); 13 scanf_s("%d", &choice); 14 if (choice == 0) 15 break; 16 if (choice < 1 || choice > 4) { 17 printf("无效的饮料编号, 请重新输入。\n"); 18 continue; 19 } 20 printf("请输入购买的数量: "); 21 scanf_s("%d", &quantity); 22 if (quantity < 0) { 23 printf("购买数量不能为负数, 请重新输入。\n"); 24 continue; 25 } 26 if (choice == 1 || choice == 2) { 27 total_price += 3 * quantity; 28 } 29 else if (choice == 3) { 30 total_price += 5 * quantity; 31 } 32 else { 33 total_price += 2 * quantity; 34 } 35 printf("请投入金额: "); 36 scanf_s( "%f", &amount_paid); 37 change = amount_paid - total_price; 38 printf("本次购买总价: %.2f 元\n", total_price); 39 printf("找零: %.2f 元\n", change); 40 total_price = 0; 41 } 42 printf("感谢您的购买, 欢迎下次光临!\n"); 43 return 0; 44 }
运行结果:

问题1答案: 会导致累计金额错误,程序无法正确计算多次购买的总价
问题2答案:用于立即结束本次循环,跳过循环中剩余的代码,直接开始下一次循环的条件判断
实验任务3:
源代码:
1 #include <stdio.h> 2 int main() { 3 char color; 4 while (scanf_s("%c", &color) != EOF) { 5 if (color == '\n') { 6 continue; 7 } 8 switch (color) { 9 case 'r': 10 printf("stop!\n"); 11 break; 12 case 'g': 13 printf("go go go\n"); 14 break; 15 case 'y': 16 printf("wait a minute\n"); 17 break; 18 default: 19 printf("something must be wrong...\n"); 20 break; 21 } 22 } 23 return 0; 24 }
运行结果:

实验任务4:
源代码:
1 #include <stdio.h> 2 int main() { 3 float expense; 4 float total = 0.0f; 5 float max_exp = 0.0f; 6 float min_exp = 20000.0f; 7 int count = 0; 8 printf("输入今日开销, 直到输入-1终止:\n"); 9 while (1) { 10 scanf_s("%f", &expense); 11 if (expense == -1) { 12 break; 13 } 14 total += expense; 15 count++; 16 if (expense > max_exp) { 17 max_exp = expense; 18 } 19 if (expense < min_exp) { 20 min_exp = expense; 21 } 22 } 23 if (count == 0) { 24 printf("今日无有效消费记录。\n"); 25 } 26 else { 27 28 printf("今日累计消费总额: %.1f\n", total); 29 printf("今日最高一笔开销: %.1f\n", max_exp); 30 printf("今日最低一笔开销: %.1f\n", min_exp); 31 } 32 return 0; 33 }
运行结果:

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

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

实验总结:通过这次实验充分锻炼了分支与循环的正确使用,在任务中有时还难以判断应该使用哪个语句,翻阅书本并使用ai工具加深了我对if which语句的理解。同时也有许多不足,对较繁琐的程序还是有些不知所措,还需要多加练习。
浙公网安备 33010602011771号