Autor Beitrag
phörsie
Hält's aus hier
Beiträge: 12

Win XP
Delphi 7
BeitragVerfasst: So 19.11.06 15:35 
Hallo,

ich will ein Programm schreiben, mitdem sich Funktionen zeichnen lassen.
Die zu zeichnende Funktion soll man frei in ein edit-Feld schreiben können, z.B. 3x^5-2x^2+7.
Wie kann ich diese Funktionsgleichung quellcodegerecht umwandeln lassen (damit das programm mit der eingabe arbeiten kann)? Dazu müsste ja zwischen die zahl vor dem x und dem x ein * geschrieben werden und die potenz umgewandelt werden...
Geht das? Wenn ja, wie?

Ich bedanke mich schon mal im voraus für die hoffentlich hilfreiche unterstützung!!!
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: So 19.11.06 15:55 
Gehen tut das schon, aber das nennt sich dann Suche in der Entwickler-Ecke FORMELPARSER und ist richtig kompliziert.

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)
wulfskin
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1349
Erhaltene Danke: 1

Win XP
D5 Pers (SSL), D2005 Pro, C, C#
BeitragVerfasst: So 19.11.06 17:08 
Hier gibt es was richtig tolles von user profile iconDelfiphan:
www.delphi-forum.de/...amp;highlight=parser.

_________________
Manche antworten um ihren Beitragszähler zu erhöhen, andere um zu Helfen.
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: So 19.11.06 18:10 
Sowas hier vielleicht:
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:
uses tyParser;

procedure TForm1.Button1Click(Sender: TObject);
var
 Expr: Expression;
 f: ExprFunc1V;
 i, w, h: Integer;
 xmin, ymin, xmax, ymax, xscale, yscale, y: Extended;
begin
  Expr := ParseExpr(Edit1.Text,['x']); // Parsen
  if Expr.Error then
  begin
   ShowMessage(FormatError(Expr.ErrInfo));
   exit;
  end;
  f := CompileExpr(Expr,tyPass1V); // Compilieren
  try
   // Achsenmaxima/minima
   xmin := -10;
   xmax := 10;
   ymin := -10;
   ymax := 10;

   // Umrechnungsparameter
   w := Image1.Width;
   h := Image1.Height;
   xscale := (xmax-xmin)/w;
   yscale := h/(ymax-ymin);

   // Zeichnen
   y := (f(xscale*0+xmin)-xmin)*yscale;
   Image1.Canvas.MoveTo(0,h-Round(y));
   for i := 1 to Image1.Width-1 do
   begin
    y := (f(xscale*i+xmin)-xmin)*yscale;
    Image1.Canvas.LineTo(i,h-Round(y));
   end;
  finally
   FreeFunc(@f);
  end;
end;

TButton, TEdit, TImage auf die Form; tyParser.pas runterladen.