[[Javaセミナー2014/阪口政徳/宿題]
import java.io.*;
import java.lang.*;
import java.math.*;
import java.util.regex.PatternSyntaxException;
import java.util.regex.*;
public class Calc {
static String currentData = "";
static String currentOperation = "+";
static BigDecimal currentResult = new BigDecimal(0);
static boolean isEofWhile = true;
static BufferedReader br = null;
/*定数*/
static final int MAX_DIGIT = 15;
static final String START_COMMENT = "計算機アプリを開始します。";
static final String NUMBER_INPUT_COMMENT = "数字を入力してください:";
static final String NUMBER_OPERATION_Q_INPUT_COMMENT = "数字・演算子・qを入力してください。";
static final String NUMBER_OPERATION_Q_INPUT_COMMENT_FOR_DIVIDE = "0以外の数字・演算子・qを入力してください。";
static final String SITEIKETASU_COMMENT = "最大桁数";
static final String OVER_COMMENT = "をオーバーしています。";
static final String SAIDO_INPUT_COMMENT = "もう1度入力してください。";
static final String END_COMMENT = "計算機アプリを終了します。";
static final String RESET_COMMENT = "値をリセットしました。";
static final String OPERATION_CHANGE_COMMENT = "演算子を変更します。";
static final String ZERO_DIVIDE_COMMENT = "0で除算しようとしました。";
public static void main (String[] args) throws IOException, NumberFormatException {
try {
br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(START_COMMENT);
System.out.print(NUMBER_INPUT_COMMENT);
currentData = br.readLine();
currentData = currentData.trim();
while (isEofWhile) {
validateAndCalc();
}
}catch (NumberFormatException nEx) {
System.out.println(nEx.toString());
}catch (ArithmeticException aEx) {
System.out.println(aEx.toString());
}catch (PatternSyntaxException pEx) {
System.out.println(pEx.toString());
} finally {
if(br != null){
br.close();
}
}
}
private static void validateAndCalc()
throws NumberFormatException, IOException,
ArithmeticException, PatternSyntaxException {
if (currentData == null || currentData.isEmpty()) {
System.out.println(NUMBER_OPERATION_Q_INPUT_COMMENT);
readLineInputData();
} else if (isDigitNotAvailable()) {
System.out.println(SITEIKETASU_COMMENT + "(" + MAX_DIGIT + ")" + OVER_COMMENT);
System.out.println(SAIDO_INPUT_COMMENT);
readLineInputData();
} else if (currentData.matches("^[qQ]$")) {
System.out.println(END_COMMENT);
isEofWhile = false;
return;
} else if (currentData.matches("^\\=$")) {
System.out.println(" = " + currentResult.toPlainString());
currentResult = new BigDecimal(0.0);//値のリセット
//System.out.println(RESET_COMMENT);
readLineInputData();
} else if (currentData.matches("^[\\+\\-]?\\d+(\\.\\d+)?$")) {
calculate();
readLineInputData();
} else if(currentData.matches("^[\\+\\-\\*/]{1,}$")){
if(!currentOperation.equals(currentData)) {
//System.out.println(OPERATION_CHANGE_COMMENT);
}
currentOperation = currentData;
readLineInputData();
} else {
System.out.println(NUMBER_OPERATION_Q_INPUT_COMMENT);
readLineInputData();
}
}
private static boolean isDigitNotAvailable(){
if(currentData != null && !currentData.isEmpty()){
return currentData.length() > MAX_DIGIT;
} else {
return false;
}
}
private static void readLineInputData() throws IOException{
System.out.print(currentResult.toPlainString() + " " + currentOperation + " ");
currentData = br.readLine();
currentData = currentData.trim();
}
private static void calculate ()
throws NumberFormatException, IOException, ArithmeticException{
boolean isOk = false;
BigDecimal temp_currentResult = new BigDecimal(0);
BigDecimal convertedCurrentData = new BigDecimal(currentData);
switch (currentOperation.charAt(0)){
case '+':
temp_currentResult = currentResult.add(convertedCurrentData);
isOk = true;
break;
case '-':
temp_currentResult = currentResult.subtract(convertedCurrentData);
isOk = true;
break;
case '*':
temp_currentResult = currentResult.multiply(convertedCurrentData);
isOk = true;
break;
case '/':
Double doubleCurrentData = Double.parseDouble(currentData);
if (doubleCurrentData == 0) {
//System.out.println(ZERO_DIVIDE_COMMENT);
System.out.println(NUMBER_OPERATION_Q_INPUT_COMMENT_FOR_DIVIDE);
} else {
try{
temp_currentResult = currentResult.divide(convertedCurrentData);
}catch(ArithmeticException aEx){
temp_currentResult = currentResult.divide(convertedCurrentData,
MAX_DIGIT - 1,
BigDecimal.ROUND_HALF_UP);
}
isOk = true;
}
break;
default:
throw new IOException();
}
String str = temp_currentResult.toPlainString();
String regex = "[\\-\\.]";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
String result = m.replaceAll("");
if(isOk && (result.length() > MAX_DIGIT)) {
System.out.println("計算結果が" + SITEIKETASU_COMMENT + "(" + MAX_DIGIT + ")" + OVER_COMMENT);
System.out.println(SAIDO_INPUT_COMMENT);
} else if(isOk) {
currentResult = temp_currentResult;
}
}
}