Kelas Penambahan.
Script java :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package BigHitung;
/**
*
* @author bapake-aisyah
* alhamdulillah dapat inspirasi dari BigAdder praktikum II
*/
public class BigJumlah {
String aPertama;
String aKedua;
public void setPertama(String a) {
this.aPertama = a;
}
public void setKedua(String a) {
this.aKedua = a;
}
public String getPertama() {
return this.aPertama;
}
public String getKedua() {
return this.aKedua;
}
public String jumlahBig(String x, String y) {
String sc[] = new String[2000];
String temp[] = new String[2000];
int ax[] = new int[2000];
int ay[] = new int[2000];
int ac[] = new int[2000];
int lx = x.length();
int ly = y.length();
int lmax = Math.max(lx, ly) + 1;
String n = "";
String c = "";
for (int i = 0; i < lmax - lx; i++) {
x = "0" + x;
}
for (int i = 0; i < lmax - ly; i++) {
y = "0" + y;
}
int sisa = 0;
for (int i = lmax - 1; i > -1; i--) {
ax[i] = Integer.parseInt(x.substring(i, i + 1));
ay[i] = Integer.parseInt(y.substring(i, i + 1));
int hs = ax[i] + ay[i];
if (i > 0) {
ac[i] = sisa + hs % 10;
} else {
ac[i] = hs % 10;
}
sisa = hs / 10;
if (i > 0) {
ac[i - 1] = sisa;
}
sc[i] = Integer.toString(ac[i]);
if (sc[0] == null) {
sc[0] = "";
}
n += sc[i];
}
int ln = n.length();
String an[] = new String[ln];
for (int i = 0; i < ln; i++) {
an[i] = n.substring(i, i + 1);
}
int j = ln - 1;
String m = "";
if ((ax[1] + ay[1]) > 10) {
m = "1";
}
for (int i = 0; i < ln; i++) {
temp[i] = an[j--];
if (temp[0] == null ? "0" == null : temp[0].equals("0")) {
temp[0] = "";
}
}
n = "";
for (int i = 0; i < ln; i++) {
n += temp[i];
}
n = m + n;
return n;
}
}
0 komentar:
Posting Komentar