Skip to content

Instantly share code, notes, and snippets.

@Santarh
Created January 26, 2012 16:37
Show Gist options
  • Save Santarh/1683681 to your computer and use it in GitHub Desktop.
Save Santarh/1683681 to your computer and use it in GitHub Desktop.
Excel Column Name
#include <stdio.h>
#include <math.h>
#define AtoZ 26
#define ASCII_A 65
int calc( char* c, int* sum )
{
if ( *c == '\0' )
{
return 0;
}
int digit = calc( c + 1, sum );
int a = (int)*c - ASCII_A + 1;
*sum += a * pow( AtoZ, digit );
return digit + 1;
}
int main( int argc, char* argv[] )
{
char* row = argv[ 1 ];
int result = 0;
calc( row, &result );
printf( "%d\n", result );
}
#include <stdio.h>
int calc( char* c, int sum )
{
if ( *c == '\0' )
{
return sum;
}
int a_to_z = 'Z' - 'A' + 1;
int now_digit = (int)*c - 'A' + 1;
return calc( c + 1, sum * a_to_z + now_digit );
}
int main( int argc, char* argv[] )
{
printf( "%d\n", calc( argv[ 1 ], 0 ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment