实验2 C语言分支与循环基础应用编程
实验结论
任务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 system("pause"); 17 return 0; 18 }
运行结果:

回答问题:
1.生成一到一百之间(包含1和100)的随机数
2.在生成的随机数左端补0,使其成为一个四位数组合
3.生成五个介于1到100之间(包含1和100)的随机数,并按照特定格式“20490042”加上4位补0的整数形式输出。
任务2
源代码:
1 #include <stdio.h> 2 #include <stdlib.h> 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 switch (choice) { 34 case 1: 35 case 2: 36 total_price += 3 * quantity; 37 break; 38 case 3: 39 total_price += 5 * quantity; 40 break; 41 case 4: 42 total_price += 2 * quantity; 43 break; 44 } 45 46 printf("请投入金额: "); 47 scanf("%f", &amount_paid); 48 49 change = amount_paid - total_price; 50 printf("本次购买总价: %.2f 元\n", total_price); 51 printf("找零: %.2f 元\n", change); 52 53 total_price = 0; 54 } 55 56 printf("感谢您的购买,欢迎下次光临!\n"); 57 system("pause"); 58 return 0; 59 }
运行结果:

回答问题:
1.用途是在每完成一次交易后将总价重置为零,如果去掉,在多次购买时,总价会不断累加,将前一次的总价累加到下一次;
2.break语句会终止并跳出当前所在的循环,继续执行循环体后面的代码,即直接结束整个购买流程;continue语句会使程序跳过本次循环中continue后面的代码,直接回到循环的条件判断处,开始下一次循环;
3.没必要,理由:在进入 switch (choice) 语句之前,程序已经通过 if (choice < 1 || choice > 4) 对 choice 的值进行了有效性检查,只有当 choice 的值为1、2、3、4或者0时程序才会继续执行后续代码。而当 choice 为0时,会通过 break 语句跳出循环,不会进入 switch 语句。所以进入 switch 语句时, choice 的值必然是1、2、3、4中的一个,不存在其他可能的值,也就不会出现需要 default 子句来处理的意外情况,因此没必要增加 default 子句。
任务3
源代码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() 5 { 6 char c; 7 8 while(scanf("%c",&c) != EOF) 9 { 10 11 12 switch(c){ 13 case'r': printf("stop!\n"); break; 14 case'g': printf("go go go\n"); break; 15 case'y': printf("wait a minute\n"); break; 16 default: printf("something must be wrong..."); 17 } 18 while((getchar()) != '\n'); 19 } 20 system("pause"); 21 return 0; 22 }
运行结果:

任务4
源代码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main(){ 5 float cost, high_cost = 0.0, low_cost = 0.0, total_cost = 0.0; 6 printf("输入今日开销,直到输入-1终止:\n"); 7 scanf("%f", &cost); 8 high_cost = cost; 9 low_cost = cost; 10 while(cost >=0 ){ 11 total_cost = total_cost + cost; 12 if(cost > high_cost) high_cost = cost; 13 if(cost < low_cost) low_cost = cost; 14 scanf("%f",&cost); 15 16 } 17 printf("今日累计消费总额:%.1f\n", total_cost); 18 printf("今日最高一笔开销:%.1f\n", high_cost); 19 printf("今日最低一笔开销:%.1f\n", low_cost); 20 21 system("pause"); 22 23 return 0; 24 }
运行结果:

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

任务6
源代码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() { 5 int n, i, j, k; 6 printf("input n:"); 7 scanf("%d", &n); 8 9 for (i = 2*n-1; i >= 1; i=i-2) { 10 int tab_count = (2*n -1-i)/2; 11 for(k = 0; k < tab_count; k++){ 12 printf("\t"); 13 } 14 for (j = 0; j < i; j++) { 15 printf(" 0 "); 16 printf(" "); 17 } 18 printf("\n"); 19 tab_count = (2*n -1-i)/2; 20 for(k = 0; k < tab_count; k++){ 21 printf("\t"); 22 } 23 24 for (j = 0; j < i; j++) { 25 printf("<H>"); 26 printf(" "); 27 } 28 printf("\n"); 29 tab_count = (2*n -1-i)/2; 30 for(k = 0; k < tab_count; k++){ 31 printf("\t"); 32 } 33 34 for (j = 0; j < i; j++) { 35 printf("I I"); 36 printf(" "); 37 } 38 printf("\n"); 39 } 40 41 system("pause"); 42 return 0; 43 }
运行结果:

实验总结:
通过本次实验,基本掌握了条件、多分支和循环语句,并尝试了多种语句的嵌套。我发现有些实验虽然看起来陌生,但是梳理之后,逻辑和之前见过的某些程序是一样的。这次实验减少了对AI工具的依赖,虽然耗时更久,但是写出的程序能够成功运行时,带来的成就感会更大。
浙公网安备 33010602011771号