实验2
任务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("%04d\n", number); 15 } 16 17 return 0; 18 }
屏幕截图

问题1 生成5个1~100随机整数,按’20490042+4位数字‘格式输出
问题2 生成1~100随机整数并赋值给number
问题3 确保输出的整数占4位,位数不足前面补0
问题4设置随机数种子,让每次运行的生成的随机数序列不同
任务2
源代码
1 #include <stdio.h> 2 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 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 char ch; 5 //循环读取输入,直到EOF 6 while (scanf("%c", &ch) != EOF) { 7 //过滤换行符 8 if (ch =='\n') continue; 9 10 switch (ch) { 11 case'r': 12 printf("stop!\n"); 13 break; 14 case'g': 15 printf("go go go\n"); 16 break; 17 case'y': 18 printf("wait a minute\n"); 19 break; 20 default: 21 printf("something must be wrong..\n"); 22 break; 23 } 24 } 25 return 0; 26 }
屏幕截图
任务4
源代码
1 #include<stdio.h> 2 int main() { 3 double expense, total = 0.0; 4 double max_exp = -1.0, min_exp = 20000.1;//初始化最大值,最小值 5 int count = 0;//记录有效开销的数量 6 printf("输入今日开销,直到输入-1终止:\n") ; 7 while (1) { 8 scanf("%lf", &expense); 9 10 //终止条件 11 if(expense == -1){ 12 break; 13 } 14 //过滤无消开销 15 if (expense <= 0|| expense >20000){ 16 printf("无效开销,请重新输入 (0 < 开销 <= 20000)\n"); 17 continue; 18 } 19 20 //累加总开销 21 total += expense; 22 count++; 23 24 //更新最大开销 25 if (expense > max_exp) { 26 max_exp = expense; 27 } 28 //更新最小开销 29 if (expense < min_exp) { 30 min_exp = expense; 31 } 32 } 33 //输出结果 34 printf("今日累计消费总额:%.1lf\n",total); 35 printf("今日最高一笔开销:%.1lf\n",max_exp); 36 printf("今日最低一笔开销:%.1lf\n",min_exp); 37 return 0; 38 }
屏幕截图

任务5
源代码
1 #include <stdio.h> 2 3 int main() { 4 int a, b, c; 5 // 持续读取输入,直到遇到EOF(Ctrl+Z) 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 } else { 11 // 判断等边三角形 12 if (a == b && b == c) { 13 printf("等边三角形\n"); 14 } 15 // 判断直角三角形 16 else if (a*a + b*b == c*c || a*a + c*c == b*b || b*b + c*c == a*a) { 17 printf("直角三角形\n"); 18 } 19 // 判断等腰三角形 20 else if (a == b || a == c || b == c) { 21 printf("等腰三角形\n"); 22 } 23 // 普通三角形 24 else { 25 printf("普通三角形\n"); 26 } 27 } 28 } 29 return 0; 30 }
屏幕截图

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



浙公网安备 33010602011771号