实验2
实验课程2
实验任务1
代码:

1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 #define N 5 6 7 int main() { 8 int number; 9 int i; 10 11 srand(time(0)); // 以当前系统时间作为随机种子 12 for(i = 0; i < N; ++i) { 13 number = rand() % 100 + 1; 14 printf("20490042%04d\n", number); 15 } 16 17 system("pause"); 18 19 return 0; 20 }
截图:
问题1:用来生成一个0~100的随机整数并赋值给number。
问题2:“d”用来生成十进制整数,“4”是生成四位数,“0”是左边用0补位,“%”是取上面生成的数值,即生成一个含有上面生成数值的左侧用0补全的四位数。
问题3:随机生成四个“2049004200xx”的数(xx是0~100的随机数)
实验任务2
代码:

1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 int main() { 6 int choice, quantity; 7 float total_price = 0, amount_paid, change; 8 while (1) { 9 printf("\n自动饮料售卖机菜单:\n"); 10 printf("1. 可乐 - 3 元/瓶\n"); 11 printf("2. 雪碧 - 3 元/瓶\n"); 12 printf("3. 橙汁 - 5 元/瓶\n"); 13 printf("4. 矿泉水 - 2 元/瓶\n"); 14 printf("0. 退出购买流程\n"); 15 printf("请输入饮料编号: "); 16 scanf("%d", &choice); 17 18 if (choice == 0) 19 break; 20 21 if (choice < 1 || choice > 4) { 22 printf("无效的饮料编号, 请重新输入。 \n"); 23 continue; 24 } 25 26 printf("请输入购买的数量: "); 27 scanf("%d", &quantity); 28 29 if (quantity < 0) { 30 printf("购买数量不能为负数, 请重新输入。 \n"); 31 continue; 32 } 33 34 switch (choice) { 35 case 1: 36 case 2: 37 total_price += 3 * quantity; 38 break; 39 case 3: 40 total_price += 5 * quantity; 41 break; 42 case 4: 43 total_price += 2 * quantity; 44 break; 45 } 46 47 printf("请投入金额: "); 48 scanf("%f", &amount_paid); 49 50 change = amount_paid - total_price; 51 printf("本次购买总价: %.2f 元\n", total_price); 52 printf("找零: %.2f 元\n", change); 53 54 total_price = 0; 55 } 56 57 printf("感谢您的购买, 欢迎下次光临! \n"); 58 59 system("pause"); 60 61 return 0; 62 }
截图:
问题1:将总价钱(total-price)重置为0,是下次循环不受影响。
问题2:“break”是结束对应括号内循环的购买等代码,“continue”则是重新进行循环的购买等代码。
问题3:我认为不需要,因为default常用于多个“switch”分支,但此程序只有一个,如果报错比较容易就能找到错误位置。
实验任务3
代码:

1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <time.h> 5 6 int main() { 7 8 printf("输入信号灯颜色:"); 9 char ans; 10 while (scanf("%c", &ans) != EOF) { 11 getchar(); 12 13 switch (ans) { 14 case'r': 15 printf("stop!\n"); 16 break; 17 case'g': 18 printf("go go go\n"); 19 break; 20 case'y': 21 printf("wait a minute\n"); 22 break; 23 default: 24 printf("something must be wrong...\n"); 25 break; 26 } 27 } 28 29 system("pause"); 30 31 return 0; 32 }
截图:
实验任务4
代码:

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

1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <time.h> 5 6 int main() { 7 8 int LuckyDay, guess, chance = 3; 9 srand(time(0)); 10 LuckyDay = rand() % 30 + 1; 11 12 printf("猜猜2025年4月哪一天是你的LuckyDay?\n"); 13 while (chance > 0) { 14 printf("开始喽,你还有 %d 次机会,猜吧(1-30)\n",chance); 15 scanf("%d", &guess); 16 17 if (guess == LuckyDay) { 18 printf("恭喜你,猜对了!\n"); 19 return 0; 20 } 21 else if (guess > LuckyDay) { 22 printf("你猜的日期晚了\n"); 23 } 24 else { 25 printf("你猜的日期早了\n"); 26 } 27 chance--; 28 29 if (chance > 0) { 30 printf("再试一次吧\n"); 31 } 32 } 33 printf("很遗憾,你用光了三次机会,你的LuckyDay是 %d 号\n",LuckyDay); 34 35 system("pause"); 36 37 return 0; 38 }
截图:
实验任务6
代码:

1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <time.h> 5 6 int main() { 7 int i, n, line; 8 line = 0; 9 scanf("%d", &n); 10 11 for (i = 0; line < n; ++line){ 12 for (i = 0; line < n; ){ 13 while (i < 2 * n - 1){ 14 if (i >= line && i < 2 * n - 1 - line){ 15 printf(" O "); 16 ++i; 17 } 18 else{ 19 printf(" "); 20 ++i; 21 } 22 } 23 printf("\n"); 24 i = 0; 25 while (i < 2 * n - 1){ 26 if (i >= line && i < 2 * n - 1 - line){ 27 printf("<H> "); 28 ++i; 29 } 30 else 31 { 32 printf(" "); 33 ++i; 34 } 35 } 36 printf("\n"); 37 i = 0; 38 while (i < 2 * n - 1){ 39 if (i >= line && i < 2 * n - 1 - line) 40 { 41 printf("I I "); 42 ++i; 43 } 44 else 45 { 46 printf(" "); 47 ++i; 48 } 49 } 50 printf("\n"); 51 break; 52 } 53 54 } 55 56 57 system("pause"); 58 59 return 0; 60 }
截图: