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

实验任务1

task1.c源代码:

#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() % 100 + 1;
        printf("20490042%04d\n", number);
    }

    return 0;
}

task1.c 源代码运行测试截图:

742ee838daf6f46fe9b3b0ec8e2ae19d

 

问题1:为某专业随机分配并输出 100 个学员编号。学号 = 固定前缀 20490042(8 位)+ 流水号 0001~0100(4 位),共 12 位,落在题目给的编号区间内。

问题2:line14:number = rand() % 100 + 1 ;

首先,rand() 产生 [0, RAND_MAX] 的伪随机整数;然后,% 100 取余数,范围为 [0,99] ;最后,+1 范围整体后移得到 [1,100] 并赋给 number。所以,line14的功能就是随机抽取最后两位数,抽取范围在[1,100] 之间。

问题3:line15:printf("20490042%04d\n", number);

%d 是十进制输出,0 是补零标志,4 是字段宽度。04%d可以保证输出的是流水号恒为 4 位,学号恒为 12 位纯数字。所以%04d作用是使输出的学号格式全部是统一,符合现实生活中对于学号的使用要求。

问题4:

删去 srand(time(0)); 后的运行测试截图:

c59b90df866b1884355d49534d3ff03c

多次运行程序,可发现输出的学号结果相同。

所以代码srand(time(0));的作用是使用当前系统时间来作为种子初始化rand(),使每次运行种子的默认值不同,从而能得到不同的随机数序列,即得到不同的学号输出。如果去掉代码,种子的默认值恒为1,所以每一次运行程序得到的序列完全一样

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

task2.c 源代码运行测试截图:

44b22baf1d7a8e3a8f26ebd4d622f115

问题1:line47: total_price = 0;

该购买程序的主体是while循环里的整串代码,若程序运行中不输入0,程序是会循环运行的,而在实际要求中,第一次购买和第二次购买的total_price应该是分开计算的,但程序中的变量只有在return0后,total_price的值才会被重置为初始值0。所以在while循环中,我们也需要在每次循环的末尾对total_price的值进行重置归零,从而确保每次购买最后输出的总价是该次购买的。如果去掉,total_price就会变成本次购买的价格加上过去购买的所有价格之和,那么就不符合实际要求了。

只有在购买一次的时候,去掉与否不会影响价格的输出。

问题2:continue的语义是立即结束本次循环,跳过本次循环中 continue 之后的所有语句,直接进入下一次循环。

实验任务3

task3.c 版本1(if)源代码:

#include <stdio.h>
int main() {
    int i;
    char color;

	for (i = 0; i < 4; i++) {
		scanf(" %c", &color);
		if (color == 'r') {
			printf("stop!\n");
		}
		else if (color == 'g') {
			printf("go go go\n");
		}
		else if (color == 'y') {
			printf("wait a minute\n");
		}
		else {
			printf("something must be wrong...\n");
		}
		
	}
	return 0;
}

运行测试截图:

fdf17472dd6d781e87e4a959609179fd

task3.c 版本2(switch)源代码:

#include <stdio.h>
int main() {
    int i;
    char color;

	for (i = 0; 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;
}

运行测试截图:

09453f5743ceb3b6e7302160eb204408

实验任务4

task4.c 源代码:

#include <stdio.h>

int main()
{
    double cost;         
    double total = 0;   
    double max = 0;  
    double min = 20000;

    printf("输入今日开销,直到输入-1终止:\n");

    while (1)                      
    {
        if (scanf("%lf", &cost) != 1)   
        {
            break;
        }

        if (cost == -1)              
        {
            break;
        }

        total += cost;              

        if (cost > max)              
        {
            max = cost;
        }

        if (cost < min)             
        {
            min = cost;
        }
    }

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

    return 0;
}

task4.c 源代码运行测试截图:

0665bb297cb4098a522d7718f4503e26

 实验任务5

task5.c 源代码:

#include <stdio.h>
int main() {
int i;
int a, b, c;

for (i = 0; i < 5; i++)
{
    scanf("%d %d %d", &a, &b, &c);

    if (a + b <= c)
    {
        printf("不能构成三角形\n");
        continue;
    }

    if (a == b && b == c)
    {
        printf("等边三角形\n");
    }

    else if (a == b || b == c || a == c)
    {
        printf("等腰三角形\n");
    }

    else if (a * a + b * b == c * c || a * a + c * c == b * b || b * b + c * c == a * a)
    {
        printf("直角三角形\n");
    }

    else printf("普通三角形\n");
}
return 0;
}

task5.c 源代码运行测试截图:

e82ee9c0ada07f69ed537ef47fdc8a15

 实验任务6

task6.c 源代码运行测试截图:

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

int main()
{
    int lucky;   
    int guess;    
    int 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;              
        }

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

task6.c 源代码运行测试截图:

c971d8e561e3ffa29245bad421b44347

06052e5e81c7fed931aad720e79e7174

 

 

 
posted @ 2026-10-29 10:18  Novacane0523  阅读(14)  评论(0)    收藏  举报