gtk编写计算器

一剑行天下 posted @ 2008年11月01日 19:20 in gtk编程 , 2114 阅读

#include<gtk/gtk.h>
#include<stdlib.h>

static GtkWidget *entry;
gint count=0;
gdouble nn=0;
gdouble mm=0;
gint s=0;
gboolean first=TRUE;
gboolean have_dot=FALSE;
gboolean have_result=FALSE;
gchar number[100];

void clear_all(void)
{
        gint i=0;
        gtk_entry_set_text(GTK_ENTRY(entry),"");
        nn=0;
        mm=0;
        s=0;
        count=0;
        first=TRUE;
        have_dot=FALSE;
        have_result=FALSE;
        for(i=0;i<100;i++)
                number[i]='\0';
}

void on_num_clicked(GtkButton *button,gpointer data)
{
        const gchar *num;
        gint i;
        if(have_result)
                clear_all();
        if(count==6)
                return;
        count++;
        num=gtk_button_get_label(GTK_BUTTON(button));
        i=g_strlcat(number,num,100);
        if(first)
                nn=strtod(number,NULL);
        else
                mm=strtod(number,NULL);
        gtk_entry_set_text(GTK_ENTRY(entry),number);
}

void on_dot_clicked(GtkButton *button,gpointer data)
{ gint i;
        if(have_result)
                clear_all();
        if(have_dot==FALSE)
        {
                have_dot=TRUE;
                i=g_strlcat(number,".",100);
                gtk_entry_set_text(GTK_ENTRY(entry),number);
        }

}


void on_clear_clicked(GtkButton *button,gpointer data)
{
        clear_all();
}

void on_suan_clicked(GtkButton *button,gpointer data)
{
        gint i;
        switch(GPOINTER_TO_INT(data))
        {
                case 1:
                        gtk_entry_set_text(GTK_ENTRY(entry),"");
                        s=1;
                        first=FALSE;
                        count=0;
                        break;
                case 2:
                        gtk_entry_set_text(GTK_ENTRY(entry),"");
                        s=2;
                        first=FALSE;
                        count=0;
                        break;
                case 3:
                        gtk_entry_set_text(GTK_ENTRY(entry),"");
                        s=3;
                        first=FALSE;
                        count=0;
                        break;
                case 4:
                        gtk_entry_set_text(GTK_ENTRY(entry),"");
                        s=4;
                        first=FALSE;
                        count=0;
                        break;
        }
        have_dot=FALSE;
        for(i=0;i<100;i++)
                number[i]='\0';
}

void on_eq_clicked(GtkButton *button,gpointer data)
{
        double numb;
        gchar *result;
        gchar num[100];
        switch(s)
        {
                case 1:
                        numb=nn+mm;
                        break;
                case 2:
                        numb=nn-mm;
                        break;
                case 3:
                        numb=nn*mm;
                        break;
                case 4:
                        if(mm==0)
                        {
                                gtk_entry_set_text(GTK_ENTRY(entry),"error");
                                break;
                        }
                        numb=nn/mm;
                        break;
        }
        result=g_ascii_dtostr(num,100,numb);
        gtk_entry_set_text(GTK_ENTRY(entry),result);
        have_result=TRUE;
}


