实验2
实验任务1
源代码
点击查看代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 100
#define CNT 5
int main() {
int number;
int i;
srand(time(0)); // 以当前系统时间作为随机种子
for(i = 0; i < CNT; ++i) {
number = rand() % N + 1;
printf("20490042%04d\n", number);
}
return 0;
}
运行结果
![image]()
问题1
- 答:程序的功能是在204900420001~204900420100的学员编号中随机抽选5个学号。
问题2
- 答:line14代码的功能是生成1~100范围内的随机数。
问题3
- 答:格式符%04d的作用是使输出的最小宽度为4个数位,在数位不足的部分用0来填充。
问题4
- 答:去掉代码srand(time(0));之后,多次运行程序,发现程序运行结果相同,推测该代码的作用为使每次生成的随机数不与上一次相同。
实验任务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;
}
if(choice == 1 || choice == 2)
total_price += 3 * quantity;
else if(choice == 3)
total_price += 5 * quantity;
else
total_price += 2 * quantity;
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
- 答:如果去掉line47代码total_price = 0会导致下次购买时的价格与上次购买时的总价累加,导致从第二次购买开始,总价将会被计算成包括此次购买在内的所有购买的总额。
问题2
- 答:while循环中continue语句可以让程序执行遇到continue语句时跳过当前循环中剩下的语句,直接进入下一次循环。
补充
运行截图
![image]()
实验任务3
源代码
点击查看代码
#include <stdio.h>
int main()
{
char ans ;
while(1)
{
ans = getchar();
getchar();
switch (ans)
{
case'r':printf("stop!\n");break;
case'g':printf("go go go\n");break;
case'y':printf("wait a minute\n");break;
default:printf("something must be wrong...\n");break;
}
}
return 0;
}
运行截图
![image]()
实验任务4
源代码
点击查看代码
#include <stdio.h>
int main()
{
double cost;
double max, min;
double total = 0;
int i = 1;
printf("输入今日开销,直到输入-1为止: \n");
while(1)
{
scanf("%lf", &cost);
if(cost == -1)
break;
if(cost > 0 && cost <= 20000)
{
total = total + cost;
if(i == 1)
{
max = cost;
min = cost;
i = 0;
}
else
{
if(cost > max)
max = cost;
if(cost < min)
min = cost;
}
}
}
printf("今日累计消费总额:%.1f\n", total);
printf("今日最高一笔开销:%.1f\n", max);
printf("今日最低一笔开销:%.1f", min);
return 0;
}
运行截图
![image]()
实验任务5
源代码
点击查看代码
#include <stdio.h>
int main()
{
int a, b, c;
while(1)
{
scanf("%d %d %d",&a ,&b ,&c);
if(a + b <= c || a + c <= b || b + c <= a)
{
printf("不能构成三角形\n");
continue;
}
if(a == b && b == c)
printf("等边三角形\n");
else if(a == b || a == c || b == c)
printf("等腰三角形\n");
else if(a * a + b * b == c * c || a * a + c * c == b * b || b * b + c * c == a * a)
printf("直角三角形\n");
else
printf("普通三角形\n");
}
return 0;
}
运行截图
![image]()
实验任务6
源代码
点击查看代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int ld, c;
int i;
srand(time(0));
ld = rand() % 30 + 1;
printf("猜猜2026年11月哪一天是你的lucky day:\n");
printf("开始喽,你有3次机会,猜吧(1~30):\n");
for(i = 0; i < 3; ++i)
{
scanf("%d", &c);
if(c == ld)
{
printf("哇,猜中啦!:)\n");
break;
}
else if(c < ld)
printf("你猜的日期早了,你的lucky day还没到呢...\n");
else
printf("你猜的日期晚了,你的lucky day在前面哦~\n");
if(i < 2)
printf("再猜猜看~(1~30):\n");
}
if(i == 3)
printf("次数用光啦,11月你的lucky day是%d号\n", ld);
return 0;
}
运行截图
![image]()
![image]()