comme il existe plusieurs editeur et compilateur différents
je poste sans zip deux fonctions qui sont l'addition et la soustraction,ça s'addresse au débutants.
Source / Exemple :
public static String Sous (String a,String b)
{ String res ="";
int i,m,n,l,w,x;
int y = 0;
int max = 0;
int r =0;
int base = 10;
// Supprimer les virgules
if (a.indexOf(",")> 0 && b.indexOf(",")> 0)
{ w = a.length() - a.indexOf(",");
x = b.length() - b.indexOf(",");
if (w>x)
{ b = b.replace(",", "");
a = a.replace(",", "");
for (i = 1 ; i <= w-x;i++)
{ b = b + "0";}
y = w;
}
else if (x > w)
{ b = b.replace(",", "");
a = a.replace(",", "");
for (i = 1 ; i <= x-w;i++)
{ a = a + "0";}
y = x;
}
else if (x == w)
{ b = b.replace(",", "");
a = a.replace(",", "");
y = x;
}
}
else if (a.indexOf(",")>0 && b.indexOf(",")==-1)
{ w = a.length() - a.indexOf(",");
for (i = 1 ; i< w; i++)
{ b = b + "0";}
a = a.replace(",", "");
y = w;
}
else if (b.indexOf(",")>0 && a.indexOf(",")== -1)
{ x = b.length() - b.indexOf(",");
for (i = 1 ; i <x;i++)
{ a = a + "0";}
b = b.replace(",", "");
y = x;
}
// Réduit chaque chaines à la même longueur
if (a.length()> b.length())
{ max = a.length();
l = max - b.length();
for (i =1 ;i <=l;i++)
{ b = "0"+ b;}
}
else if (b.length()> a.length())
{ max = b.length();
l = max - a.length();
for (i=1 ; i <= l;i++)
{ a = "0" + a;}
}
else if (a.length() == b.length())
{ max = a.length(); }
// Soustrait
String a1,b1;
for (i = max; i>0;i--)
{ a1= a.substring(i-1,i);
b1= b.substring(i-1,i);
m = Integer.valueOf(a1);
n = Integer.valueOf(b1);
r = m - r - n;
if (r<0)
{ a1 = String.valueOf(r+base);
r=1;
}
else
{ a1 = String.valueOf(r);
r=0;
}
res = a1 + res;
}
// Met la virgule s'il y a lieu
if (y != 0) res = res.substring(0,res.length()-(y-1)) + ","+ res.substring(res.length()-(y-1),res.length());
// Supprime les zéros à gauche
int k=0;
l = res.length();
for (i = 1; i < l;i++)
{ if (res.charAt(i-1) != '0') break;
k ++;
}
res = res.substring(k,res.length());
if (y !=0)
{// Supprime les zéros à droite
int j=0 ;
for (i= res.length();i > 0 ; i--)
{ if (res.charAt(i-1)!= '0') break;
j ++;
}
res = res.substring(0,res.length()-j);
if (j==(y-1))
{res = res + "0";}
}
if ( res.indexOf(",")== 0 ) res = "0" + res;
return res;
}
public static String Add (String a,String b)
{ String res ="";
int i,m,n,l,w,x;
int y = 0;
int max = 0;
int r =0;
int base = 10;
// Supprimer les virgules
if (a.indexOf(",")> 0 && b.indexOf(",")> 0)
{ w = a.length() - a.indexOf(",");
x = b.length() - b.indexOf(",");
if (w>x)
{ b = b.replace(",", "");
a = a.replace(",", "");
for (i = 1 ; i <= w-x;i++)
{ b = b + "0";}
y = w;
}
else if (x > w)
{ b = b.replace(",", "");
a = a.replace(",", "");
for (i = 1 ; i <= x-w;i++)
{ a = a + "0";}
y = x;
}
else if (x == w)
{ b = b.replace(",", "");
a = a.replace(",", "");
y = x;
}
}
else if (a.indexOf(",")>0 && b.indexOf(",")==-1)
{ w = a.length() - a.indexOf(",");
for (i = 1 ; i< w; i++)
{ b = b + "0";}
a = a.replace(",", "");
y = w;
}
else if (b.indexOf(",")>0 && a.indexOf(",")== -1)
{ x = b.length() - b.indexOf(",");
for (i = 1 ; i <x;i++)
{ a = a + "0";}
b = b.replace(",", "");
y = x;
}
// Réduit chaque chaines à la même longueur
if (a.length()> b.length())
{ max = a.length();
l = max - b.length();
for (i =1 ;i <=l;i++)
{ b = "0"+ b;}
}
else if (b.length()> a.length())
{ max = b.length();
l = max - a.length();
for (i=1 ; i <= l;i++)
{ a = "0" + a;}
}
else if (a.length() == b.length())
{ max = a.length(); }
// Additionne
String a1,b1;
for (i = max; i>0;i--)
{ a1= a.substring(i-1,i);
b1= b.substring(i-1,i);
m = Integer.valueOf(a1);
n = Integer.valueOf(b1);
r = m + r + n;
if (r>= base)
{ a1 = String.valueOf(r-base);
r=1;
}
else
{ a1 = String.valueOf(r);
r=0;
}
res = a1 + res;
if ((r==1) && (i==1)) res = "1"+ res;
}
// Met la virgule s'il y a lieu
if (y != 0)
{ res = res.substring(0,res.length()-(y-1)) + ","+ res.substring(res.length()-(y-1),res.length());
// Supprime les zéros à droite
int j=0 ;
for (i= res.length();i > 0 ; i--)
{ if (res.charAt(i-1)!= '0') break;
j ++;
}
res = res.substring(0,res.length()-j);
if (j==(y-1))
{res = res + "0";}
}
return res;
}
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.