将多个永久性的for()语句更改为仅根据用户输入所需的for()语句。

huangapple go评论64阅读模式
英文:

Change multiple permanent for() statements into only required for() statements required from user input

问题

我正在尝试创建一个循环遍历Base64符号的C程序。从技术上讲,我已经成功实现了这一点。然而,对于每个符号,都有一个特定的for()语句。我希望能够(暂时还不能,只有在我能够让代码正常工作之后才行)为其提供用户输入(通过参数,但这是以后的事情)。

以下是我的代码:

#include <stdio.h>
#include <string.h>

#define BASE64_CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

int main() {
  int i, j, k, l, m, a;
    char base64[100];
    int base64_len = strlen(BASE64_CHARS);
    for (i = 0; i < base64_len; i++) {
        base64[0] = BASE64_CHARS[i];
        for (j = 0; j < base64_len; j++) {
            base64[1] = BASE64_CHARS[j];
            for (k = 0; k < base64_len; k++) {
                base64[2] = BASE64_CHARS[k];
                for (l = 0; l < base64_len; l++) {
                    base64[3] = BASE64_CHARS[l];
                    for (m = 0; m < base64_len; m++) {
                        base64[4] = BASE64_CHARS[m];
			            for (a = 0; a < base64_len; a++) {
			                base64[5] = BASE64_CHARS[a];
			                printf("\r%s", base64);
			            }
                    }
                }
            }
        }
    }
    return 0;
}

有关如何实现这一点的任何想法将不胜感激。

英文:

I am trying to make a c program that cycles through base64 symbols. And I did technically manage to achieve that. However, for each symbol, there is a specific for() statement. And I want it so that I can give it (not yet, only until I can get the code to work) user input (through arguments, but that's for later).

Here's my code:

#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

#define BASE64_CHARS &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/&quot;

int main() {
  int i, j, k, l, m, a;
    char base64[100];
    int base64_len = strlen(BASE64_CHARS);
    for (i = 0; i &lt; base64_len; i++) {
        base64[0] = BASE64_CHARS[i];
        for (j = 0; j &lt; base64_len; j++) {
            base64[1] = BASE64_CHARS[j];
            for (k = 0; k &lt; base64_len; k++) {
                base64[2] = BASE64_CHARS[k];
                for (l = 0; l &lt; base64_len; l++) {
                    base64[3] = BASE64_CHARS[l];
                    for (m = 0; m &lt; base64_len; m++) {
                        base64[4] = BASE64_CHARS[m];
			            for (a = 0; a &lt; base64_len; a++) {
			                base64[5] = BASE64_CHARS[a];
			                printf(&quot;\r%s&quot;, base64);
			            }
                    }
                }
            }
        }
    }
    return 0;
}

Any ideas on how to do this would be appreciated.

答案1

得分: 2

以下是您要翻译的内容:

我建议您使用数组来模拟任意数量的Base 64数字计数器。然后,您只需要一个名为 incr() 的函数来添加带进位的 1 到计数器中,一个 print() 函数来将计数器转换为字符串,以及为了通用性,一个 done() 函数来检测是否达到了最大值。

在下面的示例中,我使用了一个两位数的计数器 { [1] = 0 },这只是一种表示索引 1 应该为 0 而未指定的部分都初始化为零的方式。要使用一个六位数的计数器,您可以使用 { [5] = 0 }

strlen(BASE64_CHARS) == sizeof BASE64_CHARS - 1,但后者在编译时计算。

#include &lt;assert.h&gt;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

#define BASE64_CHARS &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/&quot;
#define BASE64_LEN sizeof BASE64_CHARS - 1

// 最高位在索引 0,最低位在索引 n-1
void incr(size_t n, unsigned char counter[n]) {
    assert(n);
    for(size_t i = n - 1;; i--) {
        if(counter[i] &lt; BASE64_LEN - 1) {
            counter[i]++;
            break;
        }
        counter[i] = 0;
        // 进位在最高位结束
        if(!i) 
            break;
    }
}

int done(size_t n, unsigned char counter[n]) {
    for(int i = 0; i &lt; n; i++)
        if(counter[i] &lt; BASE64_LEN - 1)
            return 0;
    return 1;
}

void print(size_t n, unsigned char counter[n]) {
    for(size_t i = 0; i &lt; n; i++)
        printf(&quot;%c&quot;, BASE64_CHARS
0
+
网站访问量
]);
printf(&quot;\n&quot;); } int main(void) { unsigned char counter[] = { [1] = 0 }; size_t counter_len = sizeof counter / sizeof *counter; for(;;) { print(counter_len, counter); if(done(counter_len, counter)) break; incr(counter_len, counter); } }

以下是第一行和最后几行:

AA
AB
...
/+
//
英文:

I suggest you use an array to emulate an arbitrary number of base 64 digits counter. Then you just need a function incr() to add 1 to the counter with carry, print() function to convert your counter to a string, and for generality a done() function to detect when at the maximum value.

Below I used a 2 digit counter { [1] = 0 } which is just fancy way of saying index 1 should be 0 and the ones not specified are zero-initialized. To use a 6 digit counter you would use { [5] = 0 }.

strlen(BASE64_CHARS) == sizeof BASE64_CHARS - 1 but the latter is computed at compile-time.

#include &lt;assert.h&gt;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

#define BASE64_CHARS &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/&quot;
#define BASE64_LEN sizeof BASE64_CHARS - 1

// The most significant digit is at index 0, and the the least
// significant digit at index n-1
void incr(size_t n, unsigned char counter[n]) {
    assert(n);
	for(size_t i = n - 1;; i--) {
		if(counter[i] &lt; BASE64_LEN - 1) {
			counter[i]++;
			break;
		}
		counter[i] = 0;
        // carry over ends at the most significant digit
		if(!i) 
			break;
	}
}

int done(size_t n, unsigned char counter[n]) {
	for(int i = 0; i &lt; n; i++)
		if(counter[i] &lt; BASE64_LEN - 1)
			return 0;
	return 1;
}

void print(size_t n, unsigned char counter[n]) {
	for(size_t i = 0; i &lt; n; i++)
		printf(&quot;%c&quot;, BASE64_CHARS
0
+
网站访问量
]); printf(&quot;\n&quot;); } int main(void) { unsigned char counter[] = { [1] = 0 }; size_t counter_len = sizeof counter / sizeof *counter; for(;;) { print(counter_len, counter); if(done(counter_len, counter)) break; incr(counter_len, counter); } }

and here the first and last few lines:

AA
AB
...
/+
//

huangapple
  • 本文由 发表于 2023年5月26日 12:14:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76337627.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定