[JAVA] Calculator program

//キーボード入力インポート import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigDecimal;

//クラス開始 public class Original {

//メインメソッド開始************************** public static void main(String[] args) throws IOException {

//キーボード入力準備 BufferedReader br = new BufferedReader (new InputStreamReader(System.in));

	String now = "0";
	String input = "";
	String enzan = "";

String msg = "Numeric"; String data = "";

//無限ループ while(true){ try {

			//******************************************
			System.out.println("\n"+"***************************");
			System.out.println(" ");

System.out.println ("Current value:" + now); System.out.println ("operator:" + enzan); System.out.println ("Entered value:" + input); Enter System.out.print (msg + ">>"); //******************************************

//dataに入力値を代入する data = br.readLine();

			//A-1
			if(hantei(data)){

BigDecimal bd_data = new BigDecimal (data); // Convert to Decimal BigDecimal er = new BigDecimal(0);

bd_data = bd_data.stripTrailingZeros (); // Erase zero

				//B-1

if ("numerical value" .equals (msg)) { now = bd_data.toPlainString(); msg = "operator";

					//C-1

} else if ("numerical or operator" .equals (msg)) { //D-1 if("/".equals(enzan) && bd_data.equals(er)){ System.out.println ("doesn't break at 0");

					}else{
						input = bd_data.toPlainString();

msg = "operator"; } //C-1 }else{ System.out.println ("Error! Please enter operator" + "\ n"); }

				//A-2
			}else{

				if("+".equals(data) || "-".equals(data) ||
						"*".equals(data) || "/".equals(data) ||
						"=".equals(data)){

//msgが数値のとき if ("numerical value" .equals (msg)) { System.out.print ("Error! Please enter a number");

//msgが数値又は演算子のとき } else if ("numerical or operator" .equals (msg)) {

//dataが=だったら if("=".equals(data)){ System.out.print ("Error! Please enter a number"); }else{ enzan = data; msg = "Numeric or operator"; }

//msgが演算子のとき }else{

//inputが空だったら if("".equals(input)){ if("=".equals(data)){ System.out.print ("Error! Please enter something other than equal"); }else{ enzan = data; msg = "Numeric or operator"; }

//inputに値が入っていたら }else{

							BigDecimal bd1 = new BigDecimal(now);
							BigDecimal bd2 = new BigDecimal(input);

//足し算 if("+".equals(enzan)){ BigDecimal ans1 = bd1.add(bd2);

								now = ans1.toPlainString();
								input = "";

//引き算 }else if("-".equals(enzan)){ BigDecimal ans2 = bd1.subtract(bd2);

								now = ans2.toPlainString();
								input = "";

//掛け算 }else if("*".equals(enzan)){ BigDecimal ans3 = bd1.multiply(bd2);

								now = ans3.toPlainString();
								input = "";

//割り算(四捨五入・20桁まで表示) }else{ BigDecimal ans4 = bd1.divide(bd2,20,BigDecimal.ROUND_HALF_UP);

								ans4 = ans4.stripTrailingZeros();
								now = ans4.toPlainString();
								input = "";
							}

//dataが=だった場合 if("=".equals(data)){ enzan = ""; //=でない場合 }else{ enzan = data; msg = "Numeric or operator"; } } // If there is a value in input } // When msg is an operator

					//A-3
				}else if("c".equals(data)){
					now = "0";
					enzan = "";
					input = "";

msg = "numerical value"; System.out.print ("cleared");

					//A-4
				}else if("ce".equals(data)){

					//B-3
					if(input != ""){
						input = "";

msg = "Numeric or operator"; //C-3 }else{ if(enzan != ""){ enzan = ""; msg = "operator";

						}else{
							now = "0";

msg = "numerical value"; } }

					//A-5
				}else if("e".equals(data)){

System.out.print ("Exit the program"); break; }else{ System.out.print ("\ n" + "error! Unsupported input value"); }

			}//A-2
		}catch (Exception e) {

// TODO auto-generated catch block System.out.print ("Unexpected error"); e.printStackTrace(); } }//infinite loop } // Method

//判定メソッド************************************** public static boolean hantei(String data){

	boolean brea = true;

	try{
		Double.parseDouble(data);

	}catch (NumberFormatException e){
		brea = false;
	}
	return	brea;

}

//************************************************** }

Recommended Posts

Calculator program
[Java] Calculator program creation 1
Zundoko Program
ruby calculator
Guitar fret calculator
Ruby payroll program
FizzBuzz program (2 types)
Bingo judgment program