Registriert seit: 31. Okt 2003
1.120 Beiträge
Delphi 7 Personal
|
Re: zählen mit buchstaben?
2. Nov 2003, 20:28
Na, dann so:
Delphi-Quellcode:
function LongPowerLong(ABaseNum, AExponent : Word) : LongWord;
var
MultCount : Integer;
begin
Result := 1;
for MultCount := 1 to AExponent do
Result := Result * ABaseNum;
end;
const
AlphaBase = 26;
AlphaMax = 4;
function AlphaToNum(ATNstr : String ) : LongWord;
const
ValidChars : set of Char = ['A'..'Z'];
var
ACount : Integer;
function CharNum(CNchr : Char) : Byte;
begin
Case Ord(UpCase(CNchr)) Of
65..90: CharNum := Ord(UpCase(CNchr)) - 65;
end;
end;
begin
AlphaToNum := 0;
for ACount := 1 to Min(length(ATNstr), AlphaMax+1) do
begin
If not (UpCase(ATNstr[ACount]) in ValidChars) then
begin
result := 0;
Break;
end;
result := result + (CharNum(ATNstr[ACount]) * LongPowerLong(AlphaBase, Min(length(ATNstr), AlphaMax+1) - ACount));
end;
end;
function NumToAlpha(NTAnum : LongWord ) : String;
var
NumChar : array[0..AlphaBase-1] Of Char;
NumCount : Word;
begin
for NumCount := 0 to (AlphaBase-1) do NumChar[NumCount] := Chr(65 + NumCount);
// max 26 hoch (5-1)
result := '';
for NumCount := (AlphaMax+1) downto 1 do
If (NTAnum < LongPowerLong(AlphaBase, NumCount)) then
begin
If not (NumCount = (AlphaMax+1)) then
result := result + NumChar[NTAnum div LongPowerLong(AlphaBase, NumCount-1)];
NTAnum := NTAnum mod LongPowerLong(AlphaBase, NumCount-1);
end;
end;
Delphi-Quellcode:
var
ATextFile : TextFile;
bucount : LongWord;
begin
AssignFile(ATextFile, 'C:\buchstab.txt');
ReWrite(ATextFile);
for bucount := AlphaToNum('AAAA') to AlphaToNum('ZZZZ') do
WriteLn(ATextFile, NumToAlpha(bucount));
CloseFile(ATextFile);
end;
Macht 456976 Möglichkeiten * 6 Bytes (#13#10 dabei) = 2741856 Bytes
|
|
Zitat
|