实验2 C语言分支与循环基础应用编程
实验任务1
程序代码
#include <stdio.h> #include <stdlib.h> #include <time.h> #define N 5 int main() { int number; int i; srand(time(0)); // 以当前系统时间作为随机种子 for(i = 0; i < N; ++i) { number = rand() % 100 + 1; printf("20490042%04d\n", number); } return 0; }
结果截图

回答1. 功能是生成一个1到100之间的随机整数
2. 4表示这个整数应该至少占用4个字符的宽度,如果整数的位数少于4位,则在前面填充0。d表示这是一个十进制整数。目的在于保证编号的固有长度
3. 功能是生成并打印出5个学员编号,这些编号是在202400420001到202400420100范围内的随机编号
实验任务2
程序代码
#include <stdio.h> int main() { int choice, quantity; float total_price = 0, amount_paid, change; while (1) { printf("\n自动饮料售卖机菜单:\n"); printf("1. 可乐 - 3 元/瓶\n"); printf("2. 雪碧 - 3 元/瓶\n"); printf("3. 橙汁 - 5 元/瓶\n"); printf("4. 矿泉水 - 2 元/瓶\n"); printf("0. 退出购买流程\n"); printf("请输入饮料编号: "); scanf("%d", &choice); if (choice == 0) break; if (choice < 1 || choice > 4) { printf("无效的饮料编号,请重新输入。\n"); continue; } printf("请输入购买的数量: "); scanf("%d", &quantity); if (quantity < 0) { printf("购买数量不能为负数,请重新输入。\n"); continue; } switch (choice) { case 1: case 2: total_price += 3 * quantity; break; case 3: total_price += 5 * quantity; break; case 4: total_price += 2 * quantity; break; } printf("请投入金额: "); scanf("%f", &amount_paid); change = amount_paid - total_price; printf("本次购买总价: %.2f 元\n", total_price); printf("找零: %.2f 元\n", change); total_price = 0; } printf("感谢您的购买,欢迎下次光临!\n"); return 0; }
结果截图

回答:1.每次购买流程结束后重置总价格,以便下一次购买时,总价可以从零开始计算。如果去掉,则变量将保留上一次购买的总价。在下一次购买时,新的购买金额将会被加到上一次的总价上,而不是从零开始计算
2.当break语句在循环体内执行时,它会立即终止整个循环,不再执行任何后续的运算。当continue语句在循环体内执行时,它会终止当前运算,跳过当前中continue之后的代码,并立即开始下一轮运算
3.有必要,可以提前捕获不在预期范围内的选项,给出适当的错误消息或处理
实验任务3
程序代码
#include <stdio.h> int main() { char input; while (1){ scanf(" %c",&input) ; switch (input) { case 'r': case 'R': printf("stop!\n"); break; case 'g': case 'G': printf("go go go\n"); break; case 'y': case 'Y': printf("wait a minute\n"); break; default: printf("something must be wrong...\n"); break; } if (getchar() == EOF) { break; } } return 0; }
结果截图

实验任务4
程序代码
#include <stdio.h> int main(){ float expense, max = 0.0, min = 20000.0, total = 0.0; printf("输入今日开销,直到输入-1终止:\n"); while(1) { scanf("%f",&expense); if(expense == -1){ break; } total += expense; if (expense > max) { max = expense; } if (expense < min) { min = expense; } scanf("\n"); } printf("今日累计消费总额:%.1f\n", total); printf("今日最高一笔开销:%.1f\n", max); printf("今日最低一笔开销:%.1f\n", min); return 0; }
结果截图

实验任务5
程序代码
#include <stdio.h> #include <stdlib.h> int main(){ int d,g; d = rand() % 30 + 1; int n=0; printf("猜猜看4月的哪一天是你的lucky day\n 开始喽,你有三次机会,猜吧(1~30):"); while (n<3) { printf("请输入一个1到30之间的日期:"); scanf("%d", &g); if (g == d) { printf("哇,猜中了"); break; } else if (g < d) { printf("猜早了,还没到呢\n"); } else { printf("猜晚了,在前面呢\n"); } n++ ; if (n == 3) { printf("次数用完了。你的lucky day是4月%d日。\n", d); } } return 0; }
结果截图
实验任务6
程序代码
#include <stdio.h> void printCharacter(int n) { for (int i = n; i > 0; i--) { for (int j = 0; j < n - i; j++) { printf(" \t"); } for (int j = 0; j < 2 * i - 1; j++) { printf(" O\t"); } printf("\n"); for (int j = 0; j < n - i; j++) { printf("\t"); } for (int j = 0; j < 2 * i - 1; j++) { printf("<H>\t"); } printf("\n"); for (int j = 0; j < n - i; j++) { printf("\t"); } for (int j = 0; j < 2 * i - 1; j++) { printf("I I\t"); } printf("\n"); } } int main() { int n; printf("input n: "); scanf("%d", &n); printCharacter(n); return 0; }
结果截图



浙公网安备 33010602011771号