实验2 C语言分支与循环基础应用编程
实验任务1
task1.c
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 #define N 5 6 #define N1 80 7 #define N2 35 8 9 int main() { 10 int cnt = 0; 11 int random_major, random_no; 12 13 // 以当前系统时间作为随机种子 14 srand(time(NULL)); 15 16 printf("生成的随机学号:\n"); 17 printf("================\n"); 18 19 while (cnt < N) { 20 random_major = rand() % 2; // 随机选择专业:0或1 21 22 if (random_major) { 23 // 专业1:学号范围 1-80 24 random_no = rand() % N1 + 1; 25 printf("20256343%04d\n", random_no); 26 } 27 else { 28 // 专业2:学号范围 1-35 29 random_no = rand() % N2 + 1; 30 printf("20256136%04d\n", random_no); 31 } 32 cnt++; 33 } 34 35 return 0; 36 }
作用:设置随机数生成器的种子,确保每次运行程序时生成不同的随机数序列。
去掉 srand(time(NULL))
后的特点:
-
每次运行结果相同:程序会生成完全相同的"随机"学号序列
-
可预测性:在不同时间多次运行程序,输出的5个学号每次都一样
-
缺乏随机性:失去了真正的随机效果
程序功能:随机生成5个不同专业的学号。
实验任务2
task2.c
1 #include <stdio.h> 2 3 int main() { 4 int choice, quantity; 5 float total_price = 0, amount_paid, change; 6 7 printf("=== 自动饮料售卖机 ===\n"); 8 9 while (1) { 10 printf("\n当前总金额: %.2f 元\n", total_price); 11 printf("=====================\n"); 12 printf("1. 可乐 - 3 元/瓶\n"); 13 printf("2. 雪碧 - 3 元/瓶\n"); 14 printf("3. 橙汁 - 5 元/瓶\n"); 15 printf("4. 矿泉水 - 2 元/瓶\n"); 16 printf("5. 结算并付款\n"); 17 printf("0. 退出购买流程\n"); 18 printf("请输入选择: "); 19 scanf_s("%d", &choice); 20 21 if (choice == 0) { 22 if (total_price > 0) { 23 printf("您有未结算的商品!总金额: %.2f 元\n", total_price); 24 continue; 25 } 26 break; 27 } 28 29 if (choice == 5) { 30 // 结算流程 31 if (total_price <= 0) { 32 printf("您还没有选择任何商品!\n"); 33 continue; 34 } 35 36 printf("\n=== 结算 ===\n"); 37 printf("总金额: %.2f 元\n", total_price); 38 printf("请投入金额: "); 39 scanf_s("%f", &amount_paid); 40 41 if (amount_paid < total_price) { 42 printf("金额不足!还需要 %.2f 元\n", total_price - amount_paid); 43 continue; 44 } 45 46 change = amount_paid - total_price; 47 printf("找零: %.2f 元\n", change); 48 printf("购买完成,请取走您的商品!\n"); 49 50 total_price = 0; // 重置总金额 51 continue; 52 } 53 54 if (choice < 1 || choice > 4) { 55 printf("无效的选择,请重新输入。\n"); 56 continue; 57 } 58 59 printf("请输入购买数量: "); 60 scanf_s("%d", &quantity); 61 62 if (quantity <= 0) { 63 printf("购买数量必须为正数,请重新输入。\n"); 64 continue; 65 } 66 67 // 计算价格 68 switch (choice) { 69 case 1: 70 case 2: 71 total_price += 3 * quantity; 72 printf("已添加 %d 瓶可乐/雪碧\n", quantity); 73 break; 74 case 3: 75 total_price += 5 * quantity; 76 printf("已添加 %d 瓶橙汁\n", quantity); 77 break; 78 case 4: 79 total_price += 2 * quantity; 80 printf("已添加 %d 瓶矿泉水\n", quantity); 81 break; 82 } 83 } 84 85 printf("\n感谢您的购买,欢迎下次光临!\n"); 86 return 0; 87 }
去掉 total_price = 0;
的影响:
-
累计金额错误:
-
第一次购买:总金额 10元 → 付款后总金额仍为10元
-
第二次购买:新商品金额 + 上次未清零的金额 = 错误的总金额
-
continue语句的语义:
continue
语句用于跳过当前循环迭代中剩余的代码,直接开始下一次循环迭代。
实验任务3
task3.c
1 #include <stdio.h> 2 3 int main() { 4 char light_color; 5 6 printf("红绿灯信号模拟系统\n"); 7 printf("==================\n"); 8 printf("请输入信号灯颜色 (r/g/y): "); 9 10 while (scanf_s(" %c", &light_color) != EOF) { 11 // 使用switch语句实现 12 switch (light_color) { 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 printf("请输入信号灯颜色 (r/g/y): "); 28 } 29 30 printf("\n程序结束\n"); 31 return 0; 32 }
实验任务4
task4.c
1 #include <stdio.h> 2 3 int main() { 4 double expense; 5 double total = 0.0; 6 double max_expense = 0.0; 7 double min_expense = 20000.0; // 初始化为最大值 8 9 printf("输入今日开销,直到输入-1终止:\n"); 10 11 // 重复输入开销,直到输入-1 12 while (1) { 13 scanf_s("%lf", &expense); 14 15 // 检查是否终止输入 16 if (expense == -1) { 17 break; 18 } 19 20 // 验证输入范围 (0, 20000] 21 if (expense <= 0 || expense > 20000) { 22 printf("输入无效!开销应在0元到20000元之间,请重新输入:\n"); 23 continue; 24 } 25 26 // 累加总开销 27 total += expense; 28 29 // 更新最高开销 30 if (expense > max_expense) { 31 max_expense = expense; 32 } 33 34 // 更新最低开销 35 if (expense < min_expense) { 36 min_expense = expense; 37 } 38 } 39 40 // 输出统计结果,保留小数点后1位 41 printf("今日累计消费总额:%.1f\n", total); 42 printf("今日最高一笔开销:%.1f\n", max_expense); 43 printf("今日最低一笔开销:%.1f\n", min_expense); 44 45 return 0; 46 }
实验任务5
task5.c
1 #include <stdio.h> 2 3 int main() { 4 int a, b, c; 5 6 printf("请输入三角形三边边长(a b c): "); 7 8 // 支持多组输入,直到用户按下CTRL+Z 9 while (scanf_s("%d %d %d", &a, &b, &c) != EOF) { 10 // 检查是否能构成三角形 11 if (a <= 0 || b <= 0 || c <= 0 || 12 a + b <= c || a + c <= b || b + c <= a) { 13 printf("不能构成三角形\n"); 14 } 15 else { 16 // 判断三角形类型 17 if (a == b && b == c) { 18 printf("等边三角形\n"); 19 } 20 else if (a == b || a == c || b == c) { 21 printf("等腰三角形\n"); 22 } 23 else if (a * a + b * b == c * c || 24 a * a + c * c == b * b || 25 b * b + c * c == a * a) { 26 printf("直角三角形\n"); 27 } 28 else { 29 printf("普通三角形\n"); 30 } 31 } 32 33 printf("请输入三角形三边边长(a b c): "); 34 } 35 36 printf("\n程序结束\n"); 37 return 0; 38 }
实验任务6
task6.c
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 int main() { 6 int lucky_day, guess, attempts = 0; 7 const int max_attempts = 3; 8 9 // 设置随机种子 10 srand(time(NULL)); 11 // 生成1-30之间的随机数作为lucky day 12 lucky_day = rand() % 30 + 1; 13 14 printf("猜猜2025年11月哪一天是你的lucky day\n\n"); 15 printf("开始喽,你有三次机会,猜吧(1~30):"); 16 17 // 给用户3次猜的机会 18 while (attempts < max_attempts) { 19 scanf_s("%d", &guess); 20 attempts++; 21 22 // 检查输入是否在有效范围内 23 if (guess < 1 || guess > 30) { 24 printf("请输入1~30之间的数字:"); 25 continue; 26 } 27 28 // 判断猜测结果 29 if (guess == lucky_day) { 30 printf("恭喜你猜中了!%d号就是你的lucky day!\n", lucky_day); 31 break; 32 } 33 else if (guess < lucky_day) { 34 if (attempts < max_attempts) { 35 printf("你猜的日期早了,你的luckyday还没到呢\n"); 36 printf("再猜(1~30):"); 37 } 38 } 39 else { 40 if (attempts < max_attempts) { 41 printf("你猜的日期晚了,你的luckyday在前面哦\n"); 42 printf("再猜(1~30):"); 43 } 44 } 45 46 // 如果三次机会都用完了还没猜中 47 if (attempts == max_attempts && guess != lucky_day) { 48 printf("次数用光啦。偷偷告诉你,11月你的luckyday是%d号\n", lucky_day); 49 } 50 } 51 52 return 0; 53 }