Autor Beitrag
spezidekor
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 25

Win XP
D7
BeitragVerfasst: Sa 18.09.04 18:12 
folgendes problem: ich muss ein kleines programm schreiben. dieses berechnet eine prozentuale abweichung, wandelt diese in einen string um und gibt ihn in einer listbox aus. aber statt z.bsp. 327,67% steht da 3,27%. kein problem dachte ich und schrieb statt str(abw[x]) nun einfach str(abw[x]:3:2). das funktionierte aber nicht, denn nun stand da auch nicht 327,67%, sondern 32767%. am besten ihr schaut euch die bilder an, da seht ihr den unterschied. was mache ich falsch?

user defined image
ohne den eingriff

user defined image
mit eingriff

und hier der quellcode:
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:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    ListBox1: TListBox;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

type
  data=record
    artnr:string[8];
    artbez:string[30];
    menge:string[2];
    best:smallint;
    bestmax:smallint;
    bestmin:smallint;
    preis:single;
    preismax:single;
    preismin:single;
    herst:string[41];
    lzeit:byte;
end;

var
  Form1: TForm1;
  artikel:array[1..100] of data;
  f:file of data;
  abw:array[1..100] of single;
  x,max:integer;
  s:string[5];
  s1,s2:string[8];


implementation

{$R *.dfm}



procedure TForm1.Button1Click(Sender: TObject);
begin
  edit1.text:='Lade artikel.dat...';
  assignfile(f,'artikel.dat');
  reset(f);
    while not EOF(f) do
      begin
        x:=x+1;
        read (f,artikel[x]);
      end;
  closefile(f);
  max:=x;
  edit1.Text:='artikel.dat erfolgreich geladen!'
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  edit1.Text:='Berechne prozentuale Abweichung...';
  for x:=1 to max do
    begin
      abw[x]:=(artikel[x].preismax/artikel[x].preismin)*100;
      str(abw[x]:3:2,s);
      listbox1.items.add(s+'%');
    end;
  edit1.Text:='Sortierung erfolgreich beendet!';
end;

end.


Moderiert von user profile iconChristian S.: Code- durch Delphi-Tags ersetzt.
Viper
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 88



BeitragVerfasst: Sa 18.09.04 18:21 
Hallo.

Du hast in deiner Variablen Deklaration die Variable s als string[5] deklarariert. Du musst aber das Kommazeichen mitberechnen, also muss das ein string[6] sein.
Außerdem würde ich das Array abw nicht vom Typ Single nehmen, sondern mindestens Double, wenn nicht Extended.
Ansonsten kann ich keinen Fehler entdecken, aber das mit dem string[6] könnte es vielleicht schon beheben.

_________________
"Der Sinussatz ist nur was für Leute, die Teichbesitzer mit Insel und Baum sind. Alle anderen können ihn vergessen" (Mein Mathe-Prof.)
spezidekor Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 25

Win XP
D7
BeitragVerfasst: Sa 18.09.04 18:36 
hm, anscheinend nicht. hab den string auf 6 geändert. die zahl(en) wurden nun einfach um eine stelle verlängert. was ich komisch finde: es werden bei str(...:3:2) keine stellen nach dem komma angezeigt. wenn ich in der variablen-deklaration jetzt die string begrenzung rausnehme, dann wird die zahl ellenlang, hat aber plötzlich zwei stellen nach dem komma. ich versteh's nicht.
Viper
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 88



BeitragVerfasst: Sa 18.09.04 18:44 
ein Auszug aus der Delphi-Hilfe:
Zitat:
procedure Str(X [: Width [: Decimals ]]; var S);

Width ist die komplette Länge des Strings, also muss da die 6 hin und nicht nur eine 3. Den Fehler habe ich selber früher mal gemacht! Probier es bitte noch mal aus, ansonsten weiß ich leider auch nicht weiter.

_________________
"Der Sinussatz ist nur was für Leute, die Teichbesitzer mit Insel und Baum sind. Alle anderen können ihn vergessen" (Mein Mathe-Prof.)
spezidekor Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 25

Win XP
D7
BeitragVerfasst: Sa 18.09.04 19:47 
ich denke das du recht hast. ich glaube da war ich wohl blind. allerdings ändert das überhaupt nichts. es funktioniert nachwievor nicht.

