Autor Beitrag
CrOc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 48

Win XP
D5 Standard
BeitragVerfasst: Mi 03.12.03 15:05 
hi,
ich hab ein label erstellt, indem der Text '4*4' steht. Nun will ich aber, dass wenn ich auf den Button klick im Edit Fenster das ergebnis steht. Hab es schon mit StrToInt versucht, aber dann bringt der immer so ein umwandlungsproblem.
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Mi 03.12.03 15:12 
Hallo!

Für so etwas brauchst Du einen Suche in: Delphi-Forum, Delphi-Library PARSER.

StrToInt bzw. StrToFloat wandeln nur wirkliche Zahlen aber keine mathematischen Ausdrücke um.

MfG
Peter

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
CrOc Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 48

Win XP
D5 Standard
BeitragVerfasst: Mi 03.12.03 15:16 
und wie soll das dann im meinen Fall funktionieren?
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Mi 03.12.03 15:36 
Habe ich doch geschrieben: ein Parser muss her.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
ShadowThief
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 278



BeitragVerfasst: Mi 03.12.03 15:46 
du musst ein programm schreiben, das checkt, dass 4*4 bedeutet,
dass 4 mit 4 multipliziert werden muss.

shadow.

_________________
"soylent grün ist menschenfleisch!"
obbschtkuche
Gast
Erhaltene Danke: 1



BeitragVerfasst: Mi 03.12.03 17:23 
Wenn du das Rekursiv (bäh ;) ) machst, geht das recht schnell.
Den Eingabestring zuerst mit trim bearbeiten (und eventuelle Klammern an Anfang und Ende beseitigen), dann nacheinander auf '+', '-', '*' und '/' überprüfen (heißt es nicht so schön Punkt-Nach-Strich ;)) Da musst du dir eine fkt schreiben, die das nicht in Klammern findet, falls es die beachten soll.
Dann an der Fundstelle Abtrennen und die beiden einzelstrings wieder an die Fkt übergeben...
Udontknow
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2596

Win7
D2006 WIN32, .NET (C#)
BeitragVerfasst: Mi 03.12.03 17:35 
Was ja dann eine Art Parser wäre, aber ich glaube nicht, daß Croc (will dir nicht zu nahe treten) das mit seinem Wissensstand hinbekommt...

Cu, :)
Udontknow
matze
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 4613
Erhaltene Danke: 24

XP home, prof
Delphi 2009 Prof,
BeitragVerfasst: Mi 03.12.03 17:59 
da gäbe es ja fertige parser

_________________
In the beginning was the word.
And the word was content-type: text/plain.
obbschtkuche
Gast
Erhaltene Danke: 1



BeitragVerfasst: Mi 03.12.03 18:02 
oder hier den: Hat zwar keine Fkten und Variablen usw, aber sollte fürs erste reichen

ausblenden volle Höhe Delphi-Quelltext
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:
function parse(s: string): extended;

 function posx(c: char; s: string): integer;
 var
  p: pchar;
  kc: integer;
 begin
  result := -1;
  kc := 0;
  p := pchar(s);
  while p^ <> #0 do
  begin
   case p^ of
    '(': inc(kc);
    ')': dec(kc);
   end;
   if kc < 0 then exit;
   if (kc = 0) and (p^ = c) then
   begin
    result := length(s) - length(p) + 1;
    exit;
   end;
   inc(p);
  end;
 end;

 function rembrackets(s: string): string;
 begin
  while (s <> '') and (s[1] = '(') and (s[length(s)] = ')') do
   s := copy(s, 2, length(s)-2);
  result := s;
 end;
 
 function Anfang(s: string; bis: integer): string;
 begin
  result := copy(s, 1, bis-1);
 end;

 function ende(s: string; von: integer): string;
 begin
  result := copy(s, von, length(s));
 end; 

var
 ppl, pmi, pdu, pma: integer;
begin
 s := trim(rembrackets(trim(s)));
 if TryStrToFloat(s, result) then exit;
 ppl := posx('+', s);
 pmi := posx('-', s);
 pdu := posx('/', s);
 pma := posx('*', s);
 if ppl > 0 then result := parse(anfang(s, ppl)) + parse(ende(s, ppl+1))
 else if pmi > 0 then result := parse(anfang(s, pmi)) - parse(ende(s, pmi+1))
 else if pdu > 0 then result := parse(anfang(s, pdu)) / parse(ende(s, pdu+1))
 else if pma > 0 then result := parse(anfang(s, pma)) * parse(ende(s, pma+1))
 else raise Exception.Create('Fehler im Term')
end;


Wenn dus iterativ haben willst, wirds aber schon mehr arbeit...

[EDIT]Achtung: Mir ist gerade aufgefallen dass das Teil nicht mit Klammern zurechtkommt.

[EDIT2] Jetzt schon ;)