qq_32294231
qq_32294231
2015-10-27 13:47
采纳率: 50%
浏览 2.2k
已采纳

求java大整数的四则运算解题思路,把具体用什么知识点讲解出来

package org.suanfa.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test1 {
private boolean isPositive=true;
private String number="0";
public Test1() {

}
public Test1(String number) {
String flag=number.substring(0, 1);
if(flag.equals("-")){
this.isPositive=false;
this.number=number.substring(1);
}else{
this.isPositive=true;
this.number = number;
}

}
private int compare(String a,String b){
int MaxLen=0;
if(a.length()>=b.length()){
MaxLen=a.length();
while(b.length() b="0"+b;
}
}else{
MaxLen=b.length();
while(a.length() a="0"+a;
}
}
if(a.compareTo(b)>0)

return 1;
else if(a.compareTo(b)==0)
return 0;
else
return -1;
}

public Test1 add(Test1 bigint){
Test1 bi=new Test1();
String a=this.number;

String b=bigint.number;
if((this.isPositive&&bigint.isPositive)||(!this.isPositive&&!bigint.isPositive)){//同符号
bi.isPositive=this.isPositive;
bi.number=absAdd(a,b);

}else{

if(compare(a,b)>0){

bi.isPositive=this.isPositive;

bi.number=absSub(a,b);

}else if(compare(a,b)==0){

bi.isPositive=true;
bi.number="0";
}else{

bi.isPositive=bigint.isPositive;

bi.number=absSub(a,b);
}
}
return bi;

}

public Test1 sub(Test1 bigint){
Test1 bi=new Test1();
String a=this.number;
String b=bigint.number;
if((this.isPositive&&bigint.isPositive)||(!this.isPositive&&!bigint.isPositive)){

if(compare(a,b)>0){

bi.isPositive=this.isPositive;

bi.number=absSub(a,b);
}else if(compare(a,b)==0){

bi.isPositive=true;
bi.number="0";
}else{

bi.isPositive=!bigint.isPositive;

bi.number=absSub(a,b);
}
}else{

bi.isPositive=this.isPositive;

bi.number=absAdd(a,b);

}
return bi;
}

public Test1 multi(Test1 bigint){

Test1 bi=new Test1();
String a=this.number;
String b=bigint.number;
if(a.equals("0")||b.equals("0")){

bi.isPositive=true;
bi.number="0";
}else if((this.isPositive&&bigint.isPositive)||(!this.isPositive&&!bigint.isPositive)){
bi.isPositive=true;

bi.number=absMulti(a,b);
}else{

bi.isPositive=false;

bi.number=absMulti(a,b);
}
return bi;

}
public Test1 div(Test1 bigint){

Test1 bi=new Test1();
String a=this.number;
String b=bigint.number;
if(a.equals("0")||b.equals("0")){

bi.isPositive=true;
bi.number="0";
}else if((this.isPositive&&bigint.isPositive)||(!this.isPositive&&!bigint.isPositive)){
bi.isPositive=true;
bi.number=absDiv(a,b);
}else{

bi.isPositive=false;
bi.number=absDiv(a,b);
}
return bi;
}
private String absAdd(String a, String b) {

int MaxLen=0;
if(a.length()>=b.length()){

MaxLen=a.length();
while(b.length() b="0"+b;
}
}else{
MaxLen=b.length();
while(a.length() a="0"+a;
}
}
String str = "";
int JW = 0; // 进位
for (int i = MaxLen - 1; i >= 0; i--) {

int tempA = a.charAt(i)-'0';
int tempB = b.charAt(i)-'0';
int temp;
if (tempA + tempB + JW >= 10 && i != 0) { //如果有进位
temp = tempA + tempB + JW - 10;
JW = 1;
} else {
temp = tempA + tempB + JW;
JW = 0;
}
str = String.valueOf(temp) + str;
}
while(str.length()>1&&str.charAt(0)=='0'){

str=str.substring(1);
}
return str;
}
private String absSub(String a, String b) {

int MaxLen=0;
if(a.length()>=b.length()){
MaxLen=a.length();
while(b.length() b="0"+b;
}
}else{
MaxLen=b.length();
while(a.length() a="0"+a;
}
}
if(a.compareTo(b) String temp=a;
a=b;
b=temp;
}
String str = "";
int JW = 0; // 借位
for (int i = MaxLen - 1; i >= 0; i--) {
int tempA = a.charAt(i)-'0';

int tempB = b.charAt(i)-'0';

int temp;
if (tempA - JW - tempB < 0 && i != 0) {

temp = tempA + 10 - JW - tempB;
JW = 1;
} else {
temp = tempA - JW - tempB;
JW = 0;
}
str = temp + str;
}
while(str.length()>1&&str.charAt(0)=='0'){
str=str.substring(1);
}
return str;
}
private String absMulti(String a,String b){

String result="0";

if(a.equals("0")||b.equals("0"))

return "0";
for(int i = b.length()-1; i >=0; i--){

int plustimes=b.charAt(i)-'0';

String atemp=a;

for(int j=1;j<=b.length()-1-i;j++){

atemp=atemp+"0";

}
for(int m=1;m<=plustimes;m++){

result=absAdd(result,atemp);

}
}
return result;

}
private String absDiv(String a,String b){

if(a.equals("0")||b.equals("0"))

return "0";
String result="";

String dealnum="";

String left="";

for(int index=0;index dealnum=left+a.substring(index,index+1);
while(left.length()>1&&left.charAt(0)=='0'){
left=left.substring(1);
}
while(dealnum.length()>1&&dealnum.charAt(0)=='0'){

dealnum=dealnum.substring(1);
}
if(compare(dealnum,b) result+="0";
left=dealnum;
continue;
}else if(compare(dealnum,b)==0){
result+="1";
left="0";
}else{
for(int i=9;i>=0;i--){

String s=absMulti(String.valueOf(i),b);

if(compare(s,dealnum)>0){

continue;

}else if(compare(s,dealnum)==0){

result+=String.valueOf(i);

left="0";
break;
}else{

result+=String.valueOf(i);
left=absSub(s,dealnum);

break;
}
}
}
}
while(result.length()>1&&result.charAt(0)=='0'){

result=result.substring(1);
}
return result;
}

private String absMod(String a,String b){
if(a.equals("0")||b.equals("0"))
return "0";
@SuppressWarnings("unused")
String result="";
String dealnum="";
String left="";
for(int index=0;index dealnum=left+a.substring(index,index+1);
while(left.length()>1&&left.charAt(0)=='0'){
left=left.substring(1);
}
while(dealnum.length()>1&&dealnum.charAt(0)=='0'){
dealnum=dealnum.substring(1);
}
if(compare(dealnum,b) result+="0";
left=dealnum;
continue;
}else if(compare(dealnum,b)==0){
result+="1";
left="0";
}else{
for(int i=9;i>=0;i--){
String s=absMulti(String.valueOf(i),b);
if(compare(s,dealnum)>0){
continue;
}else if(compare(s,dealnum)==0){
result+=String.valueOf(i);
left="0";
break;
}else{
result+=String.valueOf(i);
left=absSub(s,dealnum);
break;
}
}
}
}
while(left.length()>1&&left.charAt(0)=='0'){
left=left.substring(1);
}
return left;
}

public String toString(){
if(this.number.equals("0")){
return this.number;
}
String str=this.isPositive?"":"-";
str=str+this.number;
return str;
}
public String getString() {

 String str = null;
 BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
 try {
         str = buf.readLine();
     }catch(IOException e) {
         e.printStackTrace();
 }
 return str;

}
public void Operator() {
System.out.println("输入enter开始");
while(true) {
String str1 = "#";
String reg="^-?[0-9]\d*$";

String temp = this.getString();
System.out.println("输入第一个数字");
String a = this.getString();
if(str1.equals(a)) {

System.out.println("结束!!!");
break;
}
if(!a.matches(reg)){
System.out.println("非整数字串!");
continue;
}
System.out.println("输入第二个数字");
String b = this.getString();
if(str1.equals(b)) {
System.out.println("结束!!!");
break;
}
if(!b.matches(reg)){
System.out.println("非整数字串!");
continue;
}
Test1 bigintA=new Test1(a);
Test1 bigintB=new Test1(b);
Test1 result=bigintA.add(bigintB);
System.out.println("加法结果:"+result.toString());
Test1 result1=bigintA.sub(bigintB);
System.out.println("减法结果:"+result1.toString());
Test1 result3=bigintA.multi(bigintB);
System.out.println("乘法结果:"+result3.toString());
Test1 result4=bigintA.div(bigintB);
System.out.println("除法结果:"+result4.toString());
}

}
public static void main(String[] args) {
new Test1().Operator();
}
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

相关推荐