Hallo
Ich habe hier ein Problem mit meinem Programm das eine Formel mit Klammern lesen und ausrechnen soll. Die Formel muss mit Klammern eingeben werden und darf bislang nur einstellige Zahlen haben. Ich schreibe dann die Operatoren oder Ziffern in eine Linked List rein. Wenn ein ) kommt beginne ich mit der Rechnung und mache so weiter bis zum Ende wo dann das Ergebniss ausgeben wird.
Nur beim Auslesen meiner Linked List passt irgendwas nicht. Vielleicht kann mir jemand helfen. Ich bin schon seit einigen Tagen dabei und komme nicht weiter.
Gruss Marcus
Hier mein Quellcode
Die FloatNode class:
Die CharNode class:
und noch die check class
Ich habe hier ein Problem mit meinem Programm das eine Formel mit Klammern lesen und ausrechnen soll. Die Formel muss mit Klammern eingeben werden und darf bislang nur einstellige Zahlen haben. Ich schreibe dann die Operatoren oder Ziffern in eine Linked List rein. Wenn ein ) kommt beginne ich mit der Rechnung und mache so weiter bis zum Ende wo dann das Ergebniss ausgeben wird.
Nur beim Auslesen meiner Linked List passt irgendwas nicht. Vielleicht kann mir jemand helfen. Ich bin schon seit einigen Tagen dabei und komme nicht weiter.
Gruss Marcus
Hier mein Quellcode
Code:
import java.lang.*;
import java.util.*;
import java.io.*;
class assignment1a
{
public static void main (String args[])
{
int length, test;
float number=0;
boolean checking;
char input;
KeyboardInput in = new KeyboardInput();
FloatNode Number=null;
FloatNode numLink=null;
FloatNode FloatNode=null;
CharNode Operator=null;
System.out.println("Enter a fully parenthesised arithmetic expression: ");
String s = in.readString(); //reading in a string from the keyboard
length = s.length(); //getting the lenght of the input string
check expression = new check(s);
check evaluate = new check();
if (expression.Letter() == false)
{
System.out.println("Invalid arithmetic expression!");
System.exit(0);
}
else
System.out.println("Valid arithmetic expression.");
for (int i = 0 ; i < length ; i ++)
{
input = s.charAt(i);
if (input == '(')
continue;
else if (input == ')')
{
System.out.println("Operator ) ");
calculate();
continue;
}
else if (input == '+')
{
System.out.println("Operator + ");
Operator = new CharNode ('+' , Operator);
continue;
}
else if (input == '-')
{
System.out.println("Operator - ");
Operator = new CharNode ('-' , Operator);
continue;
}
else if (input == '/')
{
System.out.println("Operator / ");
Operator = new CharNode ('/' , Operator);
continue;
}
else if (input == '*')
{
System.out.println("Operator * ");
Operator = new CharNode ('*' , Operator);
continue;
}
else
{
FloatNode newFloat = new FloatNode(number, FloatNode);
System.out.println("Number");
}
}
//print the solution
//it is the last entry in the linked list
System.out.println("The result is : " + FloatNode.getData());
}
public static void calculate()
{
float result = 0;
char Operator;
FloatNode Number=null;
FloatNode numLink=null;
FloatNode FloatNode=null;
//CharNode Operator=null;
System.out.println("This is the calculate method.");
//pop the Operator stack and store it in the char operator value
CharNode test = new CharNode();
char op = test.getData();
CharNode newOp = test.getLink();
//pop the FloatNode stack and store it in the float number2 value
float number2 = FloatNode.getData();
FloatNode = FloatNode.getLink();
//pop the FloatNode stack and store it in the float number1 value
float number1 = FloatNode.getData();
FloatNode = FloatNode.getLink();
try
{
switch (op)
{
case '+': result = number1 + number2;
case '-': result = number1 - number2;
case '*': result = number1 * number2;
case '/': result = number1 / number2;
default: System.out.println("Something has gone wrong.");
}
}
catch (ArithmeticException e)
{
System.out.println("! Division with Zero !");
}
}
}
Die FloatNode class:
Code:
public class FloatNode
{
private float data;
private FloatNode link;
public FloatNode()
{
data=0;
link=null;
}
public FloatNode(float initialData, FloatNode initialLink) // Explicit Constructor
{
data = initialData;
link = initialLink;
}
public float getData() // returns the data
{
return (data);
}
public FloatNode getLink() // returns the link
{
return (link);
}
}
Die CharNode class:
Code:
public class CharNode
{
private char data;
private CharNode link;
public CharNode()
{
data=0;
link=null;
}
public CharNode(char initialData, CharNode initialLink) // Explicit Constructor
{
data = initialData;
link = initialLink;
}
public char getData() // returns the data
{
return data;
}
public CharNode getLink() // returns the link
{
return link;
}
}
und noch die check class
Code:
import java.lang.*;
class check
{
String str = null;
public check(String str1)
{ str = str1; }
public check()
{
}
public boolean Letter ()
{
int close = 0, open = 0, digit = 0, op = 0;
for (int i=0;i<str.length();i++)
{
char c;
c = str.charAt(i);
if (Character.isDigit(c) == true)
digit = digit + 1;
if (c == '+' || c == '-' || c == '*' || c == '/')
op = op + 1;
if (c == '(')
open = open + 1;
if (c == ')')
close = close + 1;
}
if ((open == close) && (str.length() - close - open - digit - op == 0))
return true;
else
return false;
}
public int Length ()
{
int m = str.length();
return m;
}
}