MAIN CLASS
big digits
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package recursif;
import java.util.Scanner;
/**
*
* @agintageniusa
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
logics G = new logics();
Scanner input = new Scanner(System.in);
System.out.println("Masukkan Bilangan Pertama: ");
String number1 = input.nextLine();
String x = number1;
if (x.length() > 2000) {
return;
}
System.out.println("Masukkan Bilangan Kedua: ");
String number2 = input.nextLine();
String y = number2;
if (y.length() > 2000) {
return;
}
System.out.println(" MENU ");
System.out.println("1. Penambahan");
System.out.println("2. Pengurangan");
System.out.println("*******************************");
System.out.println("Masukkan Nomor Pilihan :");
int pil = input.nextInt();
System.out.println(" nomor pilihan " + pil);
System.out.println("Hasil perhitungan : ");
if (pil == 1) {
G.tambah(x, y);
}
if (pil == 2) {
G.minus(x, y);
}
}
}
LOGICS CLASS
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package recursif;
/**
* @author agintageniusa/part meat
*/
class logics {
private String x, y, hasilpenambahan, hasilpengurangan;
//constructor
public logics() {
hasilpenambahan = "";
hasilpengurangan = "";
}
//set x
public void setX(String a) {
x = a;
}
//return x
public String getX() {
return x;
}
//set y
public void setY(String b) {
y = b;
}
//return y
public String getY() {
return y;
}
//set hasilpenambahan
public void sethasilpenambahan(String a, String b) {
x = a;
y = b;
return;
}
//return hasilpenambahan
public String gethasilpenambahan() {
return hasilpenambahan;
}
//set hasilpengurangan
public void sethasilpengurangan(String a, String b) {
x = a;
y = b;
return;
}
//return hasilpengurangan
public String gethasilpengurangan() {
return hasilpengurangan;
}
//menghitung x ditambah y
public void tambah(String x, String y) {
int max = 0;
int carry = 0;
int zeros = 0;
int jumlah;
//Menghitung selisih digit dengan asumsi x terbesar
if (x.length() > y.length() || x.length() == y.length()) {
max = x.length();
zeros = max - y.length();
for (int i = 0; i < zeros; i++) {
y = "0" + y;
}
}
//Menghitung selisih digit dengan asumsi y terbesar
if (y.length() > x.length()) {
max = y.length();
zeros = max - x.length();
for (int i = 0; i < zeros; i++) {
x = "0" + x;
}
}
for (int i = max - 1; i > -1; i--) {
String str1 = String.valueOf(x.charAt(i));
String str2 = String.valueOf(y.charAt(i));
int a = Integer.parseInt(str1);
int b = Integer.parseInt(str2);
jumlah = a + b + carry;
String sum = String.valueOf(jumlah);
if (sum.length() == 2) {
hasilpenambahan = sum.charAt(1) + hasilpenambahan;
carry = 1;
} else {
carry = 0;
hasilpenambahan = sum + hasilpenambahan;
}
}
if (carry == 1) {
hasilpenambahan = 1 + hasilpenambahan;
}
System.out.println(gethasilpenambahan());
}
//menghitung x - y
public void minus(String x, String y) {
int max = 0;
int carry = 0;
int zeros = 0;
int minus = 0;
//jika digit x > a digit y
if (x.length() > y.length()) {
max = x.length();
zeros = max - y.length();
for (int i = 0; i < zeros; i++) {
y = "0" + y;
}
for (int i = max - 1; i > -1; i--) {
String str1 = String.valueOf(x.charAt(i));
String str2 = String.valueOf(y.charAt(i));
int a = Integer.parseInt(str1);
int b = Integer.parseInt(str2);
if (a > b || a == b) {
minus = a - b + carry;
carry = 0;
} else {
minus = 10 + a - b;
carry = -1;
}
String kurang = String.valueOf(minus);
hasilpengurangan = kurang + hasilpengurangan;
}
if (hasilpengurangan.equals("")) {
hasilpengurangan = "0";
}
System.out.println(gethasilpengurangan());
} //jika digit x < digit y
else if (x.length() < y.length()) {
max = y.length();
zeros = max - x.length();
for (int i = 0; i < zeros; i++) {
x = "0" + x;
}
for (int i = max - 1; i > -1; i--) {
String str1 = String.valueOf(y.charAt(i));
String str2 = String.valueOf(x.charAt(i));
int a = Integer.parseInt(str1);
int b = Integer.parseInt(str2);
if (a > b || b == a) {
minus = a - b + carry;
carry = 0;
} else {
minus = 10 + a - b;
carry = -1;
}
String kurang = String.valueOf(minus);
hasilpengurangan = kurang + hasilpengurangan;
}
System.out.println("-" + gethasilpengurangan());
} //jika digit x = digit y
else {
//jika nilai x > dari pada y
max = x.length();
String str1 = String.valueOf(x.charAt(0));
String str2 = String.valueOf(y.charAt(0));
int a = Integer.parseInt(str1);
int b = Integer.parseInt(str2);
if (a > b || a == b) {
for (int i = max - 1; i > -1; i--) {
String str3 = String.valueOf(x.charAt(i));
String str4 = String.valueOf(y.charAt(i));
int c = Integer.parseInt(str3);
int d = Integer.parseInt(str4);
if (c > d || a == b) {
minus = c - d + carry;
carry = 0;
} else {
minus = 10 + c - d;
carry = -1;
}
String kurang = String.valueOf(minus);
hasilpengurangan = kurang + hasilpengurangan;
}
String banding = "0";
for (int i = 0; i <= max - 2; i++) {
banding += 0;
}
if (banding == null ? hasilpengurangan == null : banding.equals(hasilpengurangan)) {
System.out.println("0");
} else {
System.out.println(gethasilpengurangan());
}
} else {
for (int i = max - 1; i > -1; i--) {
String str3 = String.valueOf(y.charAt(i));
String str4 = String.valueOf(x.charAt(i));
int c = Integer.parseInt(str3);
int d = Integer.parseInt(str4);
if (c > d || c == d) {
minus = c - d + carry;
carry = 0;
} else {
minus = 10 + c - d;
carry = -1;
}
String kurang = String.valueOf(minus);
hasilpengurangan = kurang + hasilpengurangan;
}
System.out.println("-" + gethasilpengurangan());
}
}
}
}
Only that I can write for u with JAVA, ladiess
No comments:
Post a Comment