Partage
  • Partager sur Facebook
  • Partager sur Twitter

[GDK] Convertisseur binaire<->decimal

Besoin de conseil pour l'amélioration

    6 mars 2006 à 18:48:12

    Salut
    Je viens de finir mon premier programme GUI fait avec la bibliotheque GTK.C'est un convertisseur binaire / base 10
    Je vous le montre car j'ai besoin d'aide pour l'ameliorer (et qui peut vous etre utile :-° ).

    Mon premier probleme c'est du C : je suis pour l'instant limité au valeurs d'un int. Je recupere ce qui est ecrit dans un char (ou gchar c pareil) et je le convertit en chiffre puis je fais les modif qu il faut et ensuite je le reconvertit en char pour l'afficher.
    Pour cele j'utilise itoa() qui prend que les int en parametres donc je suis limité a 9 chiffres ce qui fait 511 au max en binaire (ca fait pas beaucoup..).

    Est ce qu'il existe une fonction qui fait comme itoa() mais avec des doubles?

    Mon deuxieme probleme c'est avec GTK. J'ai essaier de connecter mes GTKEntry a un event "insert_text" mais quand je fais ca le programme bug.(operation non conforme et tout le tralala :( )

    Essayer de rajouter :
    g_signal_connect(G_OBJECT(pApp->pEntry[0]), "insert_text", G_CALLBACK(decbin), (gpointer*) pApp);
    Chez moi ca plante et je comprend pas pourquoi :'(


    Merci d'éclairer ma lanterne


    L'exe

    et la source:
    #include <stdlib.h>
    #include <gtk/gtk.h>
    #include <ctype.h>

    #define MAXCHAR 9

    //une structure pour pouvoir acceder grace a un seul pointeur a tous les widget
    struct _MainWindow
    {
        GtkWidget *pWindow;
        GtkWidget *pTable;
        GtkWidget *pEntry[2];
        GtkWidget *pButton[3];
        GtkWidget *pLabel[2];
    };
    typedef struct _MainWindow MainWindow;


    //prototypes
    void decbin(GtkWidget *pEntry‚ gpointer data);
    void bindec(GtkWidget *pEntry‚ gpointer data);
    void clear(GtkWidget *pEntry‚ gpointer data);
    gdouble formattext(char* sText‚gint base);
    char* itoabindec(int nbre‚char* sText‚ int basedenbre);

    //debut main
    int main(int argc‚ char **argv)
    {
        MainWindow *pApp;

     
        gtk_init(&argc‚ &argv);

        pApp = g_malloc(sizeof(MainWindow));


        pApp–>pWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_window_set_default_size(GTK_WINDOW(pApp–>pWindow)300100);
        gtk_window_set_title(GTK_WINDOW(pApp–>pWindow)"Convertisseur binaire");
        gtk_window_set_position(GTK_WINDOW(pApp–>pWindow)‚ GTK_WIN_POS_CENTER);
        g_signal_connect(G_OBJECT(pApp–>pWindow)"destroy"‚ G_CALLBACK(gtk_main_quit)NULL);
        gtk_window_set_resizable(GTK_WINDOW(pApp–>pWindow)0);

        /* Creation et insertion de la table 6 lignes 6 colonnes */
        pApp–>pTable=gtk_table_new(75TRUE);
        gtk_container_add(GTK_CONTAINER(pApp–>pWindow)‚ GTK_WIDGET(pApp–>pTable));

        /* Creation des boutons */
        pApp–>pButton[0]= gtk_button_new_from_stock(GTK_STOCK_GOTO_BOTTOM);
    g_signal_connect(G_OBJECT(pApp–>pButton[0])"clicked"‚ G_CALLBACK(decbin)(gpointer*) pApp);
        pApp–>pButton[1]= gtk_button_new_from_stock(GTK_STOCK_CLEAR);
    g_signal_connect(G_OBJECT(pApp–>pButton[1])"clicked"‚ G_CALLBACK(clear)(gpointer*) pApp);
        pApp–>pButton[2]= gtk_button_new_from_stock(GTK_STOCK_GOTO_TOP);
    g_signal_connect(G_OBJECT(pApp–>pButton[2])"clicked"‚ G_CALLBACK(bindec)(gpointer*) pApp);

        /* Creation des GtkEntry */
        pApp–>pEntry[0] = gtk_entry_new();
        pApp–>pEntry[1] = gtk_entry_new();
        gtk_entry_set_text(GTK_ENTRY(pApp–>pEntry[0])"0");
        gtk_entry_set_text(GTK_ENTRY(pApp–>pEntry[1])"0");

    gtk_entry_set_max_length(GTK_ENTRY(pApp–>pEntry[0])‚MAXCHAR);
    gtk_entry_set_max_length(GTK_ENTRY(pApp–>pEntry[1])‚MAXCHAR);

    //g_signal_connect(G_OBJECT(pApp–>pEntry[0])‚ "insert_text"‚ G_CALLBACK(decbin)‚ (gpointer*) pApp);

        /* Creation des label */
        pApp–>pLabel[0]=gtk_label_new("Nombre en base 10: ");
        pApp–>pLabel[1]=gtk_label_new("Nombre en binaire: ");

        /* Insertion des boutons‚des label‚ et des entrées*/
        gtk_table_attach(GTK_TABLE(pApp–>pTable)‚ pApp–>pButton[0]
            1234
            GTK_EXPAND | GTK_FILL‚GTK_EXPAND | GTK_FILL‚
            10);
        gtk_table_attach(GTK_TABLE(pApp–>pTable)‚ pApp–>pButton[1]
            2334
            GTK_EXPAND | GTK_FILL‚ GTK_EXPAND | GTK_FILL‚
            10);
        gtk_table_attach(GTK_TABLE(pApp–>pTable)‚ pApp–>pButton[2]
            3434
            GTK_EXPAND | GTK_FILL‚ GTK_EXPAND | GTK_FILL‚
            10);

        gtk_table_attach(GTK_TABLE(pApp–>pTable)‚ pApp–>pEntry[1]
            2456
            GTK_EXPAND | GTK_FILL‚ GTK_EXPAND | GTK_FILL‚
            00);
        gtk_table_attach(GTK_TABLE(pApp–>pTable)‚ pApp–>pEntry[0]
            2412
            GTK_EXPAND | GTK_FILL‚ GTK_EXPAND | GTK_FILL‚
            00);

        gtk_table_attach(GTK_TABLE(pApp–>pTable)‚ pApp–>pLabel[1]
            1256
            GTK_EXPAND | GTK_FILL‚ GTK_EXPAND | GTK_FILL‚
            00);
        gtk_table_attach(GTK_TABLE(pApp–>pTable)‚ pApp–>pLabel[0]
            1212
            GTK_EXPAND | GTK_FILL‚ GTK_EXPAND | GTK_FILL‚
            00);


        gtk_widget_show_all(pApp–>pWindow);

        gtk_main();

        return EXIT_SUCCESS;
    }
    //fin main



    void decbin(GtkWidget *pEntry‚ gpointer data)
    {
        double nbre=0;
        const gchar *sText;
        char sText2[MAXCHAR+1];
        MainWindow *pApp;
     
       
        /* Recuperation de data */
        pApp = (MainWindow*) data;

        /* Recuperation du texte contenu dans le GtkEntry */
        sText = gtk_entry_get_text(GTK_ENTRY(pApp–>pEntry[0]));


    strcpy(sText2‚sText);

    nbre=formattext(sText2‚10);//enleve les caracteres indesirables
    if (nbre>511) nbre=511; //limit actuelle

        //ecriture dans l'autre pEntry
    itoa(nbre‚sText2‚2);//ca convertit directe en base 2
        gtk_entry_set_text(GTK_ENTRY(pApp–>pEntry[1])‚sText2);

        //mise a jour de l'entry d origine en cas de lettres
    itoa(nbre‚sText2‚10);
        gtk_entry_set_text(GTK_ENTRY(pApp–>pEntry[0])‚sText2);

    //gtk_widget_show(pApp–>pEntry[0]);

    }




    void bindec(GtkWidget *pEntry‚ gpointer data)
    {
        double nbre=0;
        const gchar *sText;
        char sText2[MAXCHAR+1];
        MainWindow *pApp;
       

       
        /* Recuperation de data */
        pApp = (MainWindow*) data;

        /* Recuperation du texte contenu dans le GtkEntry */
        sText = gtk_entry_get_text(GTK_ENTRY(pApp–>pEntry[1]));


    strcpy(sText2‚sText);

    nbre=formattext(sText2‚2);




        //ecriture dans l'autre pEntry
    itoabindec(nbre‚sText2‚2);
        gtk_entry_set_text(GTK_ENTRY(pApp–>pEntry[0])‚sText2);
        //correction en cas d'erreur
    itoa(nbre‚sText2‚10);
        gtk_entry_set_text(GTK_ENTRY(pApp–>pEntry[1])‚sText2);

    }



    double formattext(char* sText‚gint base)
    {
    double nbre=0;
    gint i=0;
    guint espace=0;
    gchar* temp;

    if (base<2) base=2;
    else if (base>36) base=36;//pour linstant limite aux base qui s"ecrivent avec des chiffres


    while (sText[i]!='\0')
          {
          if (sText[i]>=97 && sText[i]<=122) sText[i]–=32;

          if (!((sText[i]>=48 && sText[i]<=48+(base–1)) || (sText[i]>=65 && sText[i]<=65+(base–11))))
                      {sText[i]=' ';espace=1;}

          i++;
          }

    i––;

    while (i>=0 && espace==1)
    {
          if (sText[i]==' ') 
          {
     sText[i]='\0';
     temp=&(sText[i])+1;
     strcat(sText‚temp);
          }

    i––;
    }

    nbre=atof(sText);

    return nbre;
    }



    char* itoabindec(int nbre‚char* sText‚ int basedenbre)
    {
    int i=0;
    int poids=1;

    itoa(nbre‚sText‚10);

    while (sText[i]!='\0') i++;

    nbre=0;
    i––;

    while(i>=0)
        {
        nbre+=poids*(int)(sText[i]48);

        poids*=2;
        i––;
        }

    itoa(nbre‚sText‚10);

    return sText;
    }


    void clear(GtkWidget *pEntry‚ gpointer data)
    {
        MainWindow *pApp;
       
        /* Recuperation de data */
        pApp = (MainWindow*) data;

    //effacement du texte des 2 pEntry
        gtk_entry_set_text(GTK_ENTRY(pApp–>pEntry[0])"0");
        gtk_entry_set_text(GTK_ENTRY(pApp–>pEntry[1])"0");

    }


    • Partager sur Facebook
    • Partager sur Twitter

    [GDK] Convertisseur binaire<->decimal

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown