From 1fd797520e44755bc636c8bfed97d386a2479cc1 Mon Sep 17 00:00:00 2001 From: qwertzniki6 <104077966+bretzNiklas@users.noreply.github.com> Date: Thu, 22 Dec 2022 17:26:07 +0100 Subject: [PATCH] Aufgabe 1 geschafft --- Aufgabe4/Token.java | 28 ++++++++ Aufgabe4/TokenType.java | 1 + Aufgabe4/main.java | 154 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 183 insertions(+) create mode 100644 Aufgabe4/Token.java create mode 100644 Aufgabe4/TokenType.java create mode 100644 Aufgabe4/main.java diff --git a/Aufgabe4/Token.java b/Aufgabe4/Token.java new file mode 100644 index 0000000..137bef2 --- /dev/null +++ b/Aufgabe4/Token.java @@ -0,0 +1,28 @@ +public class Token { + + TokenType tokenType; + String tokenString; + + Token(TokenType tokenType, String tokenString) { + + this.tokenType = tokenType; + this.tokenString = tokenString; + + } + + public TokenType getTokenType() { + return tokenType; + } + + public void setTokenType(TokenType tokenType) { + this.tokenType = tokenType; + } + + public String getTokenString() { + return tokenString; + } + + public void setTokenString(String tokenString) { + this.tokenString = tokenString; + } +} diff --git a/Aufgabe4/TokenType.java b/Aufgabe4/TokenType.java new file mode 100644 index 0000000..5e4da31 --- /dev/null +++ b/Aufgabe4/TokenType.java @@ -0,0 +1 @@ +public enum TokenType { number, var, special } diff --git a/Aufgabe4/main.java b/Aufgabe4/main.java new file mode 100644 index 0000000..c42fee5 --- /dev/null +++ b/Aufgabe4/main.java @@ -0,0 +1,154 @@ +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; + } + */ + + } +} + -- GitLab