문제

I am working on a program that solves arithmetic equations.

An example of an equation would be:

4+(5/4)+3/((5/3-2.4(*(5-7))

I have a program that converts this equation from infix to postfix form but the program is messing up with the decimal in 2.4.

It is treating 2 and 4 as separate numbers where they should be treated as one number. How would I solve this problem?

import java.util.EmptyStackException;
import java.util.Scanner;
import myUtil.*;

public class InfixToPostfix extends Asg6
{
   public static class SyntaxErrorException extends Exception
   {
      SyntaxErrorException(String message)
      {
         super(message);
      }
   }
   private AStack<Character> operatorStack;
   private static final String operators = "+-*/^()";
   private static final int[] precedence =
   {
      1, 1, 2, 2, 3, -1, -1
   };
   private StringBuilder postfix;

   public String convert(String infix) throws SyntaxErrorException
   {
      operatorStack = new AStack<Character>();
      postfix = new StringBuilder();

      try
      {
         String nextToken;
         Scanner scan = new Scanner(infix);
         while ((nextToken = scan.findInLine("[\\p{L}\\p{N}]+|[-+/\\*^()]")) != null)
         {
            char firstChar = nextToken.charAt(0);
            if (Character.isJavaIdentifierStart(firstChar) || Character.isDigit(firstChar))
            {
               postfix.append(nextToken);
               postfix.append(' ');
            }
            else if (isOperator(firstChar))
            {
               processOperator(firstChar);
            }
            else
            {
               throw new SyntaxErrorException("Unexpected Character: " + firstChar);
            }
         }
         while (!operatorStack.empty())
         {
            char op = operatorStack.pop();
            if (op == '(')
            {
               throw new SyntaxErrorException("Unmatched parenthesis");
            }
            postfix.append(op);
            postfix.append(' ');
         }
         return postfix.toString();
      }
      catch (EmptyStackException ex)
      {
         throw new SyntaxErrorException("Syntax Error: The stack is empty");
      }
   }

   private void processOperator(char op)
   {
      if (operatorStack.empty() || op == '(')
      {
         operatorStack.push(op);
      }
      else
      {
         char topOp = operatorStack.peek();
         if (precedence(op) > precedence(topOp))
         {
            operatorStack.push(op);
         }
         else
         {
            while (!operatorStack.empty() && precedence(op) <= precedence(topOp))
            {
               operatorStack.pop();
               if (topOp == '(')
               {
                  break;
               }
               postfix.append(topOp);
               postfix.append(' ');
               if (!operatorStack.empty())
               {
                  topOp = operatorStack.peek();
               }
            }
            if (op != ')')
            {
               operatorStack.push(op);
            }
         }
      }
   }

   private boolean isOperator(char ch)
   {
      return operators.indexOf(ch) != -1;
   }

   private int precedence(char op)
   {
      return precedence[operators.indexOf(op)];
   }
}
도움이 되었습니까?

해결책

This:

while((nextToken = scan.findInLine("[\\p{L}\\p{N}]+|[-+/\\*^()]")) != null)

will match a string of letters or a string of numeric characters, or one of the punctuation characters; but obviously you want something more complex than a simple string of numeric characters. The following will also match a string of 1 or more numeric characters, followed by a dot, followed by another string of 1 or more numeric characters:

while((nextToken = scan.findInLine("\\p{L}+|\\p{N}+(\\.\\p{N}+)?|[-+/\\*^()]")) != null)

The part in parentheses matches a dot and one or more digits; the ? after the group means that it's a match if it finds one of these or if it doesn't (i.e. 0 or 1 occurrences of the sub-pattern).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top