これはC の Calc セル コンバーターに関連していますが、明らかに異なります。このコードは、そのコードから迅速に派生しました。
#include <ctype.h>
#include <stdio.h>
#include <string.h>
/* These declarations should be in a header */
extern char *b3_row_encode(unsigned row, char *buffer);
extern unsigned b3_row_decode(const char *buffer);
static char *b3_encode(unsigned row, char *buffer)
{
unsigned div = row / 3;
unsigned rem = row % 3;
if (div > 0)
buffer = b3_encode(div-1, buffer);
*buffer++ = rem + 'a';
*buffer = '\0';
return buffer;
}
char *b3_row_encode(unsigned row, char *buffer)
{
if (row == 0)
{
*buffer = '\0';
return buffer;
}
return(b3_encode(row-1, buffer));
}
unsigned b3_row_decode(const char *code)
{
unsigned char c;
unsigned r = 0;
while ((c = *code++) != '\0')
{
if (!isalpha(c))
break;
c = tolower(c);
r = r * 3 + c - 'a' + 1;
}
return r;
}
#ifdef TEST
static const struct
{
unsigned col;
char cell[10];
} tests[] =
{
{ 0, "" },
{ 1, "a" },
{ 2, "b" },
{ 3, "c" },
{ 4, "aa" },
{ 5, "ab" },
{ 6, "ac" },
{ 7, "ba" },
{ 8, "bb" },
{ 9, "bc" },
{ 10, "ca" },
{ 11, "cb" },
{ 12, "cc" },
{ 13, "aaa" },
{ 14, "aab" },
{ 16, "aba" },
{ 22, "baa" },
{ 169, "abcba" },
};
enum { NUM_TESTS = sizeof(tests) / sizeof(tests[0]) };
int main(void)
{
int pass = 0;
for (int i = 0; i < NUM_TESTS; i++)
{
char buffer[32];
b3_row_encode(tests[i].col, buffer);
unsigned n = b3_row_decode(buffer);
const char *pf = "FAIL";
if (strcmp(tests[i].cell, buffer) == 0 && n == tests[i].col)
{
pf = "PASS";
pass++;
}
printf("%s: Col %3u, Cell (wanted: %-8s vs actual: %-8s) Col = %3u\n",
pf, tests[i].col, tests[i].cell, buffer, n);
}
if (pass == NUM_TESTS)
printf("== PASS == %d tests OK\n", pass);
else
printf("!! FAIL !! %d out of %d failed\n", (NUM_TESTS - pass), NUM_TESTS);
return (pass == NUM_TESTS) ? 0 : 1;
}
#endif /* TEST */
このコードには、文字列から整数に変換するテスト プログラムと関数、および整数から文字列に変換する関数が含まれています。テストは連続した変換を実行します。コードは空の文字列をゼロとして扱いません。
出力例:
PASS: Col 0, Cell (wanted: vs actual: ) Col = 0
PASS: Col 1, Cell (wanted: a vs actual: a ) Col = 1
PASS: Col 2, Cell (wanted: b vs actual: b ) Col = 2
PASS: Col 3, Cell (wanted: c vs actual: c ) Col = 3
PASS: Col 4, Cell (wanted: aa vs actual: aa ) Col = 4
PASS: Col 5, Cell (wanted: ab vs actual: ab ) Col = 5
PASS: Col 6, Cell (wanted: ac vs actual: ac ) Col = 6
PASS: Col 7, Cell (wanted: ba vs actual: ba ) Col = 7
PASS: Col 8, Cell (wanted: bb vs actual: bb ) Col = 8
PASS: Col 9, Cell (wanted: bc vs actual: bc ) Col = 9
PASS: Col 10, Cell (wanted: ca vs actual: ca ) Col = 10
PASS: Col 11, Cell (wanted: cb vs actual: cb ) Col = 11
PASS: Col 12, Cell (wanted: cc vs actual: cc ) Col = 12
PASS: Col 13, Cell (wanted: aaa vs actual: aaa ) Col = 13
PASS: Col 14, Cell (wanted: aab vs actual: aab ) Col = 14
PASS: Col 16, Cell (wanted: aba vs actual: aba ) Col = 16
PASS: Col 22, Cell (wanted: baa vs actual: baa ) Col = 22
PASS: Col 169, Cell (wanted: abcba vs actual: abcba ) Col = 169
== PASS == 18 tests OK