使用递归在C中打印字符串

时间:2014-10-20 14:09:50

标签: c recursion

首先让我说我不是在寻找有人为我做这件事。我希望有一个提示或建议。

我知道有一种更聪明的方法可以做到这一点。代码发布在下面。我正在尝试打印大纲。我的代码工作深度为3.(深度是子节的数量 - 因此3将是第1节,第1.A节和第1.A.1节)。它也适用于26的宽度(部分的数量和每种类型的子部分),它的上限。但是,为了获得更大的深度,它将涉及更多的循环。这不仅是可怕的代码,它还冻结了我正在努力的终端。 我相信递归会让它变得更好,但我在使用字符串时很难理解这个想法(我明白它是一个数字)。谢谢!

#include <stdio.h>

int sec(int width, int snum) {

    char section[100];
    sprintf(section, "Section ");
    printf("%s %i", section, snum);
    return 0;
}

int ssec_num(int width, int i) {
    char num[100];
    sprintf(num, "%i", i);
    printf(".%s", num);
}

int ssec_let(int width, char z) {
    char let[100];
    sprintf(let, ".%c", z);
    printf("%s", let);
}


int main(int argc, char* argv[]) {
    int depth = atoi(argv[1]);
    int width = atoi(argv[2]);
    int sec_int=1;
    int sec_wid = width;
    int let_wid;
    int num_int;
    int num_dep;
    int num_wid;
    int dep;
    char z = 'A';

    while(sec_wid > 0) {
        sec(width, sec_int);
        let_wid = width;
        dep = depth-1;
        printf("\n");
        while(dep > 0) {
            while(let_wid > 0) {
                num_wid = width;
                num_int = 1;
                sec(width, sec_int);
                ssec_let(let_wid, z);
                printf("\n");
                num_dep = depth-2;
                    while(num_dep > 0) {
                        while(num_wid > 0) {
                            sec(width, sec_int);
                            ssec_let(let_wid, z);
                            ssec_num(width, num_int);
                            num_wid--;
                            num_int++;
                            printf("\n");
                            num_dep--;
                        }
                    }
                let_wid --;
                z++;
            }
            dep --; 
        }
    sec_int++;
    sec_wid--;
    z = 'A';
    } 
 }

如果depth为3且width为2则为

Section 1
 Section 1.A
  Section 1.A.1
  Section 1.A.2
 Section 1.B
  Section 1.B.1
  Section 1.B.2
Section 2
 Section 2.A
  Section 2.A.1
  Section 2.A.2
 Section 2.B
  Section 2.B.1
  Section 2.B.2

1 个答案:

答案 0 :(得分:2)

您描述的算法使用width来声明每个(子)部分重复的次数。你可以通过循环实现这种重复。

该算法还使用depth来确定您拥有多少(子)部分。这是棘手的部分,你可以使用递归来解决它。递归函数基本上是一个自称有限次数的函数。必须始终有一个条件来停止递归,否则函数会调用自己,直到调用堆栈溢出,异常停止程序执行。

对于您的问题,您可以使用一个接收计数器的函数,该函数确定当前的(子)区域深度。它会循环width次(如上所述)并自行调用depth次,直到计数器达到depth的值。这样,您将拥有一个具有depth个(子)部分的函数,每个部分都有width个项目。

当您需要在先前深度处打印(子)部分时,您可以使用缓冲区将每个深度的部分值(例如int buffer[MAX_DEPTH];)与#define MAX_DEPTH 100一起存储以设置最大深度你的程序支持。

然后你会有像

这样的东西
#include <stdio.h>

#define MAX_DEPTH 100

void print_section(const int *const buffer, const int current_depth) { 
    // print all the (sub)section values stored at the buffer so far
    // use a loop like for (i = 0; i <= current_depth; i++)
}

void recursive(int *const buffer, const int current_depth, 
        const int depth, const int width) {
    if (current_depth < depth) {
        // continue recursion
        int current_width;
        for (current_width = 1; current_width <= width; current_width++) {
            buffer[current_depth] = current_width;
            print_section(buffer, current_depth);
            recursive(buffer, current_depth + 1, depth, width);
        }
    }
    // else stop recursion
}

int main(int argc, char* argv[]) {
    // ...
    int buffer[MAX_DEPTH];
    recursive(buffer, 0, depth, width);
    return 0;
}

您还需要一些额外的逻辑来确定何时在每个(子)区域深度打印字母或数字。

编辑:要打印(子)部分标题,请使用以下

void print_section(const int *const buffer, const int current_depth) { 
    int i;
    printf("Section ");
    for (i = 0; i <= current_depth; i++) {
        printf(i == 0 ? "%i" : ".%i", buffer[i]);
    }
    printf("\n");
}