[[Javaセミナー2014/阪口政徳/宿題20141213]
20141217 ※夕方に桁数チェックを中心に大幅修正しました。符号抜きの整数部分と小数部分の桁数が15桁以内かどうかを入力・計算結果双方でチェックするように替えています。割り算で割り切れるときの処理に障害があり。 ※割り算・掛け算の障害対応を試みました(20:25)
import java.io.*;
import java.lang.*;
import java.math.*;
import java.util.regex.PatternSyntaxException;
import java.util.regex.*;
enum DigitStatus {
fractionOnlyNG, OK, NG
}
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 (IOException iEx) {
System.out.println(iEx.toString());
} 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.print(END_COMMENT);
isNotEndOfWhile = false;
return;
} else if (currentData.matches("^\\=$")) {
System.out.println("= " + currentResult.toPlainString());
currentResult = new BigDecimal(0.0);//値のリセット
currentOperator = "+";
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 DigitStatus isDigitAvialable (String examData) {
BigDecimal bigDecimalExamData = new BigDecimal(examData);
String strBDExamData = bigDecimalExamData.toPlainString().replaceFirst("[\\+\\-]", "");
int totalLength = strBDExamData.length();
int integerLength = strBDExamData.indexOf(".") == -1 ? totalLength : strBDExamData.indexOf(".");
int fractionLength = strBDExamData.indexOf(".") == -1 ? 0 : totalLength - integerLength - 1;
if (integerLength <= MAX_DIGIT && fractionLength <= MAX_DIGIT) {
return DigitStatus.OK;
} else {
System.out.println(SITEIKETASU_COMMENT + "(" + MAX_DIGIT + ")" + OVER_COMMENT);
System.out.println(SAIDO_INPUT_COMMENT);
return DigitStatus.NG;
}
}
private static DigitStatus isfractionDigitOnlyAvialable (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 DigitStatus.fractionOnlyNG;
} else {
return DigitStatus.OK;
}
}
private static void calculate ()
throws NumberFormatException, IOException, ArithmeticException{
BigDecimal temp_currentResult = currentResult;
BigDecimal bigDecimalCurrentData = new BigDecimal(currentData);
if (isDigitAvialable(currentData) == DigitStatus.NG){
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);
if (isfractionDigitOnlyAvialable(temp_currentResult.toPlainString()) == DigitStatus.fractionOnlyNG) {
System.out.println("実計算結果:" + temp_currentResult.toPlainString());
System.out.println("四捨五入を実行して指定桁(" + MAX_DIGIT + ")にします:");
temp_currentResult = currentResult.multiply(bigDecimalCurrentData);
temp_currentResult = temp_currentResult.setScale(MAX_DIGIT, BigDecimal.ROUND_HALF_UP);
}
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);
if (isfractionDigitOnlyAvialable(temp_currentResult.toPlainString()) == DigitStatus.fractionOnlyNG) {
System.out.println("実計算結果:" + temp_currentResult.toPlainString());
System.out.println("四捨五入を実行して指定桁(" + MAX_DIGIT + ")にします:");
temp_currentResult = currentResult.divide(bigDecimalCurrentData,
MAX_DIGIT ,
BigDecimal.ROUND_HALF_UP);
}
} catch(ArithmeticException aEx) {
temp_currentResult = currentResult.divide(bigDecimalCurrentData,
MAX_DIGIT ,
BigDecimal.ROUND_HALF_UP);
}
}
break;
default:
throw new IOException();
}
if (isDigitAvialable(temp_currentResult.toPlainString()) == DigitStatus.OK) {
currentResult = temp_currentResult;
}
}
}import java.io.*;
import java.lang.*;
import java.math.*;
import java.util.*;
public class HW20141216 {
static String strCurrentInt = "";
static int startInt = 0;
static int currentInt = 0;
static String OPRATOR_PLUS = "*";
static Set<Integer> primeSet = new HashSet<Integer>();
static boolean isPrime = false;
static String stringPrimeFact = "";
static BufferedReader br = null;
/*定数*/
static final int MAX_DIGIT = 15;
static final String START_COMMENT = "整数を入力してください。";
static final String WARN_COMMENT = "2以上の整数を入力してください。";
public static void main (String[] args) throws IOException, NumberFormatException {
try {
br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(START_COMMENT);
readLineAndIntConvert();
while (!(currentInt >=2)) {
System.out.println(WARN_COMMENT);
readLineAndIntConvert();
}
startInt = currentInt;
int workInt = 2;
makePrimeFactorization (workInt, currentInt);
} catch (IOException iEx) {
System.out.println(iEx.toString());
} catch (NumberFormatException nEx) {
System.out.println(nEx.toString());
} finally {
if (br != null) {
br.close();
}
}
}
private static void makePrimeFactorization (int workInt, int currentInt) {
while (currentInt !=1) {
if(isPrime(workInt) && currentInt % workInt == 0) {
currentInt /= workInt;
stringPrimeFact += workInt + OPRATOR_PLUS;
} else {
++workInt;
}
}
if (stringPrimeFact.length() > 0) {
stringPrimeFact = stringPrimeFact.substring(0, stringPrimeFact.length() - 1);
System.out.println(startInt + " = " + stringPrimeFact);
}
}
private static void readLineAndIntConvert() throws IOException, NumberFormatException{
strCurrentInt = br.readLine();
currentInt = Integer.parseInt(strCurrentInt);
}
private static boolean isPrime (int workInt) {
if(primeSet.contains(new Integer(workInt))){
return true;
}
for (int i = 2; (double)i <= (double)Math.sqrt(workInt); i++) {
if(workInt % i == 0) return false;
}
primeSet.add(new Integer(workInt));
return true;
}
}