Alex Tanil
Posts: 10
Nickname: alextanil
Registered: May, 2006
|
|
adding
|
Posted: May 9, 2006 8:26 AM
|
|
take a look at this i know its to much... my problem is this now!!! as you have understand i have two coins 9.12 an 7.789 with their integer parts and the decimal parts separate. I have solved the problem (not vey nice though) with the carry but i have now this problem. If the decimal parts are of diferent length like above i want to do this adding 789+120 and not 789+12. But they dont have a fixed length. I have try to solve this but it does work. its the method as comments. what have i done wrong!!!!!!!
private int mhkosMegaluterouDecimalPart(Nomisma a, Nomisma b) { String wString = Integer.toString(a.getDecimal()); int w = wString.length(); String lString = Integer.toString(b.getDecimal()); int l = lString.length(); int proswrinos = 3;
if (w >= l) proswrinos = w; else proswrinos = l; //System.out.println(proswrinos); return proswrinos; } /** private void au3isiMikousDecimal(Nomisma a,Nomisma b){ int r=0; if(compareDecimalParts(a,b)){ r = mhkosMegaluterouDecimalPart(a, b); //System.out.println(r); int i; int v = 1; for (i = 1; i < (r-1); i++) v = v * 10; String wString = Integer.toString(a.getDecimal()); int w = wString.length(); String lString = Integer.toString(b.getDecimal()); int l = lString.length(); if (l > w) a.decimal = a.decimal * v; // System.out.println(a.getDecimal()); if (w > l) b.decimal = b.decimal * v; //System.out.println(b.getDecimal()); } } */
private int addDecimals(Nomisma a, Nomisma b) {
mhkosMegaluterouDecimalPart(a,b); //au3isiMikousDecimal(a,b); int apotelesma = a.getDecimal() + b.getDecimal(); if(elegxosCratoumenou(apotelesma,a,b)) apotelesma=apotelesma-pra3iCratoumenou(apotelesma);
//System.out.println("apotelesma prosthesis decimals: "+apotelesma); return apotelesma;
} private int addInetegers(Nomisma a,Nomisma b){ int apotelesma = a.decimal + b.decimal;
//System.out.println(elegxosCratoumenou(apotelesma,a,b)); if(elegxosCratoumenou(apotelesma,a,b)) au3isiIntegerPart(b);
int apoteleInt=a.integer+b.getInteger(); //System.out.println(apoteleInt); return apoteleInt; } /** * @return boolean */
private boolean elegxosCratoumenou(int apot,Nomisma a,Nomisma b) { String cratoumeno =Integer.toString(apot); int megethosApot = cratoumeno.length(); //System.out.println("to megethos tou apotelesmatos einai: "+megethosApot); //System.out.println("To mikos tou megaluterou decimal einai: "+mhkosMegaluterouDecimalPart(a,b));
return megethosApot>mhkosMegaluterouDecimalPart(a,b);
} private int pra3iCratoumenou(int apot){ String cratoumeno =Integer.toString(apot); int megethosApot = cratoumeno.length(); //System.out.println("megethos apotelesmatos: "+megethosApot);
int i; int t =1; for(i=1;i<megethosApot;i++){ t=t*10; } //System.out.println("megethos dunamis: "+t); return t; } private int au3isiIntegerPart(Nomisma b){ b.integer=b.integer+1; //System.out.println("au3imeno integer: "+b.integer);
return b.integer; } public String pra3iProsthesiNomismatwn(Nomisma a,Nomisma b){
if(a.getSymbol().endsWith(b.getSymbol()) || a.getSign()==b.getSign()) return "to apotelesma tis prosthesis einai: "+a.getSymbol()+a.getSign()+addInetegers(a,b)+"."+addDecimals(a,b);
else return "den einai dunati h prosthesi"; } }
|
|