实验2
实验任务1
问题1:随机生成某专业学员编号,范围在202400420001~202400420100之间
问题2:返回一个1-100之间的伪随机数
问题3:固定占4个字符宽度,不够四位在左边补0
问题4:使每一次生成的随机数不一样,删去后生成的随机数每次都一样
源代码
点击查看代码
#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;
}

实验任务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 (quantity == 0) {
printf("购买数量不能为0,请重新输入。\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);
if (amount_paid < total_price) {
printf("金额不足,无法完成购买!\n");
total_price = 0;
continue;
}
change = amount_paid - total_price;
printf("本次购买总价: %.2f 元\n", total_price);
printf("找零: %.2f 元\n", change);
total_price = 0;
}
printf("感谢您的购买,欢迎下次光临!\n");
return 0;
}

问题1:如果去掉,没有重新重置total_price为0,在第二次运算时total_price为第一次计算的值,导致数据错误
问题2∶在循环中使用 continue 语句,语义是满足该条件语句后则跳过本次循环
实验任务3
点击查看代码
#include <stdio.h>
int main() {
char ch;
while (scanf(" %c", &ch) != EOF) {
switch (ch) {
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 sum = 0.0;
double max = 0.0;
double min = 20001.0;
double money;
printf("输入今日开销,直到输入-1终止:\n");
while (1) {
scanf("%lf", &money);
if (money == -1) {
break;
}
sum += money;
if (money > max) {
max = money;
}
if (money < min) {
min = money;
}
}
printf("今日累计消费总额: %.1f\n", sum);
printf("今日最高一笔开销: %.1f\n", max);
printf("今日最低一笔开销: %.1f\n", min);
return 0;
}

实验任务5
点击查看代码
#include <stdio.h>
int main() {
int a, b, c;
while (scanf("%d %d %d", &a, &b, &c) == 3) {
int x = a, y = b, z = c;
int temp;
if (x > y)
{ temp = x;
x = y;
y = temp;
}
if (x > z)
{ temp = x;
x = z;
z = temp;
}
if (y > z)
{ temp = y;
y = z;
z = temp;
}
if (x + y <= z) {
printf("不能构成三角形\n");
continue;
}
if (x == y && y == z) {
printf("等边三角形\n");
} else if (x == y || y == z || x == z) {
printf("等腰三角形\n");
} else if ((long long)x * x + (long long)y * y == (long long)z * z)
{
printf("直角三角形\n");
} else {
printf("普通三角形\n");
}
}
return 0;
}

实验任务6
点击查看代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int lucky, guess, i;
srand((unsigned int)time(NULL));
lucky = rand() % 30 + 1;
printf("猜猜2026年4月哪一天是你的lucky day\n");
printf("开始喽,你有3次机会,猜吧(1~30):");
for (i = 0; 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 < 2) {
printf("再猜(1~30):");
}
}
printf("次数用光啦。4月你的lucky day是%d号\n", lucky);
return 0;
}

浙公网安备 33010602011771号