实验2 C语言分支与循环基础应用编程
实验一
源代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 #define N 5 5 int main() { 6 int number; 7 int i; 8 srand(time(0)); // 以当前系统时间作为随机种子 9 for(i = 0; i < N; ++i) { 10 number = rand() % 100 + 1; 11 printf("20490042%04d\n", number); 12 } 13 return 0; 14 15 }


问题一:在序号范围内随机抽选数字
问题二:在1-100中抽选数字
问题三:在20240042后占四位数字
问题四:去掉srand(time(0));之后多次运行代码,程序抽选的数字不再改变;而有这行代码时,可以以当前系统时间作为随机种子,从而使每次抽选的数字都不同。
实验二
源代码
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 42 }


问题一:如果去掉,单词购买结束后总价不会清零,使得每次循环的价格会叠加在一起,从而找零的结果也出现问题。
问题二:continue功能:结束本次循环,开始下一次循环。
实验三
源代码
1 #include <stdio.h> 2 3 int main() 4 { 5 char ans; 6 7 while(scanf("%c", &ans) !=EOF) 8 {if(ans=='r') 9 {printf("stop!\n"); } 10 else if(ans=='g') 11 {printf("go go go\n");} 12 else if(ans=='y') 13 {printf("wait a minute\n");} 14 else 15 {printf("something must be wrong\n");} 16 getchar(); 17 } 18 return 0; 19 }

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

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

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



浙公网安备 33010602011771号