코드비교

Old VersionNew Version
1 1 
2class Rational { 2class Rational { 
3BigInteger a, b; 3BigInteger a, b; 
4Rational(BigInteger a, BigInteger b) { // b가 -일때 출력양식 해결하기 4public Rational(BigInteger x, BigInteger y) {
5if (b.compareTo(BigInteger.ZERO) < 0) { 5if((x.multiply(y)).compareTo(BigInteger.ZERO)>0) {
6this.a = a.negate(); 6a = x.abs();
7this.b = b.negate(); 7b = y.abs();
   8} else if(y.compareTo(BigInteger.ZERO)<0) {
   9a = x.multiply(new BigInteger("-1"));
   10b = y.multiply(new BigInteger("-1"));
8} else { 11} else { 
9this.a = a; 9a = x;
10this.b = b; 10b = y;
11} 14} 
12} 15} 
13public static Rational getFraction(String k) { //소수부구하기 16public static Rational getFraction(String s) {
14boolean fraction = false; 17Boolean isP = false;
15int save = -1; 18String p = "";
16String s = ""; 19String q = "1";
17for (int i = 0; i < k.length(); i++) { 20for (int i = 0; i < s.length(); i++) {
18if (k.charAt(i) == '.') { 21if (s.charAt(i) == '.') {
19save = i; 22isP = true;
20fraction = true; 23}
21} else { 24else {
22s += k.charAt(i); 25if (isP) {
   26q += "0";
   27}
   28p += s.charAt(i);
23} 29} 
24} 30} 
25String l = "1"; 25BigInteger x = new BigInteger(p);
26for (int i = 0; i < k.length() - 1 - save; i++) { 26BigInteger y = new BigInteger(q);
27l += "0"; 27if (isP) {
28}   
29BigInteger x = new BigInteger(s);   
30BigInteger y = new BigInteger(l);   
31if (fraction) {   
32Rational result = new Rational(x, y); 34Rational result = new Rational(x, y); 
33return result; 35return result; 
34} else { 36}
   37else {
35Rational result = new Rational(x, BigInteger.ONE); 38Rational result = new Rational(x, BigInteger.ONE); 
36return result; 39return result; 
37} 40} 
38} 41} 
39Rational subtract(Rational r) { 39Rational add(Rational k) {
40BigInteger x = b.gcd(r.b); 40BigInteger i = b.gcd(k.b);
41x = (b.multiply(r.b)).divide(x); 41i = (b.multiply(k.b)).divide(i);
42BigInteger x1 = x.divide(b); 42BigInteger x1 = i.divide(b);
43BigInteger x2 = x.divide(r.b); 43BigInteger x2 = i.divide(k.b);
44Rational result = new Rational((a.multiply(x1)).subtract(x2.multiply(r.a)), x); 44Rational out = new Rational((a.multiply(x1)).add(x2.multiply(k.a)),i);
45return result; 45return out;
46} 49} 
47Rational add(Rational r) { 47Rational subtract(Rational k) {
48BigInteger x = b.gcd(r.b); 48BigInteger i = b.gcd(k.b);
49x = (b.multiply(r.b)).divide(x); 49i = (b.multiply(k.b)).divide(i);
50BigInteger x1 = x.divide(b); 50BigInteger x1 = i.divide(b);
51BigInteger x2 = x.divide(r.b); 51BigInteger x2 = i.divide(k.b);
52Rational result = new Rational((a.multiply(x1)).add(x2.multiply(r.a)), x); 52Rational out = new Rational((a.multiply(x1)).subtract(x2.multiply(k.a)),i);
53return result; 53return out;
54} 57} 
55Rational multiply(Rational r) { 55Rational multiply(Rational k) {
56Rational result = new Rational(a.multiply(r.a), b.multiply(r.b)); 56Rational out = new Rational(a.multiply(k.a),b.multiply(k.b));
57return result; 57return out;
58} 61} 
59Rational divide(Rational r) { 59Rational divide(Rational k) {
60Rational result = new Rational(a.multiply(r.b), b.multiply(r.a)); 60Rational out = new Rational(a.multiply(k.b),b.multiply(k.a));
61return result; 61return out;
62} 65} 
63public String toString() { 66public String toString() { 
64String result = ""; 64String output = "";
65BigInteger x = b.gcd(a); 65if(!(b.gcd(a)).equals(BigInteger.ONE)) {
66if (!(x).equals(BigInteger.ONE)) { 66BigInteger j = b.gcd(a);
67a = a.divide(x); 67b=b.divide(j);
68b = b.divide(x); 68a=a.divide(j);
69} 72} 
70if (b.equals(BigInteger.ONE)) { 73if (b.equals(BigInteger.ONE)) { 
71result += a; 71output+=a;
72} else { 72}else {
73result += a; 73output+=a+"/"+b;
74result += "/";   
75result += b;   
76} 77} 
77return result; 77return output;
78} 79} 
79} 80} 

