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: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216:
| unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type TForm1 = class(TForm) Edit1: TEdit; Button1: TButton; Memo1: TMemo; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Multi(Zahl1,Zahl2:integer; var Term:string); procedure Add(Zahl1,Zahl2:integer; var Term:string); procedure Sub(Zahl1,Zahl2:integer; var Term:string); procedure Divi(Zahl1,Zahl2:integer; var Term:string); procedure Rechnen(Eingabe:string); private public end;
var Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject); begin memo1.text := ''; end;
procedure TForm1.Button1Click(Sender: TObject); begin Memo1.Text:=''; Rechnen(Edit1.Text); end;
procedure Tform1.Rechnen(Eingabe:string); var Eingabe2,Klammer,Klammer2:string; PosKlammerauf,PosKlammerzu,i,zahl1,zahl2:integer; begin if pos('(',Eingabe)>0 then begin repeat begin posklammerzu := 0; Posklammerauf:=0; i:=0; repeat begin if eingabe[i] ='(' then begin posklammerauf := i ; end; if eingabe[i] =')' then begin posklammerzu := i ; end; i:=i+1; end until (posklammerzu <> 0) or (i=length(eingabe)+1); Klammer:= Copy(Eingabe,Posklammerauf,posklammerzu-posklammerauf+1); Klammer2:=Klammer; repeat if ((pos('/',Klammer)> pos('*',Klammer)) or (pos('/',Klammer)=0)) and (pos('*',Klammer)<>0) then Multi(Zahl1,Zahl2,Klammer) else if pos('/',Klammer)<>0 then Divi(Zahl1,Zahl2,Klammer); until (pos('*',Klammer)=0) and (pos('/',Klammer)=0); repeat if ((pos('-',Klammer)> pos('+',Klammer)) or (pos('-',Klammer)=0)) and (pos('+',Klammer)<>0) then Add(Zahl1,Zahl2,Klammer) else if pos('-',Klammer)<>0 then Sub(Zahl1,Zahl2,Klammer); until (pos('+',Klammer)=0) and (pos('-',Klammer)=0); Eingabe:=Stringreplace(Eingabe,Klammer2,copy(Klammer,2,length(Klammer)-2),[rfreplaceall]); end; until pos('(',Eingabe)=0; end; Eingabe2:=Eingabe; repeat if ((pos('/',Eingabe)> pos('*',Eingabe)) or (pos('/',Eingabe)=0)) and (pos('*',Eingabe)<>0) then Multi(Zahl1,Zahl2,Eingabe) else if pos('/',Eingabe)<>0 then Divi(Zahl1,Zahl2,Eingabe); until (pos('*',Eingabe)=0) and (pos('/',Eingabe)=0); repeat if ((pos('-',Eingabe)> pos('+',Eingabe)) or (pos('-',Eingabe)=0)) and (pos('+',Eingabe)<>0) then Add(Zahl1,Zahl2,Eingabe) else if pos('-',Eingabe)<>0 then Sub(Zahl1,Zahl2,Eingabe); until (pos('+',Eingabe)=0) and (pos('-',Eingabe)=0); Eingabe:=Stringreplace(Eingabe,Eingabe2,copy(Eingabe,2,length(Eingabe)-2),[rfreplaceall]); Memo1.Text:=Memo1.Text + Eingabe end;
procedure Tform1.Multi(zahl1,zahl2:integer ;var Term:string); var Ersatz:extended; begin zahl1:= pos('*',Term) ; repeat begin zahl1:= zahl1-1 ; end until (Term[zahl1] = '(') or (Term[zahl1] = '+') or (Term[zahl1] = '-') or (Term[zahl1] = '*') or (Term[zahl1] = '/') or (Term[zahl1] = ''); zahl2:= pos('*',Term) ; repeat begin zahl2:= zahl2+1 ; end until (Term[zahl2] = ')') or (Term[zahl2] = '+') or (Term[zahl2] = '-') or (Term[zahl2] = '*') or (Term[zahl2] = '/') or (Term[zahl2] = ''); Ersatz:=StrToFloat(Copy(Term,Zahl1+1,pos('*',Term)-Zahl1-1))* StrToFloat(Copy(Term,pos('*',Term)+1,Zahl2-pos('*',Term)-1)); Term:=Stringreplace(Term,Copy(Term,Zahl1+1,Zahl2-Zahl1-1),FloatToStr(Ersatz),[rfreplaceall]) end;
procedure Tform1.Add(zahl1,zahl2:integer ;var Term:string); var Ersatz:extended; begin zahl1:= pos('+',Term) ; repeat begin zahl1:=zahl1-1; end until (Term[zahl1] = '(') or (Term[zahl1] = '+') or (Term[zahl1] = '-') or (Term[zahl1] = '*') or (Term[zahl1] = '/') or (Term[zahl1] = ''); zahl2:= pos('+',Term) ; repeat begin zahl2:= zahl2+1 ; end until (Term[zahl2] = ')') or (Term[zahl2] = '+') or (Term[zahl2] = '-') or (Term[zahl2] = '*') or (Term[zahl2] = '/') or (Term[zahl2] = ''); Ersatz:=StrToFloat(Copy(Term,Zahl1+1,pos('+',Term)-Zahl1-1))+ StrToFloat(Copy(Term,pos('+',Term)+1,Zahl2-pos('+',Term)-1)); Term:=Stringreplace(Term,Copy(Term,Zahl1+1,Zahl2-Zahl1-1),FloatToStr(Ersatz),[rfreplaceall]) end;
procedure Tform1.Sub(Zahl1,Zahl2:integer; var Term:string); var Ersatz:extended; begin zahl1:= pos('-',Term) ; repeat begin zahl1:= zahl1-1 ; end until (Term[zahl1] = '(') or (Term[zahl1] = '+') or (Term[zahl1] = '-') or (Term[zahl1] = '*') or (Term[zahl1] = '/') or (Term[zahl1] = ''); zahl2:= pos('-',Term) ; repeat begin zahl2:= zahl2+1 ; end until (Term[zahl2] = ')') or (Term[zahl2] = '+') or (Term[zahl2] = '-') or (Term[zahl2] = '*') or (Term[zahl2] = '/') or (Term[zahl2] = ''); Ersatz:=StrToFloat(Copy(Term,Zahl1+1,pos('-',Term)-Zahl1-1))- StrToFloat(Copy(Term,pos('-',Term)+1,Zahl2-pos('-',Term)-1)); Term:=Stringreplace(Term,Copy(Term,Zahl1+1,Zahl2-Zahl1-1),FloatToStr(Ersatz),[rfreplaceall]) end;
procedure Tform1.Divi(Zahl1,Zahl2:integer; var Term:string); var Ersatz:extended; begin zahl1:= pos('/',Term) ; repeat begin zahl1:= zahl1-1 ; end until (Term[zahl1] = '(') or (Term[zahl1] = '+') or (Term[zahl1] = '-') or (Term[zahl1] = '*') or (Term[zahl1] = '/') or (Term[zahl1] = ''); zahl2:= pos('/',Term) ; repeat begin zahl2:= zahl2+1 ; end until (Term[zahl2] = ')') or (Term[zahl2] = '+') or (Term[zahl2] = '-') or (Term[zahl2] = '*') or (Term[zahl2] = '/') or (Term[zahl2] = ''); Ersatz:=StrToFloat(Copy(Term,Zahl1+1,pos('/',Term)-Zahl1-1))/ StrToFloat(Copy(Term,pos('/',Term)+1,Zahl2-pos('/',Term)-1)); Term:=Stringreplace(Term,Copy(Term,Zahl1+1,Zahl2-Zahl1-1),FloatToStr(Ersatz),[rfreplaceall]) end; end. |