// ============================================================ // // assign.cpp // // Copyright 2002, Dennis Meilicke and Rene Peralta // // ============================================================ // // Description: // // Methods for assigning values to a LargeNumber (ln). // // ============================================================ #include // ============================================================ void Assign( Base *A, long a ) { if( a == 0 ) { A->size = 0; A->sign = positive; } else { A->size = 1; if( a < 0 ) { A->sign = negative; A->digit[ 0 ] = ( 0 - a ); } else { A->sign = positive; A->digit[ 0 ] = a; } } } // ============================================================ void Assign( Base *A, unsigned long a ) { if( a == 0 ) { A->size = 0; A->sign = positive; } else { A->size = 1; A->sign = positive; A->digit[ 0 ] = a; } } // ============================================================ void Assign( Base *A, char *a ) // // This is a very simple minded character to binary conversion // routine. This thing is probably "slow as a dog", but... // // Possible improvements: // 1. Use numbers bigger than 10. That is, convert groups // of numbers all at once. // 2. A divide-and-conquer algorithm might work nicely, here. // 3. See Knuth for a base-conversion algorithm. Convert // the string to a base-10^n array. Then use a base conversion // algorithm to convert it to our base. This is probably what // ln2's algorithm was doing (who could tell?). // // Since this assignment function should not be called all that // often, is it neccessary to squeeze every cycle out of this? // { A->sign = positive; A->size = 0; sign_t newSign = positive; for( ; *a ; a++ ) { if( *a == '-' ) newSign = negative; else if( *a == '+' ) newSign = positive; else { //(*this) = 10 * (*this) + (list[i] - 48); Multiply( A, 10L ); add( A, (long)( *a - 48 ) ); } } A->sign = newSign; }