diff --git a/Aufgabe4/Main.java b/Aufgabe4/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..4fa6318de63f9c6854d5a41f720546436e6c4fe8 --- /dev/null +++ b/Aufgabe4/Main.java @@ -0,0 +1,26 @@ + + +public class Main { + public static void main(String[] args){ + + // 23, x, +, (,0, 5, x,), ^, 2, -, 1 + String ausdruck = "230lkjhx +(2 5x) ^2,jhff 3−1gf "; + String ausdruck2 = "0x +(2 5x) ^2−1f "; + + Tokenizer t = new Tokenizer(); + + t.tokenizeString(ausdruck).forEach(t1 -> { + System.out.print(t1.getTokenString() + " "); + System.out.println(t1.getTokenType()); + }); + + + t.tokenizeString(ausdruck2).forEach(t1 -> { + System.out.print(t1.getTokenString() + " "); + System.out.println(t1.getTokenType()); + }); + + + } +} + diff --git a/Aufgabe4/Tokenizer.java b/Aufgabe4/Tokenizer.java new file mode 100644 index 0000000000000000000000000000000000000000..574433deb2fa0afcf34d01a06e03edcebb8a554f --- /dev/null +++ b/Aufgabe4/Tokenizer.java @@ -0,0 +1,99 @@ +import java.util.ArrayList; +import java.util.Objects; + +public class Tokenizer { + + + + + + + public ArrayList<Token> tokenizeString(String s) { + + + return constructTokenList( buildSubstringsFromSeparatedChars( separateChars(s) ) ); + + + } + + private ArrayList<String> separateChars(String stringToTokenize) { + + ArrayList<String> separated = new ArrayList<>(); + + separated.add("|"); + + for (char c : stringToTokenize.toCharArray()) { + + if(Character.isWhitespace(c)){ + continue; + } + + if(Character.isDigit(c)) { + separated.add( String.valueOf(c) ); + } else { + separated.add("|"); + separated.add( String.valueOf(c) ); + separated.add("|"); + } + } + + separated.add("|"); + + return separated; + } + + private ArrayList<String> buildSubstringsFromSeparatedChars(ArrayList<String> separated) { + + ArrayList<String> tokensAsStrings = new ArrayList<>(); + + StringBuilder temp = new StringBuilder(); + + for(String c : separated) { + if(Objects.equals(c, "|")) { + + if(!temp.toString().equals("")) { + tokensAsStrings.add(temp.toString()); + } + + temp = new StringBuilder(); + + } else { + temp.append(c); + } + } + + return tokensAsStrings; + + } + + private ArrayList<Token> constructTokenList(ArrayList<String> tokensAsStrings) { + + ArrayList<Token> tokens = new ArrayList<>(); + + boolean notANumber = false; + + for(String s1 : tokensAsStrings) { + + try { + Integer.parseInt(s1); + tokens.add(new Token(TokenType.number, s1)); + } catch (NumberFormatException ignored) { + + notANumber = true; + + } + + if(notANumber) { + + if (Character.isLetter( s1.charAt(0) )) { + tokens.add(new Token(TokenType.var, s1)); + } else { + tokens.add(new Token(TokenType.special, s1)); + } + notANumber = false; + } + } + + return tokens; + } +} diff --git a/Aufgabe4/main.java b/Aufgabe4/main.java deleted file mode 100644 index c42fee51834636514fd883f3726d13e5f19fa21d..0000000000000000000000000000000000000000 --- a/Aufgabe4/main.java +++ /dev/null @@ -1,154 +0,0 @@ -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; - -public class main { - public static void main(String[] args){ - - // 23, x, +, (,0, 5, x,), ^, 2, -, 1 - String ausdruck = "23x +(0,5x)^20−10"; - - ArrayList<String> separatedChar = new ArrayList<String>(); - - List<Token> tokenList = new ArrayList<>(); - - separatedChar.add("|"); - for (char c : ausdruck.toCharArray()) { - - if(Character.isWhitespace(c)){ - continue; - } - - if(Character.isDigit(c)) { - - separatedChar.add( String.valueOf(c) ); - - } else { - separatedChar.add("|"); - separatedChar.add( String.valueOf(c) ); - separatedChar.add("|"); - } - } - separatedChar.add("|"); - - System.out.println(separatedChar); - - ArrayList<String> tokens = new ArrayList<String>(); - String temp = ""; - - - for(String c : separatedChar) { - - if(Objects.equals(c, "|")) { - - if(temp != "") { - tokens.add(temp); - } - temp = ""; - } else { - temp = temp + c; - } - } - - System.out.println(tokens); - - Boolean notANumber = false; - - for(String s : tokens) { - - try { - int val = Integer.parseInt(s); - tokenList.add(new Token(TokenType.number, s)); - } catch (NumberFormatException ignored) { - - notANumber = true; - - } - - if(notANumber) { - - if (Character.isLetter( s.charAt(0) )) { - tokenList.add(new Token(TokenType.var, s)); - } else { - tokenList.add(new Token(TokenType.special, s)); - } - notANumber = false; - } - - } - - for(Token t : tokenList) { - System.out.println(t.getTokenString() + " " + t.getTokenType()); - } - - - - /* - - for(int i = 0; i < ausdruck.length(); i++) { - - if(Character.isWhitespace(ausdruck.charAt(i))) { - continue; - } - - - if(typeOfLast == TokenType.number && Character.isDigit(ausdruck.charAt(i)) && i != 0) { - - temp = temp + ausdruck.charAt(i-1); - //charList.add(String.valueOf( ausdruck.charAt(i) )); - - } else { - - if(temp.equals("")) { - - charList.add(String.valueOf( "|" + ausdruck.charAt(i) + "|")); - - } else { - - charList.add(String.valueOf( "|" + temp + "|")); - charList.add(String.valueOf( "|" + ausdruck.charAt(i) + "|")); - - temp = ""; - } - } - - - if(Character.isDigit(ausdruck.charAt(i))) - typeOfLast = TokenType.number; - else if(Character.isLetter(ausdruck.charAt(i))) - typeOfLast = TokenType.var; - else - typeOfLast = TokenType.special; - - } - - System.out.println(charList); - - - /* - for (char c : ausdruck.toCharArray()) { - - if(Character.isWhitespace(c)) { - continue; - } - - - if(typeOfLast == TokenType.number && Character.isDigit(c)) { - System.out.print(c); - } else { - System.out.println(); - System.out.print(c); - } - - if(Character.isDigit(c)) - typeOfLast = TokenType.number; - else if(Character.isLetter(c)) - typeOfLast = TokenType.var; - else - typeOfLast = TokenType.special; - } - */ - - } -} - diff --git a/out/production/inf3_git/main.class b/out/production/inf3_git/main.class index fb751939f861254dfc6a1b06665080572101799a..386650f1dab3e03d3dce9cf08e58cea64d1e5f43 100644 Binary files a/out/production/inf3_git/main.class and b/out/production/inf3_git/main.class differ