ERLEDIGT
JA
JA
ANTWORTEN
8
8
ZUGRIFFE
3508
3508
EMPFEHLEN
-
Hallo Freunde,
gibt es in java eine einfache Möglichkeit mathematische Funktionen zu definieren? Möchte nicht parsen, auch keine externe Bibliothek benutzen. Hier das Beispiel funktioniert leider nicht. Da x automatisch den Wert 0 zugewiesen bekommt, sind somit function1 und function2 beide gleich 0.
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
public class Test { static double x; static double function1 = x * x; static double function2 = 2 * x; public static void solve(double[] numbers, double function) { for (int i = 0; i < numbers.length; i++) { x = numbers[i]; System.out.println("result :" + function); } } public static void main(String[] args) { solve(new double[] {1,2,3},function1); } }
Vg Erdal
-
Du könntest Dir ein "Wrapper-Interface" oder eine abstrakte Klasse, von der konkrete Klassen mit der jeweiligen Funktion erben, dafür schreiben, wie zum Beispiel das Interface Comparator für Vergleichsfunktionen aus der API.
Gruß hpvwWarum gibt (fast) keiner im Datenbankforum an, welches DBMS er benutzt?
Ich gehe im Zweifelsfall ohne Nachfrage von MySQL > 4.1 i.V.m. PHP aus.
Gewöhnt euch bitte auch an, die Fehlermeldung von mysql_error() zu posten.
-
Hallo Jörg,
kannst du mir vielleicht sagen wo ich das nachlesen oder mir ein Beispiel ansehen kann. Hab nicht so richtig verstanden, wie du es meinst.
Vg Erdal
-
Ich habe mal ein schnelles Beispiel gemacht:
Zunächst das Interface CalcStrategy:Es wird für alle Klassen, die dieses Interface implementieren, vorgeschrieben, dass sie die Methode calc implementieren, welche ein double entgegennimmt und ein double zurückgibt.Code :1 2 3 4 5 6 7 8 9
package calcStrategy; /** * * @author hpvw */ public interface CalcStrategy { public double calc(double d); }
Dann folgen die beiden konkreten Klassen zur Berechnung:
CalcPowerCode :1 2 3 4 5 6 7 8 9 10 11 12 13
package calcStrategy; /** * * @author hpvw */ public class CalcPower implements CalcStrategy { public double calc(double d) { return d*d; } }
CalcDoubleCode :1 2 3 4 5 6 7 8 9 10 11 12 13
package calcStrategy; /** * * @author hpvw */ public class CalcDouble implements CalcStrategy { public double calc(double d) { return 2 * d; } }
Und zu guter letzt die Testklasse Test:Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
package calcStrategy; /** * * @author hpvw */ public class Test { public static void solve(double[] numbers, CalcStrategy cS) { for (int i = 0; i < numbers.length; i++) { double x = numbers[i]; System.out.println("result :" + cS.calc(x)); } } public static void main(String[] args) { CalcStrategy cDouble = new CalcDouble(); CalcStrategy cPower = new CalcPower(); solve(new double[] {1,2,3},cPower); solve(new double[] {1,2,3},cDouble); } }
Die Ausgabe:Code :1 2 3 4 5 6
result :1.0 result :4.0 result :9.0 result :2.0 result :4.0 result :6.0
Gruß hpvwWarum gibt (fast) keiner im Datenbankforum an, welches DBMS er benutzt?
Ich gehe im Zweifelsfall ohne Nachfrage von MySQL > 4.1 i.V.m. PHP aus.
Gewöhnt euch bitte auch an, die Fehlermeldung von mysql_error() zu posten.
-
Herzlichen Dank Jörg, für das ausführliche Beispiel!
Vg Erdal
-
Diese Vorgehensweise ist übrigens angelehnt an das Enttwurfsmuster Strategie.
Wenn sich das Thema damit geklärt hat, markiere das Thema bitte unten rechts als erledigt.
Gruß hpvwWarum gibt (fast) keiner im Datenbankforum an, welches DBMS er benutzt?
Ich gehe im Zweifelsfall ohne Nachfrage von MySQL > 4.1 i.V.m. PHP aus.
Gewöhnt euch bitte auch an, die Fehlermeldung von mysql_error() zu posten.
-
17.01.06 19:55 #7
- Registriert seit
- Oct 2005
- Beiträge
- 35
Wenn du nen Funktionsparser für alle Mathematichen Funktionen benötigst, kannst du ne pn an mich senden, hab sowas da (selbst programmiert), will den nur noch nicht veröffentlichen da ich ihn noch für einen Wettbewerb brauche und sicher gestellt werden muss, das der Quelltext von mir stammt, was bei vervielfältigung im Inet schwierig ist. Der Parser kann + - * / sin cos tan pi e pow und sqrt von geklammerten Ausdrücken (als String vorliegend) auswerten und ist sehr leicht zu erweitern.
Greetz
-
13.09.10 10:53 #8caro10 Tutorials.de Gastzugang
Hallo MasterHimself,
ich bin gerade dabei ein Programm zu entwerfen, welches Numerisch Nullstellen berechnet.
Um natürlich verschiedene Funktionen vrwenden zu können, versuche ich einen Funktionsparser zu programmieren, was allerdings nicht ganz klappt. Da ich zufällig deinen Beitrag gerade lese, wollte ich fragen, ob du mir helfen könntest und mir deinen Funktionsparser ziegen könntest.
Wäre eine sehr große Hilfe!!
VG Caro
-
13.09.10 12:01 #9
- Registriert seit
- Oct 2005
- Beiträge
- 35
Da das 4 Jahre her ist kann ich es dir nicht mehr ganz genau erklären. Wichtig zum verstehen ist die Funktion pos0, welche die Position des char c im String s in der innersten Klammerebene zurückgibt falls c innerhalb der innersten Klammer ist, sonst -1.
Am Anfang wird also geschaut ob ein + in der innersten Klammer ist, wenn ja dann rekursiv linken Teil + rekursiv rechten Teil. Nach dem Prinzip arbeitet das Ding.
Code :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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
public class Parser { public static double h = 1E-8; public static double a = -3; private final double Pi = 3.14592653489; private final double e = 2.718281828459; private double x1; /* * Vor dem parsen werden die Leerzeichen entfernt */ public double StrToDbl(String s, double x) { x1 = x; if (s.equals("")) { return 0; } s.trim(); if (s.charAt(0) == '-') { s = "0" + s; } s = vereinfache(s); return parse(s); } private String vereinfache(String s) { for (int k = 0; k <= s.length() - 1; ++k) { if (s.length() - k > 3) { switch (s.charAt(k)) { case 's': { if (s.charAt(k + 1) == 'i') { s = s.substring(0, k) + "s" + s.substring(k + 3, s.length()); break; } else { s = s.substring(0, k) + "w" + s.substring(k + 4, s.length()); break; } } case 'c': { s = s.substring(0, k) + "c" + s.substring(k + 3, s.length()); break; } case 't': { s = s.substring(0, k) + "t" + s.substring(k + 3, s.length()); break; } case 'l': { if (s.charAt(k + 1) == 'o') { s = s.substring(0, k) + "l" + s.substring(k + 3, s.length()); break; } else { s = s.substring(0, k) + "n" + s.substring(k + 1, s.length()); break; } } case 'p': { s = s.substring(0, k) + "p" + s.substring(k + 2, s.length()); break; } } } } return s; } private int pos0(char c, String s) { int k, z; z = 0; for (k = s.length() - 1; k >= 0; --k) { if (s.charAt(k) == '(') { ++z; } if (s.charAt(k) == ')') { --z; } if ((s.charAt(k) == c) && (z == 0)) { return k; } } return -1; } private double parse(String s) { if (s.equals("")) { return 0; } s = s.trim(); if (pos0('+', s) > -1) { return parse(s.substring(0, pos0('+', s))) + parse(s.substring(pos0('+', s) + 1, s.length())); } else if (pos0('-', s) > -1) { return parse(s.substring(0, pos0('-', s))) - parse(s.substring(pos0('-', s) + 1, s.length())); } else if (pos0('*', s) > -1) { return parse(s.substring(0, pos0('*', s))) * parse(s.substring(pos0('*', s) + 1, s.length())); } else if (pos0('/', s) > -1) { return parse(s.substring(0, pos0('/', s))) / parse(s.substring(pos0('/', s) + 1, s.length())); } else if (pos0('^', s) > -1) { return Math.pow(parse(s.substring(0, pos0('^', s))), parse(s .substring(pos0('^', s) + 1, s.length()))); } else if (pos0('w', s) > -1) { return Math.sqrt(parse(s.substring(pos0('w', s) + 1, s.length()))); } else if (pos0('s', s) > -1) { return Math.sin(parse(s.substring(pos0('s', s) + 1, s.length()))); } else if (pos0('t', s) > -1) { return Math.tan(parse(s.substring(pos0('t', s) + 1, s.length()))); } else if (pos0('c', s) > -1) { return Math.cos(parse(s.substring(pos0('c', s) + 1, s.length()))); } else if (pos0('l', s) > -1) { return Math.log(parse(s.substring(pos0('l', s) + 1, s.length()))); } else if (pos0('n', s) > -1) { return Math.log(parse(s.substring(pos0('n', s) + 1, s.length()))) / Math.log(2); } else if ((s.equals("") == false) && (s.charAt(0) == '(')) { s = s.substring(1, s.length() - 1); return parse(s); } else if (s.equals("x")) { return x1; } else if (s.equals("a")) { return a; } else if (s.equals("h")) { return h; } else if (s.equals("p")) { return Pi; } else if (s.equals("e")) { return e; } else { double x; try { x = Double.parseDouble(s); return x; } catch (Exception ex) { } } return 0; } }
Ähnliche Themen
-
Keine Mathematischen Funktionen
Von HeinerPyt im Forum JavaAntworten: 2Letzter Beitrag: 06.03.08, 09:11 -
Probleme mit mathematischen Funktionen
Von Acriss im Forum PHPAntworten: 5Letzter Beitrag: 10.10.07, 15:03 -
Brushes mit Mathematischen Zeichen?
Von Chupakaba im Forum PhotoshopAntworten: 2Letzter Beitrag: 26.10.05, 21:03 -
Mathematischen code ausführen
Von openeye im Forum Visual Basic 6.0Antworten: 8Letzter Beitrag: 18.09.04, 18:34 -
Tool für graphische Darstellung math. Funktionen
Von tittli im Forum Microsoft WindowsAntworten: 3Letzter Beitrag: 15.09.04, 22:10





Zitieren
Login





