Closed Thread
Results 1 to 10 of 10

Thread: c to pascal

  1. #1
    rahafrouz is offline Newbie
    Join Date
    Jul 2009
    Posts
    5
    Rep Power
    0

    c to pascal

    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?

    Code:
    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;
    }
    thanks a lot
    Attached Files Attached Files

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: c to pascal

    What version of Pascal?
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    rahafrouz is offline Newbie
    Join Date
    Jul 2009
    Posts
    5
    Rep Power
    0

    Re: c to pascal

    It doesn't differ. one of the latest versions that you can!
    thank u so much

  5. #4
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: c to pascal

    Pascal, Turbo Pascal, Object Pascal, Delphi X? They aren't the same.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #5
    rahafrouz is offline Newbie
    Join Date
    Jul 2009
    Posts
    5
    Rep Power
    0

    Re: c to pascal

    Turbo Pascal

  7. #6
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: c to 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.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  8. #7
    rahafrouz is offline Newbie
    Join Date
    Jul 2009
    Posts
    5
    Rep Power
    0

    Unhappy Re: C to Pascal

    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
    Attached Files Attached Files

  9. #8
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: c to pascal

    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.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  10. #9
    rahafrouz is offline Newbie
    Join Date
    Jul 2009
    Posts
    5
    Rep Power
    0

    Re: c to pascal

    I want an algorithm in pascal that can plus,minus,divide on integers with 30 characters.
    do you know or have anything ?

  11. #10
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: c to pascal

    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
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Pascal
    By Hunter100 in forum Pascal and Delphi
    Replies: 13
    Last Post: 02-16-2010, 01:21 PM
  2. GUI in Pascal
    By Davide in forum Pascal and Delphi
    Replies: 7
    Last Post: 01-25-2010, 04:35 AM
  3. Help! Pascal
    By jen91 in forum Pascal and Delphi
    Replies: 1
    Last Post: 02-05-2009, 04:56 PM
  4. Dev Pascal Help
    By Pyreforge in forum Pascal and Delphi
    Replies: 3
    Last Post: 11-16-2008, 07:27 AM
  5. Dev pascal help
    By ragingfear in forum General Programming
    Replies: 3
    Last Post: 11-21-2007, 01:23 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts