C语言编写生成无限长度的正/倒三角形

这段时间忙于学习, 没怎么更新网站技术文章, 今天来一篇~

该项目使用 do while 、if 、for 等;

项目实例:

#include <stdio.h>
int main(int scanfs) {
    int a;
    int hs;
    int ds,dd;
    do {
        scanf("%d", &a); // 输入自定义行数三角形
        scanf("%d",&scanfs); // 输入判断自行正三角形或倒三角形
        if (scanfs == 1) { // 判断成功后, 开始执行正三角形
            for (int b = 1; b <= a; b++) {
                for (int c = 2 * 10; c >= b; c--)
                    printf(" ");
                for (int c = 1; c <= 2 * b - 1; c++)
                    printf("*");
                printf("\n");
 
            }
        }
        /// 倒三角形
        if (scanfs == 2) {
            scanf("%d", &hs); // 判断是倒三角形后, 需要重新输入行数
            for (ds = 0; ds < hs; ds++) {
                for (dd = 0; dd < ds; dd++)
                    printf(" ");
                for (dd = hs - dd; dd > 0; dd--)
                    printf("* ");
                printf("\n");
            }
        }
    }
    while (scanfs != 1 && scanfs != 2); // 判断不是 1或2 ,重新执行 do 里的语句
    return 0;
}

不需要二次输入倒三角形行数的:

#include <stdio.h>
int main(int scanfs) {
    int a;
    int hs;
    int ds,dd;
    do {
        scanf("%d", &a); // 输入自定义行数三角形
        scanf("%d",&scanfs); // 输入判断自行正三角形或倒三角形
        if (scanfs == 1) { // 判断成功后, 开始执行正三角形
            for (int b = 1; b <= a; b++) {
                for (int c = 2 * 10; c >= b; c--)
                    printf(" ");
                for (int c = 1; c <= 2 * b - 1; c++)
                    printf("*");
                printf("\n");
 
            }
        }
        /// 倒三角形
        if (scanfs == 2) {
//            scanf("%d", &hs); // 判断是倒三角形后, 需要重新输入行数
            for (ds = 0; ds < a; ds++) {
                for (dd = 0; dd < ds; dd++)
                    printf(" ");
                for (dd = a - dd; dd > 0; dd--)
                    printf("* ");
                printf("\n");
            }
        }
    }
    while (scanfs != 1 && scanfs != 2); // 判断不是 1或2 ,重新执行 do 里的语句
    return 0;
}

执行效果:

C语言编写生成无限长度的正/倒三角形-WordPress极简博客C语言编写生成无限长度的正/倒三角形-WordPress极简博客


 

猜你喜欢

转载自blog.csdn.net/YSP050310/article/details/111051896