계산기






import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Blog {
	public static void main(String[] args) {
		new Main();
	}
}

class Main {
	Scanner sc;
	Pattern pt;
	Matcher mt;

	Main() {
		System.out.println("계산기\n계산식을 입력\n현재 지원 기능: + - * / ! ^\n예시: 1 + 1 Enter\n종료하려면 \"종료\" 입력");
		sc = new Scanner(System.in);
		main();
		sc.close();
	}

	void main() {
		String[] ab;
		double result = -1;
		String op = null;
		boolean isFactorial = false;
		
		String input = sc.nextLine().trim();
		if(input.contains("종료")) System.exit(0);
		
		pt = Pattern.compile("[^\\.\\s0-9+\\-*/!^]");
		mt = pt.matcher(input);
		if (mt.find()) {
			System.out.println("숫자와 연산부호만 입력");
			main();
			return;
		}

		pt = Pattern.compile("[+\\-*/!^]");
		mt = pt.matcher(input);
		int c = 0;
		while (mt.find()) {
			c++;
			if (c > 1) {
				System.out.println("한번에 한 연산만");
				main();
				return;
			} 
		}
		
		if (c < 1) {
			System.out.println("연산부호 어디");
			main();
			return;
		}

		if (input.contains("+")) {
			op = "\\+";
		} else if (input.contains("-")) {
			op = "\\-";
		} else if (input.contains("*")) {
			op = "\\*";
		} else if (input.contains("/")) {
			op = "/";
		} else if (input.contains("^")) {
			op = "\\^";
		} else if (input.contains("!")) {
			isFactorial = true;
			int a = Integer.parseInt(input.substring(0, input.indexOf('!')));
			System.out.println(a + "! = " + Operation.factorial(a));
			main();
			return;
		}

		if (!isFactorial) {
			ab = input.split(op);
			try {
				String as = ab[0].trim();
				String bs = ab[1].trim();
				double a = Double.parseDouble(as);
				double b = Double.parseDouble(bs);
				result = Operation.operate(a, b, op);
				System.out.print(
						as + " " + ((op.length() > 1) ? op.substring(1, op.length()) : op) + " " + bs + " = ");
				if (result % 1 == 0) {
					System.out.println((int) result);
				} else {
					System.out.println(result);
				}
			} catch (Exception e) {
				System.out.println("숫자 2개 입력");
			}
			main();
			return;
		}
	}
}

class Operation {
	static double operate(double a, double b, String op) {
		double result = -1;
		switch (op) {
		case "\\+":
			result = add(a, b);
			break;
		case "\\-":
			result = subtract(a, b);
			break;
		case "\\*":
			result = multiply(a, b);
			break;
		case "/":
			result = divide(a, b);
			break;
		case "\\^":
			result = power(a, b);
			break;
		}
		return result;
	}

	static double add(double a, double b) {
		return a + b;
	}

	static double subtract(double a, double b) {
		return a - b;
	}

	static double multiply(double a, double b) {
		return a * b;
	}

	static double divide(double a, double b) {
		return a / b;
	}

	static int factorial(int a) {
		if (a == 0)
			return 1;
		return factorial(a - 1) * a;
	}

	static double power(double a, double b) {
		double result = 1;
		while (b > 0) {
			result *= a;
			b -= 1;
		}
		return result; // Math.pow(a,b);
	}
}
          

댓글

이 블로그의 인기 게시물

substring 예제: 문자열을 입력 받아 한 글자씩 회전시켜 모두 출력하는 프로그램을 작성하라

단체 채팅 구현