Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Code optimieren (https://www.delphipraxis.net/75392-code-optimieren.html)

Gehstock 18. Aug 2006 18:50


Code optimieren
 
Also ich versuch mir grad das Programieren beizubringen ne Idee wie man das Platzsparender und Bequemer machen kann


Delphi-Quellcode:
 begin
if StrTofloat(Resistor)>25000 then begin
  if StrTofloat(Resistor)<33001 then                 //Korrigiert
pnl_normresistor.Caption := (floattostr(27)+' KOhm')
end;
 end;
 begin
if StrTofloat(Resistor)>22000 then begin
  if StrTofloat(Resistor)<25001 then
pnl_normresistor.Caption := (floattostr(22)+' KOhm')
end;
 end;
 begin
if StrTofloat(Resistor)>16500 then begin
  if StrTofloat(Resistor)<22001 then
pnl_normresistor.Caption := (floattostr(18)+' KOhm')
end;
 end;
 begin
if StrTofloat(Resistor)>13500 then begin
  if StrTofloat(Resistor)<16501 then                   //Korrigiert
pnl_normresistor.Caption := (floattostr(15)+' KOhm')
end;
 end;
 begin
if StrTofloat(Resistor)>12000 then begin
  if StrTofloat(Resistor)<13501 then
pnl_normresistor.Caption := (floattostr(12)+' KOhm')
end;
 end;
 begin
if StrTofloat(Resistor)>10000 then begin
  if StrTofloat(Resistor)<12001 then
pnl_normresistor.Caption := (floattostr(10)+' KOhm')
end;
 end;
 begin
if StrTofloat(Resistor)>8200 then begin
  if StrTofloat(Resistor)<10001 then
pnl_normresistor.Caption := (floattostr(8.2)+' KOhm')
end;
 end;
 begin
if StrTofloat(Resistor)>6800 then begin
  if StrTofloat(Resistor)<8201 then
pnl_normresistor.Caption := (floattostr(6.8)+' KOhm')
end;
 end;
 begin
if StrTofloat(Resistor)>5600 then begin
  if StrTofloat(Resistor)<6801 then
pnl_normresistor.Caption := (floattostr(5.6)+' KOhm')
end;
 end;
 begin
if StrTofloat(Resistor)>4700 then begin
  if StrTofloat(Resistor)<5601 then
pnl_normresistor.Caption := (floattostr(4.7)+' KOhm')
end;
 end;
 begin
if StrTofloat(Resistor)>3900 then begin
  if StrTofloat(Resistor)<4701 then
pnl_normresistor.Caption := (floattostr(3.9)+' KOhm')
end;
 end;
 begin
if StrTofloat(Resistor)>3300 then begin
  if StrTofloat(Resistor)<3901 then
pnl_normresistor.Caption := (floattostr(3.3)+' KOhm')
end;
 end;
 begin
if StrTofloat(Resistor)>2200 then begin
  if StrTofloat(Resistor)<3301 then
pnl_normresistor.Caption := (floattostr(2.2)+' KOhm')
end;
 end;
 begin
if StrTofloat(Resistor)>1625 then begin
  if StrTofloat(Resistor)<2201 then
pnl_normresistor.Caption := (floattostr(1.8)+' KOhm')
end;
 end;
 begin
if StrTofloat(Resistor)>1500 then begin
  if StrTofloat(Resistor)<1626 then
pnl_normresistor.Caption := (floattostr(1.5)+' KOhm')
end;
 end;
 begin
if StrTofloat(Resistor)>1200 then begin
  if StrTofloat(Resistor)<1501 then
pnl_normresistor.Caption := (floattostr(1.2)+' KOhm')
end;
 end;
 begin
if StrTofloat(Resistor)>1000 then begin
  if StrTofloat(Resistor)<1201 then
pnl_normresistor.Caption := (floattostr(1.0)+' KOhm')
end;
 end;
 begin
if StrTofloat(Resistor)>820 then begin
 if StrTofloat(Resistor)<1001 then
pnl_normresistor.Caption := (floattostr(820)+' Ohm')
end;
 end;
 begin
if StrTofloat(Resistor)>680 then begin
 if StrTofloat(Resistor)<821 then
pnl_normresistor.Caption := (floattostr(680)+' Ohm')
end;
 end;
 begin
if StrTofloat(Resistor)>560 then begin
 if StrTofloat(Resistor)<681 then
pnl_normresistor.Caption := (floattostr(560)+' Ohm')
end;
 end;
 begin
if StrTofloat(Resistor)>430.1 then begin
 if StrTofloat(Resistor)<560 then                   //Korrigiert
pnl_normresistor.Caption := (floattostr(470)+' Ohm')
end;
 end;
 begin
if StrTofloat(Resistor)>360 then begin
 if StrTofloat(Resistor)<430.2 then                  //Korrigiert
pnl_normresistor.Caption := (floattostr(390)+' Ohm')
end;
 end;
 begin
if StrTofloat(Resistor)>300 then begin
 if StrTofloat(Resistor)<361 then                       //Korrigiert
pnl_normresistor.Caption := (floattostr(330)+' Ohm')
end;
 end;
 begin
if StrTofloat(Resistor)>270 then begin
 if StrTofloat(Resistor)<301 then
pnl_normresistor.Caption := (floattostr(270)+' Ohm')
end;
 end;
 begin
if StrTofloat(Resistor)>220 then begin
 if StrTofloat(Resistor)<271 then
pnl_normresistor.Caption := (floattostr(220)+' Ohm')
end;
 end;
 begin
if StrTofloat(Resistor)>180 then begin
 if StrTofloat(Resistor)<221 then
pnl_normresistor.Caption := (floattostr(180)+' Ohm')
end;
 end;
  begin
if StrTofloat(Resistor)>150 then begin
 if StrTofloat(Resistor)<181 then
pnl_normresistor.Caption := (floattostr(150)+' Ohm')
end;
 end;
  begin
if StrTofloat(Resistor)>120 then begin
 if StrTofloat(Resistor)<151 then
pnl_normresistor.Caption := (floattostr(120)+' Ohm')
end;
 end;
  begin
if StrTofloat(Resistor)>100 then begin
 if StrTofloat(Resistor)<121 then
pnl_normresistor.Caption := (floattostr(100)+' Ohm')
end;
 end;
  begin
if StrTofloat(Resistor)>82 then begin
 if StrTofloat(Resistor)<101 then
pnl_normresistor.Caption := (floattostr(82)+' Ohm')
end;
 end;
  begin
if StrTofloat(Resistor)>68 then begin
 if StrTofloat(Resistor)<82.1 then
pnl_normresistor.Caption := (floattostr(68)+' Ohm')
end;
 end;
  begin
if StrTofloat(Resistor)>56 then begin
 if StrTofloat(Resistor)<68.1 then
pnl_normresistor.Caption := (floattostr(56)+' Ohm')
end;
 end;
  begin
if StrTofloat(Resistor)>47 then begin
 if StrTofloat(Resistor)<56.1 then
pnl_normresistor.Caption := (floattostr(47)+' Ohm')
end;
 end;
  begin
if StrTofloat(Resistor)>39 then begin
 if StrTofloat(Resistor)<39.1 then
pnl_normresistor.Caption := (floattostr(39)+' Ohm')
end;
 end;
  begin
if StrTofloat(Resistor)>33 then begin
 if StrTofloat(Resistor)<39.1 then
pnl_normresistor.Caption := (floattostr(33)+' Ohm')
end;
 end;
  begin
if StrTofloat(Resistor)>27 then begin
 if StrTofloat(Resistor)<33.1 then
pnl_normresistor.Caption := (floattostr(27)+' Ohm')
end;
 end;
  begin
if StrTofloat(Resistor)>22 then begin
 if StrTofloat(Resistor)<27.1 then
pnl_normresistor.Caption := (floattostr(22)+' Ohm')
end;
 end;
  begin
if StrTofloat(Resistor)>18 then begin
 if StrTofloat(Resistor)<22.1 then
pnl_normresistor.Caption := (floattostr(18)+' Ohm')
end;
 end;
  begin
if StrTofloat(Resistor)>15 then begin
 if StrTofloat(Resistor)<16.5 then
pnl_normresistor.Caption := (floattostr(15)+' Ohm')
end;
 end;
  begin
if StrTofloat(Resistor)>12 then begin
 if StrTofloat(Resistor)<15.1 then
pnl_normresistor.Caption := (floattostr(12)+' Ohm')
end;
 end;
  begin
if StrTofloat(Resistor)>10 then begin
 if StrTofloat(Resistor)<11.1 then
pnl_normresistor.Caption := (floattostr(10)+' Ohm')
end;
 end;
  begin
if StrTofloat(Resistor)>8.2 then begin
 if StrTofloat(Resistor)<10.1 then
pnl_normresistor.Caption := (floattostr(8.2)+' Ohm')
end;
 end;
  begin
if StrTofloat(Resistor)>6.8 then begin
 if StrTofloat(Resistor)<8.21 then
pnl_normresistor.Caption := (floattostr(6.8)+' Ohm')
end;
 end;
  begin
if StrTofloat(Resistor)>5.6 then begin
 if StrTofloat(Resistor)<6.81 then
pnl_normresistor.Caption := (floattostr(5.6)+' Ohm')
end;
 end;
  begin
if StrTofloat(Resistor)>4.7 then begin
 if StrTofloat(Resistor)<5.61 then
pnl_normresistor.Caption := (floattostr(4.7)+' Ohm')
end;
 end;
  begin
if StrTofloat(Resistor)>3.9 then begin
 if StrTofloat(Resistor)<4.71 then
pnl_normresistor.Caption := (floattostr(3.9)+' Ohm')
end;
 end;
  begin
if StrTofloat(Resistor)>3.3 then begin
 if StrTofloat(Resistor)<3.91 then
pnl_normresistor.Caption := (floattostr(3.3)+' Ohm')
end;
 end;
  begin
if StrTofloat(Resistor)>2.7 then begin
 if StrTofloat(Resistor)<3.31 then
pnl_normresistor.Caption := (floattostr(2.7)+' Ohm')
end;
 end;
  begin
if StrTofloat(Resistor)>2.2 then begin
 if StrTofloat(Resistor)<2.71 then
pnl_normresistor.Caption := (floattostr(2.2)+' Ohm')
end;
 end;
  begin
if StrTofloat(Resistor)>1.8 then begin
 if StrTofloat(Resistor)<2.21 then
pnl_normresistor.Caption := (floattostr(1.8)+' Ohm')
end;
 end;
  begin
if StrTofloat(Resistor)>1.5 then begin
 if StrTofloat(Resistor)<1.81 then
pnl_normresistor.Caption := (floattostr(1.5)+' Ohm')
end;
 end;
  begin
if StrTofloat(Resistor)>1.2 then begin
 if StrTofloat(Resistor)<1.51 then
pnl_normresistor.Caption := (floattostr(1.2)+' Ohm')
end;
 end;
  begin
if StrTofloat(Resistor)>0.1 then begin
 if StrTofloat(Resistor)<1.21 then
pnl_normresistor.Caption := (floattostr(1)+' Ohm')
end;
 end;
end;
Sorry für den langen text aber das ist ja mein Problem nur die Stellen mit
Zitat:

//Korrigiert
sind schon fertig

fwsp 18. Aug 2006 18:53

Re: Code optimieren
 
guck dir mal
Delphi-Quellcode:
case
an.

3_of_8 18. Aug 2006 18:57

Re: Code optimieren
 
1. bei der Prüfung darfst du nicht auf < prüfen, sondern auf <= in dem Fall, denn du hast Kommazahlen.
2. Mithilfe von case und arithmetischen Operationen dürfte sich das extremst vereinfachen dürfen.

_frank_ 18. Aug 2006 18:59

Re: Code optimieren
 
das leidige thema...warum wandelst du x-mal resitor von string in float, zumal resistor scheinbar ein integer sein könnte.
und konstanten so umzuwandeln ist auch nicht nötig.
alles weitere würde eine case anweisung machen...
Delphi-Quellcode:
resistor:integer;

case resistor of
  25000..33001:pnl_normresistor.Caption := '27 KOhm';
  ...
end;
HTH Frank

Khabarakh 18. Aug 2006 19:17

Re: Code optimieren
 
@everybody vor mir: so langsam sollte doch bekannt sein, dass case nur bei ordinalen Typen funktioniert... -.-

Delphi-Quellcode:
function RoundÖhm(aValue : Double) : Double;
const
  RoundValues : Array[0..2, 0..1] of Double = ((33000, 27000), (25000, 22000),
        (22000, 18000)); // jeweils Maximum des Bereichs und Wert, auf den
        // gerundet werden soll
        // hab mich mal auf drei Angaben beschränkt
var
  i: Integer;
begin
  if (aValue > RoundValues[0, 0]) then
    raise Exception.Create('Lebensgefährlich!')
  else
  begin
    for i := 1 to Length(RoundValues) - 1 do
      if (aValue > RoundValues[i, 0]) then
      begin
        Result := RoundValues[i - 1, 1];
        Exit;
      end;
    Result := RoundValues[High(RoundValues), 1];
  end;
end;

function ÖhmToStr(aValue : Double) : string;
begin
  if (aValue > 1000) then
    Result := Format('%f KOhm', [aValue / 1000])
  else
    Result := Format('%f Ohm', [aValue])
end;

Daniel G 18. Aug 2006 19:23

Re: Code optimieren
 
Öhm... Du weißt aber schon, dass sich Umlaute in Funktionsnamen nicht empfehlen, oder?

Meflin 18. Aug 2006 19:25

Re: Code optimieren
 
Zitat:

Zitat von Daniel G
Öhm... Du weißt aber schon, dass sich Umlaute in Funktionsnamen nicht empfehlen, oder?

In Delphi 2006 alles kein Problem :roll:


Daniel G 18. Aug 2006 19:27

Re: Code optimieren
 
Zitat:

Zitat von Meflin
In Delphi 2006 alles kein Problem :roll:

Erklär das mal nem Asiaten, der sich ob der komischen Zeichen wundert, sollte der Threadersteller das Programm OS machen... :roll:

Khabarakh 18. Aug 2006 19:29

Re: Code optimieren
 
Zitat:

Zitat von Daniel G
Öhm... Du weißt aber schon, dass sich Umlaute in Funktionsnamen nicht empfehlen, oder?

Irrelevant, weil ebenso sinnlose Bezeichner nicht empfehlenswert sind und schnellstens in sinnvollere umbenannt werden sollten, womit sich ersteres Problem in Luft aufgelöst hat ;) .

Meflin 18. Aug 2006 19:33

Re: Code optimieren
 
Zitat:

Zitat von Daniel G
Erklär das mal nem Asiaten, der sich ob der komischen Zeichen wundert, sollte der Threadersteller das Programm OS machen... :roll:

Und weil du scheinbar die Ironie in meinem und das nicht-ernstgemeinte in Khabarakhs Posting nicht bemerkt hast zeigst du mir jetzt noch den Asiaten der den code hier benutzt :roll:


Gehstock 18. Aug 2006 19:34

Re: Code optimieren
 
Zitat:

resistor:integer;
Da bekomm ich aber an anderer stelle Probleme

Delphi-Quellcode:
Resistor := floattostr(((strtofloat(edit_UV.text)-          //widerstand
strtofloat(edit_UD.text))/strtofloat(edit_ID.text))*1000);           +++++++ Nämlich hier

if StrTofloat(Resistor)>=1000000 then begin                                 //mohm
pnl_calculated_resistor.caption := FloatToStr(Runden((strtofloat(resistor)/1000000),2)) +' MOhm';
end;
if StrTofloat(Resistor)<1000000 then begin
if StrTofloat(Resistor)>=1000 then begin                                 //kohm
pnl_calculated_resistor.caption := FloatToStr(Runden((strtofloat(resistor)/1000),2)) +' KOhm';
end;
end;
if StrTofloat(Resistor)<1000 then begin                                //ohm
pnl_calculated_resistor.caption := FloatToStr(Runden((strtofloat(resistor)/1),2))+' Ohm';
end;


aber sonst könnte das klappen und gefällt mir ganz gut

_frank_ 18. Aug 2006 19:40

Re: Code optimieren
 
aso...du berechnest den Widerstand...
na dann nehm als Typ extended.
resistor:extended;
und
resistor:=(strtofloat(edit_UV.text)-strtofloat(edit_UD.text))/strtofloat(edit_ID.text)*1000;

ich hoffe, die klammern passen noch...

Gruß Frank

Gehstock 18. Aug 2006 19:44

Re: Code optimieren
 
och nö

Delphi-Quellcode:
begin
case resistor of  **********ordinal type required
 25000..33001:pnl_normresistor.Caption := '27 KOhm';
 16500..22001:pnl_normresistor.Caption := '22 KOhm';

_frank_ 18. Aug 2006 19:48

Re: Code optimieren
 
jo, case geht nicht mit float
evtl. reicht ein
Delphi-Quellcode:
case trunc(resistor) of
HTH Frank

Gehstock 18. Aug 2006 19:54

Re: Code optimieren
 
Ja Danke Reicht

Nächstes Problem :


Delphi-Quellcode:
if (Resistor)>=1000000 then begin                                 //mohm        *******Hab ich schon abgeändert
pnl_calculated_resistor.caption := FloatToStr(Runden(strtofloat(resistor)/1000000),2) +' MOhm';   //*****hier is bestimmt wieder ne klammer falsch
end;
if (Resistor)<1000000 then begin
if (Resistor)>=1000 then begin                                 //kohm
pnl_calculated_resistor.caption := FloatToStr(Runden((strtofloat(resistor)/1000),2)) +' KOhm';
end;
end;
if (Resistor)<1000 then begin                                //ohm
pnl_calculated_resistor.caption := FloatToStr(Runden((strtofloat(resistor)/1),2))+' Ohm';
end;
erzeugt ein
Zitat:

There is no overloaded version of 'StrToFloat' that can be called with these arguments
eh ich das mit den Formeln habe dauerts bestimmt jahre

_frank_ 18. Aug 2006 19:56

Re: Code optimieren
 
du machst es schon wieder... (oder immer noch?)

strtofloat(resistor)


mal davon abgesehen, dass du auch dort case verwenden kannst wie oben

Frank

Gehstock 18. Aug 2006 20:03

Re: Code optimieren
 
Delphi-Quellcode:
case trunc(resistor) of


1000000:pnl_calculated_resistor.caption := FloatToStr(Runden(strtofloat(resistor)/1000000),2) +' MOhm';
999999..1000:pnl_calculated_resistor.caption := FloatToStr(Runden((strtofloat(resistor)/1000),2)) +' KOhm';
9999:pnl_calculated_resistor.caption := FloatToStr(Runden((strtofloat(resistor)/1),2))+' Ohm';
end;
So?
Nerv ich eigendlich?

_frank_ 18. Aug 2006 20:14

Re: Code optimieren
 
Junge...liest du auch mal was wir schreiben?

Delphi-Quellcode:
case trunc(resistor) of
  0..999:pnl_calculated_resistor.caption := FloatToStr(Runden(resistor,2))+' Ohm';
  1000..999999:pnl_calculated_resistor.caption := FloatToStr(Runden((resistor/1000),2)) +' KOhm';
  else :pnl_calculated_resistor.caption := FloatToStr(Runden(resistor/1000000),2) +' MOhm';
end;
(evtl. nach dem else kein ':')

das ständige umwandeln von/nach string...

omata 18. Aug 2006 20:15

Re: Code optimieren
 
wegen Nichtbeachtung gelöscht

Khabarakh 18. Aug 2006 20:16

Re: Code optimieren
 
Zitat:

Zitat von _frank_
evtl. reicht ein
Delphi-Quellcode:
case trunc(resistor) of

Schonmal das Ende der Liste angeschaut? Denn dort wird es mit case NICHT funktionieren.

Gehstock 18. Aug 2006 20:27

Re: Code optimieren
 
Liste der Anhänge anzeigen (Anzahl: 1)
doch tu ich aber du muss auch wissen ich bin anfänger und verstehe nicht alles

habs jetzt trotzdem so gemacht wel er beim ersten aufrug grundsätzlich falsch gerechnet hat


Delphi-Quellcode:
if (Resistor)>=1000000 then begin                                 //mohm
pnl_calculated_resistor.caption := FloatToStr(Runden((resistor/1000000),2)) +' MOhm';
end;
if (Resistor)<1000000 then begin
if (Resistor)>=1000 then begin                                 //kohm
pnl_calculated_resistor.caption := FloatToStr(Runden((resistor/1000),2)) +' KOhm';
end;
end;
if (Resistor)<1000 then begin                                //ohm
pnl_calculated_resistor.caption := FloatToStr(Runden((resistor/1),2))+' Ohm';
end;
aber dank dir hab wenigstens das jetzt richtig

hab das programm mal angehängt so das ihr euch das vorstellen könnt (ist ein Nachbau zum Lernen)

--wie gesagt toleranz muss ich noch tun
--die comboboxen (ring 1-5) muss ich noch leeren beim umschalten (4.ringe 5ringe) die strings müssen aber drin bleiben also clear klappt nicht
--und jetzt ersmal wieder die normwiderstände
--und bei der Leistung rechnet er auch wieder falsch hat aber schonmal geklappt
--
Zitat:

Schonmal das Ende der Liste angeschaut? Denn dort wird es mit case NICHT funktionieren.
wegen der kommastellen?

Khabarakh 18. Aug 2006 20:55

Re: Code optimieren
 
Zitat:

Zitat von Gehstock
Zitat:

Schonmal das Ende der Liste angeschaut? Denn dort wird es mit case NICHT funktionieren.
wegen der kommastellen?

Jupp.
Allerdings hast du jetzt ja schon zwei alternative, um Einiges kürzere und sogar schon völlig ausformulierte Lösungen bekommen, weshalb du auf case vollkommen verzichten kannst.
Für die Einheiten kannst du hier schauen: http://www.delphipraxis.net/internal...=462508#462508

_frank_ 18. Aug 2006 20:58

Re: Code optimieren
 
Zitat:

Zitat von Khabarakh
Schonmal das Ende der Liste angeschaut? Denn dort wird es mit case NICHT funktionieren.

ehrlich gesagt, nein ;) war mir bisschen zu viel unformatierter code...

aber auch das lässt sich mit case machen... man muss nur eine weitere case-anweisung machen ;)
Delphi-Quellcode:
case trunc(resistor) of
...
  //hier nur bis minimum 10 gehen, weil bei kleiner nachkommastellen interessant sind
else
  case trunc(resistor*10) of
    1..12://ist (0.1)..(1.2)
  end;
end;
@gehstock: ich hab damit kein Problem, dass du Anfänger bist.
Aber wir haben schon mehrfach erwähnt, dass du passende Datentypen verwenden sollst.
Um das ständige von/nach string zu vermeiden, was sinnlose rechenleistung kostet und den code unübersichtlich macht.

Gruß Frank


Alle Zeitangaben in WEZ +1. Es ist jetzt 23:29 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz