package projekte.taschenrechner;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class Taschenrechner extends MIDlet implements CommandListener {
Display display;
Form form;
Command rechnen;
Command exit;
Command loeschen;
TextField tf;
public Taschenrechner() {
// TODO Auto-generated constructor stub
rechnen = new Command("Text ausrechnen", Command.SCREEN, 1);
loeschen = new Command("Löschen", Command.SCREEN, 1);
exit = new Command("Beenden", Command.EXIT, 1);
tf = new TextField("Hier Text Eingeben:", "", 100, TextField.ANY);
form = new Form("Taschenrechner");
display = Display.getDisplay(this);
form.addCommand(exit);
form.addCommand(rechnen);
form.addCommand(loeschen);
form.append(tf);
form.setCommandListener(this);
}
protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
display.setCurrent(form);
}
public static String berechneAusdruck(String in) {
String ausdruck = "";
char operator;
int indexOperator;
int indexOperandLinksAnfang;
int indexOperandRechtsEnde;
String stringOperandLinks;
String stringOperandRechts;
String stringAusdruckLinks;
String stringAusdruckRechts;
for (int i = 0; i < in.length(); i++) {
if (in.charAt(i) == ',') {
ausdruck += ".";
} else {
if (in.charAt(i) == '-') {
ausdruck += "+-";
} else {
ausdruck += in.charAt(i);
}
}
}
while ((indexOperator = ausdruck
.indexOf(operator = naesterOperator(ausdruck))) > 0) {
indexOperandLinksAnfang = indexOperandLinksAnfang(ausdruck,
indexOperator);
indexOperandRechtsEnde = indexOperandRechtsEnde(ausdruck,
indexOperator);
stringOperandLinks = ausdruck.substring(indexOperandLinksAnfang,
indexOperator);
stringOperandRechts = ausdruck.substring(indexOperator + 1,
indexOperandRechtsEnde);
stringAusdruckLinks = ausdruck
.substring(0, indexOperandLinksAnfang);
stringAusdruckRechts = ausdruck.substring(indexOperandRechtsEnde);
ausdruck = stringAusdruckLinks
+ rechne(stringOperandLinks, operator, stringOperandRechts)
+ stringAusdruckRechts;
}
return ausdruck;
}
private static int indexOperandLinksAnfang(String ausdruck,
int indexOperator) {
int ret = indexOperator - 1;
while (ret > 0
&& (Character.isDigit(ausdruck.charAt(ret - 1)) || ausdruck
.charAt(ret - 1) == '.')) {
ret--;
}
if (ret > 0 && ausdruck.charAt(ret - 1) == '-') {
ret--;
}
return ret;
}
private static int indexOperandRechtsEnde(String ausdruck, int indexOperator) {
int ret = indexOperator + 1;
if (ret < ausdruck.length() && ausdruck.charAt(ret) == '-') {
ret++;
}
while (ret < ausdruck.length()
&& Character.isDigit(ausdruck.charAt(ret))) {
ret++;
}
return ret;
}
private static String rechne(String operandLinks, char operator,
String operandRechts) {
switch (operator) {
case '+':
return Double.toString(Double.parseDouble(operandLinks)
+ Double.parseDouble(operandRechts));
case '-':
return Double.toString(Double.parseDouble(operandLinks)
- Double.parseDouble(operandRechts));
case '*':
return Double.toString(Double.parseDouble(operandLinks)
* Double.parseDouble(operandRechts));
case '/':
return Double.toString(Double.parseDouble(operandLinks)
/ Double.parseDouble(operandRechts));
default:
return null;
}
}
private static char naesterOperator(String a) {
int div = a.indexOf('/');
int mul = a.indexOf('*');
if (div > 0 && mul > 0) { // wenn es beide Operanten gibt
if (div < mul) {
return '/';
} else {
return '*';
}
} else {
if (div > 0)
return '/';
if (mul > 0)
return '*';
}
// es gibt keinen '/' oder '*' Operanten mehr
if (a.indexOf('+') > 0) {
return '+';
} else {
return '-';
}
}
public void commandAction(Command c, Displayable d) {
if (c == rechnen) {
String in = tf.getString();
String out = berechneAusdruck(in);
form.append(out + "\n");
}
if (c == loeschen) {
form.deleteAll();
form.append(tf + "\n");
}
if (c == exit) {
try {
destroyApp(true);
} catch (MIDletStateChangeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}