实验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("20490042%04d\n", number); 15 } 16 17 return 0; 18 }
测试结果截图

问题回答
1.这个程序的功能是:从该专业所有学员里随机抽取5个人的编号
2.line3代码的作用:控制抽取的学员编号后四位数在1~100之间
3.line14格式符%04d的作用:在前面固定的“20490042”后面加上占四个字符的数字
4.srand(time(0))的作用:(去掉该行代码多次运行发现每次抽取的学号都一样),起到一个计次重新抽 取新结果的作用
任务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.continue语义是如果出现“输入无效饮料编号”或者“输入购买数量为负数”的情况,立即停止运行,返回while处重新开始运行
任务3
源代码
1 #include <stdio.h> 2 3 int main() { 4 char color; 5 6 while (scanf("%c", &color) != EOF) { 7 if (color == '\n'){ 8 continue; 9 } 10 switch (color) { 11 case 'r': printf("stop!\n"); break; 12 case 'g': printf("go go go\n"); break; 13 case 'y': printf("wait a minute\n"); break; 14 default: printf("something must be wrong\n"); 15 } 16 } 17 return 0; 18 }
测试结果截图·

任务4
源代码
1 #include <stdio.h> 2 3 int main() { 4 double cost, total = 0.0; 5 double max_cost = 0.0; 6 double min_cost = 20000.0; 7 8 printf("输入今日开销,直到输入-1为止: \n"); 9 10 while (1) { 11 scanf("%lf", &cost); 12 13 if (cost == -1) { 14 break; 15 } 16 if (cost <= 0.0 || cost >= 20000.0) { 17 continue; 18 } 19 20 total += cost; 21 22 if (cost > max_cost) { 23 max_cost = cost; 24 } 25 if (cost < min_cost) { 26 min_cost = cost; 27 } 28 } 29 printf("今日累计消费总额: %.lf\n", total); 30 printf("今日最高的一笔开销:%.lf\n", max_cost); 31 printf("今日最低的一笔开销:%.lf\n", min_cost); 32 33 return 0; 34 }
测试结果截图

任务5
源代码
1 #include <stdio.h> 2 3 int main() { 4 int a, b, c; 5 6 while (scanf("%d%d%d", &a, &b, &c) != EOF) { 7 if (a + b <= c || a + c <= b || b + c <= a) { 8 printf("不能构成三角形\n"); 9 } else { 10 if (a == b && b == c) { 11 printf("等边三角形\n"); 12 } 13 else if (a == b || a == c || b == c) { 14 printf("等腰三角形\n"); 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 else { 20 printf("普通三角形\n"); 21 } 22 } 23 24 while (getchar() != '\n'); 25 } 26 return 0; 27 }
测试结果截图

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

实验总结
任务三时一开始总是出现如图结果

在增加7,8行代码后问题得到解决

浙公网安备 33010602011771号