实验二
实 验 1
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 5 #define N 5 6 #define N1 80 7 #define N2 35 8 9 int main() 10 { 11 int cnt; 12 int random_major, random_no; 13 14 srand(time(NULL)); 15 16 cnt = 0; 17 while (cnt < N) 18 { 19 random_major = rand() % 2; 20 21 if (random_major) 22 { 23 random_no = rand() % N1 + 1; 24 printf("20256343%04d\n", random_no); 25 } 26 else 27 { 28 random_no = rand() % N2 + 1; 29 printf("20256343%04d\n", random_no); 30 } 31 32 cnt++; 33 } 34 35 return 0; 36 }
问题1:获取当前时间
问题2:抽取5个倒霉蛋儿
实 验 2
1 #include<stdio.h> 2 3 int main() 4 { 5 int choice, quality; 6 float total_price = 0, amount_paid, change; 7 8 while (1) 9 { 10 printf("\n自动饮料售卖机菜单:\n"); 11 printf("1. 可乐 - 3 元/瓶\n"); 12 printf("2. 雪碧 - 3 元/瓶\n"); 13 printf("3. 橙汁 - 5 元/瓶\n"); 14 printf("4. 矿泉水 - 2 元/瓶\n"); 15 printf("0. 退出购买流程\n"); 16 printf("请输入饮料编号:"); 17 scanf("%d", &choice); 18 19 if (choice == 0) 20 break; 21 22 if (choice < 1 || choice > 4) 23 { 24 printf("无效的饮料编号,请重新输入。\n"); 25 continue; 26 } 27 printf("请输入购买的数量:"); 28 scanf("%d", &quality); 29 30 if (quality < 0) 31 { 32 printf("购买数量不能为负数,请重新输入。\n"); 33 continue; 34 } 35 if (choice == 1 || choice == 2) 36 total_price += 3 * quality; 37 else if (choice == 3) 38 total_price += 5 * quality; 39 else 40 total_price += 2 * quality; 41 42 printf("请投入金额:"); 43 scanf("%f", &amount_paid); 44 45 change = amount_paid - total_price; 46 printf("本次购买总价:%.2f 元\n", total_price); 47 printf("找零:%.2f 元\n", change); 48 49 total_price = 0; 50 } 51 52 printf("感谢您的购买,欢迎下次光临!\n"); 53 return 0; 54 }
问题1:总价被保留到下一个循环
问题2:立即执行下一个循环
实 验 3
1 #include<stdio.h> 2 int main() 3 { 4 char light = 0; 5 while (light != EOF) 6 { 7 light = getchar(); 8 if (light == 'r') 9 printf("stop!\n"); 10 else if (light == 'g') 11 printf("go go go\n"); 12 else if (light == 'y') 13 printf("wait a minute\n"); 14 else 15 printf("something must be wrong...\n"); 16 getchar(); 17 light = 0; 18 } 19 return 0; 20 }
实 验 4
1 #include<stdio.h> 2 3 int main() 4 { 5 double $ = 0; 6 double all = 0; 7 double d = 0; 8 double x = 99999; 9 while (1) 10 { 11 scanf_s("%lf", &$); 12 if ($ == -1) 13 break; 14 if ($ <= 0 || $ > 20000) 15 continue; 16 all += $; 17 if ($ > d) 18 d = $; 19 if ($ < x) 20 x = $; 21 } 22 printf("今日累计消费总额:%.1f\n", all); 23 printf("今日最高一笔开销:%.1f\n", d); 24 printf("今日最低一笔开销:%.1f\n", x); 25 return 0; 26 }
实 验 5
1 #include<stdio.h> 2 3 int main() 4 { 5 int a, b, c; 6 while (scanf_s("%d%d%d", &a, &b, &c) != EOF) 7 { 8 if (a + b <= c || a + c <= b || b + c <= a) 9 { 10 printf("不能构成三角形\n"); 11 continue; 12 } 13 if (a == b && b == c) 14 printf("等边三角形\n"); 15 else if (a == b || a == c || b == c) 16 printf("等腰三角形\n"); 17 else if (a* a + b * b == c * c || a * a + c * c == b * b || b * b + c * c == a * a) 18 printf("直角三角形\n"); 19 else 20 printf("普通三角形\n"); 21 } 22 return 0; 23 }
实 验 6
1 #include<stdio.h> 2 #include<time.h> 3 #include<stdlib.h> 4 int main() 5 { 6 srand(time(NULL)); 7 int ld = rand() % 30 + 1; 8 int ans; 9 int n = 3; 10 int i; 11 printf("猜猜2025年11月哪一天是你的lucky day\n\n"); 12 printf("开始喽,你有三次机会,猜吧(1~30):"); 13 for (i = 0; i < n; i++) 14 { 15 scanf_s("%d", &ans); 16 printf("\n"); 17 if (ans == ld) 18 { 19 break; 20 } 21 else if (ans > ld) 22 { 23 printf("你猜的日期晚了,你的lucky day在前面哦\n\n"); 24 while (i < n-1) 25 { 26 printf("再猜(1~30):"); 27 break; 28 } 29 } 30 else 31 { 32 printf("你猜的日期早了,你的lucky day还没到呢\n\n"); 33 while (i < n-1) 34 { 35 printf("再猜(1~30):"); 36 break; 37 } 38 } 39 } 40 if (ans == ld) 41 { 42 printf("lucky day"); 43 } 44 else 45 { 46 printf("次数用光了。偷偷告诉你,11月你的lucky day是%d号", ld); 47 } 48 return 0; 49 }