Algebraischer Ausdruck mit java

um Algebraischen Ausdrücke darzustellen, habe ich mir folgende Klasse entworfen


Code:
public class AlgebraischerAusdruck { 
// links
public AlgebraischerAusdruck links; 
// symbolOperator
public String symbolOperator;
// rechts
public AlgebraischerAusdruck rechts;
public AlgebraischerAusdruck(AlgebraischerAusdruck myLinks, String mySymbolOperator, AlgebraischerAusdruck myRechts) {
this.links = myLinks; 
this.symbolOperator = mySymbolOperator; 
this.rechts = myRechts;

}
public AlgebraischerAusdruck getLinks() {
return links;
} 
public String getSymbolOperator() {
return symbolOperator;
}
public AlgebraischerAusdruck getRechts() {
return rechts;
}
}


Mit dieser Klasse kann ich leider keine Ausdrücke wie -2+3 oder (2-5)*5 darstellen. Hat jemand eine Abhilfe? Ich wäre Ihnen sehr dankbar
 
Hallo stefaniemayer088

Hier ein Beispiel wie das aussehen könnte mit einem anderen Layout der Klassen. Wie du das in dein Setup bekommst dürfte eine gute Übung sein um zu verstehen wie das funktioniert. Und natürlich deckt das auch nur ein subset der möglichen algebraischen Ausdrücke ab, aber du kannst damit sicher mal beginnen.

Java:
/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    interface Expression {
        public double evaluate();
    }
   
    static class UnaryExpression implements Expression {
        boolean positiveOperator;
        Expression operand;
       
        public UnaryExpression(Expression operand, boolean positive) {
            this.operand = operand;
            this.positiveOperator = positive;
        }
       
        @Override
        public double evaluate() { 
            double value = operand.evaluate();
            return positiveOperator ? value : -value;
        }
    }
   
    static class ConstantExpression implements Expression {
        double value;
       
        public ConstantExpression(double value) {
            this.value = value;
        }
       
        @Override
        public double evaluate() { return this.value; }
    }
   
    static class BinaryExpression implements Expression {
        Expression left;
        Expression right;
        int operator;
       
        public static final int PLUS = 0;
        public static final int MINUS = 1;
        public static final int MULTIPLY = 2;
        public static final int DIVIDE = 3;
       
        public BinaryExpression(Expression left, Expression right, int operator) {
            this.left = left;
            this.right = right;
            this.operator = operator;
        }
       
        @Override
        public double evaluate() {
            double lValue = left.evaluate();
            double rValue = right.evaluate();
           
            switch(operator) {
                case PLUS:
                    return lValue + rValue;
                case MINUS:
                    return lValue - rValue;
                case MULTIPLY:
                    return lValue * rValue;
                case DIVIDE:
                    return lValue / rValue;
                default:
                    throw new IllegalStateException("Cannot execute operator: " + operator);
            }
        }
    }
   
    public static void main (String[] args) throws java.lang.Exception
    {
        ConstantExpression two = new ConstantExpression(2);
        ConstantExpression three = new ConstantExpression(3);
        UnaryExpression negTwo = new UnaryExpression(two, false);
        BinaryExpression result = new BinaryExpression(negTwo, three, BinaryExpression.PLUS);
       
        System.out.println(result.evaluate());
    }
}

Demo: http://ideone.com/NWnuQG

Grüsse
Cromon
 
Zurück