Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我需要在我的中缀到后缀程序中有一些错误消息_Java_Postfix Notation_Infix Notation_Custom Error Handling - Fatal编程技术网

Java 我需要在我的中缀到后缀程序中有一些错误消息

Java 我需要在我的中缀到后缀程序中有一些错误消息,java,postfix-notation,infix-notation,custom-error-handling,Java,Postfix Notation,Infix Notation,Custom Error Handling,因此,我编译并运行了我的程序,但如果有人输入,我必须捕获一些错误:左括号的数量与右括号的数量不一致;连续出现2个操作数2个运算符连续出现;中缀表达式中遇到非法字符左括号或右括号立即出现我还需要一个指示器,用于将用户指向检测到错误的位置。我不知道如何指示错误的位置并将其显示给用户,如果您能告诉我如何操作,我将不胜感激 import java.util.*; import java.io.*; public class InfixToPostfix { private ArrayStack<

因此,我编译并运行了我的程序,但如果有人输入,我必须捕获一些错误:左括号的数量与右括号的数量不一致;连续出现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; }
   }
import java.util.*;
导入java.io.*;
公共类InfixToPostfix{
专用阵列堆栈;
私有字符串中缀;
私有字符串后缀=”;
私有字符topSymb;
私营企业;
专用阵列堆栈evalStack;
私有int结果=0;
/**************************************************************** 
*模块名称:InfixToPostfix
* 
*模块描述:将初始化
*通过读取字符串的长度来创建堆栈
* 
*@Param newInfix String是要计算的中缀表达式
* 
*****************************************************************/
公共InfixToPostfix(字符串newInfix){
中缀=新中缀;
int stackSize=infix.length();
堆栈=新阵列堆栈(堆栈大小);
}
/**************************************************************** 
*模块名称:convertInfixString
* 
*模块描述:它将按每个字符遍历一个字符串
*并合并操作数,直到到达运算符,然后
*将运算符推到堆栈并重新排列中缀表达式
*变成后缀表达式
* 
*@返回后缀表达式中的字符串
*****************************************************************/
公共字符串转换infixstring(){
试一试{
对于(int i=0;i