实验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("20240042%04d\n", number);
}
return 0;
}
运行截图:

回答问题:
1.生成一个介于1到100之间的随机整数,并存储在变量number里
2.输入数据为整型
3.生成随机202400420001~202400420100的编号
实验任务二
源代码:
点击查看代码
#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_s("%d", &choice);
if (choice == 0)
break;
if (choice < 1 || choice > 4) {
printf("无效的饮料编号,请重新输入。\n");
continue;
}
printf("请输入购买的数量: ");
scanf_s("%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_s("%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是重新开始循环
3.没有必要,在输入switch语句前已经对数据进行了筛选与验证,不需要default语句再来处理
实验任务三
源代码:
点击查看代码
#include <stdio.h>
int main() {
char color;
char r, g, y;
r = 'r';
g = 'g';
y = 'y';
while (1 ) {
color = getchar();
scanf_s("%c", &color);
if (color == r) {
printf("stop!\n");
continue;
}
else if (color == g) {
printf("go go go\n");
continue;
}
else if (color == y) {
printf("wait a minute\n");
continue;
}
else
{
printf("something must be wrong...");
}
}
printf("感谢您的购买,欢迎下次光临!\n");
return 0;
}
运行截图:

实验任务四
源代码:
点击查看代码
#include <stdio.h>
int main() {
double expense, max = 0, min = 200000, total = 0;
printf("输入今日开销,直到输入 - 1终止:\n");
while (1) {
scanf_s("%lf", &expense);
if (expense == -1){
break;
}
total += expense;
if (expense > max){
max = expense;
}
if (expense < min && expense > 0){
min = expense;
}
}
printf("今日累计消费总额:%.lf\n", total);
printf("今日最高一笔开销:%.lf\n", max);
printf("今日最低一笔开销:%.lf\n", min);
return 0;
}
运行截图:

试验任务五
源代码:
点击查看代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int day,guess;
int chance = 3;
srand((unsigned int)time(NULL));
day = rand() % 30 +1;
printf("猜猜2025年4月哪一天是你的lucky day\n");
printf("开始喽,你有三次机会,猜吧(1-30):");
while (chance>0) {
scanf_s("%d",&guess);
if (guess < day)
{
printf("你猜的日期早了,你的lucky day还没到呢\n");
chance--;
}
else if (guess > day)
{
printf("你猜的日期晚了,你的lucky day在前面哦\n");
chance--;
}
else
{
printf("哇,猜中了:-)");
return 0;
}
if (chance>0)
{
printf("再猜(1-30):");
}
}
printf("次数用完啦。偷偷告诉你,4月你的lucky day是%d号\n",day);
return 0;
}
运行截图:


实验任务六
源代码:
点击查看代码
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
int i, k, m;
i = 1;
printf("input n: ");
scanf("%d", &n);
while(i <= n )
{
m = 0;
while(m < i - 1)
{
printf("\t");
m = m + 1;
}
k = 0;
while(k < 2 * (n + 1 - i) - 1)
{
printf(" o \t");
k = k + 1;
}
printf("\n");
m = 0;
while(m < i - 1)
{
printf("\t");
m = m + 1;
}
k = 0;
while(k < 2 * (n + 1 - i) - 1)
{
printf("<H>\t");
k = k + 1;
}
printf("\n");
m = 0;
while(m < i - 1)
{
printf("\t");
m = m + 1;
}
k = 0;
while(k < 2 * (n + 1 - i) - 1)
{
printf("I I\t");
k = k + 1;
}
printf("\n");
i = i + 1;
}
system("pause");
return 0;
}
运行截图:



浙公网安备 33010602011771号