11111010111 1010 1011

Programar por Programar


¿Te gusta programar?
Inicio


Prueba en C++ de la librería BigNum_ltm

Un ejemplo de uso de BigNum_ltm.dll desde C++


Publicado: 19/Oct/2007
Actualizado: 22/oct/2007
Autor: Guillermo 'guille' Som


 

Contenido

Un ejemplo en C++ (compilado con Borland C++ 5.5) que usa la librería BigNum_ltm.dll.

No seas demasiado crítico con el código usado, que lo mío no es el C++, je, je.


 

El código de ejemplo en C++

En el código se indica cómo compilarlo con Borland C++ 5.5 y con Visual C++ de VS2005

//-----------------------------------------------------------------------------
// BigNum_ltm_t.cpp
// Para usar la DLL BigNum_ltm.dll
//
// ©Guillermo 'guille' Som, 2006
// Fecha: 07/Sep/2006
//
// Para compilar con BCC55:
// bcc -WC -P -O2 -IE:\gnuDev\libmath BigNum_ltm_t.cpp
//
// Para compilar con C++ de VS2005:
// cl /D "WIN32" /D "_CONSOLE" BigNum_ltm_t.cpp
//-----------------------------------------------------------------------------

#include <windows.h>
#include <stdio.h>

#include <tommath.h>

#define BigNum mp_int

//typedef void (CALLBACK* FPInicializar)();
typedef BSTR (CALLBACK* FPToString)(BigNum);
typedef void (CALLBACK* FPIni)(BigNum*);
typedef BigNum (CALLBACK* FPFromInt)(unsigned int);
typedef BigNum (CALLBACK* FPFromString)(BSTR);
typedef BigNum (CALLBACK* FPMul)(BigNum, BigNum);
typedef BigNum (CALLBACK* FPDiv)(BigNum, BigNum, BigNum*);

int main()
{
    HINSTANCE hDLL; // handle de la DLL
    int ret;
    // punteros a las funciones
    FPToString toString;
    FPIni ini;
    FPFromInt fromInt;
    FPFromString fromString;
    FPMul mul;
    FPDiv div;
    
    ret = 0;
    // Cargar la DLL
    hDLL = LoadLibrary("BigNum_ltm.dll");
    
    if( hDLL != NULL )
    {

        toString = (FPToString)GetProcAddress( hDLL, "BigNumToString");
        if( !toString )
        {
            printf("Error al asignar la función.");
            ret = 1;
        }
        ini = (FPIni)GetProcAddress( hDLL, "BigNumInit" );
        if( !ini )
        {
            printf("Error al asignar la función.");
            ret = 1;
        }
        fromInt = (FPFromInt)GetProcAddress( hDLL, "BigNumFromInt");
        if( !fromInt )
        {
            printf("Error al asignar la función.");
            ret = 1;
        }
        
        fromString = (FPFromString)GetProcAddress( hDLL, "BigNumFromString");
        if( !fromString )
        {
            printf("Error al asignar la función.");
            ret = 1;
        }
        mul = (FPMul)GetProcAddress( hDLL, "BigNumMul");
        if( !mul )
        {
            printf("Error al asignar la función.");
            ret = 1;
        }
        div = (FPDiv)GetProcAddress( hDLL, "BigNumDiv");
        if( !div )
        {
            printf("Error al asignar la función.");
            ret = 1;
        }
        // Si llegamos aquí, es que todo está bien
        BigNum n1, n2, r1;
        n1 = fromInt(12345);
        n2 = fromString((BSTR)"2345");
        
        // Multiplicamos los dos números
        r1 = mul(n1, n2);
        
        BSTR s;
        s = toString(n1);
        printf("%s x ", s);
        s = toString(n2);
        printf("%s = ", s);
        s = toString(r1);
        printf("%s\n", s);
        
        // Para comprobar que da el mismo resultado
        unsigned int u1 = 12345;
        unsigned int u2 = 2345;
        printf("%u x %u = %u\n", u1, u2, u1 * u2);
        printf("\n");
        
        n1 = fromString((BSTR)"100000000");
        n2 = fromString((BSTR)"12345678901234567890123456789");
        r1 = mul(n1, n2);
        s = toString(n1);
        printf("%s x ", s);
        s = toString(n2);
        printf("%s = ", s);
        //printf("%s x %s = ", s1, s2);
        s = toString(r1);
        printf("%s\n", s);
        
        BigNum r;
        ini(&r);
        n2 = fromInt(100);
        n1 = fromInt(3);

        r1 = div(n2, n1, &r);
        s = toString(r1);
        printf("resto= %s ", s);
        s = toString(r);
        printf("Divisor= %s\n", s);
        
    }
    FreeLibrary(hDLL);
    return ret;
}

Programar por programar... ¡porque te gusta programar!
Ir al índice principal
Ir al sitio del Guille - Ir a los foros del Guille

Has entrado usando el host www.programarporprogramar.org