【问题标题】:Sierpinski Triangle debugging --- C谢尔宾斯基三角调试---C
【发布时间】:2015-10-16 03:26:23
【问题描述】:

我正在开发一个基于用户输入的高度和分形级别打印谢尔宾斯基三角形的程序。这是我的程序在输入高度 8 和分形级别 1 时应该产生的结果:

       *
      ***
     *****
    *******
   *       *
  ***     ***
 *****   *****
******* *******

这是我目前所拥有的:

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

int main(int argc, const char *argv[]) {

    int height, draw, errno, fractal_level;

    char *p;
    char *q;
    errno = 0;
    height = strtol(argv[1], &p, 10);
    fractal_level = strtol(argv[2],&q, 10);
    if (errno != 0 || p == argv[1]) {
        printf("ERROR: Height must be integer.\n");
        exit(1);
    }
    else if (errno != 0 || q == argv[2]) {
        printf("ERROR: Fractal Level must be integer.\n");
        exit(1);
    }
    int x,y;
    x=(2*height-1) / 2;
    y=(2*height-1) / 2;
    printf("x: %d   y: %d \n", x, y);
    drawSier(height, fractal_level, x, y);

    return 0;   
}

int drawSier(height, fractal_level, x, y) {

    //If the fractal level is zero, it's just a normal triangle.
    if (fractal_level==0)
    {
        drawTriangle(height, x, y);
    }
    //the function calls itself, but with a slight variance 
    //in the starting point of the triangle, for the top, bottom left, and bottom right
    else {
    //top
    drawSier(height/2, fractal_level-1, x, y);
    //bottom left
    drawSier(height/2, fractal_level-1, x-height/2, y-height/2);
    //bottom right
    drawSier(height/2, fractal_level-1, x+height/2, y-height/2);
    }
}

int drawTriangle(height, x, y){

    if (height<1) {
        printf("ERROR: Height too small.\n");
        exit(1);

    }
    else if (height>129) {
        printf("ERROR: Height too large.\n");
        exit(1);
    }

    for (int i = 1; i <= height; i++)
    {
        int draw=0;

        // this 'for' loop will take care of printing the blank spaces
        for (int j = i; j <= x; j++)
        {
            printf(" ");
        }
        //This while loop actually prints the "*"s of the triangle by multiplying the counter
        //by 2R-1, in order to output the correct pattern of stars. This is done AFTER the for
        //loop that prints the spaces, and all of this is contained in the larger 'for' loop.
        while(draw!=2*i-1) {
                printf("*");
                draw++;
        }
        draw=0;
        //We print a new line and start the loop again
        printf("\n");
    }

return 0;
}

这是我的程序当前使用相同输入生成的内容:

       *
      ***
     *****
    *******
   *
  ***
 *****
*******
           *
          ***
         *****
        *******

我不确定出了什么问题。这似乎是 y 变量的问题。

【问题讨论】:

  • 我已经盯着这个看了一个小时,并尝试三次重写循环。没有什么能产生我需要的东西。也许如果你能给我一个提示,我需要在哪里寻找而不是放置传统的迂腐 Stack Overflow 评论,那可能会更有帮助:)
  • 根据您的代码设置方式,您一次只能在一组给定的行上打印一个三角形。您需要重组事物,以便可以在一行上打印多个三角形,即打印三角形 1 的第 1 行、空格、打印三角形 2 的第 1 行、换行符...
  • 我建议在内存中进行所有绘图(通过填充一个数组来跟踪每个空间是空还是满),然后打印出该数组。
  • @DavidSchwartz,有没有办法在 C 中创建一个全局二维数组?我从一个有 [x][y] 的数组开始,但它反复抛出错误,所以我只是切换了。
  • @KommanderKitten 你可以制作一个全局二维数组。二维数组只是数组的数组。

标签: c recursion


【解决方案1】:

y 被传递给 drawTriangle() 但函数不使用它。它只是在之前打印的内容下方打印带有三角形的新行。

您可以在打印前使用控制台控制代码将光标移动到所需位置(注意不要覆盖以前打印的输出),或者您可以先在内存中创建完整的图像,最后再打印出来.

【讨论】:

  • 我假设“在内存中创建”是指将所有信息放入一个数组中。不过,我在调用带有数组参数的函数时遇到了一些麻烦。
猜你喜欢
  • 1970-01-01
  • 2015-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多