1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160:
| unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
const ELEMENT_LISTE = 'H,He,Li,Be,B,C,N,O,'+ 'F,Ne,Na,Mg,Al,Si,P,S,'+ 'Cl,Ar,K,Ca,Sc,Ti,V,Cr,'+ 'Mn,Fe,Co,Ni,Cu,Zn,Ga,Ge,'+ 'As,Se,Br,Kr,Rb,Sr,Y,Zr,'+ 'Nb,Mo,Tc,Ru,Rh,Pd,Ag,Cd,'+ 'In,Sn,Sb,Te,I,Xe,Cs,Ba,'+ 'La,Ce,Pr,Nd,Pm,Sm,Eu,Gd,'+ 'Tb,Dy,Ho,Er,Tm,Yb,Lu,Hf,'+ 'Ta,W,Re,Os,Ir,Pt,Au,Hg,'+ 'Tl,Pb,Bi,Po,At,Rn,Fr,Ra,'+ 'Ac,Th,Pa,U,Np,Pu,Am,Cm,'+ 'Bk,Cf,Es,Fm,Md,No,Lr,Rf,'+ 'Db,Sg,Bh,Hs,Mt,Ds,Rg,Uub,'+ 'Uut,Uuq,Uup,Uuh,Uus,Uuo';
ElementHead: Set of Char = ['A'..'Z']; ElementBody: Set of Char = ['a'..'z']; Numbers: Set of Char = ['0'..'9'];
type TParserState = (psScan = 0, psElement, psNumber, psError);
TForm1 = class(TForm) Formel: TEdit; BtnStart: TButton; Log: TMemo; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure BtnStartClick(Sender: TObject); procedure FormelKeyPress(Sender: TObject; var Key: Char); private public Element: TStringList; end;
var Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject); begin Element := TStringList.Create; Element.CommaText := ELEMENT_LISTE; end;
procedure TForm1.FormDestroy(Sender: TObject); begin Element.Free; end;
procedure TForm1.BtnStartClick(Sender: TObject); var i: Integer; ParserState: TParserState; Current: String;
procedure CheckElement(const S: String); begin if (Element.IndexOf(S) > -1) then Log.Lines.Add('Element gefunden: '+S) else begin Log.Lines.Add('Fehler (Element ungültig): '+S); ParserState := psError; end; end;
procedure CheckNumber(const S: String); begin if (StrToIntDef(S,0) > 0) then Log.Lines.Add('Zahl gefunden: '+S) else begin Log.Lines.Add('Fehler (Zahl ungültig): '+S); ParserState := psError; end; end;
begin Log.Clear; Current := ''; ParserState := psScan; i := 1; while ( (i <= Length(Formel.Text)) and (ParserState < psError) ) do begin case ParserState of psScan: begin if (Formel.Text[i] in ElementHead) then begin ParserState := psElement; Current := Formel.Text[i]; end else if (Formel.Text[i] in Numbers) then begin ParserState := psNumber; Current := Formel.Text[i]; end else begin ParserState := psError; Log.Lines.Add('Fehler in Formel an Position '+IntToStr(i)+' ['+Formel.Text[i]+']'); end; end; psElement: begin if (Formel.Text[i] in ElementBody) then begin Current := Current +Formel.Text[i]; end else begin ParserState := psScan; CheckElement(Current); Dec(i); end; end; psNumber: begin if (Formel.Text[i] in Numbers) then begin Current := Current +Formel.Text[i]; end else begin ParserState := psScan; CheckNumber(Current); Dec(i); end; end; end; Inc(i); end; case ParserState of psElement: CheckElement(Current); psNumber: CheckNumber(Current); end; end;
procedure TForm1.FormelKeyPress(Sender: TObject; var Key: Char); begin if (Key = #13) then begin BtnStartClick(Self); Formel.SelectAll; Formel.SetFocus; end; end;
end. |