hello
I am beginner and I wrote an algorithm in BCB++ and I need it in pascal language. Does anyone here can rewrite this algorithm or a program for me in pascal language?
thanks a lotCode:TBigInteger TBigInteger::operator + ( const TBigInteger &r ) const { TBigInteger temp; if( !data || !r.data ) return temp; if( sign && !r.size ) { temp = -*this; return ( r - temp ); } if( !sign && r.sign ) { temp = -r; return ( *this - temp ); } unsigned maxsize = size > r.size ? size : r.size; unsigned minsize = size > r.size ? r.size : size; unsigned i, c = 0; vector< char > v; for( i = 0 ; i < minsize ; i++ ) { v.push_back( ( ( r.data[ i ] - '0' ) + ( data[ i ] - '0' ) + c ) % 10 + '0' ); c = ( ( r.data[ i ] - '0' ) + ( data[ i ] - '0' ) + c ) / 10; } if( size == maxsize ) for( i = minsize; i < maxsize ; i++ ) { v.push_back( ( ( data[ i ] - '0' ) + c ) % 10 + '0' ); c = ( ( data[ i ] - '0' ) + c ) / 10; } else for( i = minsize; i < maxsize ; i++ ) { v.push_back( ( ( r.data[ i ] - '0' ) + c ) % 10 + '0' ); c = ( ( r.data[ i ] - '0' ) + c ) / 10; } if( c ) v.push_back( c + '0' ); temp.data = new char[ v.size() ]; if( temp.data ) { temp.size = v.size(); for( i = 0 ; i < temp.size ; i++ ) temp.data[ i ] = v[ i ]; temp.sign = sign; } return temp; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TBigInteger TBigInteger::operator - ( const TBigInteger &r ) const { TBigInteger temp; if( !data || !r.data ) return temp; if( sign && !r.sign ) { temp = ( -*this + r ); temp.sign = !temp.sign; return temp; } if( !sign && r.sign ) { temp = r; temp.sign = !temp.sign; return ( *this + temp ); } if( *this < r ) { temp = r - *this; temp.sign = !temp.sign; return temp; } TBigInteger t1, t2; if( sign ) { t1 = r; t2 = *this; } else { t1 = *this; t2 = r; } temp.sign = false; vector< char > result( t1.size ); unsigned i, j; for( i = 0 ; i < t2.size ; i++ ) { if( t1.data[ i ] < t2.data[ i ] ) { t1.data[ i ] += 10; t1.data[ i + 1 ]--; } result[ i ] += t1.data[ i ] - t2.data[ i ]; } for( i = t2.size ; i < t1.size ; i++ ) { if( t1.data[ i ] < 0 ) { t1.data[ i ] += 10; t1.data[ i + 1 ]--; } result[ i ] += t1.data[ i ] - '0'; } for( i = 0 ; i < t1.size ; i++ ) if( result[ t1.size - i - 1 ] != 0 ) break; temp.data = new char[ t1.size - i ]; if( temp.data ) { temp.size = t1.size - i; for( j = 0 ; j < temp.size ; j++ ) temp.data[ j ] = result[ j ] + '0'; } return temp; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TBigInteger TBigInteger::operator * ( const TBigInteger &r ) const { TBigInteger temp; if( !data || !r.data ) return temp; if( *this == "0" || r == "0" ) { temp.data = new char[ 1 ]; if( temp.data ) { temp.size = 1; temp.sign = false; temp.data[ 0 ] = '0'; return temp; } } vector< unsigned > v( size + r.size ); unsigned i, j, t, c; for( i = 0 ; i < size ; i++ ) { c = 0; for( j = 0 ; j < r.size ; j++ ) { t = v[ i + j ]; v[ i + j ] = ( t + c + ( data[ i ] - '0' ) * ( r.data[ j ] - '0') ) % 10; c = ( t + c + ( data[ i ] - '0' ) * ( r.data[ j ] - '0' ) ) / 10; } if( c ) v[ i + j ] = c; } for( i = 0 ; i < v.size() ; i++ ) if( v[ v.size() - i - 1 ] != 0 ) break; temp.data = new char[ v.size() ]; if( temp.data ) { temp.size = v.size() - i; temp.sign = ( sign && !r.sign ) || ( !sign && r.sign ); for( j = 0 ; j < temp.size ; j++ ) temp.data[ j ] = v[ j ] + '0'; } return temp; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TBigInteger TBigInteger::operator / ( const TBigInteger &r ) const { TBigInteger result; if( !data || !r.data || r == "0" ) return result; if( size < r.size ) { result = "0"; return result; } vector< unsigned > temp; vector< char > rem; unsigned i, j, c; bool t; for( i = 0 ; i < size ; i++ ) rem.push_back( data[ i ] ); for( i = 0 ; i <= size - r.size ; i++ ) { c = 0; t = true; while( t ) { if( i == 0 ) { for( j = 0 ; j < r.size && t ; j++ ) { if( rem[ size - i - j - 1 ] < r.data[ r.size - j - 1 ] ) t = false; if( rem[ size - i - j - 1 ] > r.data[ r.size - j - 1 ] ) break; } } else if( rem[ size - i ] == '0' ) for( j = 0 ; j < r.size && t ; j++ ) { if( rem[ size - i - j - 1 ] < r.data[ r.size - j - 1 ] ) t = false; if( rem[ size - i - j - 1 ] > r.data[ r.size - j - 1 ] ) break; } if( t ) { for( j = 0 ; j < r.size ; j++ ) { if( rem[ size - r.size - i + j ] < r.data[ j ] ) { rem[ size - r.size - i + j ] += 10; rem[ size - r.size - i + j + 1 ]--; } rem[ size - r.size - i + j ] -= ( r.data[ j ] - '0' ); } c++; } } temp.push_back(c); } for( i = 0 ; i < temp.size() ; i++ ) if( temp[ i ] != 0 ) break; if( i == temp.size() ) result = "0"; else { result.data = new char[ temp.size() - i ]; if( result.data ) { result.size = temp.size() - i; result.sign = false; for( j = 0 ; j < result.size ; j++ ) result.data[ result.size - j - 1 ] = temp[ j + i ] + '0'; } } if( sign && !r.sign ) { result = result + 1; result.sign = true; } else if( !sign && r.sign ) result.sign = true; else if( sign && r.sign ) result = result + 1; return result; }
What version of Pascal?
It doesn't differ. one of the latest versions that you can!
thank u so much
Pascal, Turbo Pascal, Object Pascal, Delphi X? They aren't the same.
Turbo Pascal
At this point, you appear to be trying to convert C++ to Turbo Pascal. The code you posted is the overloading of the +,-,*,/ operators for a C++ class. This is one small piece of a much larger chunk of code. The rest of the code is required to be able to do a conversion.
here is the complete code of the program.
It can */-+ on big integers.
I have the code written in C# language.I can send it for you if you want.
please help me! I need it for tomorrow.
thanks a lot
You do understand that Pascal won't allow you to overload operators, right? You'll have to create Plus, minus, divide, times functions on structs that are called big integers. It's been a long time since I did Pascal coding, so there's no way I can do this for you by tomorrow, but you should be able to extract the logic. Understand: C++ lets you do things in a way that Turbo Pascal just doesn't support. You have to extract the logic, but you can't do it exactly the same.
I want an algorithm in pascal that can plus,minus,divide on integers with 30 characters.
do you know or have anything ?
Files to Download -- from Harry J. Smith look for the PrimeFA - Multi-precision prime factor algorithm entry
GMP - The GNU Pascal Manual is a library for this
http://www.math.niu.edu/~rusin/known-math/94/bignum lists projects
And there's always google: pascal multi precision integer - Google Search
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks