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

task1

源代码

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

 

程序运行截图

task2

源代码

#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) {
        case1:
        case2:
            total_price += 3 * quantity;
            break;
        case3:
            total_price += 5 * quantity;
            break;
        case4:
            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;
}

 

程序运行截图

task3

源代码

#include<stdio.h>

int main()
{
    char input;
    printf("请输入交通信号灯颜色字符(r/g/y):\n");
    while (scanf_s("%c", &input) != EOF){
        switch (input) {
        case'r':printf("stop!\n"); 
            break;
        case'g':printf("go go go\n");
            break;
        case'y':printf("wait a minute\n"); 
            break;
        
        }

            
        }
    return 0;
}

 

程序运行截图

task4

源代码

#include<stdio.h>

int main()
{
    double expense=0.0;
        double total = 0.0, max = 0.0, min = 20000.0;
    printf("输入今日开销,直到输入-1为止:\n");
    while (expense != -1) {
        scanf_s("%lf", &expense);
        if (expense == -1)
            break;
        if (expense > max)
            max = expense;
        if (expense < min)
            min = expense;
        total += expense;
    }
    printf("今日累计消费总额:%.1f\n", total);
    printf("今日最高消费:%.lf\n", max);
    printf("今日最低消费:%.lf\n", min);
    return 0;
}

 

程序运行截图

 

task5

源代码

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

int main()
{
    srand(time(NULL));
    int lucky_day = rand() % 30 + 1;
    int guess;
    int attempts = 3;
    printf("猜猜2025年4月份哪一天是你的lucky_day\n");
    printf("开始喽,你有三次机会,猜吧(1~30):\n");
    while (attempts > 0) {
        scanf_s("%d", &guess);
        if (guess == lucky_day) {
            printf("恭喜你,猜中了!你的lucky_day是%d号\n", lucky_day);
            return 0;
            break;
        }
        else if (guess < lucky_day) {
            printf("你猜的日期早了,你的lucky_day还没到呢\n");
        }
        else {
            printf("你猜的日期晚了,你的lucky_day在前面哦\n");
        }
        if (attempts > 0) {
            printf("再猜(1~30):\n");
        }
    }
        printf("次数用光啦。偷偷告诉你,4月你的lucky_day是%d号\n", lucky_day);
        return 0;
    }

 

程序运行截图

task6

源代码

#include<stdio.h>

int main() {
    int n, i, j;
    printf("请输入一个整数n:");
    scanf_s("%d", &n);
    for (i = n; i > 0; i--) {
        for (j = 0; j < n-i; j++) {
            printf("  ");
        }
         for (j = 0; j < i; j++) {
            printf(" O ");
        }
         printf("\n");
            for (j = 0; j <n- i; j++) {
                printf(" ");
            }
        for (j = 0; j < i; j++) {
            printf("<H>");
        }
        printf("\n");
        for (j = 0; j < n - i; j++) {
            printf(" ");
        }
        for (j = 0; j < i; j++) {
            printf("I I");
        }
        printf("\n\n");
    }
    return 0;
}

 

程序运行截图

 

posted @ 2025-03-20 21:46  姚斯文  阅读(8)  评论(0)    收藏  举报