public classDemo6 {public static voidmain(String[]args){
System.out.println(add("-989","989"));
System.out.println(sub("890","-235"));
System.out.println(divide("-21","22"));
System.out.println(mtp("-123","4567"));
System.out.println(remainder("22","21"));
}public staticString add(String s1,String s2){
StringBuffer res=newStringBuffer();if(s1.contains("-")&&s2.contains("-")){
res.append("-");
s1=s1.replace("-", "");
s2=s2.replace("-", "");
}else if((!s1.contains("-")&&s2.contains("-"))){return sub(s1,s2.replace("-", ""));
}else if((s1.contains("-")&&!s2.contains("-"))){return sub(s2,s1.replace("-", ""));
}int len =Math.abs(s1.length()-s2.length());if(s1.length()>s2.length()){
s2=castSame(s2,len);
}else{
s1=castSame(s1,len);
}int n=0;for(int i=s1.length()-1;i>=0;i--){int temp = (s1.charAt(i)-'0'+s2.charAt(i)-'0'+n);if(i==0){
res.append(temp%10).append(temp/10==0?"":temp/10);
}else{
res.append(temp%10);
n=temp/10;
}
}returnkickZero(String.valueOf(res.reverse()));
}public staticString sub(String s1,String s2){boolean flag = false;
StringBuffer res=newStringBuffer();if(s1.contains("-")&&s2.contains("-")){return sub(s2.replace("-", ""),s1.replace("-", ""));
}else if((!s1.contains("-")&&s2.contains("-"))){return add(s1,s2.replace("-", ""));
}else if((s1.contains("-")&&!s2.contains("-"))){return "-"+add(s2,s1.replace("-", ""));
}if(!isGreater(s1,s2)){
flag= true;
String temp=s1;
s1=s2;
s2=temp;
}
s2= castSame( s2,s1.length()-s2.length());int n=0;for(int i=s1.length()-1;i>=0;i--){
Integer temp= s1.charAt(i)-'0'-s2.charAt(i)+'0'-n;
temp= i==0?(temp==0?null:temp):temp;
res.append(temp==null?"":temp>=0?temp:temp+10);
n=temp==null?0:temp<0?1:0;
}return kickZero(flag?String.valueOf(res.append("-").reverse()):String.valueOf(res.reverse()));
}public staticString divide(String s1,String s2){
StringBuffer res= newStringBuffer();int c=0;if(s1.contains("-")){
s1=s1.replace("-", "");
c++;
}if(s2.contains("-")){
s2=s2.replace("-", "");
c++;
}if(c==1)
res.append("-");if("0".equals(s2)){return "ERROR";
}if(!isGreater(s1,s2)){return "0";
}int n=s2.length();
String temp=s1.substring(0,n);while(true){int count=0;while(sub(temp,s2).contains("-")){
temp+=s1.charAt(n);
n++;
}while(true){
count++;
temp=sub(temp,s2);if(sub(temp,s2).contains("-")){break;
}
}
res.append(count);if(n>=s1.length()){returnString.valueOf(res);
}
}
}public staticString mtp(String s1,String s2){
String res= "";int c=0;if(s1.contains("-")){
s1=s1.replace("-", "");
c++;
}if(s2.contains("-")){
s2=s2.replace("-", "");
c++;
}if(!isGreater(s1,s2)){
String t=s1;
s1=s2;
s2=t;
}
String temp=s1;for(int i=s2.length()-1;i>=0;i--){for(int j=0;j
res=add(res,temp);
}
temp=String.valueOf(new StringBuffer(temp).append("0"));
}return c==1?"-"+res:res;
}public staticString remainder(String s1,String s2){if("0".equals(s2)){return "ERROR";
}if(!isGreater(s1,s2)){returns1;
}
StringBuffer res= newStringBuffer();int n=s2.length();
String temp=s1.substring(0,n);while(true){int count=0;
System.out.println("n:"+n);while(sub(temp,s2).contains("-")){
temp+=s1.charAt(n);
n++;
}while(true){
count++;
temp=sub(temp,s2);if(sub(temp,s2).contains("-")){break;
}
}
res.append(count);if(n>=s1.length()){returntemp;
}
}
}public static boolean isGreater(String s1,String s2){//890 235
if(s1.length()>s2.length()){return true;
}else if(s1.length()
}else if(s1.length()==s2.length()){int i=0;while(i
}else if(s1.charAt(i)>s2.charAt(i)){return true;
}
i++;
}
}return true;
}public static String castSame(String s,intlen){
StringBuffer sb=newStringBuffer();for(int i=0;i
sb.append("0");
}
s=String.valueOf(sb.append(s));returns;
}public staticString kickZero(String s){int i=0;while(s.length()>1){if('0'==s.charAt(0)){
s=s.substring(1);
}else{break;
}
}returns;
}
}