实验任务1
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 #define N 5 5 #define N1 80 6 #define N2 35 7 int main() { 8 int cnt; 9 int random_major, random_no; 10 srand(time(NULL)); // 以当前系统时间作为随机种子 11 cnt = 0; 12 while(cnt < N) { 13 random_major = rand() % 2; 14 if(random_major) { 15 random_no = rand() % N1 + 1; 16 printf("20256343%04d\n", random_no); 17 } 18 else { 19 random_no = rand() % N2 + 1; 20 printf("20256136%04d\n", random_no); 21 } 22 cnt++; 23 } 24 return 0; 25 }
截图
这行代码起到随机生成数字的作用,去掉这行代码之后多次运行出来的结果都是一样的
这个程序可以生成5个随机的学号
实验任务2
1 #include <stdio.h> 2 int main() { 3 int choice, quantity; 4 float total_price = 0, amount_paid, change; 5 while (1) { 6 printf("\n自动饮料售卖机菜单:\n"); 7 printf("1. 可乐 - 3 元/瓶\n"); 8 printf("2. 雪碧 - 3 元/瓶\n"); 9 printf("3. 橙汁 - 5 元/瓶\n"); 10 printf("4. 矿泉水 - 2 元/瓶\n"); 11 printf("0. 退出购买流程\n"); 12 printf("请输入饮料编号: "); 13 scanf("%d", &choice); 14 if (choice == 0) 15 break; 16 if (choice < 1 || choice > 4) { 17 printf("无效的饮料编号,请重新输入。\n"); 18 continue; 19 } 20 printf("请输入购买的数量: "); 21 scanf("%d", &quantity); 22 if (quantity < 0) { 23 printf("购买数量不能为负数,请重新输入。\n"); 24 continue; 25 } 26 if(choice == 1 || choice == 2) 27 total_price += 3 * quantity; 28 else if(choice == 3) 29 total_price += 5 * quantity; 30 else 31 total_price += 2 * quantity; 32 printf("请投入金额: "); 33 scanf("%f", &amount_paid); 34 change = amount_paid - total_price; 35 printf("本次购买总价: %.2f 元\n", total_price); 36 printf("找零: %.2f 元\n", change); 37 total_price = 0; 38 } 39 printf("感谢您的购买,欢迎下次光临!\n"); 40 return 0; 41 }
截图
这行代码去除之后购买的总价和找零会出现错误,每次进行购买的总价会不断累加而不是重新开始计算
使用continue可以不退出整个循环而跳过本次循环中的剩余部分开始新的循环
实验任务3
1 #include<stdio.h> 2 int main() 3 { 4 char light; 5 while(scanf("%c",&light)!=EOF) 6 { 7 if(light=='r') 8 { 9 printf("stop!\n"); 10 getchar(); 11 } 12 else if(light=='g') 13 { 14 printf("go go go\n"); 15 getchar(); 16 } 17 else if(light=='y') 18 { 19 printf("wait a minute\n"); 20 getchar(); 21 } 22 else 23 { 24 printf("something must be wrong...\n"); 25 getchar(); 26 } 27 } 28 return 0; 29 }
截图
实验任务4
1 #include<stdio.h> 2 int main() 3 { 4 double x,max,min,s=0; 5 max=0; 6 min=100; 7 printf("输入今日开销,直到输入-1终止:\n"); 8 while(x>0&&x<=20000) 9 { 10 scanf("%lf",&x); 11 if(x>=0) 12 { 13 s=s+x; 14 if(x>max) max=x; 15 if(x<min) min=x; 16 } 17 } 18 printf("今日累计消费总额:%.1lf\n今日最高一笔开销:%.1lf\n今日最低一笔开销:%.1lf\n",s,max,min); 19 return 0; 20 }
截图
实验任务5
1 #include<stdio.h> 2 int main() 3 { 4 int a,b,c; 5 while(scanf("%d %d %d",&a,&b,&c)!=EOF) 6 { 7 if(a+b>c&&a+c>b&&b+c>a) 8 { 9 if(a==b&&b==c) 10 { 11 printf("等边三角形\n"); 12 } 13 else if((a==b&&b!=c)||(b==c&&c!=a)||(a==c&&c!=b)) 14 { 15 printf("等腰三角形\n"); 16 } 17 else if(a*a+b*b==c*c||a*a+c*c==b*b||b*b+c*c==a*a) 18 { 19 printf("直角三角形\n"); 20 } 21 else 22 { 23 printf("普通三角形\n"); 24 } 25 } 26 else 27 { 28 printf("不能构成三角形\n"); 29 } 30 } 31 return 0; 32 }
截图
实验任务6
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 int main() 6 { 7 srand(time(NULL)); 8 int lucky_day = rand() % 30 + 1; 9 10 int guess; 11 int attempts = 3; 12 int guessed_correctly = 0; 13 14 printf("猜猜2025年11月哪一天是你的lucky day\n\n"); 15 printf("开始喽,你有三次机会,猜吧(1~30):"); 16 17 for (int i = 0; i < attempts; i++) { 18 scanf("%d", &guess); 19 20 if (guess == lucky_day) { 21 printf("哇,猜中了:)\n"); 22 guessed_correctly = 1; 23 break; 24 } 25 else if (i < attempts - 1) { 26 if (guess < lucky_day) { 27 printf("\n你猜的日期早了,你的lucky day还没到呢\n\n"); 28 printf("再猜(1~30):"); 29 } else { 30 printf("\n你猜的日期晚了,你的lucky day在前面哦\n\n"); 31 printf("再猜(1~30):"); 32 } 33 } 34 else{ 35 if (guess < lucky_day){ 36 printf("\n你猜的日期早了,你的lucky day还没到呢\n\n"); 37 } 38 else { 39 printf("\n你猜的日期晚了,你的lucky day在前面哦\n\n"); 40 } 41 } 42 } 43 44 if (!guessed_correctly) { 45 printf("次数用光啦。偷偷告诉你,11月你的lucky day是:%d号\n", lucky_day); 46 } 47 48 return 0; 49 }
截图