int main(int argc,char* argv[])
{
        GtkWidget *window;
        GtkWidget *vbox;
        GtkWidget *hbox,*hbox1,*hbox2,*hbox3,*hbox4;
        GtkWidget *button;
        GtkWidget *label;
        GtkWidget *label1;


        gtk_init(&argc,&argv);

        window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_window_set_default_size(GTK_WINDOW(window),500,300);
        gtk_container_set_border_width(GTK_CONTAINER(window),10);
        gtk_window_set_title(GTK_WINDOW(window),"计算器");
        gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
        g_signal_connect(G_OBJECT(window),"delete_event",G_CALLBACK(gtk_main_quit),NULL);

        vbox=gtk_vbox_new(TRUE,0);
        gtk_container_add(GTK_CONTAINER(window),vbox);
        hbox=gtk_hbox_new(TRUE,0);
        gtk_box_pack_start(GTK_BOX(vbox),hbox,TRUE,TRUE,5);
        label=gtk_label_new("Calculator");
        gtk_box_pack_start(GTK_BOX(hbox),label,TRUE,TRUE,5);
        label1=gtk_label_new(" ");
gtk_box_pack_start(GTK_BOX(hbox),label1,TRUE,TRUE,5);

time_t now;
        struct tm *l_time;
        gchar buf[100];
        now=time((time_t *)NULL);
        l_time=localtime(&now);
        sprintf(buf,"%d:%d:%d",l_time->tm_hour,l_time->tm_min,l_time->tm_sec);
        gtk_label_set_text(GTK_LABEL(label1),buf);



        button=gtk_button_new_with_label("C");
        gtk_box_pack_start(GTK_BOX(hbox),button,TRUE,TRUE,5);
        g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_clear_clicked),NULL);

        entry=gtk_entry_new();
        gtk_editable_set_editable(GTK_EDITABLE(entry),FALSE);
        gtk_widget_set_direction(entry,GTK_TEXT_DIR_RTL);
        gtk_box_pack_start(GTK_BOX(vbox),entry,TRUE,TRUE,5);

        hbox1=gtk_hbox_new(FALSE,0);
        gtk_box_pack_start(GTK_BOX(vbox),hbox1,TRUE,TRUE,5);
        button=gtk_button_new_with_label("3");
        gtk_box_pack_start(GTK_BOX(hbox1),button,TRUE,TRUE,5);
        g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_num_clicked),NULL);
        button=gtk_button_new_with_label("2");
        gtk_box_pack_start(GTK_BOX(hbox1),button,TRUE,TRUE,5);
        g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_num_clicked),NULL);
        button=gtk_button_new_with_label("1");
        gtk_box_pack_start(GTK_BOX(hbox1),button,TRUE,TRUE,5);
        g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_num_clicked),NULL);
        button=gtk_button_new_with_label("+");
        gtk_box_pack_start(GTK_BOX(hbox1),button,TRUE,TRUE,5);
        g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_suan_clicked),(gpointer)1);


        hbox2=gtk_hbox_new(FALSE,0);
        gtk_box_pack_start(GTK_BOX(vbox),hbox2,TRUE,TRUE,5);
        button=gtk_button_new_with_label("6");
        gtk_box_pack_start(GTK_BOX(hbox2),button,TRUE,TRUE,5);
        g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_num_clicked),NULL);
        button=gtk_button_new_with_label("5");
        gtk_box_pack_start(GTK_BOX(hbox2),button,TRUE,TRUE,5);
        g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_num_clicked),NULL);
        button=gtk_button_new_with_label("4");
        gtk_box_pack_start(GTK_BOX(hbox2),button,TRUE,TRUE,5);
        g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_num_clicked),NULL);
        button=gtk_button_new_with_label("-");
        gtk_box_pack_start(GTK_BOX(hbox2),button,TRUE,TRUE,5);
        g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_suan_clicked),(gpointer)2);


        hbox3=gtk_hbox_new(FALSE,0);
        gtk_box_pack_start(GTK_BOX(vbox),hbox3,TRUE,TRUE,5);
        button=gtk_button_new_with_label("9");
        gtk_box_pack_start(GTK_BOX(hbox3),button,TRUE,TRUE,5);
        g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_num_clicked),NULL);
        button=gtk_button_new_with_label("8");
        gtk_box_pack_start(GTK_BOX(hbox3),button,TRUE,TRUE,5);
        g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_num_clicked),NULL);
        button=gtk_button_new_with_label("7");
        gtk_box_pack_start(GTK_BOX(hbox3),button,TRUE,TRUE,5);
        g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_num_clicked),NULL);
        button=gtk_button_new_with_label("*");
        gtk_box_pack_start(GTK_BOX(hbox3),button,TRUE,TRUE,5);
        g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_suan_clicked),(gpointer)3);


        hbox4=gtk_hbox_new(FALSE,0);
        gtk_box_pack_start(GTK_BOX(vbox),hbox4,TRUE,TRUE,5);
        button=gtk_button_new_with_label("0");
        gtk_box_pack_start(GTK_BOX(hbox4),button,TRUE,TRUE,5);
        g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_num_clicked),NULL);
        button=gtk_button_new_with_label(".");
        gtk_box_pack_start(GTK_BOX(hbox4),button,TRUE,TRUE,5);
        g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_dot_clicked),NULL);
        button=gtk_button_new_with_label("=");
        gtk_box_pack_start(GTK_BOX(hbox4),button,TRUE,TRUE,5);
        g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_eq_clicked),NULL);
        button=gtk_button_new_with_label("/");
        gtk_box_pack_start(GTK_BOX(hbox4),button,TRUE,TRUE,5);
        g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_suan_clicked),(gpointer)4);


        gtk_widget_show_all(window);
        gtk_main();
        return FALSE;
}

gtk图


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter