public void calculate( ) { string ns = _numerator.ToString( ); string ds = _denominator.ToString( ); // int pointLocation = ns.Length; ns = ns.PadRight( ns.Length + _fractionalLimit, '0' ); Integer num = new Integer( ); num.Value = ns; Integer denom = new Integer( ); denom.Value = ds; num.divide( denom ); _store = num.Value; if ( _numerator.ToString( ).Length == _denominator.ToString( ).Length ) _store = _store.Substring( 1 ); else { _store = _store.PadLeft( ( ( ( _denominator.ToString( ).Length - _numerator.ToString( ).Length ) - 1 ) + _store.Length ), '0' ); } }
/// <summary> /// divides /// </summary> /// <param name="divisor"></param> /// <returns>remainder</returns> public Integer divide( Integer divisor ) { if( !isBiggerThan( divisor ) ) throw new ArgumentOutOfRangeException( ); Integer dividend = new Integer( ); dividend.Value = store.Substring( 0, divisor.Value.Length - 1 ); string answer = ""; for( int pos = divisor.Value.Length - 1; pos < store.Length; pos++ ) { dividend.Value += store.Substring( pos, 1 ); int count = 0; while( isBiggerThan( dividend.Value, divisor.Value ) ) { dividend.subtract( divisor ); count++; } answer += count.ToString( ); } store = answer; return dividend; }
/// <summary> /// adds the specified number to the value held by this number /// </summary> /// <param name="value"></param> public void add( Integer value ) { value.Value = trim( value.Value ); store = trim( store ); if( store.Length > value.ToString( ).Length ) { store = trim( add( value.ToString( ).PadLeft( store.Length, '0' ), 0, store ) ); } else if( store.Length == value.ToString( ).Length ) { store = trim( add( value.ToString( ), 0, store ) ); } else if( store.Length < value.ToString( ).Length ) { store = store.PadLeft( value.ToString( ).Length, '0' ); store = trim( add( value.ToString( ), 0, store ) ); } else { throw new InvalidOperationException( ); } }
private string multiplyValue( Integer value, string original ) { ArrayList answers = new ArrayList( ); for( int i = 0; i < value.Value.Length; i++ ) { Integer currAns = new Integer( ); // prepend zeroes currAns.Value = ""; currAns.Value.PadRight( i, '0' ); for( int x = 0; x < i; x++ ) { currAns.Value += "0"; } int carry = 0; for( int j = original.Length; j != 0; j-- ) { int result = ( int.Parse( value.Value.Substring( value.Value.Length - ( i + 1 ), 1 ) ) * int.Parse( original.Substring( j - 1, 1 ) ) ) + carry; string resultstr = result.ToString( ); if( resultstr.Length > 1 ) { carry = int.Parse( resultstr.Substring( 0, resultstr.Length - 1 ) ); resultstr = resultstr.Substring( resultstr.Length - 1 ); } else { carry = 0; } currAns.Value = resultstr + currAns.Value; } currAns.Value = carry + currAns.Value; answers.Add( currAns ); } Integer export = new Integer( ); export.Value = "0"; foreach( Integer item in answers ) { export.add( item ); } return trim( export.Value ); }
private string subtractValue( Integer value, Integer subractor ) { if( value.Value.Length > subractor.Value.Length ) { subractor.Value = subractor.Value.PadLeft( value.Value.Length, '0' ); return doSubtract( value.Value, subractor.Value ).TrimStart( '0' ); } else if( value.Value.Length == subractor.Value.Length ) { return doSubtract( value.Value, subractor.Value ).TrimStart( '0' ); } else if( value.Value.Length < subractor.Value.Length ) { value.Value = value.Value.PadLeft( subractor.Value.Length, '0' ); return doSubtract( value.Value, subractor.Value ).TrimStart( '0' ); } else throw new InvalidOperationException( ); }
/// <summary> /// subtracts the value of the specified number from the current number. /// </summary> /// <param name="value"></param> public void subtract( Integer value ) { Integer foo = new Integer( ); foo.Value = store; store = subtractValue( foo, value ); }
private string factorial( Integer value ) { if( value.Value.TrimStart( '0' ) == "" ) return "1"; Integer one = new Integer( ); one.Value = "1"; Integer valminusone = new Integer( ); valminusone.Value = subtractValue( value, one ); return trim( multiplyValue( value, factorial( valminusone ) ).TrimStart( '0' ) ); }
/// <summary> /// divides this number by provided divisor number, result stored by this number. /// </summary> /// <param name="divisor"></param> /// <returns>remainder</returns> public Integer slowDivide( Integer divisor ) { Integer counter = new Integer( ); Integer incrementor = new Integer( ); incrementor.Value = "1"; counter.Value = "0"; while( isBiggerThan( divisor ) ) { counter.add( incrementor ); this.subtract( divisor ); } Integer remainder = new Integer( ); remainder.Value = store; store = counter.Value; return remainder; }
/// <summary> /// raises the stored value within this number to the exponent provided. /// </summary> /// <param name="exponent"></param> public void pow( int exponent ) { Integer orig = new Integer( ); orig.Value = trim( store ); while( exponent > 1 ) { this.multiply( orig ); exponent--; store = trim( store ); } store = store.TrimStart( '0' ); }
/// <summary> /// multiplies the specified number with the value held by this number. /// </summary> /// <param name="value"></param> public void multiply( Integer value ) { store = multiplyValue( value, store ); }
/// <summary> /// multiplies the specified value with the value held by this number. /// </summary> /// <param name="value"></param> public void multiply( short value ) { Integer orig = new Integer( ); orig.Value = store; while( value > 1 ) { this.add( orig ); value--; } }
/// <summary> /// true if I am bigger than it. /// </summary> /// <param name="value"></param> /// <returns></returns> public bool isBiggerThan( Integer value ) { return isBiggerThan( store, value.Value ); }