实验2 C语言分支与循环基础应用编程

实验结论

1.实验任务1

task1.c源代码

#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;
}

运行结果

image

问题回答

  • Q1:解释lin13代码的功能。
    • A1:在[1,100]里取一随机整数存放在number里。
  • Q2:解释line14使用格式符%04d起到什么作用。
    • A2:保证每次输出结果为12位。
  • Q3:这个程序的功能是什么?
    • A3:随机生成五个202400420001~202400420100的学员编号。

2.实验任务2

task2.c源代码

#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;
        }

        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("%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:line53代码的用途?如果去掉,会有什么影响?
    • A1:将购买总价重置为0,如果去掉会导致总价=每次购买总价之和。
  • Q2:line17-18, 使用了break语句;line20-23, line28-31, 使用了continue语句。在循环中使用break和continue有什么区别?
    • A2:break是中断循环,continue则是跳过该循环的剩余代码进入下一次循环。
  • Q3:line33-44,使用了switch语句实现多分支。通常情况下,在switch语句中使用default子句有助于排查代码运行时错误,这个代码中,是否有必要增加default子句?你的答案和理由。
    • A3:没有必要,前面代码对choice的值进行了规范,只能为1~4。

3.实验任务3

task3.c源代码

#include<stdio.h>

int main()
{
	char color;
	scanf("%c",&color);
	getchar();
	while(color!=EOF)
	{
		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;
		}
		scanf("%c",&color);
		getchar();
	}
	return 0;
}

运行结果

image

4.实验任务4

task4.c源代码

#include<stdio.h>

int main()
{
	double x,all=0,max,min;
	printf("输入今日开销,直到输入-1终止:\n");
	scanf("%lf",&x);
	getchar();
	max=x,min=x;
	while(x!=-1)
	{
		if(x>0&&x<20000)
		{
			all+=x;
		if(x>max)
			max=x;
		if(x<min)
			min=x;
		}
		
		scanf("%lf",&x);
		getchar();

	}
	printf("今日累计消费总额:%.1lf\n今日最高一笔开销:%.1lf\n今日最低一笔开销:%.1lf",all,max,min);
	return 0;
}

运行结果

image

5.实验任务5

task5.c源代码

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


#define N 3
int main()
{
	int day,i,gus;

	printf("猜猜2025年4月哪一天是你的lucky day\n");

	srand(time(0));
	day = rand() % 30 + 1;

	
	for (i = 0; i < N; i++)
	{
		if (i == 0)
			printf("开始喽,你有三次机会,猜吧(1~30):");
		else
			printf("再猜(1~30):");
		scanf("%d", &gus);
		

		if (gus > day)
			printf("你猜的日期晚了,你的lucky day在前面哦\n");
		else if (gus < day)
			printf("你猜的日期早了,你的lucky day还没到呢\n");
		else
		{
			printf("哇,猜中了:-)\n");
			break;
		}
	}
		


	if (i == 3)
		printf("次数用完啦。悄悄告诉你,4月你的lucky day是%d号\n", day);


	return 0;
}

运行结果

image

image

6.实验任务6

task6.c源代码

#include<stdio.h>

int main()
{

	int n,i,j;
	printf("input n: ");
	scanf("%d", &n);

	for (i = n; i >0; i--)
	{
		for (j = 0; j < n-i; j++)
			printf(" \t");

		for (j = 0; j < 2*i-1; j++)
			printf(" O \t");

		for (j = 0; j < n - i; j++)
			printf(" \t");

		printf("\n");

		for (j = 0; j < n - i; j++)
			printf(" \t");

		for (j = 0; j < 2*i-1; j++)
			printf("<H>\t");

		for (j = 0; j < n - i; j++)
			printf(" \t");

		printf("\n");

		for (j = 0; j < n - i; j++)
			printf(" \t");

		for (j = 0; j < 2*i-1; j++)
			printf("I I\t");

		for (j = 0; j < n - i; j++)
			printf(" \t");

		printf("\n");
		printf("\n");
	}

	return 0;
}

运行结果

image

image

实验总结

  • 强化了对选择结构和循环结构的理解,以及while if switch的运用
  • 通过设置断点,可以观察变量的变化情况,以便对程序运行的错误进行排查
posted @ 2025-03-18 17:09  yjk2053  阅读(17)  评论(0)    收藏  举报