实验2
实验任务1
task.1.c源代码
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("20240042%04d\n", number); 15 } 16 return 0; 17 }
运行截图

问题1:随机从202400420001~202400420100抽取五个学员编号
问题2:生成1~100之间的随机整数
问题3:使20240042后缀部分是4位数
问题4:使每次运行程序生成不同的随机数序列,确保抽取的随机性
实验任务2
task.2.c源代码
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 34 if (choice == 1 || choice == 2) 35 total_price += 3 * quantity; 36 else if (choice == 3) 37 total_price += 5 * quantity; 38 else 39 total_price += 2 * quantity; 40 41 42 printf("请投入金额:"); 43 scanf("%f", &amount_paid); 44 change = amount_paid - total_price; 45 printf("总价:%.2f 元\n", total_price); 46 printf("找零:%.2f 元\n", change); 47 48 total_price = 0; 49 } 50 51 printf("感谢您的购买,欢迎下次光临!\n"); 52 return 0; 53 }
运行截图

问题1:会导致总价计算错误
问题2:过滤无效输入,直接进入下一循坏判断
实验任务3
task.3.c源代码
1 #include <stdio.h> 2 3 int main() { 4 char color; 5 while (scanf("%c", &color) == 1) { 6 getchar(); 7 switch (color) { 8 case 'r': // 输入 r (red) 9 printf("stop!\n"); 10 break; 11 case 'g': // 输入 g (green) 12 printf("go go go\n"); 13 break; 14 case 'y': // 输入 y (yellow) 15 printf("wait a minute\n"); 16 break; 17 default: // 输入其他任意字符 18 printf("something must be wrong...\n"); 19 break; 20 } 21 } 22 23 return 0; 24 }
运行截图

实验任务4
task.4.c源代码
#include<stdio.h> int main(){ double expense; double total=0; double max_expense; double min_expense; int count=0; printf("请输入今天的若干笔开销,直到输入-1终止\n"); while(1){ scanf("%lf",&expense); if(expense==-1){ break; } if(expense<0||expense>20000){ printf("输入错误\n"); continue; } total+=expense; count++; if(count==1){ max_expense=min_expense=expense; }else{ if(expense>max_expense){ max_expense=expense; } if(expense<min_expense){ min_expense=expense; } } } if(count==0){ printf("输入有误,请重新输入\n"); }else{ printf("今日消费总额:%lf\n",total); printf("今日消费最高额为:%lf\n",max_expense); printf("今日消费最低额为:%lf\n",min_expense); } return 0; }
运行截图

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

实验任务6
test.6.c源代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 int main() { 5 int guess,day; 6 int chance=3; 7 srand(time(0)); 8 day = rand() % 30 + 1; 9 printf("猜猜2026年4月哪一天是你的lucky day\n"); 10 printf("开始喽,你有3次机会,猜吧(1~30):\n"); 11 12 while(chance>0){ 13 scanf("%d",&guess); 14 if(guess==day){ 15 printf("哇,猜中了:)\n"); 16 break; 17 } else if(guess<day) 18 printf("你猜的日期早了,你的lucky day还没到呢\n"); 19 else if(guess>day) 20 printf("你猜的日期晚了,你的lucky day在前面哦\n"); 21 chance--; 22 if(chance>0) 23 printf("再猜(1~30):\n"); 24 } 25 printf("次数用光啦。4月你的lucky day是%d号\n",day); 26 return 0; 27 }



浙公网安备 33010602011771号