Delphi-PRAXiS
Seite 6 von 6   « Erste     456   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi Taschenrechner Programmieren (https://www.delphipraxis.net/20361-taschenrechner-programmieren.html)

Wuaegner 29. Sep 2004 15:10

Re: Taschenrechner Programmieren
 
Dann zeig mal deinen ganzen Code, wie du Umwandeln deklarierst usw.

Excalibur 29. Sep 2004 16:07

Re: Taschenrechner Programmieren
 
Is halt etwas lang geraten, wegen den vielen groß/klein-Möglichkeiten (und das sind nur die häufigsten) :roll: :roll:

Delphi-Quellcode:
unit wandler;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Edit2: TEdit;
    Edit3: TEdit;
    Label3: TLabel;
    Button1: TButton;
    Label4: TLabel;
    Label5: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var Eingabe, Ergebnis : real;
Einheit, Einheitneu:string;
begin
Eingabe:=strtoint(Edit1.text);
Einheit:=(Edit2.Text);
EInheitneu:=Edit3.Text;

If (lowercase(Einheit)='cm') and (lowercase(Ergebnis)='m')
then Ergebnis:=Eingabe /100;
If (Einheit='CM') and (einheitneu='m')
then Ergebnis:=Eingabe /100;
If (Einheit='Cm') and (einheitneu='m')
then Ergebnis:=Eingabe /100;
If (Einheit='cM') and (einheitneu='m')
then Ergebnis:=Eingabe /100;
If (Einheit='CM') and (einheitneu='M')
then Ergebnis:=Eingabe /100;
If (Einheit='cm') and (einheitneu='M')
then Ergebnis:=Eingabe /100;
If (Einheit='Cm') and (einheitneu='M')
then Ergebnis:=Eingabe /100;
If (Einheit='cM') and (einheitneu='M')
then Ergebnis:=Eingabe /100;

If (Einheit='m') and (einheitneu='cm')
then Ergebnis:=Eingabe *100;
If (Einheit='M') and (einheitneu='cm')
then Ergebnis:=Eingabe *100;
If (Einheit='m') and (einheitneu='Cm')
then Ergebnis:=Eingabe *100;
If (Einheit='m') and (einheitneu='cM')
then Ergebnis:=Eingabe *100;
If (Einheit='M') and (einheitneu='Cm')
then Ergebnis:=Eingabe *100;
If (Einheit='M') and (einheitneu='cM')
then Ergebnis:=Eingabe *100;
If (Einheit='M') and (einheitneu='CM')
then Ergebnis:=Eingabe *100;
If (Einheit='m') and (einheitneu='CM')
then Ergebnis:=Eingabe *100;

If (Einheit='DM') and (einheitneu='cm')
then Ergebnis:=Eingabe *10;
If (Einheit='DM') and (einheitneu='CM')
then Ergebnis:=Eingabe *10;
If (Einheit='DM') and (einheitneu='Cm')
then Ergebnis:=Eingabe *10;
If (Einheit='DM') and (einheitneu='cM')
then Ergebnis:=Eingabe *10;
If (Einheit='DM') and (einheitneu='CM')
then Ergebnis:=Eingabe *10;
If (Einheit='dm') and (einheitneu='CM')
then Ergebnis:=Eingabe *10;
If (Einheit='dm') and (einheitneu='Cm')
then Ergebnis:=Eingabe *10;
If (Einheit='dm') and (einheitneu='cM')
then Ergebnis:=Eingabe *10;
If (Einheit='dm') and (einheitneu='cm')
then Ergebnis:=Eingabe *10;

If (Einheit='cm') and (einheitneu='dm')
then Ergebnis:=Eingabe /10;
If (Einheit='CM') and (einheitneu='dm')
then Ergebnis:=Eingabe /10;
If (Einheit='cm') and (einheitneu='DM')
then Ergebnis:=Eingabe /10;
If (Einheit='Cm') and (einheitneu='Dm')
then Ergebnis:=Eingabe /10;
If (Einheit='cM') and (einheitneu='dM')
then Ergebnis:=Eingabe /10;
If (Einheit='CM') and (einheitneu='DM')
then Ergebnis:=Eingabe /10;
If (Einheit='cM') and (einheitneu='dm')
then Ergebnis:=Eingabe /10;
If (Einheit='Cm') and (einheitneu='dM')
then Ergebnis:=Eingabe /10;
If (Einheit='cM') and (einheitneu='Dm')
then Ergebnis:=Eingabe /10;

If (Einheit='M') and (einheitneu='dm')
then Ergebnis:=Eingabe *10;
If (Einheit='m') and (einheitneu='dm')
then Ergebnis:=Eingabe *10;
If (Einheit='M') and (einheitneu='DM')
then Ergebnis:=Eingabe *10;
If (Einheit='m') and (einheitneu='DD')
then Ergebnis:=Eingabe *10;
If (Einheit='M') and (einheitneu='dM')
then Ergebnis:=Eingabe *10;
If (Einheit='M') and (einheitneu='Dm')
then Ergebnis:=Eingabe *10;
If (Einheit='m') and (einheitneu='dM')
then Ergebnis:=Eingabe *10;
If (Einheit='m') and (einheitneu='Dm')
then Ergebnis:=Eingabe *10;

If (Einheit='m') and (einheitneu='mm')
then Ergebnis:=Eingabe *1000;
If (Einheit='M') and (einheitneu='mm')
then Ergebnis:=Eingabe *1000;
If (Einheit='m') and (einheitneu='MM')
then Ergebnis:=Eingabe *1000;
If (Einheit='m') and (einheitneu='mM')
then Ergebnis:=Eingabe *1000;
If (Einheit='m') and (einheitneu='Mm')
then Ergebnis:=Eingabe *1000;
If (Einheit='M') and (einheitneu='mM')
then Ergebnis:=Eingabe *1000;
If (Einheit='M') and (einheitneu='Mm')
then Ergebnis:=Eingabe *1000;
If (Einheit='M') and (einheitneu='MM')
then Ergebnis:=Eingabe *1000;

If (Einheit='km') and (einheitneu='mm')
then Ergebnis:=Eingabe *1000000;
If (Einheit='KM') and (einheitneu='MM')
then Ergebnis:=Eingabe *1000000;
If (Einheit='Km') and (einheitneu='Mm')
then Ergebnis:=Eingabe *1000000;
If (Einheit='kM') and (einheitneu='MM')
then Ergebnis:=Eingabe *1000000;
If (Einheit='kM') and (einheitneu='mM')
then Ergebnis:=Eingabe *1000000;
If (Einheit='Km') and (einheitneu='mm')
then Ergebnis:=Eingabe *1000000;
If (Einheit='kM') and (einheitneu='mm')
then Ergebnis:=Eingabe *1000000;
If (Einheit='Km') and (einheitneu='Mm')
then Ergebnis:=Eingabe *1000000;

If (Einheit='km') and (einheitneu='m')
then Ergebnis:=Eingabe *1000;
If (Einheit='KM') and (einheitneu='M')
then Ergebnis:=Eingabe *1000;
If (Einheit='km') and (einheitneu='M')
then Ergebnis:=Eingabe *1000;
If (Einheit='kM') and (einheitneu='M')
then Ergebnis:=Eingabe *1000;
If (Einheit='Km') and (einheitneu='m')
then Ergebnis:=Eingabe *1000;
If (Einheit='KM') and (einheitneu='m')
then Ergebnis:=Eingabe *1000;
If (Einheit='kM') and (einheitneu='m')
then Ergebnis:=Eingabe *1000;

If (Einheit='mm') and (einheitneu='km')
then Ergebnis:=Eingabe /1000000;
usw

Label5.caption:=floattostr (Ergebnis)+(Einheitneu);

end;

end.

JasonDX 29. Sep 2004 16:19

Re: Taschenrechner Programmieren
 
ich hab mir nicht die ganze diskussion durchgelesen, aber mal 'n kleiner tipp, wie du den Code 'n bissl übersichtlich machen könntst:
versuch mal, mit
Delphi-Quellcode:
UpperCase()
die ganzen IFs mit dem 'CM', 'cm', 'Cm', 'cM'.... wegzuräumen!

alcaeus 29. Sep 2004 16:22

Re: Taschenrechner Programmieren
 
Hi Excalibur,

Delphi-Quellcode:
If (lowercase(Einheit)='cm') and (lowercase(Ergebnis)='m')
Hier liegt der Fehler. Die Richtige Variable müsste Einheitneu, und nicht Ergebnis heißen. Jedenfalls verwendest du später immer wieder Einheitneu..

Greetz
alcaeus

Wuaegner 29. Sep 2004 18:41

Re: Taschenrechner Programmieren
 
HI Excalibur!
Zitat:

Quellcode: markieren
If (lowercase(Einheit)='cm') and (lowercase(Ergebnis)='m')


Hier liegt der Fehler. Die Richtige Variable müsste Einheitneu, und nicht Ergebnis heißen. Jedenfalls verwendest du später immer wieder Einheitneu..
Genau, da Ergebnis vom Typ Real ist , die Funktion lowercase() aber einen String braucht und keinen Real kommt die Fehlermeldung. :wink:

Excalibur 29. Sep 2004 18:45

Re: Taschenrechner Programmieren
 
aber da kommt immernoch: Inkompatible Typen: 'String' und 'Real'
-> ich muss des erst noch umwandeln, aber wie und wo muss des hin?

alcaeus 29. Sep 2004 18:59

Re: Taschenrechner Programmieren
 
Hi Excalibur,

Hier mal ein Auszug aus deinem Code:
Delphi-Quellcode:
If (lowercase(Einheit)='cm') and (lowercase(Ergebnis)='m')
then Ergebnis:=Eingabe /100;
If (Einheit='CM') and (einheitneu='m')
then Ergebnis:=Eingabe /100;
If (Einheit='Cm') and (einheitneu='m')
then Ergebnis:=Eingabe /100;
If (Einheit='cM') and (einheitneu='m')
then Ergebnis:=Eingabe /100;
If (Einheit='CM') and (einheitneu='M')
then Ergebnis:=Eingabe /100;
If (Einheit='cm') and (einheitneu='M')
then Ergebnis:=Eingabe /100;
If (Einheit='Cm') and (einheitneu='M')
then Ergebnis:=Eingabe /100;
If (Einheit='cM') and (einheitneu='M')
then Ergebnis:=Eingabe /100;
Du verwendest in den einzelnen if-Abfragen Einheit und Einheitneu, und anschließend rechnest du das ergebnis aus. Also musst du auch im lowercase-teil Einheit und Einheitneu verwenden. So kompliziert dürfte das wohl nicht sein :zwinker:

Greetz
alcaeus

Wuaegner 29. Sep 2004 19:04

Re: Taschenrechner Programmieren
 
Code:
procedure TForm1.Button1Click(Sender: TObject);
var Eingabe, Ergebnis : real;
Einheit, Einheitneu:string;
begin
Eingabe:=strto[size=18]float[/size]('17');
Einheit:='CM';
EInheitneu:='M';

If (lowercase(Einheit)='cm') and (lowercase(EinheitNeu)='m')
then Ergebnis:=Eingabe /100;
So geht's.

Nothine 29. Sep 2004 19:18

Re: Taschenrechner Programmieren
 
Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
var Eingabe,Ergebnis:  Real;
    Einheit,EinheitNeu: string;
begin
  Eingabe   := StrToFloat(Edit1.Text); //StrToFLOAT !
  Einheit   := Edit2.Text;
  EinheitNeu := Edit3.Text;
  Ergebnis  := 0;

  if (UpperCase(Einheit)='CM') and (UpperCase(EinheitNeu)='DM') then
    Ergebnis := Eingabe / 10;
  if (UpperCase(Einheit)='CM') and (UpperCase(EinheitNeu)='M') then
    Ergebnis := Eingabe / 100;
  if (UpperCase(Einheit)='MM') and (Uppercase(EinheitNeu)='KM') then
    Ergebnis := Eingabe / 1000000;
  if (UpperCase(Einheit)='DM') and (UpperCase(EinheitNeu)='CM') then
    Ergebnis := Eingabe * 10;
  if (UpperCase(Einheit)='M') and (UpperCase(EinheitNeu)='DM') then
    Ergebnis := Eingabe * 10;
  if (UpperCase(Einheit)='M') and (UpperCase(EinheitNeu)='CM') then
    Ergebnis := Eingabe * 100;
  if (UpperCase(Einheit)='M') and (UpperCase(EinheitNeu)='MM') then
    Ergebnis := Eingabe * 1000;
  if (UpperCase(Einheit)='KM') and (UpperCase(EinheitNeu)='M') then
    Ergebnis := Eingabe * 1000;
  if (UpperCase(Einheit)='KM') and (UpperCase(EinheitNeu)='MM') then
    Ergebnis := Eingabe * 1000000;

  Label5.Caption := FloatToStr(Ergebnis) + EinheitNeu;
end;
so sollte deine button-prozedur aussehen... (abgesehn davon dass noch nicht alle kombinationen drin sind :mrgreen:)

//Edit: und das mit UpperCase und LowerCase is reine geschmackssache...


Alle Zeitangaben in WEZ +1. Es ist jetzt 11:19 Uhr.
Seite 6 von 6   « Erste     456   

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