我需要在我的Infix到Postfix程序中输入一些错误消息

时间:2013-09-23 17:13:45

标签: java postfix-notation infix-notation custom-error-handling

所以我已经编译并运行了我的程序,但是如果有人输入我必须发现一些错误:左括号的数量与右括号的数量不一致; 2个操作数相继出现; 2位运营商相继出现;在中缀表达中遇到非法字符; 立即显示左括号或右括号我还需要一个指示器,将用户定向到检测到错误的位置。我不知道如何指出错误的位置并将其显示给用户所以非常感谢如果你能告诉我怎么做。

import java.util.*;
import java.io.*;

public class InfixToPostfix {
private ArrayStack<Character> stack ;
private String infix;
private String postfix= "";
    private char topSymb;
    private char topStk;
    private ArrayStack<Integer> evalStack;
    private int result = 0;

    /**************************************************************** 
    * Module Name: InfixToPostfix
    * 
    * Module Description: Constructer that will initate the size of
    * the stack by reading the strings length
    * 
    * @Param newInfix String is the infix expression to be evaluated 
    * 
    *****************************************************************/
public InfixToPostfix(String newInfix) {
        infix = newInfix;
        int stackSize = infix.length();
        stack = new ArrayStack(stackSize);
}
    /**************************************************************** 
    * Module Name: convertInfixString
    * 
    * Module Description: It will go through a string by each character 
    * and combine operands until it reaches an operator, then it will 
    * push the operator to a stack and rearrange the infix expression
    * into a postfix expression
    * 
    * @Return String that is in postfix expression
    *****************************************************************/
public String convertInfixString (){
        try{
            for (int i = 0; i < infix.length(); i++) {
                char ch = infix.charAt(i);
                if (Character.isDigit(ch)) {
                    postfix = postfix + ch;     
                }
        //check for parenthesis

                else if (ch == ')'){
                    postfix = postfix +' ';
                    while (!stack.isEmpty() && stack.peek() != '('){
                        topStk = stack.pop();                            
                        postfix = postfix + topStk;
                        postfix = postfix + ' ';
                    }
                    if (!stack.isEmpty()) {
                        topStk = stack.pop();                            
                    }
                    else 
                        System.out.println("No left parenthasis try again if you                    want");
                        break;
                }
                else {
                    if(ch != '(' || topStk != '(')
                        postfix = postfix + ' ';
                    while (!stack.isEmpty() &&(precident(stack.peek(),ch)) 
                            && stack.peek()!= '('){
                        topStk = stack.pop();
                        if (topStk != '(') {
                            postfix = postfix + topStk;
                            postfix = postfix + ' ';
                        }
                    }
                    stack.push(ch);
                }
            }
            while(!stack.isEmpty()){
                topStk = stack.pop();
                postfix = postfix + ' ';
                postfix = postfix + topStk;
                }                
        }    
        catch (StackIsEmptyException e) {
            e.printStackTrace();
        }
        return postfix; 
    }

/**************************************************************** 
    * Module Name: evaluate
    * 
    * Module Description: Evaluates the specified postfix expression. 
    * If an operand encountered, it is pushed onto the stack. If an 
    * operator is encountered, two operands are popped, the operation is
* evaluated, and the result is pushed onto the stack.
    * 
    * @Param expr String representation of a postfix expression
    * 
    * @Return int value of the given expression
    *****************************************************************/
    public int evaluate (String expr)
    {
        int evalStackSize = expr.length(); 
        evalStack = new ArrayStack(evalStackSize);
        try{
        int op1, op2 = 0;
        String token;
        StringTokenizer tokenizer = new StringTokenizer (expr, " ");
        while (tokenizer.hasMoreTokens())
        {
            token = tokenizer.nextToken();
            if (isOperator(token))
            {
                op2 = (evalStack.pop()).intValue();
                op1 = (evalStack.pop()).intValue();
                result = evalSingleOp (token.charAt(0), op1, op2);
                evalStack.push (new Integer(result));
            }
            else
                evalStack.push (new Integer(Integer.parseInt(token)));
        }
        }
        catch(StackIsEmptyException e){
            e.printStackTrace();
        }
    return result;
    }
    /**************************************************************** 
    * Module Name: 
    * 
    * Module Description: Determines if the specified token is an operator.
    * 
    * @Param token String representing a single token
    * 
    * @Return boolean true if token is operator
    *****************************************************************/
private boolean isOperator (String token)
{
return ( token.equals("+") || token.equals("-") ||
token.equals("*") || token.equals("/") );
}
    /**************************************************************** 
    * Module Name: evalSingleOp
    * 
    * Module Description: Performs integer evaluation on a single 
    * expression consisting of the specified operator and operands.
    * 
    * @param operation operation to be performed
* @param op1 the first operand
* @param op2 the second operand
    * 
    * @Return int value of the expression
    *****************************************************************/
private int evalSingleOp (char operation, int op1, int op2)
{
    int result = 0;
    switch (operation)
{
    case '+':
        result = op1 + op2;
        break;
    case '-':
        result = op1 - op2;
        break;
    case '*':
        result = op1 * op2;
        break;
    case '/':
        result = op1 / op2;
}
    return result;
}

/**************************************************************** 
    * Module Name: 
    * 
    * Module Description: 
    * 
    * @param ch 
    * 
    * @Return
    *****************************************************************/
private boolean isOperator(char ch) { 
    if(ch=='/'||ch=='*'||ch=='+'||ch=='-' || ch==')' || ch == '(') 
        return true; 
    else 
        return false; 
    }

    /**************************************************************** 
    * Module Name: precident
    * 
    * Module Description: Determines whether one operator has a higher
    * precident to preform
    * 
    * @param one
    * @param two
    * 
    * @return boolean true if the operator has higher precident false
    * if not
    *****************************************************************/
private boolean precident(char one, char two) {
    if(charPrec(one) >= charPrec(two))
        return true;
        else
            return false;
}
    /**************************************************************** 
    * Module Name: charPrec
    * 
    * Module Description: Gives a value to each operator for which has 
    * higher precident
    * 
    * @param ch
    * 
    * @return int of a value corresponding the operator
    *****************************************************************/
private int charPrec(char ch) { 
    switch(ch) { 
        case '-':
            return 1; 
        case '+':
            return 1; 
        case '*':
            return 2; 
        case '/':
            return 2;
        case '(':
            return 3;
        case ')':
            return 3;
    } 
    return 0; }
   }

0 个答案:

没有答案