@超导体元素 调出来了
以下是基于C语言的顺序任意进制代码的实现:
#include <stdio.h>
#include <string.h>
#define MAX_LEN 256
int current[MAX_LEN] = {0}; // 用于储存当前的任意进制数组
char library[MAX_LEN] = {0}; // 定义任意进制数组的每一位要如何显示
// 初始化进制
void init(long long int start, char lib[]) {
strcpy(library, lib);
int base = strlen(library);
int index = 0;
// 把十进制的start转换成base进制
while (start > 0) {
current[index++] = start % base;
start /= base;
}
}
// 增加任意进制数组
void increase(char output[]) {
int base = strlen(library);
int carry = 1;
// 末位自增1,如果达到base就进位
for (int i = 0; i < MAX_LEN; i++) {
current[i] += carry;
if (current[i] == base) {
current[i] = 0;
carry = 1;
} else {
carry = 0;
}
}
// 根据library将current转换为显示的格式
int index = 0;
for (int i = MAX_LEN - 1; i >= 0; i--) {
if (current[i] > 0 || index > 0) {
output[index++] = library[current[i]];
}
}
output[index] = '\0'; // 字符串末尾添加'\0',表示字符串的结束
}
// 主函数
int main() {
// 测试init和increase能否正常运行
char lib[] = "0123456789ABCDEF"; // 字符集
long long int start = 1000; // 起始点
init(start, lib); // 初始化进制
char output[MAX_LEN] = {0}; // 用于储存输出的任意进制数
increase(output); // 增加任意进制数组并输出
printf("%s\n", output);
return 0;
}
在上述代码中,我们首先定义了两个全局变量 current 和 library,分别用于储存当前的任意进制数组和定义任意进制数组的每一位要如何显示。在 init 函数中,我们首先将 lib 复制到 library 中,确定其长度 base 作为进制的底数。接着,我们将十进制的 start 转换成 base 进制,并将转换后的结果储存在 current 中。在 increase 函数中,我们将任意进制数组 current 的末位自增1,如果达到 base 就进位,并根据 library 将 current 转换为显示的格式,赋值给参数中指针指向的 output。在 main 函数中,我们测试 init 和 increase 能否正常运行。