{"id":44,"date":"2009-10-03T16:52:40","date_gmt":"2009-10-03T13:52:40","guid":{"rendered":"http:\/\/yesybl.org\/blogcn\/2009\/10\/03\/44\/"},"modified":"2019-07-06T04:03:48","modified_gmt":"2019-07-05T20:03:48","slug":"%e4%b8%80%e6%ac%be%e5%be%88%e7%ae%80%e5%8d%95%e7%9a%84%e8%ae%a1%e7%ae%97%e5%99%a8%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/www.yesybl.com\/?p=44","title":{"rendered":"\u4e00\u6b3e\u5f88\u7b80\u5355\u7684\u8ba1\u7b97\u5668\u7a0b\u5e8f"},"content":{"rendered":"\n<p>\u5b66\u4e60GTK\uff0b2.0\uff0c\u5199\u4e86\u4e00\u4e2a\u5c0f\u8ba1\u7b97\u5668\u7a0b\u5e8f\uff0c\u53d1\u4e0a\u6765\uff0c\u57fa\u672c\u4e0a\u53ef\u4ee5\u7528\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;string.h&gt;\n#include &lt;gtk\/gtk.h&gt;\nstatic GtkWidget *entry;\ngdouble mm=0.0; \/* define mm for storing first number in operation *\/\ngdouble nn=0.0; \/* define nn for storing second number in operation *\/\ngint operator=0; \/* define an operator symbol for addition, subtrace, dividing, multiplication *\/\ngboolean first=TRUE; \/* define a flag for determining if has another operation before current input or not *\/\ngboolean second=FALSE; \/* deine a flag for nn if input or not input *\/\ngboolean has_dot=FALSE; \/* define a flag for determining if has dot in current input or not *\/\ngboolean has_result=FALSE; \/* define a flag for determing if has a result already or not *\/\ngchar number&#91;100]; \/* define a buffer for storing number *\/\n\nvoid clear()\n{\n  int i;\n  \/* This function for purpose of clear all flags and numbers *\/\n  gtk_entry_set_text(GTK_ENTRY(entry), &quot;0&quot;);\n  mm=0.0;\n  nn=0.0;\n  first=FALSE;\n  second=FALSE;\n  has_dot=FALSE;\n  has_result=FALSE;\n  for (i=0; 100; i++)\n    number&#91;i]='';\n}\n\nvoid number_clear()\n{\n  int i;\n  for (i=0; 100; i++)\n    number&#91;i]='';\n}\n\nvoid calculate_last_time()\n{\n  switch (operator)\n    {\n    case 1:\n      \/* Define the operation of addition *\/\n      if(second)\n\t{\n\t  g_ascii_dtostr(number, 100, mm+nn);\n\t  gtk_entry_set_text(GTK_ENTRY(entry), number);\n\t  mm=mm+nn; \/* Set the left operand to be result *\/\n\t  number_clear();\n\t  second=FALSE;\n\t}\n      else\n\t{\n\t  \/* init second operand *\/\n\t  nn=0.0;\n\t  number_clear();\n\t}\n      break;\n    case 2:\n      \/* Define the operation of subtrace *\/\n      if(second)\n\t{\n\t  g_ascii_dtostr(number, 100, mm-nn);\n\t  gtk_entry_set_text(GTK_ENTRY(entry), number);\n\t  mm=mm-nn;\n\t  number_clear();\n\t  second=FALSE;\n\t}\n      else\n\t{\n\t  \/* init second operand (nn) *\/\n\t  nn=0.0;\n\t  number_clear();\n\t}\n      break;\n    case 3:\n      \/* Define the operation of multiplication *\/\n      if(second)\n\t{\n\t  g_ascii_dtostr(number, 100, mm*nn);\n\t  gtk_entry_set_text(GTK_ENTRY(entry), number);\n\t  mm=mm*nn;\n\t  number_clear();\n\t  second=FALSE;\n\t}\n      else\n\t{\n\t  number_clear();\n\t  nn=0.0;\n\t}\n      break;\n    case 4:\n      \/* Define the operation of dividing *\/\n      if(second)\n\t{\n\t  g_ascii_dtostr(number, 100, mm\/nn);\n\t  gtk_entry_set_text(GTK_ENTRY(entry), number);\n\t  mm=mm\/nn;\n\t  number_clear();\n\t  second=FALSE;\n\t}\n      else\n\t{\n\t  nn=0.0;\n\t  number_clear();\n\t}\n      break;\n    default:\n      number_clear();\n    }\n}\n\nvoid on_number_button_clicked(GtkWidget *button, gpointer data)\n{\n  const gchar *num; \/* record current input number as char *\/\n  num=gtk_button_get_label(GTK_BUTTON(button));\n  g_strlcat(number, num, 100);\n  if(has_result)\n    clear();\n\n  if(first==FALSE)\n    {\n      mm=g_strtod(number, NULL);\n    }\n  else\n    {\n      nn=g_strtod(number, NULL);\n      second=TRUE;\n    }\n\n  gtk_entry_set_text(GTK_ENTRY(entry), number); \/* display the number user input in entry box *\/\n}\n\nvoid on_dot_button_clicked(GtkWidget *button, gpointer data)\n{\n  if(has_dot)\n    ; \/* Nothing to do if current input has a dot already*\/\n  else\n    {\n      has_dot=TRUE;\n      g_strlcat(number, &quot;.&quot;, 100);\n      if(has_result)\n\tclear();\n\n      if (first==FALSE)\n\tmm=g_strtod(number, NULL);\n      else\n\t{\n\t  nn=g_strtod(number, NULL);\n\t  second=TRUE;\n\t}\n    }\n  gtk_entry_set_text(GTK_ENTRY(entry), number);\n}\n\nvoid on_operator_button_clicked(GtkWidget *button, gpointer data)\n{\n\n  switch (GPOINTER_TO_INT(data))\n    {\n    case 1:\n      calculate_last_time();\n      operator=1;\n      has_result=FALSE;\n      has_dot=FALSE;\n      first=TRUE;\n      break;\n    case 2:\n      calculate_last_time();\n      operator=2;\n      has_dot=FALSE;\n      has_result=FALSE;\n      first=TRUE;\n      break;\n    case 3:\n      calculate_last_time();\n      operator=3;\n      has_dot=FALSE;\n      has_result=FALSE;\n      first=TRUE;\n      break;\n    case 4:\n      calculate_last_time();\n      operator=4;\n      has_dot=FALSE;\n      has_result=FALSE;\n      first=TRUE;\n      break;\n    }\n}\n\nvoid on_equal_button_clicked(GtkWidget *button, gpointer data)\n{\n  if (!has_result)\n    {\n      calculate_last_time();\n      operator=0;\n      number_clear();\n      has_dot=FALSE;\n      has_result=TRUE;\n    }\n}\n\nvoid on_clear_button_clicked(GtkWidget *button, gpointer data)\n{\n  clear();\n}\n\nint main(int argc, char *argv&#91;])\n{\n  GtkWidget *window;\n  GtkWidget *vbox;\n  GtkWidget *hbox;\n  GtkWidget *label;\n  GtkWidget *button;\n\n  gtk_init(&amp;argc, &amp;argv);\n  window=gtk_window_new(GTK_WINDOW_TOPLEVEL);\n  g_signal_connect(G_OBJECT(window), &quot;destroy&quot;, G_CALLBACK(gtk_main_quit), NULL);\n  gtk_window_set_title(GTK_WINDOW(window), &quot;Simple Calculator&quot;);\n  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_MOUSE);\n  gtk_container_set_border_width(GTK_CONTAINER(window), 10);\n\n  vbox=gtk_vbox_new(FALSE, 5);\n  gtk_container_add(GTK_CONTAINER(window), vbox);\n\n  \/* Following content sets up label and clear button on main window *\/\n  hbox=gtk_hbox_new(FALSE, 5);\n  label=gtk_label_new(&quot;Calculator&quot;);\n  gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);\n\n  button=gtk_button_new_with_label(&quot;C&quot;);\n  g_signal_connect(G_OBJECT(button), &quot;clicked&quot;, G_CALLBACK(on_clear_button_clicked), NULL);\n  gtk_box_pack_end(GTK_BOX(hbox), button, FALSE, FALSE, 0);\n  gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 5);\n\n  \/* Following content sets up entry box in main window *\/\n  hbox=gtk_hbox_new(FALSE, 0);\n  entry=gtk_entry_new();\n  gtk_widget_set_direction(entry, GTK_TEXT_DIR_RTL);\n  gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);\n  gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 5);\n\n  \/* Sets up first line of buttons *\/\n  hbox=gtk_hbox_new(TRUE, 5);\n  button=gtk_button_new_with_label(&quot;3&quot;);\n  g_signal_connect(G_OBJECT(button), &quot;clicked&quot;, G_CALLBACK(on_number_button_clicked), NULL);\n  gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 5);\n\n  button=gtk_button_new_with_label(&quot;2&quot;);\n  g_signal_connect(G_OBJECT(button), &quot;clicked&quot;, G_CALLBACK(on_number_button_clicked), NULL);\n  gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 5);\n\n  button=gtk_button_new_with_label(&quot;1&quot;);\n  g_signal_connect(G_OBJECT(button), &quot;clicked&quot;, G_CALLBACK(on_number_button_clicked), NULL);\n  gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 5);\n\n  button=gtk_button_new_with_label(&quot;+&quot;);\n  g_signal_connect(G_OBJECT(button), &quot;clicked&quot;, G_CALLBACK(on_operator_button_clicked), (gpointer) 1);\n  gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 5);\n  gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 5);\n  \/* End of setting up first line of buttons *\/\n\n  \/* Sets up second line of buttons *\/\n  hbox=gtk_hbox_new(TRUE, 5);\n  button=gtk_button_new_with_label(&quot;6&quot;);\n  g_signal_connect(G_OBJECT(button), &quot;clicked&quot;, G_CALLBACK(on_number_button_clicked), NULL);\n  gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 5);\n\n  button=gtk_button_new_with_label(&quot;5&quot;);\n  g_signal_connect(G_OBJECT(button), &quot;clicked&quot;, G_CALLBACK(on_number_button_clicked), NULL);\n  gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 5);\n\n  button=gtk_button_new_with_label(&quot;4&quot;);\n  g_signal_connect(G_OBJECT(button), &quot;clicked&quot;, G_CALLBACK(on_number_button_clicked), NULL);\n  gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 5);\n\n  button=gtk_button_new_with_label(&quot;-&quot;);\n  g_signal_connect(G_OBJECT(button), &quot;clicked&quot;, G_CALLBACK(on_operator_button_clicked), (gpointer) 2);\n  gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 5);\n  gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 5);\n  \/* End of setting up second line of buttons *\/\n\n  \/* Sets up third line of buttons *\/\n  hbox=gtk_hbox_new(TRUE, 5);\n  button=gtk_button_new_with_label(&quot;9&quot;);\n  g_signal_connect(G_OBJECT(button), &quot;clicked&quot;, G_CALLBACK(on_number_button_clicked), NULL);\n  gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 5);\n\n  button=gtk_button_new_with_label(&quot;8&quot;);\n  g_signal_connect(G_OBJECT(button), &quot;clicked&quot;, G_CALLBACK(on_number_button_clicked), NULL);\n  gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 5);\n\n  button=gtk_button_new_with_label(&quot;7&quot;);\n  g_signal_connect(G_OBJECT(button), &quot;clicked&quot;, G_CALLBACK(on_number_button_clicked), NULL);\n  gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 5);\n\n  button=gtk_button_new_with_label(&quot;*&quot;);\n  g_signal_connect(G_OBJECT(button), &quot;clicked&quot;, G_CALLBACK(on_operator_button_clicked), (gpointer) 3);\n  gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 5);\n  gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 5);\n  \/* End of setting up third line of buttons *\/\n\n  \/* Sets up fourth line of buttons*\/\n  hbox=gtk_hbox_new(TRUE, 5);\n  button=gtk_button_new_with_label(&quot;0&quot;);\n  g_signal_connect(G_OBJECT(button), &quot;clicked&quot;, G_CALLBACK(on_number_button_clicked), NULL);\n  gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 5);\n\n  button=gtk_button_new_with_label(&quot;.&quot;);\n  g_signal_connect(G_OBJECT(button), &quot;clicked&quot;, G_CALLBACK(on_dot_button_clicked), NULL);\n  gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 5);\n\n  button=gtk_button_new_with_label(&quot;=&quot;);\n  g_signal_connect(G_OBJECT(button), &quot;clicked&quot;, G_CALLBACK(on_equal_button_clicked), NULL);\n  gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 5);\n\n  button=gtk_button_new_with_label(&quot;\/&quot;);\n  g_signal_connect(G_OBJECT(button), &quot;clicked&quot;, G_CALLBACK(on_operator_button_clicked), (gpointer) 4);\n  gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 5);\n  gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 5);\n  \/* End of setting up fourth line of buttons *\/\n\n  clear();\n  gtk_widget_show_all(window);\n  gtk_main();\n\n  return EXIT_SUCCESS;\n\n}\n<\/pre><\/div>\n\n<p>\u7f16\u8bd1\u547d\u4ee4<\/p>\n<pre lang=\"bash\">gcc -o calculator calculator.c `pkg-config --libs --clfags gtk+-2.0`\n<\/pre>\n<p>\u7f16\u8bd1\u540e\u8fd0\u884c\uff0c\u6548\u679c\u5982\u56fe\uff1a<br \/><img decoding=\"async\" src=\"http:\/\/farm4.static.flickr.com\/3122\/3138498995_198974f649.jpg?v=0\" alt=\"Calculator\" \/><\/p>","protected":false},"excerpt":{"rendered":"<a href=\"https:\/\/www.yesybl.com\/?p=44\" rel=\"bookmark\" title=\"Permalink to \u4e00\u6b3e\u5f88\u7b80\u5355\u7684\u8ba1\u7b97\u5668\u7a0b\u5e8f\"><p>\u5b66\u4e60GTK\uff0b2.0\uff0c\u5199\u4e86\u4e00\u4e2a\u5c0f\u8ba1\u7b97\u5668\u7a0b\u5e8f\uff0c\u53d1\u4e0a\u6765\uff0c\u57fa\u672c\u4e0a\u53ef\u4ee5\u7528\u3002 \u7f16\u8bd1\u547d\u4ee4 gcc -o calculator calculator.c `pkg-config &#8211;libs &#8211;clfags gtk+-2.0` \u7f16\u8bd1\u540e\u8fd0\u884c\uff0c\u6548\u679c\u5982\u56fe\uff1a<\/p>\n<\/a>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pa4z28-I","_links":{"self":[{"href":"https:\/\/www.yesybl.com\/index.php?rest_route=\/wp\/v2\/posts\/44"}],"collection":[{"href":"https:\/\/www.yesybl.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yesybl.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yesybl.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yesybl.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=44"}],"version-history":[{"count":3,"href":"https:\/\/www.yesybl.com\/index.php?rest_route=\/wp\/v2\/posts\/44\/revisions"}],"predecessor-version":[{"id":753,"href":"https:\/\/www.yesybl.com\/index.php?rest_route=\/wp\/v2\/posts\/44\/revisions\/753"}],"wp:attachment":[{"href":"https:\/\/www.yesybl.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=44"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yesybl.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=44"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yesybl.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=44"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}