学习GTK+2.0,写了一个小计算器程序,发上来,基本上可以用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include <gtk/gtk.h> static GtkWidget *entry; gdouble mm=0.0; /* define mm for storing first number in operation */ gdouble nn=0.0; /* define nn for storing second number in operation */ gint operator=0; /* define an operator symbol for addition, subtrace, dividing, multiplication */ gboolean first=TRUE; /* define a flag for determining if has another operation before current input or not */ gboolean second=FALSE; /* deine a flag for nn if input or not input */ gboolean has_dot=FALSE; /* define a flag for determining if has dot in current input or not */ gboolean has_result=FALSE; /* define a flag for determing if has a result already or not */ gchar number[100]; /* define a buffer for storing number */ void clear() { int i; /* This function for purpose of clear all flags and numbers */ gtk_entry_set_text(GTK_ENTRY(entry), "0" ); mm=0.0; nn=0.0; first=FALSE; second=FALSE; has_dot=FALSE; has_result=FALSE; for (i=0; 100; i++) number[i]= '' ; } void number_clear() { int i; for (i=0; 100; i++) number[i]= '' ; } void calculate_last_time() { switch (operator) { case 1: /* Define the operation of addition */ if (second) { g_ascii_dtostr(number, 100, mm+nn); gtk_entry_set_text(GTK_ENTRY(entry), number); mm=mm+nn; /* Set the left operand to be result */ number_clear(); second=FALSE; } else { /* init second operand */ nn=0.0; number_clear(); } break ; case 2: /* Define the operation of subtrace */ if (second) { g_ascii_dtostr(number, 100, mm-nn); gtk_entry_set_text(GTK_ENTRY(entry), number); mm=mm-nn; number_clear(); second=FALSE; } else { /* init second operand (nn) */ nn=0.0; number_clear(); } break ; case 3: /* Define the operation of multiplication */ if (second) { g_ascii_dtostr(number, 100, mm*nn); gtk_entry_set_text(GTK_ENTRY(entry), number); mm=mm*nn; number_clear(); second=FALSE; } else { number_clear(); nn=0.0; } break ; case 4: /* Define the operation of dividing */ if (second) { g_ascii_dtostr(number, 100, mm/nn); gtk_entry_set_text(GTK_ENTRY(entry), number); mm=mm/nn; number_clear(); second=FALSE; } else { nn=0.0; number_clear(); } break ; default : number_clear(); } } void on_number_button_clicked(GtkWidget *button, gpointer data) { const gchar *num; /* record current input number as char */ num=gtk_button_get_label(GTK_BUTTON(button)); g_strlcat(number, num, 100); if (has_result) clear(); if (first==FALSE) { mm=g_strtod(number, NULL); } else { nn=g_strtod(number, NULL); second=TRUE; } gtk_entry_set_text(GTK_ENTRY(entry), number); /* display the number user input in entry box */ } void on_dot_button_clicked(GtkWidget *button, gpointer data) { if (has_dot) ; /* Nothing to do if current input has a dot already*/ else { has_dot=TRUE; g_strlcat(number, "." , 100); if (has_result) clear(); if (first==FALSE) mm=g_strtod(number, NULL); else { nn=g_strtod(number, NULL); second=TRUE; } } gtk_entry_set_text(GTK_ENTRY(entry), number); } void on_operator_button_clicked(GtkWidget *button, gpointer data) { switch (GPOINTER_TO_INT(data)) { case 1: calculate_last_time(); operator=1; has_result=FALSE; has_dot=FALSE; first=TRUE; break ; case 2: calculate_last_time(); operator=2; has_dot=FALSE; has_result=FALSE; first=TRUE; break ; case 3: calculate_last_time(); operator=3; has_dot=FALSE; has_result=FALSE; first=TRUE; break ; case 4: calculate_last_time(); operator=4; has_dot=FALSE; has_result=FALSE; first=TRUE; break ; } } void on_equal_button_clicked(GtkWidget *button, gpointer data) { if (!has_result) { calculate_last_time(); operator=0; number_clear(); has_dot=FALSE; has_result=TRUE; } } void on_clear_button_clicked(GtkWidget *button, gpointer data) { clear(); } int main( int argc, char *argv[]) { GtkWidget *window; GtkWidget *vbox; GtkWidget *hbox; GtkWidget *label; GtkWidget *button; gtk_init(&argc, &argv); window=gtk_window_new(GTK_WINDOW_TOPLEVEL); g_signal_connect(G_OBJECT(window), "destroy" , G_CALLBACK(gtk_main_quit), NULL); gtk_window_set_title(GTK_WINDOW(window), "Simple Calculator" ); gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_MOUSE); gtk_container_set_border_width(GTK_CONTAINER(window), 10); vbox=gtk_vbox_new(FALSE, 5); gtk_container_add(GTK_CONTAINER(window), vbox); /* Following content sets up label and clear button on main window */ hbox=gtk_hbox_new(FALSE, 5); label=gtk_label_new( "Calculator" ); gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0); button=gtk_button_new_with_label( "C" ); g_signal_connect(G_OBJECT(button), "clicked" , G_CALLBACK(on_clear_button_clicked), NULL); gtk_box_pack_end(GTK_BOX(hbox), button, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 5); /* Following content sets up entry box in main window */ hbox=gtk_hbox_new(FALSE, 0); entry=gtk_entry_new(); gtk_widget_set_direction(entry, GTK_TEXT_DIR_RTL); gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 5); /* Sets up first line of buttons */ hbox=gtk_hbox_new(TRUE, 5); button=gtk_button_new_with_label( "3" ); g_signal_connect(G_OBJECT(button), "clicked" , G_CALLBACK(on_number_button_clicked), NULL); gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 5); button=gtk_button_new_with_label( "2" ); g_signal_connect(G_OBJECT(button), "clicked" , G_CALLBACK(on_number_button_clicked), NULL); gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 5); button=gtk_button_new_with_label( "1" ); g_signal_connect(G_OBJECT(button), "clicked" , G_CALLBACK(on_number_button_clicked), NULL); gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 5); button=gtk_button_new_with_label( "+" ); g_signal_connect(G_OBJECT(button), "clicked" , G_CALLBACK(on_operator_button_clicked), (gpointer) 1); gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 5); gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 5); /* End of setting up first line of buttons */ /* Sets up second line of buttons */ hbox=gtk_hbox_new(TRUE, 5); button=gtk_button_new_with_label( "6" ); g_signal_connect(G_OBJECT(button), "clicked" , G_CALLBACK(on_number_button_clicked), NULL); gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 5); button=gtk_button_new_with_label( "5" ); g_signal_connect(G_OBJECT(button), "clicked" , G_CALLBACK(on_number_button_clicked), NULL); gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 5); button=gtk_button_new_with_label( "4" ); g_signal_connect(G_OBJECT(button), "clicked" , G_CALLBACK(on_number_button_clicked), NULL); gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 5); button=gtk_button_new_with_label( "-" ); g_signal_connect(G_OBJECT(button), "clicked" , G_CALLBACK(on_operator_button_clicked), (gpointer) 2); gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 5); gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 5); /* End of setting up second line of buttons */ /* Sets up third line of buttons */ hbox=gtk_hbox_new(TRUE, 5); button=gtk_button_new_with_label( "9" ); g_signal_connect(G_OBJECT(button), "clicked" , G_CALLBACK(on_number_button_clicked), NULL); gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 5); button=gtk_button_new_with_label( "8" ); g_signal_connect(G_OBJECT(button), "clicked" , G_CALLBACK(on_number_button_clicked), NULL); gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 5); button=gtk_button_new_with_label( "7" ); g_signal_connect(G_OBJECT(button), "clicked" , G_CALLBACK(on_number_button_clicked), NULL); gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 5); button=gtk_button_new_with_label( "*" ); g_signal_connect(G_OBJECT(button), "clicked" , G_CALLBACK(on_operator_button_clicked), (gpointer) 3); gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 5); gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 5); /* End of setting up third line of buttons */ /* Sets up fourth line of buttons*/ hbox=gtk_hbox_new(TRUE, 5); button=gtk_button_new_with_label( "0" ); g_signal_connect(G_OBJECT(button), "clicked" , G_CALLBACK(on_number_button_clicked), NULL); gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 5); button=gtk_button_new_with_label( "." ); g_signal_connect(G_OBJECT(button), "clicked" , G_CALLBACK(on_dot_button_clicked), NULL); gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 5); button=gtk_button_new_with_label( "=" ); g_signal_connect(G_OBJECT(button), "clicked" , G_CALLBACK(on_equal_button_clicked), NULL); gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 5); button=gtk_button_new_with_label( "/" ); g_signal_connect(G_OBJECT(button), "clicked" , G_CALLBACK(on_operator_button_clicked), (gpointer) 4); gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 5); gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 5); /* End of setting up fourth line of buttons */ clear(); gtk_widget_show_all(window); gtk_main(); return EXIT_SUCCESS; } |
编译命令
gcc -o calculator calculator.c `pkg-config --libs --clfags gtk+-2.0`
编译后运行,效果如图: