|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace Mars_Mission
{
public class cLibString
{
static string strCompareList = "";
static string[] strCompare = { " AAÀÁÂÃĀ",
" CÇ",
" EÈÉÊËĒĒ",
" IÌÍÎÏĪ",
" NÑ",
" OÒÓÔÕÖŌ",
" SŠ",
" UÙÚÛÜŪ",
" YÝ",
" ZŽŽ",
" aàáâãāäå",
" cç",
" eèéêëēē",
" iìíîïī",
" nñ",
" oòóôõöō",
" sš",
" uùúûüū",
" yýÿ",
" zž",
"AEÆ",
"aeæ",
"oeœ",
"OEŒ"
};
public static string deAccent(string strText)
{
char chrTestChar;
string strRetVal;
if (strText.Trim().Length == 0)
{
return strText;
}
if (strCompareList.Length == 0)
{
for (int intCounter = 0; intCounter < strCompare.Length; intCounter++)
strCompareList = strCompareList + strCompare[intCounter].Substring(3).Trim().PadRight(20);
}
strRetVal = "";
string strReplace = "";
bool bolReplace;
for (int intCounter = 0; intCounter < strText.Length; intCounter++)
{
chrTestChar = strText[intCounter];
bolReplace = false;
if (chrTestChar != ' ')
{
int intIndexChar = strCompareList.IndexOf(chrTestChar);
if (intIndexChar >= 0)
{
int intIndexList = (int)Math.Floor((double)intIndexChar / 20.0);
bolReplace = true;
strReplace = strCompare[intIndexList].Substring(0, 2).Trim();
}
}
strRetVal += (bolReplace ? strReplace : chrTestChar.ToString());
}
return strRetVal;
}
public static bool isUpperCase(char c)
{
return (c >= 'A' && c<='Z');
}
public static bool isAlpha(char c)
{
return ((c>= 'a' && c<='z')
||(c>='A' && c<='Z'));
}
public static bool isNumeral(char c)
{
return (c >= '0' && c <= '9');
}
public class classBase26Variable
{
public classBase26Variable()
{
}
public static classBase26Variable fromXmlFile(string strFilename)
{
if (System.IO.File.Exists(strFilename))
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(strFilename);
XmlNode xRoot = xDoc.DocumentElement;
XmlNode xBase26Value = xRoot.FirstChild;
classBase26Variable cRetVal = new classBase26Variable();
cRetVal.Value = xBase26Value.InnerText;
return cRetVal;
}
else
{
classBase26Variable cRetVal = new classBase26Variable();
cRetVal.saveToXml(strFilename);
return cRetVal;
}
}
public void saveToXml(string strFilename)
{
XmlDocument xDoc = new XmlDocument();
XmlElement xRoot = xDoc.CreateElement("Root");
XmlNode xBase26 = xDoc.CreateElement("Value");
xBase26.InnerText = strValue;
xRoot.AppendChild(xBase26);
xDoc.AppendChild(xRoot);
xDoc.Save(strFilename);
}
public classBase26Variable(uint uintValue)
{
strValue = "";
int intMostSignificantChar = (int)(Math.Floor(Math.Log(uintValue) / Math.Log(26)));
int intCharRank = intMostSignificantChar;
while (uintValue >= 0 && intCharRank >=0)
{
uint uintRankValue = (uint)Math.Pow(26, intCharRank--);
byte bytNumRank = (byte)(Math.Floor((double)uintValue / (double)uintRankValue));
uintValue -= (uint)(bytNumRank * uintRankValue);
strValue += (char)(bytNumRank + (byte)'A');
}
}
string strValue = "";
public string Value
{
get { return strValue; }
set
{
string strTemp = value.ToUpper();
foreach (char c in strTemp)
if (!isAlpha(c)) return;
strValue = strTemp;
}
}
public ulong ConvertTo_ULong()
{
ulong ulngRetVal = 0;
for (int intCharCounter = 0; intCharCounter < strValue.Length; intCharCounter ++)
{
char c = strValue[strValue.Length - 1 - intCharCounter];
byte bytValue = (byte)((byte)c - (byte)'A');
ulngRetVal += (ulong)(Math.Pow(26, intCharCounter) * bytValue);
}
return ulngRetVal;
}
public classBase26Variable Add(uint uintAddendeum)
{
return Add(new classBase26Variable(uintAddendeum));
}
public classBase26Variable Trim()
{
int intCharChop = 0;
while (intCharChop < strValue.Length && strValue[intCharChop] == 'A')
{
intCharChop++;
}
strValue = strValue.Substring(intCharChop);
return Copy();
}
public classBase26Variable Add(classBase26Variable addendum)
{
int intNumChars = 5 + (strValue.Length > addendum.Value.Length
? strValue.Length
: addendum.Value.Length);
string strTemp = strValue.PadLeft(intNumChars, 'A');
string strAddendum = addendum.Value.PadLeft(intNumChars, 'A');
classBase26Variable cRetVal = new classBase26Variable();
string strRetVal = "";
byte bytAddOn = 0;
for (int intCharCounter =strTemp.Length-1;intCharCounter >=0; intCharCounter--)
{
strRetVal = addChar(strTemp[intCharCounter], strAddendum[intCharCounter], ref bytAddOn) + strRetVal;
}
if (bytAddOn == 1)
strRetVal = "B" + strRetVal;
cRetVal.Value = strRetVal;
cRetVal.Trim();
return cRetVal;
}
public classBase26Variable increment()
{
Value = Add(new classBase26Variable(1)).Value;
return Copy();
}
public classBase26Variable Copy()
{
classBase26Variable cRetVal = new classBase26Variable();
cRetVal.Value = strValue;
return cRetVal;
}
char addChar(char cA, char cB, ref byte bytAddOn)
{
char cRetVal = (char)((byte)'A' + ((byte)cA + (byte)cB + bytAddOn) % 26);
bytAddOn = ((byte)cRetVal < (byte)cA)
? (byte)1
: (byte)0;
return cRetVal;
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Christ Kennedy grew up in the suburbs of Montreal and is a bilingual Quebecois with a bachelor’s degree in computer engineering from McGill University. He is unemployable and currently living in Moncton, N.B. writing his next novel.