Skip to content

Instantly share code, notes, and snippets.

@lithtle
Created January 30, 2012 18:20
Show Gist options
  • Save lithtle/1705783 to your computer and use it in GitHub Desktop.
Save lithtle/1705783 to your computer and use it in GitHub Desktop.
エクセル列名変換問題
/*
エクセル列名変換問題
NOTE:
A = 1, B = 2, .....
AA = 27.....
XFD = 16384
@created at: 2012-01-31
@author: lithtle
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>
#define CHARACTER_A 65
#define ALPHABETS 26
int
argvCheck(char *arg)
{
int c;
while(*arg)
{
c = *arg;
if(!isalpha(c))
{
return 1;
}
arg++;
}
return 0;
}
int
i_pow(int i, int j)
{
int ret = 1;
int k;
for(k = 0; k < j; k ++)
{
ret *= i;
}
return ret;
}
int
conv(char *s)
{
int ret = 0;
int c;
int len;
while(*s)
{
c = *s;
len = strlen(s);
c = toupper(c);
ret += i_pow(ALPHABETS, len - 1) * (c - CHARACTER_A + 1);
s++;
}
return ret;
}
int
main(int argc, char *argv[])
{
if(argc != 2 || argvCheck(argv[1]))
{
fprintf(stderr, "invalid argument\n");
fprintf(stderr, "usage: %s [string]\n", argv[0]);
exit(1);
}
printf("%s converts into => %5d\n", argv[1], conv(argv[1]));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment