实验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;
}
~~程序功能:以当前时间为随机种子,循环 5 次,每次产生一个 1100 的随机数,与固定前缀 20490042 拼成 12 位学号,即从 204900420001204900420100 中随机抽 5 个(随机点名)。每次运行结果不同,允许重复。
line14 number = rand() % N + 1;:rand() 返回 0RAND_MAX,% N(N 为 100)把范围压到 099,+ 1 得到 1~100 并赋给 number;取余优先于加法,等价于 (rand() % N) + 1。
line15 的 %04d:20490042 原样输出作前缀,%04d 把 number 补足 4 位(1→0001),从而拼成 204900420001~204900420100 的完整学号;若写成 %d 则位数不齐(如 204900421)。前缀是普通字符,不参与宽度计算。
srand(time(0));:为 rand() 设置随机种子。默认种子固定为 1,不播种时每次运行的序列完全相同;用不断变化的当前时间作种子后,每次运行才得到不同结果(同一秒内运行两次仍可能相同)。

实验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;
}
~~
去掉 line47 的 total_price = 0;:累计金额不清零,第 2 笔交易的“本次购买总价”会把前面几笔一起算进去;找零 = 投入金额 − 累计金额,于是变小甚至为负(例如先买 9 元再买 6 元,第二笔显示总价 15 元,投 10 元时找零 −5 元)。
continue 的语义:结束本次循环,跳过其后所有语句,直接回到循环条件判断处开始下一轮——本题即重新显示菜单、重新读取编号。两处分别处理“编号非法”和“数量为负”,可避免深层嵌套;与 break(跳出整个循环,本题 choice == 0 时使用)不同。

实验3
源代码
~~
include <stdio.h>
int main()
{
int i;
char color;
for (i = 1; i <= 4; i++) {
scanf(" %c", &color);
switch (color) {
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;
}

实验4
源代码
~~
include <stdio.h>
int main()
{
double value, total = 0.0, max = 0.0, min = 0.0;
int count = 0;
printf("输入今日开销,直到输入-1终止:\n");
while (1) {
scanf("%lf", &value);
if (value == -1)
break;
total += value;
if (count == 0) {
max = value;
min = value;
} else {
if (value > max)
max = value;
if (value < min)
min = value;
}
count++;
}
if (count == 0) {
printf("今天没有开销记录。\n");
} else {
printf("今日累计消费总额: %.1f\n", total);
printf("今日最高一笔开销: %.1f\n", max);
printf("今日最低一笔开销: %.1f\n", min);
}
return 0;
}

实验5
源代码
~~
include <stdio.h>
int main()
{
int i, a, b, c, tmp;
for (i = 0; i < 5; i++) {
scanf("%d%d%d", &a, &b, &c);
/* 先按从小到大排序,保证 c 是最长边 */
if (a > b) { tmp = a; a = b; b = tmp; }
if (a > c) { tmp = a; a = c; c = tmp; }
if (b > c) { tmp = b; b = c; c = tmp; }
if (a + b <= c) {
printf("不能构成三角形\n");
} else if (a == b && b == c) {
printf("等边三角形\n");
} else if (a * a + b * b == c * c) {
printf("直角三角形\n");
} else if (a == b || b == c || a == c) {
printf("等腰三角形\n");
} else {
printf("普通三角形\n");
}
}
return 0;
}

试验6
源代码
include <stdio.h>
include <stdlib.h>
include <time.h>
int main()
{
int lucky, guess, i;
srand(time(0));
lucky = rand() % 30 + 1;
printf("猜猜2026年11月哪一天是你的lucky day\n");
printf("开始喽,你有3次机会,猜吧(1~30):");
for (i = 1; i <= 3; i++) {
scanf("%d", &guess);
if (guess == lucky) {
printf("哇,猜中了:)\n");
return 0;
} else if (guess < lucky) {
printf("你猜的日期早了,你的lucky day还没到呢\n");
} else {
printf("你猜的日期晚了,你的lucky day在前面哦\n");
}
if (i < 3)
printf("再猜(1~30):");
}
printf("次数用光啦。11月你的lucky day是%d号\n", lucky);
return 0;
}

源代码

浙公网安备 33010602011771号