gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 02:46:51 25 4
gpt4 key购买 nike

首先让我说我不是在找人为我做这件事。我希望得到提示或建议。

我知道有一种更聪明的方法可以做到这一点。代码贴在下面。我正在尝试打印大纲。我的代码的工作深度为 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 来声明每个(子)部分重复了多少次。您可以通过循环实现这种重复。

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

对于您的问题,您可以有一个接收计数器的函数,该计数器确定当前的(子)部分深度。它会循环 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");
}

关于c - 在 C 中使用递归打印字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26467683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com