sample code

import java.math.*;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int t = 0; t < T; t++) {
Rational r1 = Rational.getFraction(sc.next());
Rational r2 = Rational.getFraction(sc.next());
System.out.println(r1 + " + " + r2 + " = " + r1.add(r2));
System.out.println(r1 + " - " + r2 + " = " + r1.subtract(r2));
System.out.println(r1 + " * " + r2 + " = " + r1.multiply(r2));
System.out.println(r1 + " / " + r2 + " = " + r1.divide(r2));
}
}
}
class Rational {
BigInteger a, b;
Rational(BigInteger a, BigInteger b) { // b가 -일때 출력양식 해결하기
if (b.compareTo(BigInteger.ZERO) < 0) {
this.a = a.negate();
this.b = b.negate();
} else {
this.a = a;
this.b = b;
}
}
public static Rational getFraction(String k) { //소수부구하기
boolean fraction = false;
int save = -1;
String s = "";
for (int i = 0; i < k.length(); i++) {
if (k.charAt(i) == '.') {
save = i;
fraction = true;
} else {
s += k.charAt(i);
}
}
String l = "1";
for (int i = 0; i < k.length() - 1 - save; i++) {
l += "0";
}
BigInteger x = new BigInteger(s);
BigInteger y = new BigInteger(l);
if (fraction) {
Rational result = new Rational(x, y);
return result;
} else {
Rational result = new Rational(x, BigInteger.ONE);
return result;
}
}
Rational subtract(Rational r) {
BigInteger x = b.gcd(r.b);
x = (b.multiply(r.b)).divide(x);
BigInteger x1 = x.divide(b);
BigInteger x2 = x.divide(r.b);
Rational result = new Rational((a.multiply(x1)).subtract(x2.multiply(r.a)), x);
return result;
}
Rational add(Rational r) {
BigInteger x = b.gcd(r.b);
x = (b.multiply(r.b)).divide(x);
BigInteger x1 = x.divide(b);
BigInteger x2 = x.divide(r.b);
Rational result = new Rational((a.multiply(x1)).add(x2.multiply(r.a)), x);
return result;
}
Rational multiply(Rational r) {
Rational result = new Rational(a.multiply(r.a), b.multiply(r.b));
return result;
}
Rational divide(Rational r) {
Rational result = new Rational(a.multiply(r.b), b.multiply(r.a));
return result;
}
public String toString() {
String result = "";
BigInteger x = b.gcd(a);
if (!(x).equals(BigInteger.ONE)) {
a = a.divide(x);
b = b.divide(x);
}
if (b.equals(BigInteger.ONE)) {
result += a;
} else {
result += a;
result += "/";
result += b;
}
return result;
}
}
class Rational {
BigInteger a, b;
public Rational(BigInteger x, BigInteger y) {
if((x.multiply(y)).compareTo(BigInteger.ZERO)>0) {
a = x.abs();
b = y.abs();
} else if(y.compareTo(BigInteger.ZERO)<0) {
a = x.multiply(new BigInteger("-1"));
b = y.multiply(new BigInteger("-1"));
} else {
a = x;
b = y;
}
}
public static Rational getFraction(String s) {
Boolean isP = false;
String p = "";
String q = "1";
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '.') {
isP = true;
}
else {
if (isP) {
q += "0";
}
p += s.charAt(i);
}
}
BigInteger x = new BigInteger(p);
BigInteger y = new BigInteger(q);
if (isP) {
Rational result = new Rational(x, y);
return result;
}
else {
Rational result = new Rational(x, BigInteger.ONE);
return result;
}
}
Rational add(Rational k) {
BigInteger i = b.gcd(k.b);
i = (b.multiply(k.b)).divide(i);
BigInteger x1 = i.divide(b);
BigInteger x2 = i.divide(k.b);
Rational out = new Rational((a.multiply(x1)).add(x2.multiply(k.a)),i);
return out;
}
Rational subtract(Rational k) {
BigInteger i = b.gcd(k.b);
i = (b.multiply(k.b)).divide(i);
BigInteger x1 = i.divide(b);
BigInteger x2 = i.divide(k.b);
Rational out = new Rational((a.multiply(x1)).subtract(x2.multiply(k.a)),i);
return out;
}
Rational multiply(Rational k) {
Rational out = new Rational(a.multiply(k.a),b.multiply(k.b));
return out;
}
Rational divide(Rational k) {
Rational out = new Rational(a.multiply(k.b),b.multiply(k.a));
return out;
}
public String toString() {
String output = "";
if(!(b.gcd(a)).equals(BigInteger.ONE)) {
BigInteger j = b.gcd(a);
b=b.divide(j);
a=a.divide(j);
}
if(b.equals(BigInteger.ONE)) {
output+=a;
}else {
output+=a+"/"+b;
}
return output;
}
}