繁体   English   中英

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

[英]Using recursion to print strings in C

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

我知道有更聪明的方法可以做到这一点。 代码在下面发布。 我正在尝试打印轮廓。 我的代码的工作深度为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

您描述的算法使用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");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM