Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Probleme mit prozentrechnung (https://www.delphipraxis.net/74477-probleme-mit-prozentrechnung.html)

Gehstock 3. Aug 2006 19:12


Probleme mit prozentrechnung
 
Delphi-Quellcode:
tolerance := pnl_result.caption/100*(strtofloat(prozent));
tolerance is ne variable
prozent is ne variable
pnl_result.caption ist das ergebnis einer anderen rechnung


muss mich echt mal damit beschäftigen was genau strtofloat und floattostr

DGL-luke 3. Aug 2006 19:16

Re: Probleme mit prozentrechnung
 
du musst in Codezeile 532 die zahl 12 gegen die zahl 42 austauschen.

Das war deine Frage, oder?

_frank_ 3. Aug 2006 19:16

Re: Probleme mit prozentrechnung
 
muss es nicht eigentlich

Delphi-Quellcode:
tolerance := strtofloat(pnl_result.caption)/100*prozent;
heisen, worausgesetzt prozent ist eine Zahl (integer, real, ...)?
Weil caption ist ja ein string, welcher erst in eine Fließkommazahl umgewandelt werden muss.

War doch die frage, oder?

Gruß Frank

mkinzler 3. Aug 2006 19:18

Re: Probleme mit prozentrechnung
 
Zitat:

muss mich echt mal damit beschäftigen was genau strtofloat und floattostr
Ein kleiner Tipp, wenn du mal keien Ahnung hast, was eine Funktion macht, schau einfach mal in die Delphi-Hilfe ;-)
Aber in diesem Fall sagt die Namen der Funktionen doch aus, was sie machen
StrToFloat = String To Float = Von String Nach Gleitkommazahl wandeln.

Die andere Funktion verat ich die nicht :-D

Gehstock 3. Aug 2006 19:25

Re: Probleme mit prozentrechnung
 
Hab ja schon viel geschafft auch wenn der code mit sicherheit noch besser zu lösen ist


Delphi-Quellcode:
procedure TForm2.btn_executeClick(Sender: TObject);
var
wert : string;
wiederstand : string;
tolerance: string;
prozent: string;

begin
if rb_4rings.Checked then begin
wert := floattostr(((cb_ring1.ItemIndex+1)*10) + cb_ring2.ItemIndex);
if cb_ring3.ItemIndex = 0 then wiederstand := floattostr((strtofloat(wert)/100));
if cb_ring3.ItemIndex = 1 then wiederstand := floattostr((strtofloat(wert)/10));
if cb_ring3.ItemIndex = 2 then wiederstand := floattostr((strtofloat(wert)*1));
if cb_ring3.ItemIndex = 3 then wiederstand := floattostr((strtofloat(wert)*10));
if cb_ring3.ItemIndex = 4 then wiederstand := floattostr((strtofloat(wert)*100));
if cb_ring3.ItemIndex = 5 then wiederstand := floattostr((strtofloat(wert)*1000));
if cb_ring3.ItemIndex = 6 then wiederstand := floattostr((strtofloat(wert)*10000));
if cb_ring3.ItemIndex = 7 then wiederstand := floattostr((strtofloat(wert)*100000));
if cb_ring3.ItemIndex = 8 then wiederstand := floattostr((strtofloat(wert)*1000000));
if cb_ring3.ItemIndex = 9 then wiederstand := floattostr((strtofloat(wert)*10000000));
end;

if rb_5rings.Checked then begin
wert := floattostr(((cb_ring1.ItemIndex+1)*100) + ((cb_ring2.ItemIndex)*10) + (cb_ring53.ItemIndex));
if cb_ring54.ItemIndex = 0 then wiederstand := floattostr((strtofloat(wert)/100));
if cb_ring54.ItemIndex = 1 then wiederstand := floattostr((strtofloat(wert)/10));
if cb_ring54.ItemIndex = 2 then wiederstand := floattostr((strtofloat(wert)*1));
if cb_ring54.ItemIndex = 3 then wiederstand := floattostr((strtofloat(wert)*10));
if cb_ring54.ItemIndex = 4 then wiederstand := floattostr((strtofloat(wert)*100));
if cb_ring54.ItemIndex = 5 then wiederstand := floattostr((strtofloat(wert)*1000));
if cb_ring54.ItemIndex = 6 then wiederstand := floattostr((strtofloat(wert)*10000));
if cb_ring54.ItemIndex = 7 then wiederstand := floattostr((strtofloat(wert)*100000));
if cb_ring54.ItemIndex = 8 then wiederstand := floattostr((strtofloat(wert)*1000000));
if cb_ring54.ItemIndex = 9 then wiederstand := floattostr((strtofloat(wert)*10000000));
end;
begin
if StrTofloat(wiederstand)>999000 then begin
pnl_result.caption := (floattostr((strtofloat(wiederstand)/
1000000))+' MOhm');
pnl_worstcasemin.caption := (floattostr((strtofloat(wiederstand)/  //soll später die minimale toleranz werden
1))+' Ohm');
pnl_worstcasemax.caption := (floattostr((strtofloat(wiederstand)/   //soll später die maximale toleranz werden
1))+' Ohm');
end;
if StrTofloat(wiederstand)>999  then begin
if  StrTofloat(wiederstand)<1000000  then begin
pnl_result.caption := (floattostr((strtofloat(wiederstand)/
1000))+' KOhm');
pnl_worstcasemin.caption := (floattostr((strtofloat(wiederstand)/  //soll später die minimale toleranz werden
1))+' Ohm');
pnl_worstcasemax.caption := (floattostr((strtofloat(wiederstand)/  //soll später die maximale toleranz werden
1))+' Ohm');
end;
end;
if StrTofloat(wiederstand)<1000 then begin
pnl_result.caption := (floattostr((strtofloat(wiederstand)/
1))+' Ohm');
pnl_worstcasemin.caption := (floattostr((strtofloat(wiederstand)/  //soll später die minimale toleranz werden
1))+' Ohm');
pnl_worstcasemax.caption := (floattostr((strtofloat(wiederstand)/  //soll später die maximale toleranz werden
1))+' Ohm');

end;
tolerance := strtofloat(pnl_result.caption)/100*(prozent); // hier die rechnung



if rb_4rings.Checked then begin
if cb_ring4.ItemIndex = 0 then begin
pnl_Tolerance.Caption:= '10%';
prozent:=floattostr(10);       //   muss bestmmt alles strtofloat
end;
if cb_ring4.ItemIndex = 1 then begin
pnl_Tolerance.Caption:= '5%';
prozent:=floattostr(5);
end;
if cb_ring4.ItemIndex = 2 then begin
pnl_Tolerance.Caption:= '1%';
prozent:=floattostr(1);
end;
if cb_ring4.ItemIndex = 3 then begin
pnl_Tolerance.Caption:= '2%';
prozent:=floattostr(2);
if cb_ring4.ItemIndex = 4 then begin
pnl_Tolerance.Caption:= '0,5%';
prozent:=floattostr(0.5);
end;
if cb_ring4.ItemIndex = 5 then begin
pnl_Tolerance.Caption:= '0,25%';
prozent:=floattostr(0.25);
end;
if cb_ring4.ItemIndex = 6 then begin
pnl_Tolerance.Caption:= '0,1%';
prozent:=floattostr(0.1);
end;
end;
if rb_5rings.Checked then begin
if cb_ring55.ItemIndex = 0 then begin
pnl_Tolerance.Caption:= '5%';
prozent:=floattostr(5);
end;
if cb_ring55.ItemIndex = 1 then begin
pnl_Tolerance.Caption:= '1%';
prozent:=floattostr(1);
end;
if cb_ring55.ItemIndex = 2 then begin
pnl_Tolerance.Caption:= '2%';
prozent:=floattostr(2);
end;
if cb_ring55.ItemIndex = 3 then begin
pnl_Tolerance.Caption:= '0,5%';
prozent:=floattostr(0.5);
end;
if cb_ring55.ItemIndex = 4 then begin
pnl_Tolerance.Caption:= '0,25%';
prozent:=floattostr(0.25);
end;
if cb_ring55.ItemIndex = 5 then begin
pnl_Tolerance.Caption:= '0,1%';
prozent:=floattostr(0.1);
end;
end;
end;
end;
end;
nich meckern es soll nur laufen bin ja noch am lernen

mkinzler 3. Aug 2006 19:27

Re: Probleme mit prozentrechnung
 
Es wäre einfacher, wenn du akurate Typen verwenden würdest, dann würde auch das ständige Casten entfallen.

Gehstock 3. Aug 2006 19:59

Re: Probleme mit prozentrechnung
 
Delphi-Quellcode:
procedure TForm2.btn_executeClick(Sender: TObject);
var
wert : string;
wiederstand : string;
//tolerance: string;
prozent: string;

begin
if rb_4rings.Checked then begin
wert := floattostr(((cb_ring1.ItemIndex+1)*10) + cb_ring2.ItemIndex);
if cb_ring3.ItemIndex = 0 then wiederstand := floattostr((strtofloat(wert)/100));
if cb_ring3.ItemIndex = 1 then wiederstand := floattostr((strtofloat(wert)/10));
if cb_ring3.ItemIndex = 2 then wiederstand := floattostr((strtofloat(wert)*1));
if cb_ring3.ItemIndex = 3 then wiederstand := floattostr((strtofloat(wert)*10));
if cb_ring3.ItemIndex = 4 then wiederstand := floattostr((strtofloat(wert)*100));
if cb_ring3.ItemIndex = 5 then wiederstand := floattostr((strtofloat(wert)*1000));
if cb_ring3.ItemIndex = 6 then wiederstand := floattostr((strtofloat(wert)*10000));
if cb_ring3.ItemIndex = 7 then wiederstand := floattostr((strtofloat(wert)*100000));
if cb_ring3.ItemIndex = 8 then wiederstand := floattostr((strtofloat(wert)*1000000));
if cb_ring3.ItemIndex = 9 then wiederstand := floattostr((strtofloat(wert)*10000000));
end;

if rb_5rings.Checked then begin
wert := floattostr(((cb_ring1.ItemIndex+1)*100) + ((cb_ring2.ItemIndex)*10) + (cb_ring53.ItemIndex));
if cb_ring54.ItemIndex = 0 then wiederstand := floattostr((strtofloat(wert)/100));
if cb_ring54.ItemIndex = 1 then wiederstand := floattostr((strtofloat(wert)/10));
if cb_ring54.ItemIndex = 2 then wiederstand := floattostr((strtofloat(wert)*1));
if cb_ring54.ItemIndex = 3 then wiederstand := floattostr((strtofloat(wert)*10));
if cb_ring54.ItemIndex = 4 then wiederstand := floattostr((strtofloat(wert)*100));
if cb_ring54.ItemIndex = 5 then wiederstand := floattostr((strtofloat(wert)*1000));
if cb_ring54.ItemIndex = 6 then wiederstand := floattostr((strtofloat(wert)*10000));
if cb_ring54.ItemIndex = 7 then wiederstand := floattostr((strtofloat(wert)*100000));
if cb_ring54.ItemIndex = 8 then wiederstand := floattostr((strtofloat(wert)*1000000));
if cb_ring54.ItemIndex = 9 then wiederstand := floattostr((strtofloat(wert)*10000000));
end;
begin
if StrTofloat(wiederstand)>999000 then begin
pnl_result.caption := (floattostr((strtofloat(wiederstand)/
1000000))+' MOhm');
{pnl_worstcasemin.caption := (floattostr(((strtofloat(wiederstand)-tolerance)/
1000000))+' MOhm');
pnl_worstcasemax.caption := (floattostr(((strtofloat(wiederstand)+tolerance)/
1000000))+' MOhm');  }
end;
if StrTofloat(wiederstand)>999  then begin
if  StrTofloat(wiederstand)<1000000  then begin
pnl_result.caption := (floattostr((strtofloat(wiederstand)/
1000))+' KOhm');
{pnl_worstcasemin.caption := (floattostr(((strtofloat(wiederstand)-tolerance)/
1000))+' KOhm');
pnl_worstcasemax.caption := (floattostr(((strtofloat(wiederstand)+tolerance)/
1000))+' KOhm');    }
end;
end;
if StrTofloat(wiederstand)<1000 then begin
pnl_result.caption := (floattostr((strtofloat(wiederstand)/
1))+' Ohm');
{pnl_worstcasemin.caption := (floattostr(((strtofloat(wiederstand)-tolerance)/
1))+' Ohm');
pnl_worstcasemax.caption := (floattostr(((strtofloat(wiederstand)+tolerance)/
1))+' Ohm');       }
end;
//tolerance := strtofloat(pnl_result.caption)/100*(prozent);



