public class MathAdd {
public static void main(String[] args){
String addA="30812";
String addB="208";
System.out.println(addA+"+"+addB+"="+strvalue(addA,addB));
}
public static String strvalue(String addA,String addB){
String strvalue="";
int lenA=addA.length();
int lenB=addB.length();
int templen=0;
//调整长度相同
if(lenA>=lenB){
templen=lenA-lenB;
addB=maxlen(addB,templen);
}else{
templen=lenB-lenA;
addA=maxlen(addA,templen);
}
char addcharA[]=addA.toCharArray();
char addcharB[]=addB.toCharArray();
int len=addcharA.length;
int valueC[]=new int[len];
for(int i=0;i
int a=Integer.parseInt(String.valueOf(addcharA[i]));
int b=Integer.parseInt(String.valueOf(addcharB[i]));
valueC[i]=a+b;
}//每项相加
int tmp=0;
//处理进位
for(int i=valueC.length-1;i>=0;i--){
if(valueC[i]>=10){
strvalue=String.valueOf(valueC[i]+tmp-10)+strvalue;
tmp=valueC[i]/10;
}else{
strvalue=String.valueOf(valueC[i]+tmp)+strvalue;
tmp=0;
}
}
return strvalue;
}
//调整长度,使其长度一样
private static String maxlen(String str,int templen){
String strmax=null;
StringBuffer buff=new StringBuffer();
for(int i=0;i buff.append("0");
}
strmax=buff.toString()+str;
return strmax;
}
}
【责编:Chuan】
--------------------next---------------------
阅读(270) | 评论(0) | 转发(0) |