#9113: CE,Java 類別名稱請使用 "JAVA",請問哪裡有問題?


highcollege90027 (冰人雪糕)

學校 : 不指定學校
編號 : 41648
來源 : [218.161.27.179]
最後登入時間 :
2021-10-20 18:15:36
a664. 四則運算 | From: [1.164.253.83] | 發表日期 : 2014-08-26 14:10

CE:
第 1 測資點(0%): CE () 
Java 類別名稱請使用 "JAVA" 

程式碼:
import java.util.Scanner;
import java.util.Stack;
import java.util.Arrays;
class Calculator {
public static Long operatorCalculation(char operator, Long top, Long under) {
Long currentSummation = 0L;

if(operator == '+')
{ currentSummation = under + top; }
else if(operator == '-')
{ currentSummation = under - top; }
else if(operator == '*')
{ currentSummation = under * top; }
else
{ currentSummation = under / top; }

return currentSummation;
}
public static String result(String[] postfix) {
Long summation = 0L;
Stack<Long> operandStack = new Stack<Long> ();
Long top, under;

for(int i = 0; i < postfix.length; i++) {
char[] tokenDetermine = postfix[i].toCharArray();

if(StackAndPostfixTest.isOperator(tokenDetermine[0])) {
top = operandStack.pop();
under = operandStack.pop();

operandStack.push(operatorCalculation(tokenDetermine[0], top, under));

if(i == (postfix.length - 1))
{ summation = operandStack.pop(); }
} else {
operandStack.push(Long.parseLong(postfix[i]));
}
}

return Long.toString(summation);
}
}
class InfixToPostfix {
// 優先順序:越小越大
public static int priorities(char operator) {
if(operator == '(')
{ return 2; }
else if(operator == '*' || operator == '/')
{ return 0; }
else
{ return 1; }
}
public static String[] transform(String[] infix) {
String[] tempPostfix = new String[StackAndPostfixTest.postfixLength];
Arrays.fill(tempPostfix, "");
Stack<String> tokensStack = new Stack<String>();
int token = 0;
for(int i = 0; i < infix.length; i++) {
char[] tokenDetermine = infix[i].toCharArray();
if(StackAndPostfixTest.isOperator((tokenDetermine[0]))) {
// 遇到')'彈出堆疊內運算子,直到遇到堆疊內'(',兩者互相抵銷
if(tokenDetermine[0] == ')') {
char[] topOperator = tokensStack.peek().toCharArray();

while(topOperator[0] != '(') {
tempPostfix[token] = tokensStack.pop();
token ++;

topOperator = tokensStack.peek().toCharArray();
}
tokensStack.pop();
continue;
}

if(tokensStack.isEmpty()) {
tokensStack.push(infix[i]);
} else {
char[] topOperator = tokensStack.peek().toCharArray();
if(priorities(tokenDetermine[0]) < priorities(topOperator[0])) {
tokensStack.push(infix[i]);
} else {
if(tokenDetermine[0] == '(') {
tokensStack.push(infix[i]);
} else {
tempPostfix[token] = tokensStack.pop();
token ++;

if(!(tokensStack.isEmpty())) {
topOperator = tokensStack.peek().toCharArray();
while(priorities(tokenDetermine[0]) == priorities(topOperator[0])) {
tempPostfix[token] = tokensStack.pop();
token ++;

if(!(tokensStack.isEmpty())) {
topOperator = tokensStack.peek().toCharArray();
} else {
break;
}
}
}
tokensStack.push(infix[i]);
}
}
}
} else {
tempPostfix[token] = infix[i];
token ++;
}
}
// 堆疊內剩餘的運算子
while(!(tokensStack.isEmpty())) {
tempPostfix[token] = tokensStack.pop();
token ++;
}

return tempPostfix;
}
}
public class StackAndPostfixTest {
public static int postfixLength;
public static boolean isOperator(char c) { // c = eachInputCharacter
if(c == '(' ||
c == ')' ||
c == '+' ||
c == '-' ||
c == '*' ||
c == '/') {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

String input, output;
int m;

// 輸入測資且執行
while(sc.hasNext()) {
m = Integer.parseInt(sc.nextLine());

for(int g = 0; g < m; g++) {
input = sc.nextLine();

// 測量運算式長度 && 後置運算式長度
int operationLength = 0;
postfixLength = 0;
for(int i = 0; i < input.length(); i++) {
if(isOperator(input.charAt(i))) {
operationLength ++;

// postfix
if((input.charAt(i) != '(') && (input.charAt(i) != ')')) {
postfixLength ++;
}
} else {
if(i == input.length() - 1) {
operationLength ++;
postfixLength ++;
continue;
}

if(isOperator(input.charAt(i + 1))) {
operationLength ++;
postfixLength ++;
} else {
continue;
}
}
}

// 中置運算式陣列
String[] infix = new String[operationLength];
Arrays.fill(infix, "");

// 以陣列儲存中置運算式
operationLength = 0;
for(int i = 0; i < input.length(); i++) {
if(isOperator(input.charAt(i))) {
infix[operationLength] = Character.toString(input.charAt(i));
operationLength ++;
} else {
infix[operationLength] += Character.toString(input.charAt(i));

if(i < input.length() - 1) {
if(isOperator(input.charAt(i + 1))) {
operationLength ++;
} else {
continue;
}
}
}
}

// 後置運算式陣列
String[] postfix = Arrays.copyOf(InfixToPostfix.transform(infix), postfixLength);
// System.out.println("Postfix is " + Arrays.toString(postfix));

System.out.println(Calculator.result(postfix));
}
} //next
} // main
}
 

請問哪裡有問題? 

 
#9114: Re:CE,Java 類別名稱請使用


highcollege90027 (冰人雪糕)

學校 : 不指定學校
編號 : 41648
來源 : [218.161.27.179]
最後登入時間 :
2021-10-20 18:15:36
a664. 四則運算 | From: [1.164.253.83] | 發表日期 : 2014-08-26 14:15

用IDE可以正常執行,

但是測試和解題都傳不上去,

請問哪裡需要修改? 

 
ZeroJudge Forum