if rb_4rings.Checked then begin
if cb_ring4.ItemIndex = 0 then begin
pnl_Tolerance.Caption:= '10%';
prozent:=floattostr(10);
end;
if cb_ring4.ItemIndex = 1 then begin
pnl_Tolerance.Caption:= '5%';
prozent:=floattostr(5);
end;
if cb_ring4.ItemIndex = 2 then begin
pnl_Tolerance.Caption:= '1%';
prozent:=floattostr(1);
end;
if cb_ring4.ItemIndex = 3 then begin
pnl_Tolerance.Caption:= '2%';
prozent:=floattostr(2);
if cb_ring4.ItemIndex = 4 then begin
pnl_Tolerance.Caption:= '0,5%';
prozent:=floattostr(0.5);
end;
if cb_ring4.ItemIndex = 5 then begin
pnl_Tolerance.Caption:= '0,25%';
prozent:=floattostr(0.25);
end;
if cb_ring4.ItemIndex = 6 then begin
pnl_Tolerance.Caption:= '0,1%';
prozent:=floattostr(0.1);
end;
end;
if rb_5rings.Checked then begin
if cb_ring55.ItemIndex = 0 then begin
pnl_Tolerance.Caption:= '5%';
prozent:=floattostr(5);
end;
if cb_ring55.ItemIndex = 1 then begin
pnl_Tolerance.Caption:= '1%';
prozent:=floattostr(1);
end;
if cb_ring55.ItemIndex = 2 then begin
pnl_Tolerance.Caption:= '2%';
prozent:=floattostr(2);
end;
if cb_ring55.ItemIndex = 3 then begin
pnl_Tolerance.Caption:= '0,5%';
prozent:=floattostr(0.5);
end;
if cb_ring55.ItemIndex = 4 then begin
pnl_Tolerance.Caption:= '0,25%';
prozent:=floattostr(0.25);
end;
if cb_ring55.ItemIndex = 5 then begin
pnl_Tolerance.Caption:= '0,1%';
prozent:=floattostr(0.1);
end;
end;
end;
end;
end;


kannst du mir bitte trotzdem helfen

mkinzler 3. Aug 2006 20:10

Re: Probleme mit prozentrechnung
 
Delphi-Quellcode:
procedure TForm2.btn_executeClick(Sender: TObject);
var
wert : Extended;
widerstand : Extended;
tolerance: Extended;
prozent: Extended;

begin
    if rb_4rings.Checked then
    begin
        wert := (cb_ring1.ItemIndex+1)*10 + cb_ring2.ItemIndex;
        case cb_ring3.ItemIndex of
          0: widerstand := wert / 100;
          1: widerstand := wert /10;
          2: widerstand := wert;
          3: ...
end;
...

_frank_ 3. Aug 2006 20:25

Re: Probleme mit prozentrechnung
 
Zitat:

Zitat von Gehstock
...

bitte gewöhne dir ordentliche code-formatierung (Einrückung) an...

(mal davon abgesehen schreibt man widerstand ohne 'e' ;) )

Delphi-Quellcode:
procedure TForm2.btn_executeClick(Sender: TObject);
var
  wert : integer;
  widerstand,prozent,tolerance : double;
  einheit:string;
begin
  if rb_4rings.Checked then
  begin
    wert := (cb_ring1.ItemIndex+1)*10 + cb_ring2.ItemIndex;
    case cb_ring3.ItemIndex of
      0: widerstand := wert/100;
      1: widerstand := wert/10;
      2: widerstand := wert;
      3: widerstand := wert*10;
      4: widerstand := wert*100;
      5: widerstand := wert*1000;
      6: widerstand := wert*10000;
      7: widerstand := wert*100000;
      8: widerstand := wert*1000000;
      9: widerstand := wert*10000000));
    end;

...

  //widerstandswert ausgeben (2 nachkommastellen)
  if wiederstand>999000 then
  begin
    widerstand:=widerstand / 1000000;
    Einheit:='MOhm';
  end else if ...

  pnl_result.caption := format ('%.2f '+einheit,[widerstand]);


...

  if rb_4rings.Checked then
  begin
    case cb_ring4.ItemIndex of
      0: prozent:=10;
      1: prozent:=5;
      2: prozent:=1;
      3: prozent:=2;
      4: prozent:=0.5;
      5: prozent:=0.25;
      6: prozent:=0.1;
    end;
  end;

  if rb_5rings.Checked then
  begin
    case cb_ring55.ItemIndex of
      0: prozent:=5;
      1: prozent:=1;
      2: prozent:=2;
      3: prozent:=0.5;
      4: prozent:=0.25;
      5: prozent:=0.1;
    end;
  end;
  pnl_Tolerance.Caption:= FloatToStr(prozent)+' %';
  tolerance := wiederstand/100*prozent;
  pnl_worstcasemin.caption := floattostr(wiederstand-tolerance)+' '+Einheit);
  pnl_worstcasemax.caption := floattostr(wiederstand+tolerance)+' '+Einheit);
end;
HTH Frank

marabu 3. Aug 2006 20:38

Re: Probleme mit prozentrechnung
 
Hallo,

hier noch ein thread zum gleichen Thema: klick

Gute Nacht

marabu


Alle Zeitangaben in WEZ +1. Es ist jetzt 18:08 Uhr.
Seite 1 von 2  1 2      

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