实验2
实验内容
任务1
源代码
#include <stdio.h> #include <stdlib.h> #include <time.h> #define N 5 int main() { int number; int i; srand(time(0)); // 以当前系统时间作为随机种子 for(i = 0; i < N; ++i) { number = rand() % 100 + 1; printf("20490042%04d\n", number); } return 0; }
实验结果截图

问题
1.随机生成5个专业学员编号。
2.生成1到100的随机数。
3.生成四位数,前面用0补齐。
4.读取当前时间,使每次输出的结果变化。
任务2
源代码
#include <stdio.h> int main() { int choice, quantity; float total_price = 0, amount_paid, change; while (1) { printf("\n自动饮料售卖机菜单:\n"); printf("1. 可乐 - 3 元/瓶\n"); printf("2. 雪碧 - 3 元/瓶\n"); printf("3. 橙汁 - 5 元/瓶\n"); printf("4. 矿泉水 - 2 元/瓶\n"); printf("0. 退出购买流程\n"); printf("请输入饮料编号: "); scanf("%d", &choice); if (choice == 0) break; if (choice < 1 || choice > 4) { printf("无效的饮料编号,请重新输入。\n"); continue; } printf("请输入购买的数量: "); scanf("%d", &quantity); if (quantity < 0) { printf("购买数量不能为负数,请重新输入。\n");continue; } if(choice == 1 || choice == 2) total_price += 3 * quantity; else if(choice == 3) total_price += 5 * quantity; else total_price += 2 * quantity; printf("请投入金额: "); scanf("%f", &amount_paid); change = amount_paid - total_price; printf("本次购买总价: %.2f 元\n", total_price); printf("找零: %.2f 元\n", change); total_price = 0; } printf("感谢您的购买,欢迎下次光临!\n");return 0; }
实验结果截图

问题
1.使每次循环开始时total_price的值为0;
2.使循环返回开始时,重复进行,重新开始。
任务3
源代码
#include<stdio.h> #include<stdlib.h> int main(){ char color; while(scanf("%c",&color)==1){ while(getchar()!='\n'); switch(color){ case'r':printf("stop!\n");break; case'g':printf("go go go\n");break; case'y':printf("wait a minute\n");break; default:printf("something must be wrong\n");break; } } system("pause"); return 0; }
实验结果截图

任务4
源代码
#include <stdio.h> #include <stdlib.h> int main(){ printf("输入今日开销,直到输入-1终止:\n"); double exp; double total=0.0,max=0.0,min=20000; while (1){ scanf("%lf",&exp); if(exp==-1) break; if(exp<=0||exp>=20000){ continue; } total+=exp; if(exp>max) max=exp; if(exp<min) min=exp; } printf("今日累计消费总额:%.1f\n",total); printf("今日最高一笔开销:%.1f\n",max); printf("今日最低一笔开销:%.1f\n",min); system("pause"); return 0; }
实验结果截图

任务5
源代码
#include <stdio.h> #include <stdlib.h> int main() { int a,b,c,t; while (scanf("%d %d %d", &a, &b, &c) != EOF) { if (a > b) { t = a; a = b; b = t; } if (b > c) { t = b; b = c; c = t; } if (a > b) { t = a; a = b; b = t; } if (a + b <= c) printf("不能构成三角形\n"); else { if (a == b && b == c) printf("等边三角形\n"); else if (a == b || b == c) if (a*a + b*b == c*c) printf("直角三角形\n"); else printf("等腰三角形\n"); else if (a*a + b*b == c*c) printf("直角三角形\n"); else printf("普通三角形\n"); } } system("pause"); return 0; }
实验结果截图

任务6
源代码
#include <stdio.h> #include <stdlib.h> int main() { int lucky_day = 1 + rand() % 30; int guess,i; int attempts = 3; printf("猜猜2026年4月哪一天是你的lucky day\n"); printf("开始喽,你有3次机会,猜吧(1-30):"); for (i = 1; i <= attempts; i++) { scanf("%d", &guess); if (guess == lucky_day) { printf("哇,猜中了:)\n"); break; } else if (guess < lucky_day) printf("你猜的日期早了,你的lucky day还没到呢\n"); else printf("你猜的日期晚了,你的lucky day在前面哦\n"); if (i < attempts) printf("再猜(1-30):"); } if (i >attempts) { printf("次数用光啦。4月你的lucky day是%d号\n", lucky_day); } system("pause"); return 0; }
实验结果截图

实验总结
通过本次实验,我熟练学习了if,while,switch等循环的用法,更加理解代码的关系。
浙公网安备 33010602011771号