Last active
August 29, 2015 14:16
-
-
Save gangmul12/9590e86aad0fc615d474 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; // 입력을 받기 위해 이 라이브러리가 필요하다. | |
public class BigInteger | |
{ | |
public static void main(String args[]) | |
{ | |
// 입력을 받기 위한 작업이다. | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
// quit가 나올 때 까지 입력을 받아야 하므로 | |
// while(true) 문을 사용하여 계속 반복한다. | |
while (true) | |
{ | |
try // try 와 catch 문은 오류 발생을 감지한다. | |
{ | |
String input = br.readLine(); // 한 줄을 입력 받아 input 문자열에 저장 | |
if (input.compareTo("quit") == 0) | |
{ | |
// quit 라고 입력받았을 경우 종료한다. | |
// 종료하려면 while 문을 빠져나와야 하므로 break를 사용한다. | |
break; | |
} | |
// quit가 아니라면 식을 계산해야 한다. | |
calculate(input); | |
} | |
catch (Exception e) | |
{ | |
// 만약 try { } 안에서 오류가 발생했다면 이곳으로 오게 된다. | |
// 이렇게 함으로써 입력을 이상하게 했을 경우 발생하는 오류를 방지한다. | |
System.out.println("입력이 잘못되었습니다. 오류 : " + e.toString()); | |
} | |
} | |
} | |
// 이 함수에서 입력받은 input 문자열을 이용하여 계산을 실시한다. | |
// 위의 main 함수를 완벽하게 이해한 뒤 이 함수의 내용을 자유롭게 구성해보라. | |
private static void calculate(String input) throws Exception | |
{ | |
input=suitabilityChecker(input); | |
final int [] signStruct = structureChecker(input); | |
// TODO : Main 함수를 이해했다면 아래 문장을 삭제하고 구현해라. | |
System.out.println("<< calculate 함수에서 " + input + "을 계산할 예정입니다 >>"); | |
} | |
private static String suitabilityChecker(String input) throws Exception | |
{ | |
input=withoutBlank(input); //makes input without blank | |
characterChecker(input); | |
structureChecker(input); | |
return input; | |
} | |
private static boolean isNumber(char c){ | |
if(c>=48&&c<=57) | |
return true; | |
return false; | |
} | |
private static String withoutBlank(String input) | |
{ | |
String output= new String(""); | |
for(int i=0; i<input.length(); i++) | |
{ | |
if(input.charAt(i)!=' ') | |
output+=input.charAt(i); | |
} | |
return output; | |
} | |
private static void characterChecker(String input) throws Exception | |
{ | |
for(int i=0; i<input.length(); i++) | |
{ | |
char c=input.charAt(i); | |
if(!(c=='+'||c=='-'||c=='*'||isNumber(c))) | |
throw new Exception(); | |
} | |
} | |
private static int signChecker(char c) throws Exception | |
{ | |
if(c=='*') | |
throw new Exception(); | |
if(c=='+'||c=='-') | |
{ | |
return 1; | |
} | |
return 0; | |
} | |
private static int [] structureChecker(String input) throws Exception | |
{ | |
final int firstOperandSign = 0; | |
final int operator = 1; | |
final int secondOperandSign = 2; | |
int [] output ={0,-1,0}; | |
int indexOfOperand=0; | |
if(input.length()<3) | |
throw new Exception(); | |
output[firstOperandSign]=signChecker(input.charAt(0)); | |
indexOfOperand+=output[firstOperandSign]; | |
for(;indexOfOperand<input.length();indexOfOperand++) | |
{ | |
if(!isNumber(input.charAt(indexOfOperand))) | |
break; | |
if(indexOfOperand==input.length()-1) | |
throw new Exception(); | |
} | |
switch(input.charAt(indexOfOperand)){ | |
case '+': | |
output[operator]=0; | |
break; | |
case '-': | |
output[operator]=1; | |
break; | |
case '*': | |
output[operator]=2; | |
break; | |
default: | |
throw new Exception(); | |
} | |
if(indexOfOperand==input.length()-1) | |
{ | |
throw new Exception(); | |
} | |
indexOfOperand++; | |
output[secondOperandSign]=signChecker(input.charAt(indexOfOperand)); | |
if(indexOfOperand==input.length()-1&&output[secondOperandSign]==1) | |
throw new Exception(); | |
indexOfOperand+=output[secondOperandSign]; | |
for(;indexOfOperand<input.length();indexOfOperand++) | |
{ | |
if(!isNumber(input.charAt(indexOfOperand))) | |
throw new Exception(); | |
} | |
return output; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment