JAVA大整数加减乘除源代码完全个人编写
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
res3=res3+"0.";//小数点前面为 0
num1=num1+"0";//被除数后面加个 0
//判断两个数的第一个字符的大小
if(num1.charAt(0)<num2.charAt(0)){
} while(res2.indexOf("0") == 0){ res2 = res2.substring(1); } System.out.print(sign + res2);
} }
public static void chu(String num1,String num2){ int count=0;//统计相减的次数,即为商 //判断符号位 if(num2.charAt(0)=='-'&&num1.charAt(0)=='-'){ sign=""; num1=num1.substring(1); num2=num2.substring(1); } if(num2.charAt(0)=='-'&&num1.charAt(0)!='-'){ sign="-"; num2=num2.substring(1); } if(num2.charAt(0)!='-'&&num1.charAt(0)=='-'){ sign="-"; num1=num1.substring(1); }
}
} System.out.print(sign + res1);
} }
public static void jia(String num1,String num2){
if(num2.charAt(0)!='-'&&num1.charAt(0)!='-'||num2.charAt(0)=='-'&&num1.charAt(0)=='-') {
import java.util.Scanner;
public class Jianfajiafa{ static String res1="";//减法结果值 static String res2="";//加法结果值 static String res3="";//除法结果值 static String sign = ""; //多个方法共同使用一个符号位,要注意符号位的初始值 public static void jian(String num1,String num2){
num2 = tmp; } } //将 num1 跟 num2 位数置相同 while(num2.length() < num1.length()){
num2 = "0" + num2; } // System.out.println("num2="+num2);
int index = num1.length() - 1; int borrow = 0; //借位 while(index >= 0){
if(num1.equals(num2)){ System.out.println("0");
} //判断 num2,num1 第一位是不是负号,若有负号需要另作处理
if(num2.charAt(0)!='-'&&num1.charAt(0)!='-'||num2.charAt(0)=='-'&&num1.charAt(0)=='-') {
while(num1.length() < num2.length())
num1 = "0" + num1;
}
else{
//将 num1 跟 num2 位数置相同
while(num2.length() < num1.length())
num2 = "0" + num2;
}
//System.out.println("num2="+num2);
} // System.out.println("***");
while(res1.indexOf("0") == 0){ res1 = res1.substring(1);
} System.out.print(sign + res1); }else if(num1.charAt(0)=='-'&&num2.charAt(0)!='-'||num1.charAt(0)!='-'&&num2.charAt(0)=='-'){
else sign="-";
} else if(num1.length()>num2.length()){
sign="-"; }else{
sign=""; } if(num1.length() < num2.length()
|| (num1.length() == num2.length() && num1.compareTo(num2) < 0)){ String tmp = num1; num1 = num2; num2 = tmp; } } else{ //处理长度 num1<num2 或者长度 num1=num2 if(num1.length() < num2.length() || (num1.length() == num2.length() && num1.compareTo(num2) < 0)){ sign = "-"; String tmp = num1; num1 = num2;
}来自百度文库
res1 = left + res1;
//
System.out.println("res="+res);
//
System.out.println("borrow="+borrow);
index--;
//判断最高位是否有进位,若有加 1
if(index==-1&&borrow==1){
res1='1'+res1;
int index = num1.length() - 1;
// System.out.println("aaaindex="+index); int borrow = 0; //进位
while(index >= 0){
//System.out.println(num1.charAt(index)+"*-*"+num2.charAt(index)+"*-*"+borrow);
if(num1.charAt(0)=='-'){ num1=num1.substring(1);//将负号去除 sign = "-"; //符号位
} if(num2.charAt(0)=='-'){
num2=num2.substring(1);//将负号去除 sign = ""; //符号位 }
if(num1.length() < num2.length() ){ //将 num1 跟 num2 位数置相同
int left = (num1.charAt(index)-'0')+(num2.charAt(index)-'0') + borrow;
//System.out.println("left="+left);
if(left >=10){
borrow = 1;
left -= 10;
}else{
borrow=0;
//相同输出 1
if(num1.equals(num2)){
System.out.println(sign+"1");
}
if(num1.length() < num2.length()
||(num1.length() == num2.length() && num1.compareTo(num2)<0)){
if(num1.charAt(0)=='-'){ num1=num1.substring(1);//将负号去除 num2=num2.substring(1);//将负号去除
//符号位的判断 if(num1.length() == num2.length()){
if(num1.compareTo(num2) < 0) sign="";
else if(num1.charAt(0)=='-'&&num2.charAt(0)!='-'||num1.charAt(0)!='-'&&num2.charAt(0)=='-'){
//符号位的判断 if(num1.charAt(0)=='-'){
num1=num1.substring(1);//将负号去除 if(num1.length()>num2.length()){
//System.out.println(num1.charAt(index)+"*-*"+num2.charAt(index)+"*-*"+borrow); int left = num1.charAt(index) - num2.charAt(index) + borrow;
// System.out.println("left="+left); if(left < 0){ borrow = -1; left += 10; }else{ borrow=0; } res1 = left + res1; // System.out.println("res="+res); index--;
if(num1.charAt(0)=='-'){ num1=num1.substring(1);//将负号去除
num2=num2.substring(1);//将负号去除
sign="-";//符号位 } else{
sign=""; }
//将 num1 跟 num2 位数置相同 if(num1.length() < num2.length() ){ while(num1.length() < num2.length()) num1 = "0" + num1; } else{ //将 num1 跟 num2 位数置相同 while(num2.length() < num1.length()) num2 = "0" + num2; } int index = num1.length() - 1;//统计字符的个数减一 int borrow = 0; //进位 while(index >= 0){ //相加进位处理 过程 int left = (num1.charAt(index)-'0')+(num2.charAt(index)-'0') + borrow; if(left >=10){ borrow = 1; left -= 10; }else{ borrow=0; } res2 = left + res2; index--; //判断最高位是否有进位,若有加 1 if(index==-1&&borrow==1){ res2='1'+res2; } } System.out.print(sign + res2); }
sign="-"; }else{
sign=""; } } if(num2.charAt(0)=='-'){ num2=num2.substring(1);//将负号去除 if(num1.length()>num2.length()){
sign=""; }else{
sign="-"; } } if(num1.length() == num2.length()){ if(num1.compareTo(num2) < 0)
sign=""; else
sign="-"; } //交换,使 num1 大,num2 小 if(num1.length() < num2.length()
|| (num1.length() == num2.length() && num1.compareTo(num2) < 0)){
String tmp = num1; num1 = num2; num2 = tmp; } //将 num1 跟 num2 位数置相同 while(num2.length() < num1.length()){ num2 = "0" + num2; } //减法的过程 int index = num1.length() - 1;//计数 int borrow = 0; //借位 while(index >= 0){ int left = num1.charAt(index) - num2.charAt(index) + borrow; if(left < 0){ borrow = -1; left += 10; }else{ borrow=0; } res2 = left + res2; index--;