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

实验2

实验任务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("20240042%04d\n", number);
    }
    system("pause");
    return 0;
}

运行结果:

问题:

问题一:

将1~100内的一个随机数赋值给number

问题二:

将number被赋值的数,以四个字符宽度跟在“20240042”后输出

问题三:

程序的功能是得到学号从202400420001到202400420100的随机五个学生的学号

实验任务2

task2.c

代码:

#include <stdio.h>
#include <stdlib.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");
    system("pause");
    return 0;
	
}

运行结果:

问题:

问题1:

将total_price赋值为零,即将饮料总价还原为零。如果去掉,用户下一次购买饮料的饮料总价会加上上一次购买的价钱

问题2:

使用break语句,使当前while语句循环结束。在此场景中控制,当客户输入0时,取消贩卖机贩卖饮料并输出欢迎购买。
使用continue语句,仅结束本次循环的剩余部分。20-23行,如果顾客输入的choice满足此次if判断条件,输出提示后结束此次循环,进入下一次while循环,让顾客重新输入choice的值。28-31同理。

问题3:

不需要。因为choice<1和choice>4的情况已经被20-23行的if语句判断过了,只要choice输入整数,就一定会有输出。

实验任务3

task3.c:

代码:

#include <stdio.h>
#include <stdlib.h>
int main(){
	char symbol;
	while(scanf_s("%c",&symbol)!=EOF){
		scanf_s("%*c");
		switch(symbol)
		{
		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;
		}
		
	}
	system("pause");
	return 0;
}

运行结果:

实验任务4

task4.c:

代码:

#include <stdio.h>
#include <stdlib.h>
int main(){
	double total_price=0,spe;
	double max,min;
	printf("输入今日开销,直到输入-1终止:\n");
	scanf_s("%lf\n",&spe);
	max=spe,min=spe;
        while(spe!=-1)
	{
		total_price+=spe;
		if(spe>max)
		{
			max=spe;
		}
		if(spe<min)
		{
			min=spe;
		}
		scanf_s("%lf",&spe);

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

运行结果:

实验任务五

task5.c

代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 3
int main(){
	int day,stday;
	int i;
	srand(time(0));
	stday=rand()%30+1;
	printf("猜猜2025年4月哪一天是你的lucky day\n");
	printf("开始喽,你有三次机会,猜吧(1~30):");
	scanf_s("%d",&day);
	for(i=1;i<N;++i)
	{
		if(day==stday){
			printf("哇,猜中了:-)\n");
			break;
		}
		if(day<stday){
			printf("你猜的日期早了,你的lucky day还没到呢\n");
			printf("再猜(1~30):");
		}
		if(day>stday){
			printf("你猜的日期晚了,你的lucky day在前面哦\n");
			printf("再猜(1~30):");
		}
		scanf_s("%d",&day);
		

	}
	if(i==3)
        {printf("次数用完啦。偷偷告诉你,4月你的lucky day是%d号\n",stday);}
	
	system("pause");
	return 0;
}

运行结果:

实验任务6

task6.c:

代码:

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

int main() {
    int n, i, j;


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


    for (i = n; i >= 1; --i)
    {

        for (j = 0; j < n - i; ++j) {
            printf("      ");
        }

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

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

         }
         printf("\n");
         for (j = 0; j < n - i; ++j) {
             printf(" ");
         }
          printf("\n");
    }
	system("pause");
    return 0;
}

运行结果:

实验总结

  1. 本次试验学习了获得随机数的方法,以当前系统时间作为随机种子。
  2. if、switch语句选择使用,可以使代码更简洁易懂。
posted @ 2025-03-18 15:02  Bloon682  阅读(20)  评论(0)    收藏  举报