实验2
#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:格式化输出整数number使其至少有四位数字,若不足四位前面会用0填充。
问题3:生成五个随机学号,且五个学号前缀一样,即前8位数字相同。
实验任务2
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:重置变量为0,便于计算下次购买金额;若缺少会将本次的购买金额加入到下次的购买金额中。
问题2:break会直接跳出整个循环并不再执行循环中的任何代码;continue会跳出本次循环的剩余部分,但会继续执行下个循环。
问题3:有必要;原因:在当前代码中,若输入一个不在1~4范围内的数字,程序会打印一条错误信息并重新执行下一次循环,但若没有default子句,程序不会明确指出该情况被处理过。
实验任务3
#include<stdio.h> int main() { char light; while(1){ printf("请输入交通信号灯颜色:"); light=getchar(); getchar(); switch(light){ case'r':printf("stop\n"); break; case'g':printf("gogogo\n"); break; case'y':printf("wait a moment\n"); break; default: printf("something must be wrong...\n"); break; } } return 0; }
实验任务4
#include <stdio.h> int main() { double expense, maxExpense = 0, minExpense = 20000, totalExpense = 0; int isFirst = 1; printf("输入今日开销,直到输入 -1 终止:\n"); while (1) { scanf("%lf", &expense); if (expense == -1) { break; } if (expense <= 0 || expense > 20000) { printf("输入的开销必须大于0且不超过20000元。\n"); continue; } if (isFirst) { maxExpense = minExpense = expense; isFirst = 0; } else { if (expense > maxExpense) { maxExpense = expense; } if (expense < minExpense) { minExpense = expense; } } totalExpense += expense; } printf("今日累计消费总额:%.1f\n", totalExpense); printf("今日最高一笔开销:%.1f\n", maxExpense); printf("今日最低一笔开销:%.1f\n", minExpense); return 0; }
实验任务5
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int luckyDay, guess, attempts = 0; const int maxAttempts = 3; srand((unsigned int)time(NULL)); luckyDay = rand() % 30 + 1; printf("猜猜2025年4月哪一天是你的lucky day\n"); printf("开始喽,你有三次机会,猜吧(1~30):"); for (attempts = 0; attempts < maxAttempts; attempts++) { scanf("%d", &guess); if (guess == luckyDay) { printf("哇,猜中了:-)\n"); return 0; } else if (guess < luckyDay) { printf("你猜的日期早了,你的lucky day还没到呢\n"); } else { printf("你猜的日期晚了,你的lucky day在前面哦\n"); } if (attempts < maxAttempts - 1) { printf("再猜(1~30):"); } } printf("次数用完啦。偷偷告诉你,4月你的lucky day是%d号\n", luckyDay); return 0; }
实验任务6
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,i,j;
scanf("%d",&n);
for (i=n;i>=1;--i)
{
for (j=0;j<n-i;++j)
{
printf(" ");
}
for (j = 0; j < 2 * i - 1; ++j)
{
printf(" 0 ");
printf(" ");
}
printf("\n");
for (j=0;j<n-i;++j)
{
printf(" ");
}
for (j=0;j<2*i-1;++j)
{
printf("<H>");
printf(" ");
}
printf("\n");
for (j=0;j<n-i;++j)
{
printf(" ");
}
for (j = 0; j < 2 * i - 1; ++j)
{
printf("I I");
printf(" ");
}
printf("\n");
for (j=0;j<n-i;++j)
{
printf(" ");
}
printf("\n");
}
return 0;
}