[Java][VB.NET] Übersetzung von Do Until

  • VB.NET

Es gibt 7 Antworten in diesem Thema. Der letzte Beitrag () ist von ThePlexian.

    [Java][VB.NET] Übersetzung von Do Until

    Hi,

    Ich möchte gerade meinen Parser in Java umschreiben um ihn auch dort nutzen zu können. Soweit funktioniert auch alles bis auf eine Schleife:

    VB.NET-Quellcode

    1. Do Until operatorStack.Peek.Type = TokenType.Function OrElse operatorStack.Peek.Content = "(" OrElse operatorStack.Count = 0
    2. outputList.Add(operatorStack.Pop)
    3. Loop

    Java-Quellcode

    1. do {
    2. outputlist.add(operatorstack.pop());
    3. } while (!(operatorstack.peek().Type==Token.TokenType.Function|| operatorstack.peek().Content.equals("(") || operatorstack.size()==0));


    Verhält sich vollkommen unterschiedlich, allerdings ist das doch das selbe, oder?

    Wenn jemand der Meinung ist, dass es am Rest des Algos liegt der kann auch mal einen Blick darauf werfen:

    VB.NET-Quellcode

    1. Private Function ShuntingYard(ByVal tokens As List(Of Token)) As List(Of Token)
    2. Dim outputList As New List(Of Token)
    3. Dim operatorStack As New Stack(Of Token)
    4. For Each tk As Token In token
    5. If tk.Type = TokenType.Number Then
    6. outputList.Add(tk)
    7. Continue For
    8. End If
    9. If tk.Type = TokenType.Function OrElse tk.Content = "(" Then
    10. operatorStack.Push(tk)
    11. Continue For
    12. End If
    13. If tk.Content = ")" Then
    14. Do Until operatorStack.Peek.Type = TokenType.Function OrElse operatorStack.Peek.Content = "(" OrElse operatorStack.Count = 0
    15. outputList.Add(operatorStack.Pop)
    16. Loop
    17. If operatorStack.Peek.Content <> "(" Then
    18. outputList.Add(operatorStack.Pop)
    19. Else
    20. operatorStack.Pop()
    21. End If
    22. Continue For
    23. End If
    24. If tk.Type = TokenType.Operator AndAlso operatorStack.Count = 0 Then
    25. operatorStack.Push(tk)
    26. Continue For
    27. End If
    28. If operatorStack.Count > 0 AndAlso tk.Type = TokenType.Operator Then
    29. While operatorStack.Count > 0 AndAlso tk.EvaluatesBefore(operatorStack.Peek) AndAlso operatorStack.Peek.Content <> "(" AndAlso operatorStack.Peek.Type <> TokenType.Function
    30. outputList.Add(operatorStack.Pop)
    31. End While
    32. operatorStack.Push(tk)
    33. End If
    34. Next
    35. While operatorStack.Count > 0
    36. outputList.Add(operatorStack.Pop)
    37. End While
    38. Return outputList
    39. End Function



    Java-Quellcode

    1. public static ArrayList<Token> ShuntingYard(ArrayList<Token> tokens){
    2. ArrayList<Token> outputlist = new ArrayList<Token>();
    3. Stack<Token> operatorstack = new Stack<Token>();
    4. for (Token tk : tokens ) {
    5. if (tk.Type==Token.TokenType.Number) {
    6. outputlist.add(tk);
    7. continue;
    8. }
    9. if (tk.Type==Token.TokenType.Function || tk.Content.equals("(")) {
    10. operatorstack.push(tk);
    11. continue;
    12. }
    13. if (tk.Content.equals(")")) {
    14. do {
    15. outputlist.add(operatorstack.pop());
    16. } while (!(operatorstack.peek().Type==Token.TokenType.Function|| operatorstack.peek().Content.equals("(") || operatorstack.size()==0)); // end of do-while
    17. if (!operatorstack.peek().Content.equals("(")) {
    18. outputlist.add(operatorstack.pop());
    19. }else{
    20. operatorstack.pop() ;
    21. }
    22. continue;
    23. }
    24. if (tk.Type==Token.TokenType.Operator && operatorstack.size() == 0) {
    25. operatorstack.push(tk);
    26. continue;
    27. }
    28. if (operatorstack.size() >0 && tk.Type==Token.TokenType.Operator) {
    29. while (operatorstack.size()>0 && tk.EvaluatesBefore(operatorstack.peek()) && !operatorstack.peek().Content.equals("(") && operatorstack.peek().Type==Token.TokenType.Function) {
    30. outputlist.add(operatorstack.pop());
    31. }
    32. operatorstack.push(tk);
    33. }
    34. }
    35. while (operatorstack.size()>0) {
    36. outputlist.add(operatorstack.pop());
    37. }
    38. return outputlist;
    39. }


    Vielen Dank für eure Hilfe :D

    8-) faxe1008 8-)
    Until ist nur While mit umgekehrter Bedingung:

    VB.NET-Quellcode

    1. Dim a = 0
    2. Do Until a > 10
    3. a += 1
    4. Loop
    'Selbes wie

    VB.NET-Quellcode

    1. Dim a = 0
    2. Do While Not a > 10
    3. a += 1
    4. Loop
    Das:

    Java-Quellcode

    1. do
    2. {
    3. // ...
    4. }
    5. while (Condition);
    ist das selbe wie das in VB:

    VB.NET-Quellcode

    1. Do
    2. '...
    3. Loop While Condition
    "Luckily luh... luckily it wasn't poi-"
    -- Brady in Wonderland, 23. Februar 2015, 1:56
    Desktop Pinner | ApplicationSettings | OnUtils
    Ich denke, dass das Problem ist, dass einmal eine Schleife mit Eingangsbedingung und einmal mit AusgangBedingzng genutzt wird.
    »There's no need to "teach" atheism. It's the natural result of education without indoctrination.« — Ricky Gervais
    Vielen Dank euch beiden habe den Fehler gefunden :D . Hier der korregierte Algo:

    Quellcode

    1. public static ArrayList<Token> ShuntingYard(ArrayList<Token> tokens){
    2. ArrayList<Token> outputlist = new ArrayList<Token>();
    3. Stack<Token> operatorstack = new Stack<Token>();
    4. for (Token tk : tokens ) {
    5. if (tk.Type==Token.TokenType.Number) {
    6. outputlist.add(tk);
    7. continue;
    8. }
    9. if (tk.Type==Token.TokenType.Function || tk.Content.equals("(")) {
    10. operatorstack.push(tk);
    11. continue;
    12. }
    13. if (tk.Content.equals(")")) {
    14. while (!operatorstack.empty() && (operatorstack.peek().Type!=Token.TokenType.Function || operatorstack.peek().Content.equals("("))) {
    15. outputlist.add(operatorstack.pop());
    16. }
    17. if (!operatorstack.empty() ) {
    18. if (operatorstack.peek().Type==Token.TokenType.Function) {
    19. outputlist.add(operatorstack.pop());
    20. }else{
    21. operatorstack.pop();
    22. }
    23. }
    24. continue;
    25. }
    26. if (tk.Type==Token.TokenType.Operator && operatorstack.size() == 0) {
    27. operatorstack.push(tk);
    28. continue;
    29. }
    30. if (operatorstack.size() >0 && tk.Type==Token.TokenType.Operator) {
    31. while (operatorstack.size()>0 && tk.EvaluatesBefore(operatorstack.peek())) {
    32. if (!operatorstack.peek().Content.equals("(") && operatorstack.peek().Type!=Token.TokenType.Function) {
    33. outputlist.add(operatorstack.pop());
    34. }else{
    35. break;
    36. }
    37. }
    38. operatorstack.push(tk);
    39. }
    40. }
    41. while (operatorstack.size()>0) {
    42. outputlist.add(operatorstack.pop());
    43. }
    44. return outputlist;
    45. }

    8-) faxe1008 8-)
    Ich habe erneut ein Übersetzungsproblem und finde einfach nicht den Fehler :/ :

    VB.NET-Quellcode

    1. If tk.Type = TokenType.ClosingBracket Then
    2. Dim seperatorcounter As Integer = 0
    3. Do Until operatorstack.Peek.Type = TokenType.MultivariateFunction OrElse operatorstack.Peek.Type = TokenType.Function OrElse operatorstack.Peek.Content = "(" OrElse operatorstack.Count = 0
    4. If Not operatorstack.Peek.Type = TokenType.Seperator Then
    5. outputlist.Add(operatorstack.Pop)
    6. Else
    7. operatorstack.Pop()
    8. seperatorcounter += 1
    9. End If
    10. Loop
    11. If operatorstack.Peek.Type = TokenType.MultivariateFunction Then
    12. operatorstack.Peek.Content = operatorstack.Peek.Content & (seperatorcounter + 1).ToString
    13. End If
    14. If operatorstack.Peek.Content <> "(" Then
    15. outputlist.Add(operatorstack.Pop)
    16. Else
    17. operatorstack.Pop()
    18. End If
    19. Continue For
    20. End If


    Java-Quellcode

    1. if (tk.Type==Token.TokenType.ClosingBracket) {
    2. int seperator = 0;
    3. while ( !( operatorstack.empty() || operatorstack.peek().Content.equals("(") || operatorstack.peek().Type==Token.TokenType.MultivariateFunction || operatorstack.peek().Type==Token.TokenType.Function )) {
    4. if (operatorstack.peek().Type!=Token.TokenType.Seperator) {
    5. outputlist.add(operatorstack.pop() );
    6. }else{
    7. operatorstack.pop();
    8. seperator++;
    9. } // end of if
    10. }
    11. if (!operatorstack.empty() && !operatorstack.peek().Content.equals("(")) {
    12. outputlist.add(operatorstack.pop() );
    13. }else{
    14. operatorstack.pop();
    15. } // end of if
    16. continue;
    17. }

    8-) faxe1008 8-)
    Zeile 19 im VB-Code stimmt nicht mit Zeile 14 im Java-Code zusammen.
    If operatorstack.Peek.Content <> "(" Then
    Müsste nur folgendes sein:
    if (!operatorstack.peek().Content.equals("(")) {
    Also !operatorstack.empty() && kommt weg.

    Und das kommt im Java-Code gar nicht mehr vor:

    VB.NET-Quellcode

    1. If operatorstack.Peek.Type = TokenType.MultivariateFunction Then
    2. operatorstack.Peek.Content = operatorstack.Peek.Content & (seperatorcounter + 1).ToString
    3. End If
    "Luckily luh... luckily it wasn't poi-"
    -- Brady in Wonderland, 23. Februar 2015, 1:56
    Desktop Pinner | ApplicationSettings | OnUtils
    Du hast in Zeile 3 des Java Codes
    while ( !(bool || bool || bool || bool))
    das ist in VB.Net, wenn du while nutzen würdest, das gleiche.
    Doch du nutzt "Until", also hast d richtigerweise das "!" bzw. das "Not"" weggelassen, aber nach der boolschen Algebra müssen alle "||" zu "&&" werden, bzw. deine "OrElse" zud "AndAlso".


    Was ein Schwachsinn, sry.
    »There's no need to "teach" atheism. It's the natural result of education without indoctrination.« — Ricky Gervais