Autor Beitrag
Jerk
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 251

Vista Ultimate, Ubuntu
Turbo Delphi 2006
BeitragVerfasst: Fr 08.08.08 15:15 
Vorweg, mir ist leider kein besserer Titel eingefallen.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
  const
    Operators : Array[0..6of String = ('+','*','-','/','(',')','^');
...
  brO := 0;
  brC := 0;
...
  for j := 1 to Length(sLine) do
  begin 
   Op := False;
   for i := 0 to 6 do
    if sLine[j] = Operators[i] then
    begin
     Op := true;
     case i of
      4: inc(brO,1);
      5: inc(brC,1);
     end;
    end;
    If  Op = true then
   ...
   end;


Dieser Schnippsel soll einen String auf Operatoren untersuchen, wenn einer Gefunden wird, soll der Wert OP auf True gesetzt werden. Ist der Operator eine Klammer, dann soll entweder brO oder brC um eins erhöht werden.


Aus irgendeinem Grund, wenn keine Klammer in sLine vorhanden ist, wird brO um 1 erhöht. Wieso? Liegt es am Case ?


Wenn ich z.b. '1+1' als sLine übergebe wird brO auf 1 gesetzt.
Boldar
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1555
Erhaltene Danke: 70

Win7 Enterprise 64bit, Win XP SP2
Turbo Delphi
BeitragVerfasst: Fr 08.08.08 16:52 
mache es so:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
var
    Operators : set of char;
...
  brO := 0;
  brC := 0;
...
operators := ['+','*','-','/','(',')','^'];
  for j := 1 to Length(sLine) do
  begin 
   Op := False;
    if sLine[j] in operators then
    begin
     Op := true;
     case sline[j] of
      '(': inc(brO);
      ')': inc(brC);
     end;
    end;
    If  Op = true then
   ...
   end;
Jerk Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 251

Vista Ultimate, Ubuntu
Turbo Delphi 2006
BeitragVerfasst: Fr 08.08.08 21:26 
Hmm durch das verwenden von set of kann ich ne ganze Menge an sich schon Sparen. Vielen Dank dafür!
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: Fr 08.08.08 21:37 
Noch hübscher :-P :

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
  for j := 1 to Length(sLine) do  
  begin   
    Op := True;  
    case sLine[j] of  
      '(': inc(brO);  
      ')': inc(brC);
    else
      Op := False;  
    end;  

    if Op then
      // ...  
  end;


Dann brauchste nicht mal ein Set. Achja, "= True" wird nicht gern gesehen. Leider hat user profile iconLuckie den Artikel dazu nicht online (oder ich find ihn nicht).

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)
Jerk Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 251

Vista Ultimate, Ubuntu
Turbo Delphi 2006
BeitragVerfasst: Fr 08.08.08 21:41 
Die Variable Op brauche ich garnicht mehr, ich habe es jetzt so gemacht:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
const
     Operators : set of Char = ['+','*','-','/','(',')','^'];
...
if sLine[Step] in Operators then
  begin
   if sLine[Step]=')' then inc(brC,1);
   if sLine[Step]='(' then inc(brO,1);
  ...
  end


Mit dem gleich true meinst du sicherlich sowas oder?

ausblenden Delphi-Quelltext
1:
 if Op then....					
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: Fr 08.08.08 21:44 
Ja, es kann passieren, dass - obwohl Op scheinbar True ist - die If-Anweisung fehlschlägt, weil True nicht wie von den Entwicklern vorgesehen -1 ist. Wenn du aber nur if Op then schreibst, dann kann nichts passieren :)

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)
Jerk Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 251

Vista Ultimate, Ubuntu
Turbo Delphi 2006
BeitragVerfasst: Fr 08.08.08 21:50 
Ja ich dachte jetzt nur rein an die Logik dabei weil es ja irgendwie Doppeltgemoppelt ist.

Vielen Dank aufjedenfall dafür.