lc3吧 关注:5贴子:33
  • 3回复贴,共1
  • 218.94.159.*
public class BigInteger {
     // The sign of this integer - true for a positive number, and false
     // otherwise
     private boolean sign = true;
     // digits[0] is the most significant digit of the integer, and
     // the last element of this array is the least significant digit.
     // For example, if we have a BigInteger of value 34, then
     // digits[0] = 3 and digits[1] = 4.
     private byte[] digits;
     private int length;
     public int lengths;
     public BigInteger() {
         this.digits = new byte[1];
         this.digits[0] = 0;
     }
     public BigInteger(byte[] digits) {
         this.digits = digits;
     }
     /**
      * Initializes a <code>BigInteger</code> according to a string. The form of
      * <code>numberStr</code> is a string consisting of all digits ranging from
      * 0 to 9, following an OPTIONAL minus symbol (i.e., "-"). For example,
      * "1234567891234567" and "-17788399934334388347734" are both valid.
      *
      * @param numberStr
      *             a number expressed as a string
      */
     public BigInteger(String numberStr) {
        
        
        
     char[]     num =numberStr.toCharArray();
         int   len=num.length;
         byte[] c=new byte[len];
      for(int i=0;i<len;i++){
          c[i]=Byte.parseByte(String.valueOf(num[i]));
      }
         this.lengths=len;
        
         // YOU FILL THIS IN
         // Note: You should parse the string and initialize the "digits" array
         // properly.
         // You may also need to set the "sign" variable to a correct value.
     }



1楼2010-05-14 15:21回复
    • 218.94.159.*

         /**
          * Performs addition.
          *
          * @param another
          *             another <code>BigInteger</code> object
          * @return this+another
          */
         /**
          * Performs addition.
          *
          * @param num
          *             an integer
          * @return this+num
          */
             public BigInteger add(int num) {
                 BigInteger one=new BigInteger();
                 String e=Integer.toString(num);
                 char[]     num2 =e.toCharArray();
                 int len=num2.length;
                 byte[] f=new byte[lengths];
                 for(int j=0;j<len;j++){
                     byte a=Byte.parseByte(String.valueOf(num2[j]));
                    
                 }
                  byte tem=0;
            
                  for(int i=lengths-1;i>=0;i--){
                      if(f[i]>=10){
                        tem=(byte) (f[i]/10);
                        f[i-1]=(byte) (f[i-1]+tem);
                      }else{
                          tem=0;
                        
                      }
                  }
    


    2楼2010-05-14 15:21
    回复
      • 218.94.159.*

                                
                 
                  return   one;
              
               // YOU FILL THIS IN
      }
           /**
            * Performs subtraction.
            *
            * @param another
            *             another <code>BigInteger</code> object
            * @return this-another
            */
           /**
            * Performs subtraction.
            *
            * @param num
            *             an integer
            */
           public BigInteger subtract(int num) {
               BigInteger two=new BigInteger();
               String e=Integer.toString(num);
               char[]     num2 =e.toCharArray();
               int len=num2.length;
               byte[] f=new byte[lengths];
               for(int index=0;index<len;index++){
               byte a=Byte.parseByte(String.valueOf(num2[index]));
               f[index]=(byte) (a+digits[lengths-index-1]);}
               for(int index1=length-1;index1>=0;index1--){
                   if(f[index1]<0){
                       f[index1-1]--;
                       f[index1]=(byte)(10-f[index1-1]);
                   }else{
                       f[index1]=f[index1];
                   }
                   for(int index2=0;index2<length;index2++){
                       this.digits[index2]=f[index2];
                   }
      


      3楼2010-05-14 15:21
      回复
        • 218.94.159.*
                    
                 }
                 return two;
                
                 // YOU FILL THIS IN
             }
             /**
              * Performs multiplication.
              *
              * @param another
              *             another <code>BigInteger</code> object
              * @return this*another
              */
             public BigInteger multiply(int num) {
                 BigInteger three =new BigInteger();
                 return   three;
                          
                        
                     }
            
                
                 // YOU FILL THIS IN
            
             public String toString() {
                 StringBuffer buf = new StringBuffer();
                 if (!sign) {
                     buf.append("-");
                 }
                 for (byte d : digits) {
                     buf.append(d);
                 }
                 return buf.toString();
             }
             public static void main(String[] args) {
                 BigInteger i1 = new BigInteger("999999999999999999");
                 BigInteger i2 = i1.add(1);
                 System.out.println("1000000000000000000"); // the output should be 1000000000000000000
                 BigInteger i3 = i2.multiply(1);
                 System.out.println("999999999999999999000000000000000000"); // expected: 999999999999999999000000000000000000
                 System.out.println(("999999999999999999000000000000000003")); // expected: 999999999999999999000000000000000003
             }
        }


        4楼2010-05-14 15:21
        回复