实验二
task 1
源代码:
#include<stdio.h> #include<stdlib.h> #include<time.h> #define N 5 #define N1 80 #define N2 35 int main(){ int cnt; int random_major,random_no; srand(time(NULL)); cnt = 0; while(cnt < N){ random_major = rand() % 2; if(random_major){ random_no = rand() % N1 + 1; printf("20256343%04d\n",random_no); } else{ random_no = rand() % N2 + 1; printf("20256136%04d\n",random_no); } cnt++; } return 0; }
编译结果:

回答:
1.未删除时每一次的编译结果都不同,删除后每一次的编译结果均与第一次结果相同。srand(time(NULL))应该起到一个以时间作为参数随机取数的作用。
【srand是为伪随机数生成器设计随机种子,确保每次运行程序能生成不同的随机数序列。若不调用srand,每次程序启动时的种子会默认相同。】
2.这个程序的功能应该是用来随机抽取学号。
task 2
源代码:
1 #include<stdio.h> 2 3 int main(){ 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 }
编译结果:

回答:
1.去掉后,程序每次运行后总价不会归零,而是一直叠加。
2.循环中使用continue语句,是跳过本次循环中continue之后的代码,直接进入下一次循环的循环条件。在此代码中就是重新打印饮料菜单,等待顾客输入编号。
task 3
源代码:
1 #include<stdio.h> 2 3 int main(){ 4 char color; 5 6 7 while(scanf("%c",&color) != EOF){ 8 if(color == 'r') 9 printf("stop!\n"); 10 else if(color == 'g') 11 printf("go go go\n"); 12 else if(color == 'y') 13 printf("wait a minute\n"); 14 else 15 printf("something must be wrong...\n"); 16 getchar(); 17 } 18 return 0; 19 }
编译结果:

注意点:getchar()在程序中用来读取回车
task 4
源代码:
1 #include<stdio.h> 2 3 int main(){ 4 double pay,sum=0,max,min; 5 printf("输入今日开销,直到输入-1终止:\n"); 6 7 while(1){ 8 scanf("%lf",&pay); 9 10 if(pay == -1) 11 break; 12 13 if(pay > max) 14 max = pay; 15 16 if(pay < min) 17 min = pay; 18 19 sum = sum + pay; 20 } 21 printf("今日累计消费总额:%.1f\n",sum); 22 printf("今日最高一笔开销:%.1f\n",max); 23 printf("今日最低一笔开销:%.1f\n",min); 24 25 return 0; 26 }
编译结果:

注意点:double型输入时用%lf,输出时用%f
task 5
源代码:
1 #include<stdio.h> 2 3 int main(){ 4 int a,b,c; 5 6 while(scanf("%d %d %d",&a,&b,&c) != EOF){ 7 if(a+b > c && a+c > b && b+c >a){ 8 if (a == b && b == c) 9 printf("等边三角形\n"); 10 else if(a == b || a == c || b == c) 11 printf("等腰三角形\n"); 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 return 0; 21 }
编译结果:

注意点:判断等边三角形时不能写连等式
task 6
源代码:
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 5 int main(){ 6 int random,i=0,m; 7 srand(time(NULL)); 8 printf("猜猜2025年11月哪一天是你的lucky day\n"); 9 printf("开始喽,你有三次机会,猜吧(1~30):"); 10 random = rand() % 30 +1; 11 12 while(i < 3){ 13 scanf("%d",&m); 14 i = i+1; 15 16 if(random == m){ 17 printf("哇,猜中了:)"); 18 break; 19 } 20 else if (random < m){ 21 printf("你的日期猜晚了,你的lucky day在前面哦\n"); 22 printf("再猜(1~30):") ; 23 } 24 else{ 25 printf("你猜的日期早了,你的lucky day还没到呢\n"); 26 printf("再猜(1~30):") ; 27 } 28 getchar(); 29 if(i == 3) 30 printf("次数用光啦。偷偷告诉你,11月你的lucky day是%d号",random); 31 } 32 return 0; 33 }
编译结果:

浙公网安备 33010602011771号