Monthly Archives: October 2009

Emacs下登录BBS的方法

Emacs里提供了ansi-term来进行彩色term的显示,Linux下luit可以将不同的encoding流转换成utf-8,这样就可以使本机设定为en.utf-8 locale的系统在Emacs下显示中文BBS了。
打开emacs,启动ansi-term.

M-x ansi-term

然后在emacs内嵌的shell中登录BBS.

luit -encoding gbk telnet bbs.mitbbs.com

哈哈,终于可以正常的在emacs里面使用BBS了。爽!

linux下调试命令

调试程序时有用的命令:
gdb Linux下的调试器
pstack 查看进程的调用栈,如果是多线程的话,可以查看每个线程的调用栈
pmap 查看内存的映射情况
strace 跟踪进程的系统调用
ltrace 跟踪程序的系统调用
ldd 查看依赖库
lsof 查看进程打开的所有文件

what a fucking day!

朋克:
我们初中老师,手淫被我发现。
朋克:
男性。
饼干:
那你手淫不?
饭:
从此你就学会了???
what a fucking day!

朋克:
今天我从我15岁的女儿电脑上搜出来一部A片,藏在“未来的职业”文件夹里,未来的职业
!What a fucking day!

朋克:
昨天晚上我裸着就穿一内裤躺床上,边喝酸奶边上网,不小心酸奶滴到身上了,我正在擦,
我爸突然进来了,居然以为我在SY什么话也没说扭头又关门出去了。What a fucking day!

朋克:
昨天晚上我裸着就穿一内裤躺床上,边喝酸奶边上网,不小心酸奶滴到身上了,我正在擦,
我爸突然进来了,居然以为我在SY什么话也没说扭头又关门出去了。What a fucking day!

朋克:
今天和我小外甥坐沙发上一边吃薯片一边看电视,吃着吃着感觉薯片都湿乎乎的,扭头一看
,他把每个薯片都整个舔一圈然后又放回袋子里了。What a fucking day!

朋克:
今天我在QQ上跟我女朋友说话,然后收到信息说:“**不在,我是她室友,她在她男朋友那
儿。”我愣了3秒钟,回头看看我屋子,好像没发现她。what a fucking day!

一款很简单的计算器程序

学习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
330
331
332
#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(&amp;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`

编译后运行,效果如图:
Calculator

Linux下Qt程序的调试方法

首先执行

qmake --project

然后在生成的.pro文件中加入下面一行代码

CONFIG += qt debug warn_on

然后执行

qmake projectName.pro
make

接下来就可以使用gdb,ddd,kdb等进行调试!

gdb多文件调试方法
输入

gdb projectName.bin

进入gdb的调试界面,C++和C的代码在gdb里的对待方法不同,C语言编写的程序只需
要在gdb界面内输入:

list filename:line_number

就可以转换源代码文件,设置断点。但在C++中,gdb按域来区分代码,比如说需要
查看某个函数体内的代码,要按如下方法查看:

list MainWindow::MainWindow()

以上指明了查看在MainWindow类中的构造函数的源代码。然后使用break即可设定断
点。