티스토리 뷰
사용자가 실수를 입력하면 분수로 바꿔주는 프로그램을 작성하시오.
힌트 : 실수를 string으로 읽을 때, 정수부분과 소수부분으로 나누고 Rational 클래스의 BigInteger를 사용해서 소수를 유리수 형식(a/b)으로 쓰세요.
(Convert decimals to fractions) Write a program that prompts the user to enter a decimal number and displays the number in a fraction. Hint: read the decimal number as a string, extract the integer part and fractional part from the string, and use the BigInteger implementation of the Rational class in Programming Exercise 13.15 to obtain a rational number for the decimal number.
* Line 1 : 테스트케이스의 개수 N
* Line 2 ~ N+1 : 각 케이스 별 소수점 숫자 a b
* Line 1 ~ 4N : 각 테스트 케이스마다 다음과 같이 4줄씩 출력
- Line 1 : a + b = 결과
- Line 2 : a - b = 결과
- Line 3 : a * b = 결과
- Line 4 : a / b = 결과
- 모든 숫자는 분수형태의 유리수
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)); } } } YOUR_CODE
2 3.25 -3 -1.0 -9
13/4 + -3 = 1/4 13/4 - -3 = 25/4 13/4 * -3 = -39/4 13/4 / -3 = -13/12 -1 + -9 = -10 -1 - -9 = 8 -1 * -9 = 9 -1 / -9 = 1/9
JAVA2015 PE13.19
class Rational { | |
BigInteger x, y; | |
Rational(BigInteger a, BigInteger b) { | |
if (b.compareTo(BigInteger.ZERO) < 0) { | |
x = a.negate(); | |
y = b.negate(); | |
} else { | |
x = a; | |
y = b; | |
} | |
} | |
public static Rational getFraction(String k) { | |
int count=-1; | |
int save=-1; | |
String l=""; | |
for (int i=0;i<k.length();i++) { | |
if (k.charAt(i)=='.') { | |
count++; | |
save=i; | |
} | |
else { | |
l+=k.charAt(i); | |
} | |
} | |
String m="1"; | |
for (int i=0;i<k.length()-save-1;i++) { | |
m+="0"; | |
} | |
BigInteger x1=new BigInteger(l); | |
BigInteger y1=new BigInteger(m); | |
if (count==0) { | |
Rational result=new Rational(x1,y1); | |
return result; | |
} | |
else { | |
Rational result=new Rational(x1,BigInteger.ONE); | |
return result; | |
} | |
} | |
Rational add(Rational k) { | |
BigInteger r = y.gcd(k.y); | |
r = (y.multiply(k.y)).divide(r); | |
BigInteger x1 = r.divide(y); | |
BigInteger x2 = r.divide(k.y); | |
Rational result = new Rational((x.multiply(x1)).add(x2.multiply(k.x)), r); | |
return result; | |
} | |
Rational subtract(Rational k) { | |
BigInteger r = y.gcd(k.y); | |
r = (y.multiply(k.y)).divide(r); | |
BigInteger x1 = r.divide(y); | |
BigInteger x2 = r.divide(k.y); | |
Rational result = new Rational((x.multiply(x1)).subtract(x2.multiply(k.x)), r); | |
return result; | |
} | |
Rational multiply(Rational k) { | |
Rational result=new Rational(x.multiply(k.x),y.multiply(k.y)); | |
return result; | |
} | |
Rational divide(Rational k) { | |
Rational result=new Rational(x.multiply(k.y),y.multiply(k.x)); | |
return result; | |
} | |
public String toString() { | |
String result = ""; | |
BigInteger kk = y.gcd(x); | |
if (!(y.gcd(x)).equals(BigInteger.ONE)) { | |
x = x.divide(kk); | |
y = y.divide(kk); | |
} | |
if (y.equals(BigInteger.ONE)) { | |
result += x; | |
} else { | |
// result+=kk+" "; | |
result += x; | |
result += "/"; | |
result += y; | |
} | |
return result; | |
} | |
/*static BigInteger gcd(BigInteger a, BigInteger b) { | |
BigInteger r=new BigInteger("1"); | |
while (!b.equals(BigInteger.ZERO)) { | |
r=a. | |
} | |
}*/ | |
} |
'학교공부 > JAVA' 카테고리의 다른 글
[JAVA] Fibonacci numbers (0) | 2017.07.26 |
---|---|
[JAVA] Factorial (0) | 2017.07.26 |
[JAVA] 유리수 클래스1 (0) | 2017.07.26 |
[JAVA] Colorable 인터페이스 (0) | 2017.07.26 |
[JAVA] 비교가능한 GeometricObject (0) | 2017.07.26 |