Last active
August 29, 2015 14:17
-
-
Save pse1202/810652dae7953acaa7b4 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.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.util.Arrays; | |
import java.util.regex.MatchResult; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class BigInteger | |
{ | |
public static final String QUIT_COMMAND = "quit"; | |
public static final String MSG_INVALID_INPUT = "입력이 잘못되었습니다."; | |
public static final Pattern EXPRESSION_PATTERN = Pattern.compile("\\s*([+-]?[0-9]+)\\s*([-+*])\\s*([+-]?[0-9]+)\\s*"); | |
private int[] nums; | |
private boolean positive = true; | |
//CONSTRUCTORS | |
public BigInteger(){ | |
// null constructor for initializing | |
this.positive = true; | |
this.nums = new int[200]; | |
} | |
public BigInteger(String s) | |
{ | |
// constructor used for input | |
this.nums = new int[200]; | |
boolean unary = false; | |
int i=0; | |
this.positive = true; | |
if(s.charAt(0) == '-'){ | |
this.positive = false; | |
unary = true; | |
} | |
else if(s.charAt(0) == '+'){ | |
unary = true; | |
} | |
if(unary){ | |
i=1; | |
} | |
int j = 0; | |
while(i+j<s.length()){ | |
this.nums[j] = Character.getNumericValue(s.charAt(s.length() - j - 1)); | |
j++; | |
} | |
} | |
//METHODS | |
public int len(){ | |
int length=1; | |
for(int i=0;i<200;i++){ | |
if(this.nums[199-i]!=0){ | |
length = 200-i; | |
break; | |
} | |
} | |
return length; | |
} | |
public boolean largerthan(BigInteger big){ | |
if(this.len() != big.len()){ | |
boolean lencompare = (this.len() > big.len()) ; | |
return lencompare; | |
} | |
boolean numcompare = false; | |
for(int i=this.len() - 1;i>=0;i--){ | |
if(this.nums[i] != big.nums[i]){ | |
numcompare = (this.nums[i] > big.nums[i]); | |
break; | |
} | |
} | |
return numcompare; | |
} | |
//OPERATIONS | |
public BigInteger add(BigInteger big) | |
{ | |
BigInteger ans = new BigInteger(); | |
if(this.positive != big.positive){ | |
big.positive = !big.positive; | |
return this.subtract(big); | |
} | |
ans.positive = this.positive; | |
for(int i=0;i<200;i++){ | |
ans.nums[i] += this.nums[i] + big.nums[i]; | |
if(ans.nums[i]>=10){ | |
ans.nums[i+1]+=1; | |
ans.nums[i] -=10; | |
} | |
} | |
return ans; | |
} | |
public BigInteger subtract(BigInteger big) | |
{ | |
BigInteger ans = new BigInteger(); | |
if(this.positive != big.positive){ | |
big.positive = !big.positive; | |
return this.add(big); | |
} | |
if(big.largerthan(this)){ | |
ans = big.subtract(this); | |
ans.positive = !ans.positive; | |
return ans; | |
} | |
ans.positive = this.positive; | |
for(int i = 0;i<200;i++){ | |
ans.nums[i] += this.nums[i] - big.nums[i]; | |
if(ans.nums[i]<0){ | |
ans.nums[i+1]--; | |
ans.nums[i]+=10; | |
} | |
} | |
return ans; | |
} | |
public BigInteger multiply(BigInteger big) | |
{ | |
BigInteger ans = new BigInteger(); | |
for(int i=0;i<this.len();i++){ | |
for(int j=0;j<big.len();j++){ | |
ans.nums[i+j] += this.nums[i] * big.nums[j]; | |
if(ans.nums[i+j]>=10){ | |
ans.nums[i+j+1] += ans.nums[i+j]/10; | |
ans.nums[i+j] = ans.nums[i+j]%10; | |
} | |
} | |
} | |
ans.positive = !(this.positive ^ big.positive); | |
return ans; | |
} | |
@Override public String toString() | |
{ | |
StringBuilder result = new StringBuilder(); | |
int len = this.len(); | |
if(!positive && !(len==1 && this.nums[0]==0)){ | |
result.append('-'); | |
} | |
for(int i=0;i<len;i++){ | |
result.append(this.nums[len-1-i]); | |
} | |
return result.toString(); | |
} | |
static BigInteger evaluate(String input) throws IllegalArgumentException | |
{ | |
Matcher m = EXPRESSION_PATTERN.matcher(input); | |
BigInteger result = new BigInteger(); | |
while(m.find()){ | |
MatchResult parsedata = m.toMatchResult(); | |
String arg1 = parsedata.group(1); | |
char operator = parsedata.group(2).charAt(0); | |
String arg2 = parsedata.group(3); | |
BigInteger num1 = new BigInteger(arg1); | |
BigInteger num2 = new BigInteger(arg2); | |
switch(operator){ | |
case '+': | |
result = num1.add(num2); | |
break; | |
case '-': | |
result = num1.subtract(num2); | |
break; | |
case '*': | |
result = num1.multiply(num2); | |
break; | |
} | |
} | |
if(!input.matches("\\s*([+-]?[0-9]+)\\s*([-+*])\\s*([+-]?[0-9]+)\\s*")){ | |
throw new IllegalArgumentException(); | |
} | |
return result; | |
} | |
public static void main(String[] args) throws Exception | |
{ | |
try (InputStreamReader isr = new InputStreamReader(System.in)) | |
{ | |
try (BufferedReader reader = new BufferedReader(isr)) | |
{ | |
boolean done = false; | |
while (!done) | |
{ | |
String input = reader.readLine(); | |
try | |
{ | |
done = processInput(input); | |
} | |
catch (IllegalArgumentException e) | |
{ | |
System.err.println(MSG_INVALID_INPUT); | |
} | |
} | |
} | |
} | |
} | |
static boolean processInput(String input) throws IllegalArgumentException | |
{ | |
boolean quit = isQuitCmd(input); | |
if (quit) | |
{ | |
return true; | |
} | |
else | |
{ | |
BigInteger result = evaluate(input); | |
System.out.println(result.toString()); | |
return false; | |
} | |
} | |
static boolean isQuitCmd(String input) | |
{ | |
return input.equalsIgnoreCase(QUIT_COMMAND); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment