实验二

实验任务1

实验代码:

#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("20490041%04d\n", number);
	}

	return 0;
}

运行截图:
image

Q1:line13用来随机生成一个1~100之间的整数。
Q2:“0”表示在输出数值时如果数值的位数不足指定宽度,则在数值前面用0进行补充,“4”表示输出的数值占4个字符位,“d”表示输出的是整数。
Q3:随机产生5个学号,用于随机抽取对应学号的同学。

实验任务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_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;
}

运行截图:
image
Q1:将前一次的总价重置为0,便于计算下一次购买的总价。
Q2:break语句用于立即终止当前所在的循环,继续执行循环体之后的语句;而continue是跳过本次循环中continue之后的语句,开始下一次循环。
Q3:没有必要。在switch之前已经有了if语句用于排除无效的饮料编号,所以此时没有必要加default语句来排除choice为其他值的情况。

实验任务3

实验代码:

#include<stdio.h>
#include<stdlib.h>

int main() {
	char color;
	while ((color = getchar()) != EOF) {
		getchar();
		if (color == 'r') {
			printf("stop!\n");
		}
		else if (color == 'y') {
			printf("wait a minute\n");
		}
		else if (color == 'g') {
			printf("go go go\n");
		}
		else { printf("something must be wrong...\n"); }
	}
	system("pause");

	return 0;
}

运行截图:
image

实验任务4

实验代码:

#include<stdio.h>
#include<stdlib.h>
int main()
{
	double x, max = 0, min = 20000, sum = 0;
	printf("输入今日开销,直到输入-1终止:\n");
	do{
		scanf("%lf", &x);
		if (x != -1) {
			if (x > max) {
				max = x;
			}
			if (x < min) {
				min = x;
			}
			sum += x;
		}
	} while (x != -1);
	printf("今日累计消费总额:%.1lf\n", sum);
	printf("今日最高一笔开销:%.1lf\n", max);
	printf("今日最低一笔开销:%.1lf\n", min);
	system("pause");
	return 0;
}

运行截图:
image

实验任务5

实验代码:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main() {
	int ans, day, i = 1;
	srand(time(0));
	day = rand() % 30 + 1;
	printf("猜猜2025年4月哪一天是你的lucky day\n");
	printf("开始喽,你有三次机会,猜吧(1~30):");
	do {
		scanf_s("%d", &ans);
		if (ans == day) {
			printf("哇,猜中了!\n");
			break;
		}
		else if (ans > day) {
			printf("你猜的日期晚了,你的lucky day在前面哦\n");
			if (i < 3)
				printf("再猜(1~30):");
			i += 1;
			continue;
		}
		else {
			printf("你猜的日期早了,你的lucky day还没到呢\n");
			if (i < 3)
				printf("再猜(1~30):");
			i += 1;
			continue;
		}
	} while (i <= 3);
	if (i > 3)
		printf("次数用完啦。偷偷告诉你,4月你的lucky day是%d号\n", day);
	system("pause");
	return 0;
}

运行截图:
image

image

image

实验任务6

实验代码:

#include<stdio.h>
int main() {
	int i = 1, n, s = 0,a,b;
	printf("input n:");
	scanf_s("%d", &n);
	while (i <= n) {
		s = 2 * (n + 1 - i) - 1;

		for (b = 1; b < i; b++) {
			printf("   \t");
		}
		for (a = 1; a <= s; ++a) {
			printf(" O \t");
		}printf("\n");

		for (b = 1; b < i; b++) {
			printf("   \t");
		}
		for (a = 1; a <= s; ++a) {
			printf("<H>\t");
		}printf("\n");

		for (b = 1; b < i; b++) {
			printf("   \t");
		}
		for (a = 1; a <= s; ++a) {
			printf("I I\t");
		}printf("\n\n");
		i += 1;
	}
	return 0;
}

运行截图:
image

image

posted @ 2025-03-19 22:57  子春拾捌  阅读(4)  评论(0)    收藏  举报