实验2
task1
代码
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include<time.h> 5 6 #define N 5 7 #define N1 80 8 #define N2 35 9 10 int main() { 11 int cnt; 12 int random_major, random_no; 13 srand(time(NULL)); // 以当前系统时间作为随机种子 14 cnt = 0; 15 while (cnt < N) { 16 random_major = rand() % 2; 17 if (random_major) { 18 random_no = rand() % N1 + 1; 19 printf("20256343%04d\n", random_no); 20 } 21 else { 22 random_no = rand() % N2 + 1; 23 printf("20256136%04d\n", random_no); 24 } 25 cnt++; 26 } 27 return 0; 28 }
截图

代码 srand(time(NULL))的作用:保证每次程序运行时,rand() 生成的随机数序列都不同,避免每次运行都得到相同的“随机”结果。
这个程序的功能是: 随机生成 N 个学号,前缀根据专业不同,后四位为 1~N1 或 1~N2 的随机数,补零对齐。
task2
代码
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <stdlib.h> 4 int main() { 5 int choice, quantity; 6 float total_price = 0, amount_paid, change; 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 if (choice == 0) 17 break; 18 if (choice < 1 || choice > 4) { 19 printf("无效的饮料编号,请重新输入。\n"); 20 continue; 21 } 22 printf("请输入购买的数量: "); 23 scanf("%d", &quantity); 24 if (quantity < 0) { 25 printf("购买数量不能为负数,请重新输入。\n"); 26 continue; 27 } 28 if (choice == 1 || choice == 2) 29 total_price += 3 * quantity; 30 else if (choice == 3) 31 total_price += 5 * quantity; 32 else 33 total_price += 2 * quantity; 34 printf("请投入金额: "); 35 scanf("%f", &amount_paid); 36 change = amount_paid - total_price; 37 printf("本次购买总价: %.2f 元\n", total_price); 38 printf("找零: %.2f 元\n", change); 39 total_price = 0; 40 } 41 printf("感谢您的购买,欢迎下次光临!\n"); 42 return 0; 43 }
截图

代码 total_price = 0:保证每次循环购买时,total_price 都是从 0 开始计算,不会累加上一次的金额,避免金额计算错误
continue:终止此次循环,进行下一次循环
task3
代码
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <stdlib.h> 4 int main() { 5 char ch; 6 while(scanf(" %c",&ch)!=EOF){ 7 if(ch=='r') 8 printf("stop!\n"); 9 else if(ch=='g') 10 printf("go go go\n"); 11 else if(ch=='y') 12 printf("wait a minute\n"); 13 else 14 printf("something must be wrong...\n"); 15 } 16 17 return 0; 18 }
截图

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

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

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


浙公网安备 33010602011771号