hier nochmal der geänderte quellcode:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
var s:string[6];
...
...
...
procedure TForm1.Button2Click(Sender: TObject);
begin
  edit1.Text:='Berechne prozentuale Abweichung...';
  for x:=1 to max do
    begin
      abw[x]:=(artikel[x].preismax/artikel[x].preismin)*100;
      str(abw[x]:6:2,s);
      listbox1.items.add(s+'%');
    end;
  edit1.Text:='Sortierung erfolgreich beendet!';
end;
...
end.


ich bin echt am verzweifeln!

Moderiert von user profile iconChristian S.: Code- durch Delphi-Tags ersetzt.
sourcehunter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 482

Win XP | Suse 10.1
Delphi 2005 Pers.
BeitragVerfasst: Sa 18.09.04 20:48 
Haste schon mal mit FloatToStrF probiert?

z.B. so:
ausblenden Delphi-Quelltext
1:
s:=FloatToStrF(abw[x],ffFixed,6,1)+'%';					


möglicherweise musst du den String dann noch beschneiden.

_________________
Linux und OpenSource rulez!
spezidekor Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 25

Win XP
D7
BeitragVerfasst: So 19.09.04 10:05 
ich werde es als eine möglichkeit in betracht ziehen. aber es muss doch auch mit meiner methode funktionieren! ich werde einfach nochmal alles durchgehen und alle variablen prüfen. vielleicht ist ja auch die ausgangdatei nicht in ordnung oder sonstwas. der fehler wird sich schon finden.

danke für die nette hilfe
spezidekor Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 25

Win XP
D7
BeitragVerfasst: So 19.09.04 11:07 
hab die sache nochmal neu angefangen und mal ein paar sachen getestet. irgendetwas grundlegendes muss hier falsch sein. aber was? ich komm mir vor wie bei so 'nem fehler-such-bild.

quellcode:
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:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    ListBox1: TListBox;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

type
  data=record
    artnr:string[8];
    artbez:string[30];
    menge:string[2];
    best:smallint;
    bestmax:smallint;
    bestmin:smallint;
    preis:single;
    preismax:single;
    preismin:single;
    herst:string[41];
    lzeit:byte;
end;

var
  Form1: TForm1;
  artikel:array[1..100] of data;
  f:file of data;
  abw:array[1..100] of double;
  x,max:integer;
  s:string;

implementation

{$R *.dfm}

{$apptype console}

procedure TForm1.Button1Click(Sender: TObject);
begin
  assignfile(f,'artikel.dat');
  reset(f);
    while not EOF(f) do
      begin
        x:=x+1;
        read (f,artikel[x]);
      end;
  closefile(f);
  max:=x;
  for x:=1 to max do
    begin
      writeln(artikel[x].preis:6:2);
    end;
end;


end.



und hier das ergebnis. ich wollte drei stellen vor und zwei stellen nach dem komma. was ist mein fehler?
user defined image

Moderiert von user profile iconChristian S.: Code- durch Delphi-Tags ersetzt.
sourcehunter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 482

Win XP | Suse 10.1
Delphi 2005 Pers.
BeitragVerfasst: So 19.09.04 12:59 
Ich würde das Verhalten so interpretieren, dass vor dem Komma mindestens 3 Zeichen stehen, also fehlende Ziffer ersetzt werden (3.Zeile von unten), wenn die Zahl größer ist, dann wird das Ding halt länger. Aber wie man das in den Griff kriegt weiß ich nich.

_________________
Linux und OpenSource rulez!
spezidekor Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 25

Win XP
D7
BeitragVerfasst: So 19.09.04 13:31 
also kann der fehler theoretisch nur in der .dat-datei liegen, das dort die zahlen zu groß angelegt wurden. der quellcode ist doch grundsätzlich erstmal richtig, oder? somit bleibt als ursprung des fehlers theoretisch nur die datenquelle.
sourcehunter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 482

Win XP | Suse 10.1
Delphi 2005 Pers.
BeitragVerfasst: Fr 24.09.04 19:21 
Würd ich auch sagen. Wenn de vieleicht mal was posten könntest, wie so'ne Datenquelle aussieht?

_________________
Linux und OpenSource rulez!