接下来我们做一个简单版的计算器。
1)获取按钮上的内容。
2)如果获取的内容是“ c ”,则代表进行退格操作,相当于删去最后一个字符。
3)如果获取的内容不是“ c ”,则把每一次获取的内容连接起来,如:
第一次按了 “ 1 ”, 第二次按了 “ + ”, 第三次按了 “ 1 ”, 连起来则变为 “1+1”。
4)如果获取的内容是“ = ”,则需要相应的运算。先把连起来的字符串拆出来,然后再进行相应操作,最后把运算结果重新显示到行编辑上。
完整代码如下:
#include <gtk/gtk.h> // 头文件
#include <string.h>
char buf[50] = {0}; //全局变量,保存数据
// 按钮的回调函数
void deal_num(GtkButton *button, gpointer data)
{
const char *txt = gtk_button_get_label( button ); // 获取点击按钮的内容
// 退格操作
if( 0 == strcmp("c", txt) ){ // 如果是退格键
buf[strlen(buf) - 1] = 0; // 最后一个字符变为 0
//gtk_entry_set_text(GTK_ENTRY(data), buf); //重新给行编辑设定内容
}else{
int a = 0, b = 0;
char c;
strcat(buf, txt); // 内容追加
// 运算操作
if(0 == strcmp("=", txt) ){ // 如果是 " = "
sscanf(buf, "%d%c%d", &a, &c, &b); // 拆包
printf("a=%d, c = %c, d=%d\n", a, c, b);
if('+' == c){
sprintf(buf, "%d", a+b);
}else if('-' == c){
sprintf(buf, "%d", a-b);
}else if('*' == c){
sprintf(buf, "%d", a*b);
}else if('/' == c){
sprintf(buf, "%d", a/b);
}
}
}
gtk_entry_set_text(GTK_ENTRY(data), buf); // 给行编辑设定内容
}
int main( int argc, char *argv[] )
{
gtk_init (&argc, &argv); // 初始化
// 窗口的操作
GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL); // 创建窗口
gtk_widget_set_size_request(window, 270, 320);
// 表格的操作
GtkWidget *table = gtk_table_new(5, 4, TRUE); // 表格布局,5行4列
// 行编辑的操作
GtkWidget *entry = gtk_entry_new(); // 行编辑的创建
//gtk_entry_set_text(GTK_ENTRY(entry), "2+2=4"); // 给行编辑设置内容
gtk_editable_set_editable(GTK_EDITABLE(entry), FALSE);// 设置行编辑不允许编辑,只做显示用
// 按钮的操作
GtkWidget *button0 = gtk_button_new_with_label("0"); // 按钮0
GtkWidget *button1 = gtk_button_new_with_label("1"); // 按钮1
GtkWidget *button2 = gtk_button_new_with_label("2"); // 按钮2
GtkWidget *button3 = gtk_button_new_with_label("3"); // 按钮3
GtkWidget *button4 = gtk_button_new_with_label("4"); // 按钮4
GtkWidget *button5 = gtk_button_new_with_label("5"); // 按钮5
GtkWidget *button6 = gtk_button_new_with_label("6"); // 按钮6
GtkWidget *button7 = gtk_button_new_with_label("7"); // 按钮7
GtkWidget *button8 = gtk_button_new_with_label("8"); // 按钮8
GtkWidget *button9 = gtk_button_new_with_label("9"); // 按钮9
GtkWidget *button_add = gtk_button_new_with_label("+"); // 加
GtkWidget *button_minus = gtk_button_new_with_label("-"); // 减
GtkWidget *button_multiply = gtk_button_new_with_label("*"); // 乘
GtkWidget *button_divide = gtk_button_new_with_label("/"); // 除
GtkWidget *button_equal = gtk_button_new_with_label("="); // 等于
GtkWidget *button_detele = gtk_button_new_with_label("c"); // 退格
// 布局操作
gtk_container_add(GTK_CONTAINER(window), table); // 表格放入窗口
gtk_table_attach_defaults(GTK_TABLE(table), entry, 0, 4, 0, 1); // 行编辑放入表格
gtk_table_attach_defaults(GTK_TABLE(table), button0, 0, 1, 4, 5); // 按钮放入表格
gtk_table_attach_defaults(GTK_TABLE(table), button1, 0, 1, 3, 4);
gtk_table_attach_defaults(GTK_TABLE(table), button2, 1, 2, 3, 4);
gtk_table_attach_defaults(GTK_TABLE(table), button3, 2, 3, 3, 4);
gtk_table_attach_defaults(GTK_TABLE(table), button4, 0, 1, 2, 3);
gtk_table_attach_defaults(GTK_TABLE(table), button5, 1, 2, 2, 3);
gtk_table_attach_defaults(GTK_TABLE(table), button6, 2, 3, 2, 3);
gtk_table_attach_defaults(GTK_TABLE(table), button7, 0, 1, 1, 2);
gtk_table_attach_defaults(GTK_TABLE(table), button8, 1, 2, 1, 2);
gtk_table_attach_defaults(GTK_TABLE(table), button9, 2, 3, 1, 2);
gtk_table_attach_defaults(GTK_TABLE(table), button_add, 1, 2, 4, 5);
gtk_table_attach_defaults(GTK_TABLE(table), button_minus, 2, 3, 4, 5);
gtk_table_attach_defaults(GTK_TABLE(table), button_multiply, 3, 4, 2, 3);
gtk_table_attach_defaults(GTK_TABLE(table), button_divide, 3, 4, 3, 4);
gtk_table_attach_defaults(GTK_TABLE(table), button_equal, 3, 4, 4, 5);
gtk_table_attach_defaults(GTK_TABLE(table), button_detele, 3, 4, 1, 2);
// 注册信号函数,把 entry 传给回调函数 deal_num()
g_signal_connect(button0, "pressed", G_CALLBACK(deal_num), entry);
g_signal_connect(button1, "pressed", G_CALLBACK(deal_num), entry);
g_signal_connect(button2, "pressed", G_CALLBACK(deal_num), entry);
g_signal_connect(button3, "pressed", G_CALLBACK(deal_num), entry);
g_signal_connect(button4, "pressed", G_CALLBACK(deal_num), entry);
g_signal_connect(button5, "pressed", G_CALLBACK(deal_num), entry);
g_signal_connect(button6, "pressed", G_CALLBACK(deal_num), entry);
g_signal_connect(button7, "pressed", G_CALLBACK(deal_num), entry);
g_signal_connect(button8, "pressed", G_CALLBACK(deal_num), entry);
g_signal_connect(button9, "pressed", G_CALLBACK(deal_num), entry);
g_signal_connect(button_add, "pressed", G_CALLBACK(deal_num), entry);
g_signal_connect(button_minus, "pressed", G_CALLBACK(deal_num), entry);
g_signal_connect(button_multiply, "pressed", G_CALLBACK(deal_num), entry);
g_signal_connect(button_divide, "pressed", G_CALLBACK(deal_num), entry);
g_signal_connect(button_equal, "pressed", G_CALLBACK(deal_num), entry);
g_signal_connect(button_detele, "pressed", G_CALLBACK(deal_num), entry);
gtk_widget_show_all(window); // 显示所有控件
gtk_main(); //进入事件循环
return 0;
}
操作图如下: