2017-02-21 11 views
0

私はこの割り当てに固執しています。私はファイルを読んでおり、各行で私はcheckInfix静的メソッドを実行しています。 checkInfixメソッドの実行後、私は読んでいる中置行の計算を返すべきです。たとえば、最初は5 * 6 + 4 = 34です。そうでなければ「無効」と言います。しかし、あなたが見ているように、いくつかの行についてこのプログラムの出力は間違っています。 私の出力は、それが34インフィニティコードの出力が正しくありません

= 34

3印刷するので、これが正しい=


5 * 6 + 4 < ....以下である - 2 + < =正解

無効

(3 * 4 - (2 + 5))4月2日<は=これは、印刷する必要*、回答の10が、doesntの..pri NTS無効

無効

10 + 6 * 11 - (* 2 3 + 14)/ 2 < =この1つは、回答66を印刷する必要がありますが無効

無効

をdoesntの..prints

2 *(12 +(3 + 5)×2 = <これが正しい

ある無効

無効


ありがとうございました! code ::::>そこには何もなかったので、私は主な機能を投稿しませんでした。もしあなたがそれを見たいなら、私はそれを掲示することができます。

public static int checkInfix(String inf) 
{ 
    char[] c = inf.toCharArray(); 
    Stack<Integer> into = new Stack<Integer>(); 
    Stack<Character>charo = new Stack<Character>(); 


    for (int i = 0; i < c.length; i++) 
    { 

     if (c[i] == ' ' || c[i]==',') 
      continue; 

     if (iOperand(c[i])){ // checking for operand 0 to 9 
      StringBuffer z = new StringBuffer(); 
      while (i < c.length && c[i] >= '0' && c[i] <= '9') 
       z.append(c[i++]); 
      into.push(Integer.parseInt(z.toString())); 
     } 
     else if (c[i] == '(') 
      charo.push(c[i]); 

     else if (c[i] == ')') 
     { 
      while (!charo.empty()&& charo.peek() != '(') 
       into.push(calucator(charo.pop(), into.pop(), into.pop())); 
      charo.pop(); 
     } 

     else if (iOperator(c[i])){ // checking for operator +,-,*,/ 
      while (!charo.empty() && HigerPreced(c[i],charo.peek())) 
       into.push(calucator(charo.pop(), into.pop(), into.pop())); 
      charo.push(c[i]); 
     } 
    }//end of for loop 

    while (!charo.empty()) 
     into.push(calucator(charo.pop(), into.pop(), into.pop())); 
    return into.pop(); 
}//end of checkinfix class 



public static boolean iOperator(char C) { 
    if (C == '+' || C == '-' || C == '*' || C == '/' || C == '%') 
     return true; 
    return false; 
} 
public static boolean iOperand(char C){ 
    if (C >= '0' && C<='9') 
     return true; 
    return false; 
} 
//check for precedence 
public static boolean HigerPreced(char oprt1,char oprt2){ 
    if (oprt2 == ')' || oprt2 == '(')return false; 
    if ((oprt1 == '*'|| oprt1=='/')&&(oprt1=='+'||oprt1=='-'))return false; 
    else 
     return true; 
} 
//calculating 
public static int calucator(char into, int oprnd1, int oprnd2) 
{ 
    switch(into) 
    { 
    case '+': return oprnd1 + oprnd2; 
    case '-': return oprnd1 - oprnd2; 
    case '*': return oprnd1 * oprnd2; 
    case '/': return oprnd1/oprnd2; 
    } 
    return 0; 
} 
+0

2 *(12 +(3 + 5)* 2 <=これが正しいかどうかは、閉じ括弧が欠けているとは思わないか)最初の考え方 – VinayVeluri

+0

閉じておく必要がある最初の開き括弧がないため – calo

答えて

0
(oprt1 == '*'|| oprt1=='/')&&(oprt1=='+'||oprt1=='-') 

oprt2この式に表示されるはずです...現時点ではそれが本当であることはできません。

+0

私はそれを試みたが、それでも私は同じ出力を与える。 – calo

関連する問題