实验二:C语言分支与循环基础应用编程
实验二: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 17 return 0; 18 }

问题一:生成五个随机数值
问题二:生成一个随机数除以100取余即0~99的区间,再加一即1~100
问题三:输入上面生产的数字,不足四位的由0补齐,加在数字前面
问题四:以当前时间为节点,不同时间即导致生成数字不同,完成生成随机数的目的
任务二
1 #include <stdio.h> 2 int main() { 3 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 }

问题一:去掉会影响下一次程序运行,导致购物金额无法清零,下一次金额为从前的累加金额。
问题二:continue会跳过本次循环,进行下一循环。
任务三
1 #include <stdio.h> 2 int main() { 3 char ch; 4 5 while(scanf("%c",&ch)!=EOF){ 6 if(ch=='\n')continue; 7 if(ch=='r') 8 printf("stop!\n"); 9 10 else if(ch=='g') 11 printf("go go go\n"); 12 13 else if(ch=='y') 14 printf("wait a minute\n"); 15 16 else 17 printf("something must be wrong..."); 18 19 } 20 return 0; 21 }

任务四
1 #include<stdio.h> 2 int main(){ 3 double cost,total=0; 4 double max_cost=0,min_cost=20000; 5 int count=0; 6 7 printf("输入今日开销,直到输入-1终止:\n"); 8 while(scanf("%lf",&cost)!=EOF){ 9 if(cost==-1){ 10 break; 11 } 12 if(cost>0&&cost<=20000){ 13 total+=cost; 14 count++; 15 if(cost>max_cost){ 16 max_cost=cost; 17 } 18 if(cost<min_cost){ 19 min_cost=cost; 20 } 21 } 22 } 23 printf("今日累计消费总额:%.1f\n",total); 24 printf("今日最高一笔开销:%.1f\n",max_cost); 25 printf("今日最低一笔开销:%.1f\n",min_cost); 26 return 0; 27 28 }

任务五
1 #include<stdio.h> 2 int main(){ 3 4 int a,b,c; 5 printf("输入三角形三边;\n"); 6 7 while(scanf("%d %d %d",&a,&b,&c)!=EOF){ 8 9 if(a+b<=c||a+c<=b||b+c<=a){ 10 printf("不能构成三角形\n"); 11 continue; 12 } 13 14 if(a==b&&b==c){ 15 printf("等边三角形\n"); 16 } 17 18 else if(a==b||a==c||b==c){ 19 printf("等腰三角形\n"); 20 } 21 22 else if(a*a+b*b==c*c||a*a+c*c==b*b||b*b+c*c==a*a){ 23 printf("直角三角形\n"); 24 } 25 26 else{ 27 printf("普通三角形\n"); 28 } 29 30 } 31 return 0; 32 }

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


浙公网安备 33010602011771号