英文:
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 <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;
}
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 <assert.h>
#include <stdio.h>
#include <string.h>
#define BASE64_CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
#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] < 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 < n; i++)
if(counter[i] < BASE64_LEN - 1)
return 0;
return 1;
}
void print(size_t n, unsigned char counter[n]) {
for(size_t i = 0; i < n; i++)
printf("%c", BASE64_CHARS0+网站访问量]);
printf("\n");
}
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 <assert.h>
#include <stdio.h>
#include <string.h>
#define BASE64_CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
#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] < 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 < n; i++)
if(counter[i] < BASE64_LEN - 1)
return 0;
return 1;
}
void print(size_t n, unsigned char counter[n]) {
for(size_t i = 0; i < n; i++)
printf("%c", BASE64_CHARS0+网站访问量]);
printf("\n");
}
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
...
/+
//
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论