1.实验任务1
task1.c
源代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 #define N 5 5 int main() 6 { 7 int number; 8 int i; 9 srand(time(0)); 10 for(i=0;i<N;++i) 11 { 12 number = rand() % 100 + 1; 13 printf("20490042%04d\n",number); 14 } 15 return 0; 16 }
运行结果截图

回答问题
问题1:随机生成一个介于204900420001~204900420100的数字
问题2:赋值给number一个1~100之间的随机数
问题3:能占四位,左边用0补位
问题4:每一次运行程序,给出的随机数都不相同
2.实验任务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 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之后的剩余代码,直接开始下次循环。
3.实验任务3
task3.c
源代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 int main() 4 { 5 char colour; 6 while((colour = getchar())!=EOF) 7 { 8 getchar(); 9 switch(colour) 10 { 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");break; 15 } 16 17 } 18 system("pause"); 19 return 0; 20 }
运行结果截图

4.实验任务4
task4.c
源代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 int main() 4 { 5 double total,x,max,min; 6 total=0; 7 printf("输入今日开销,直至输入-1终止\n"); 8 scanf("%lf",&x); 9 max=0; 10 min=20000; 11 while(x!=-1) 12 { 13 if(x<0||x>20000) 14 { 15 printf("error input"); 16 break; 17 } 18 total+=x; 19 if(x>max) 20 max=x; 21 if(x<min) 22 min=x; 23 scanf("%lf",&x); 24 } 25 printf("今日累计消费总额:%.1lf\n",total); 26 printf("今日最高一笔开销:%.1lf\n",max); 27 printf("今日最低一笔开销:%.1lf\n",min); 28 system("pause"); 29 return 0; 30 }
运行结果截图

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

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


实验总结
实验六的问题所在是:最后1次刚好猜中应该直接显示“哇,猜中了”,不应该再显示“次数用光了,4月你的lucky day是 号”,不知道怎么改代码可以做到。
感悟是自己敲键盘速度太慢了还不熟练,也总是忘记最后要加分号,以后要注意。