试验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 }
问题回答:1.用来生成随机种子 以当前时间为标准 时间在变 其值也在变2.去掉之后运行结果都相同不变了
试验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 }
实验结果
问题回答:1.没有total_price会导致除第一次购买 以后每次在前一次的总价基础上继续付款 不会重新计算总价2.结束本次循环 开始下一次循环
实验3
源代码

1 #include<stdio.h> 2 int main(){ 3 char a; 4 while((a=getchar())!=EOF){ 5 if(a=='\n'){ 6 continue; 7 } 8 if(a=='r'){ 9 printf("stop\n"); 10 } 11 else if(a=='g'){ 12 printf("gogogo\n"); 13 } 14 else if(a=='y'){ 15 printf("wait a minute\n"); 16 } 17 else{ 18 printf("something wrong try again\n"); 19 } 20 while(getchar()!='\n'); 21 } 22 return 0; 23 }
实验结果
实验4
源代码

1 #include<stdio.h> 2 int main(){ 3 double expense; 4 double total=0.0; 5 double max=0.0; 6 double min=20000.0; 7 printf("输入今日的开销,直到输入-1终止\n"); 8 while(1){ 9 scanf("%lf",&expense); 10 if(expense==-1){ 11 break; 12 } 13 if(expense>0&&expense<=20000){ 14 total+=expense; 15 if(expense>max){ 16 max=expense; 17 } 18 if(expense<min){ 19 min=expense; 20 } 21 } 22 } 23 printf("总:%.1f,最高:%1f,最低:%.1f\n",total,max,min); 24 return 0; 25 }
实验结果
实验5
源代码

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

1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 int main(){ 5 srand((unsigned int)time(NULL)); 6 int luckyDay=rand()%30+1; 7 int guess,chance=3; 8 printf("猜猜2025年11月哪一天是你的luckyday\n"); 9 while(chance>0){ 10 printf("开始咯,你有%d次机会,猜吧(1~30):",chance); 11 scanf("%d",&guess); 12 if(guess==luckyDay){ 13 printf("哇 猜中了:)\n"); 14 return 0; 15 }else if(guess<luckyDay){ 16 printf("你猜的日期早了,你的luckyday在后面\n"); 17 }else{ 18 printf("你的日期猜晚了,你的luckyday在前面\n"); 19 } 20 chance--; 21 if(chance>0){ 22 printf("再猜(1~30):"); 23 } 24 } 25 printf("次数用完了 偷偷告诉你,11月的luckyday是%d号\n",luckyDay); 26 return 0; 27 }
实验结果