VB icon

Convert number in to Colum Refrences and vise versa

Email
Submitted on: 1/1/2015 7:46:00 AM
By: Robert ''Bob'' Engelhardt (from psc cd)  
Level: Advanced
User Rating: By 2 Users
Compatibility: C++ (general)
Views: 1159
 
     This code was created to properly asign names to columns in a database program I have been writing. an example of how it numbers is as following: A,B,C...Z,AA,AB...ZZ,AAA,AAB... and so on This code uses recursion
 

INCLUDE files:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//INCLUDE files for :Convert number in to Colum Refrences and vise versa
//**************************************
#include <condefs.h>
#include <conio.h>
#include <iostream.h>
#include <string.h>
#pragma hdrstop
#define AsciiText 64
#define AsciiA 65
#define AsciiZ 90
#define AlphaSize 26
#pragma argsused
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Convert number in to Colum Refrences and vise versa
// Description:This code was created to properly asign names to columns in a database program I have been writing. an example of how it numbers is as following:
A,B,C...Z,AA,AB...ZZ,AAA,AAB... and so on
This code uses recursion
// By: Robert ''Bob'' Engelhardt (from psc cd)
//
// Inputs:The name of the column or the number of the column
//
// Returns:The code returns the Column name as a string or a numbber
//
// Assumes:The code assumes that there is no '0' column and there is no colum greater that the maximum integer value ( around 32,000)
This program was written in Borland Builder under console (with out wich you may not have access to the AnsiString class)
//
// Side Effects:The code that makes Names in to numbers is only abel to curently handel up to two letter names ie A-ZZ, I have yet figured out a way to make it work other wise
//**************************************

AnsiString MakeCol(int value);// convert # into column ref A,B,C...AA,AB...
int MakeNum(AnsiString string); // convert column into #
void main()
{
// A,B,C...Z,AA,AB,..AZ,BA,BC...ZZ,AAA,AAB... and so on
int col=1; // value must be >1 & <=MAXINT to properly work
AnsiString string;// just a string to use
do
{
 cout << "Enter value to convert (0 for exit) ";
 cin >> col;
 string = MakeCol(col); // copy the sting to as string for handeling
 cout << MakeNum(string) << "= " << string.c_str() << endl;
}
while(col>0);
}
AnsiString MakeCol(int value)
{
AnsiString Finalstring;
int overz=0;
if(value<=AlphaSize) // if it is A-Z
 return( char(value+AsciiText) );
if(value>AlphaSize)// if it goes beyond Z
{
 while(value>AlphaSize)// calculate how many times it goes beyond Z
 {
 overz++; // add to over singel digit counter
 value-=AlphaSize;
 }
 //_____________Recurse_Call__________________________
 Finalstring+=MakeCol(overz);// recursively convert
}
Finalstring+=char(value+AsciiText); // append letter to back of string
return(Finalstring);
}
int MakeNum(AnsiString string)
{
string=string.UpperCase(); // make string uppercase letters
char *TrueString=string.c_str(); // make into string of characters
int length=string.Length();// find length of string
int value=0; // whole value of string as int
int lcv=0;// loop controll variable
if(!length)// no size to string
 return(0); // string is empty return with error
// check to be sure string is of valid character (ONLY LETTERS any case)
for(lcv=0;lcv<length;lcv++)
 if((TrueString[lcv] < AsciiA) || (TrueString[lcv] > AsciiZ)) // proper ?
 return(0); // return 0 for error with string
if(length==1) // singel character column number
 value+=int(TrueString[0]-AsciiText);// give proper value of cahracter
if(length>1)// string contains more than one character
{
 value+=int(TrueString[length-1]-AsciiText); // remove leading value of 64
 for(lcv=2;lcv<=length;lcv++)
 {
 value+=(int(TrueString[length-lcv]-AsciiText) * AlphaSize);
 }
}
return(value);
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.