GTK实现简单计算器(calculator.c)

/* 计算器 calc.c */
#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;
   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; //够6位数则不能再输入数字
   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: //"+"
         s = 1;
         gtk_entry_set_text(GTK_ENTRY(entry),"");
         first = FALSE ; count = 0; break;
      case 2: //"-"
         s = 2;
         gtk_entry_set_text(GTK_ENTRY(entry),"");
         first = FALSE ; count = 0; break;
      case 3: //"*"
         s = 3;
         gtk_entry_set_text(GTK_ENTRY(entry),"");
         first = FALSE ; count = 0; break;
      case 4: //"/"
         s = 4;
         gtk_entry_set_text(GTK_ENTRY(entry),"");
         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 num[100];
   gchar *result;
   switch(s)
   {
      case 1: //"+"
         numb = nn+mm;
         break;
      case 2: //"-"
         numb = nn-mm;
         break;
      case 3: //"*"
         numb = nn*mm;
         break;
      case 4: //"/"
         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;

   gtk_init(&argc,&argv);

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

   vbox = gtk_vbox_new(FALSE,0);
   gtk_container_add(GTK_CONTAINER(window),vbox);

   hbox = gtk_hbox_new(FALSE,0);
   gtk_box_pack_start(GTK_BOX(vbox),hbox,FALSE,FALSE,5);

   label = gtk_label_new("Calculator");
   gtk_box_pack_start(GTK_BOX(hbox),label,TRUE,TRUE,5);

   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_widget_set_direction(entry,GTK_TEXT_DIR_RTL);
   gtk_box_pack_start(GTK_BOX(vbox),entry,FALSE,FALSE,5);

   hbox1 = gtk_hbox_new(FALSE,0);
   gtk_box_pack_start(GTK_BOX(vbox),hbox1,FALSE,FALSE,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("+");
   g_signal_connect(G_OBJECT(button),"clicked",
         G_CALLBACK(on_suan_clicked),(gpointer)1);
   gtk_box_pack_start(GTK_BOX(hbox1),button,TRUE,TRUE,5);

   hbox2 = gtk_hbox_new(FALSE,0);
   gtk_box_pack_start(GTK_BOX(vbox),hbox2,FALSE,FALSE,5);

   button = gtk_button_new_with_label("6");
   g_signal_connect(G_OBJECT(button),"clicked",
         G_CALLBACK(on_num_clicked),NULL);
   gtk_box_pack_start(GTK_BOX(hbox2),button,TRUE,TRUE,5);

   button = gtk_button_new_with_label("5");
   g_signal_connect(G_OBJECT(button),"clicked",
         G_CALLBACK(on_num_clicked),NULL);
   gtk_box_pack_start(GTK_BOX(hbox2),button,TRUE,TRUE,5);

   button = gtk_button_new_with_label("4");
   g_signal_connect(G_OBJECT(button),"clicked",
         G_CALLBACK(on_num_clicked),NULL);
   gtk_box_pack_start(GTK_BOX(hbox2),button,TRUE,TRUE,5);

   button = gtk_button_new_with_label("-");
   g_signal_connect(G_OBJECT(button),"clicked",
         G_CALLBACK(on_suan_clicked),(gpointer)2);
   gtk_box_pack_start(GTK_BOX(hbox2),button,TRUE,TRUE,5);

   hbox3 = gtk_hbox_new(FALSE,0);
   gtk_box_pack_start(GTK_BOX(vbox),hbox3,FALSE,FALSE,5);

   button = gtk_button_new_with_label("9");
   g_signal_connect(G_OBJECT(button),"clicked",
         G_CALLBACK(on_num_clicked),NULL);
   gtk_box_pack_start(GTK_BOX(hbox3),button,TRUE,TRUE,5);

   button = gtk_button_new_with_label("8");
   g_signal_connect(G_OBJECT(button),"clicked",
         G_CALLBACK(on_num_clicked),NULL);
   gtk_box_pack_start(GTK_BOX(hbox3),button,TRUE,TRUE,5);

   button = gtk_button_new_with_label("7");
   g_signal_connect(G_OBJECT(button),"clicked",
         G_CALLBACK(on_num_clicked),NULL);
   gtk_box_pack_start(GTK_BOX(hbox3),button,TRUE,TRUE,5);

   button = gtk_button_new_with_label("*");
   g_signal_connect(G_OBJECT(button),"clicked",
         G_CALLBACK(on_suan_clicked),(gpointer)3);
   gtk_box_pack_start(GTK_BOX(hbox3),button,TRUE,TRUE,5);

   hbox4 = gtk_hbox_new(FALSE,0);
   gtk_box_pack_start(GTK_BOX(vbox),hbox4,FALSE,FALSE,5);

   button = gtk_button_new_with_label("0");
   g_signal_connect(G_OBJECT(button),"clicked",
         G_CALLBACK(on_num_clicked),NULL);
   gtk_box_pack_start(GTK_BOX(hbox4),button,TRUE,TRUE,5);

   button = gtk_button_new_with_label(".");
   g_signal_connect(G_OBJECT(button),"clicked",
         G_CALLBACK(on_dot_clicked),NULL);
   gtk_box_pack_start(GTK_BOX(hbox4),button,TRUE,TRUE,5);

   button = gtk_button_new_with_label("=");
   g_signal_connect(G_OBJECT(button),"clicked",
         G_CALLBACK(on_eq_clicked),NULL);
   gtk_box_pack_start(GTK_BOX(hbox4),button,TRUE,TRUE,5);

   button = gtk_button_new_with_label("/");
   g_signal_connect(G_OBJECT(button),"clicked",
         G_CALLBACK(on_suan_clicked),(gpointer)4);
   gtk_box_pack_start(GTK_BOX(hbox4),button,TRUE,TRUE,5);


   gtk_widget_show_all(window);
   gtk_main();

   return FALSE;
}



编译运行:

-bash-4.1$ gcc -o a calculater.c `pkg-config --cflags --libs gtk+-2.0`
-bash-4.1$ ./a


已标记关键词 清除标记
相关推荐
课程简介: 历经半个多月的时间,Debug亲自撸的 “企业员工角色权限管理平台” 终于完成了。正如字面意思,本课程讲解的是一个真正意义上的、企业级的项目实战,主要介绍了企业级应用系统中后端应用权限的管理,其中主要涵盖了六大核心业务模块、十几张数据库表。 其中的核心业务模块主要包括用户模块、部门模块、岗位模块、角色模块、菜单模块和系统日志模块;与此同时,Debug还亲自撸了额外的附属模块,包括字典管理模块、商品分类模块以及考勤管理模块等等,主要是为了更好地巩固相应的技术栈以及企业应用系统业务模块的开发流程! 核心技术栈列表: 值得介绍的是,本课程在技术栈层面涵盖了前端和后端的大部分常用技术,包括Spring Boot、Spring MVC、Mybatis、Mybatis-Plus、Shiro(身份认证与资源授权跟会话等等)、Spring AOP、防止XSS攻击、防止SQL注入攻击、过滤器Filter、验证码Kaptcha、热部署插件Devtools、POI、Vue、LayUI、ElementUI、JQuery、HTML、Bootstrap、Freemarker、一键打包部署运行工具Wagon等等,如下图所示: 课程内容与收益: 总的来说,本课程是一门具有很强实践性质的“项目实战”课程,即“企业应用员工角色权限管理平台”,主要介绍了当前企业级应用系统中员工、部门、岗位、角色、权限、菜单以及其他实体模块的管理;其中,还重点讲解了如何基于Shiro的资源授权实现员工-角色-操作权限、员工-角色-数据权限的管理;在课程的最后,还介绍了如何实现一键打包上传部署运行项目等等。如下图所示为本权限管理平台的数据库设计图: 以下为项目整体的运行效果截图: 值得一提的是,在本课程中,Debug也向各位小伙伴介绍了如何在企业级应用系统业务模块的开发中,前端到后端再到数据库,最后再到服务器的上线部署运行等流程,如下图所示:
©️2020 CSDN 皮肤主题: 酷酷鲨 设计师:CSDN官方博客 返回首页