实验2
实验任务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 }
保留srand:
删除srand:
问题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 { 4 char x; 5 while(scanf("%c",&x)!=EOF) 6 { getchar(); 7 if(x=='r') 8 printf("stop!\n"); 9 else if(x=='g') 10 printf("go go go\n"); 11 else if(x=='y') 12 printf("wait a minute\n"); 13 else 14 printf("something must be wrong...\n"); 15 } 16 return 0; 17 }
实验任务4

1 #include <stdio.h> 2 int main() 3 { 4 double s=0,x,y=20000,z=0; 5 printf("输入今日开销,直到输入-1终止:\n"); 6 while (1) 7 { 8 scanf("%lf",&x); 9 s=s+x; 10 if (x==-1) 11 break; 12 if (x<y) 13 y=x; 14 if (x>z) 15 z=x; 16 } 17 printf("今日累计消费总额:%.1lf\n",s); 18 printf("今日最高一笔开销:%.1lf\n",z); 19 printf("今日最低一笔开销:%.1lf\n",y); 20 system("pause"); 21 return 0; 22 }
实验任务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 printf("等边三角形"); 11 else if(a==b||b==c||a==c) 12 printf("等腰三角形"); 13 else if (a*a+b*b==c*c||a*a+c*c==b*b||b*b+c*c==a*a) 14 printf("直角三角形"); 15 else 16 printf("普通三角形"); 17 } 18 else 19 printf("不能构成三角形"); 20 printf("\n"); 21 } 22 return 0; 23 }
实验任务6

1 #define _CRT_SECURE_NO_WARNINGS 2 #include<stdio.h> 3 #include<stdlib.h> 4 int main() 5 { 6 printf("猜猜2025年11月哪一天是你的lucky day\n"); 7 int number = rand() % 30 + 1; 8 int i = 0, a; 9 printf("开始咯,你有三次机会,猜吧(1~30):"); 10 while (i < 3) 11 { 12 scanf("%d", &a); 13 if (number < a) 14 { 15 printf("你猜的日期晚了,你的lucky day在前面哦\n"); 16 i++; 17 } 18 else if (number > a) 19 { 20 printf("你猜的日期早了,你的lucky day还没到呢\n"); 21 i++; 22 } 23 else 24 { 25 printf("哇,猜中了:)"); 26 break; 27 } 28 29 if (i == 3) 30 { 31 printf("次数用光啦。偷偷告诉你,11月你的lucky day是%d号", number); 32 break; 33 } 34 printf("再猜(1~30):"); 35 } 36 37 return 0; 38 }