[[Javaセミナー2014/阪口政徳/宿題]
20141217夕方に桁数チェックを中心に大幅修正しました。符号抜きの整数部分と小数部分の桁数が15桁以内かどうかを入力・計算結果双方でチェックするように替えています。
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 currentOperator = "+";
static BigDecimal currentResult = new BigDecimal(0);
static boolean isNotEndOfWhile = 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_Operator_Q_INPUT_COMMENT = "数字・演算子・qを入力してください。";
static final String NUMBER_Operator_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 Operator_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 (isNotEndOfWhile) {
makeValidation();
}
} 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 makeValidation()
throws NumberFormatException, IOException,
ArithmeticException, PatternSyntaxException {
if (currentData == null || currentData.isEmpty()) {
System.out.println(NUMBER_Operator_Q_INPUT_COMMENT);
readLineInputData();
} else if (currentData.matches("^[qQ]$")) {
System.out.println(END_COMMENT);
isNotEndOfWhile = false;
return;
} else if (currentData.matches("^\\=$")) {
System.out.println(" = " + currentResult.toPlainString());
currentResult = new BigDecimal(0.0);//値のリセット
readLineInputData();
} else if (currentData.matches("^[\\+\\-]?\\d+(\\.\\d+)?$")) {
calculate();
readLineInputData();
} else if(currentData.matches("^[\\+\\-\\*/]$")){
currentOperator = currentData;
readLineInputData();
} else {
System.out.println(NUMBER_Operator_Q_INPUT_COMMENT);
readLineInputData();
}
}
private static void readLineInputData() throws IOException{
System.out.print(currentResult.toPlainString() + " " + currentOperator + " ");
currentData = br.readLine();
currentData = currentData.trim();
}
private static boolean isDigitAvialable (String examData) {
BigDecimal bigDecimalExamData = new BigDecimal(examData);
String strBDExamData = bigDecimalExamData.toPlainString().replaceFirst("[\\+\\-]", "");
int totalLength = strBDExamData.length();
int integerLength = 0;
int fractionLength = 0;
integerLength = strBDExamData.indexOf(".") == -1 ? totalLength : strBDExamData.indexOf(".");
fractionLength = strBDExamData.indexOf(".") == -1 ? 0 : totalLength - integerLength - 1;
if (integerLength <= MAX_DIGIT && fractionLength <= MAX_DIGIT) {
return true;
} else {
System.out.println(SITEIKETASU_COMMENT + "(" + MAX_DIGIT + ")" + OVER_COMMENT);
System.out.println(SAIDO_INPUT_COMMENT);
return false;
}
}
private static void calculate ()
throws NumberFormatException, IOException, ArithmeticException{
BigDecimal temp_currentResult = currentResult;
BigDecimal bigDecimalCurrentData = new BigDecimal(currentData);
if(!isDigitAvialable(currentData)){
return;
}
switch (currentOperator.charAt(0)){
case '+':
temp_currentResult = currentResult.add(bigDecimalCurrentData);
break;
case '-':
temp_currentResult = currentResult.subtract(bigDecimalCurrentData);
break;
case '*':
temp_currentResult = currentResult.multiply(bigDecimalCurrentData);
break;
case '/':
Double doubleCurrentData = Double.parseDouble(currentData);
if (doubleCurrentData == 0) {
System.out.println(NUMBER_Operator_Q_INPUT_COMMENT_FOR_DIVIDE);
return;
} else {
try{
temp_currentResult = currentResult.divide(bigDecimalCurrentData);
}catch(ArithmeticException aEx){
temp_currentResult = currentResult.divide(bigDecimalCurrentData,
MAX_DIGIT ,
BigDecimal.ROUND_HALF_UP);
}
}
break;
default:
throw new IOException();
}
if(isDigitAvialable(temp_currentResult.toPlainString())) {
currentResult = temp_currentResult;
}
}
}