linux下gtk编写的并交集合运算器
题目:集合并,交,差的运算程序,集合元素限定小写字母,结果按照字母顺序显示,目前我只做了并的运算,就出现了乱码的问题
- #include <gtk/gtk.h>
- #include <stdlib.h>
- #include <stdio.h>
- typedef struct LNode
- {
- char data;
- struct LNode *next;
- }LNode,*LinkList;
- GtkWidget *window;
- GtkWidget *entry1;
- GtkWidget *entry2;
- GtkWidget *button1;
- GtkWidget *button2;
- GtkWidget *button3;
- GtkWidget *label;
- GtkWidget *table;
- LinkList CreateList (int j) //将文本框的text创建线性链表
- {
- char txt[26];
- int i;
- LinkList L;
- LNode *p,*q;
- if(j=0)
- strcpy(txt,gtk_entry_get_text(GTK_ENTRY(entry1))); //第一个文本框的text
- else
- strcpy(txt,gtk_entry_get_text(GTK_ENTRY(entry2))); //第二个文本框的text
- L=(LinkList)malloc(sizeof (LNode));
- L->next=NULL;
- q=L;
- for(i=0;txt[i]!=NULL;i++)
- {
- p=(LinkList)malloc(sizeof(LNode));
- p->data=txt[i];
- q->next=p;
- q=p;
- }
- q->next=NULL;
- return L;
- }
- void on_clicked1(GtkWidget *widget,gpointer data)
- {
- LinkList L1,L2;
- LNode *p1,*p2;
- L1=CreateList(0); 将链表传给L1
- L2=CreateList(1); 将链表传给L2
- p1=L1;
- p2=L2;
- int i,j=0;
- char result[52];
- char letter;
- for(i=97;i<=122;i++) 并集合运算的算法
- {
- letter=i;
- while(p1)
- {
- if(p1->data==letter)
- {
- result[j]=letter;
- j++;
- }
- else p1=p1->next;
- }
- while(p2)
- {
- if(j==0) j=j+1;
- else
- {
- if(p2->data==letter&&result[j-1]!=letter) //70
- {
- result[j]=letter;
- j++;
- }
- else p2=p2->next;
- }
- }
- }
- gtk_label_set_text(GTK_LABEL(label),result);
- gtk_widget_show(label);
- }
- void on_clicked2(GtkWidget *widget,gpointer data) //交运算,还没做
- {
- }
- void on_clicked3(GtkWidget *widget,gpointer data) //差运算,还没做
- {
- }
- int main(int argc,char *argv[]) //main函数
- {
- gtk_init(&argc,&argv);
- window=gtk_window_new(GTK_WINDOW_TOPLEVEL); //创建窗口
- gtk_window_set_title(GTK_WINDOW (window),"集合运算器");
- gtk_widget_set_usize(window,300,300);
- gtk_widget_show(window);
- table=gtk_table_new(4,2,FALSE); //创建表格
- gtk_container_add(GTK_CONTAINER(window),table);
- gtk_widget_show(table);
- entry1=gtk_entry_new(); //创建第一个文本框
- gtk_table_attach(GTK_TABLE (table),entry1,0,1,0,1,(GtkAttachOptions) (GTK_FILL),(GtkAttachOptions) (GTK_FILL),0,0);
- gtk_widget_show(entry1);
- entry2=gtk_entry_new(); //创建第二个文本框
- gtk_table_attach(GTK_TABLE (table),entry2,1,2,0,1,(GtkAttachOptions) (GTK_FILL),(GtkAttachOptions) (GTK_FILL),0,0);
- gtk_widget_show(entry2);
- button1=gtk_button_new_with_label("并运算"); //并运算按钮
- gtk_table_attach(GTK_TABLE(table),button1,0,1,1,2,(GtkAttachOptions) (GTK_FILL),(GtkAttachOptions) (GTK_FILL),0,0);
- g_signal_connect(G_OBJECT(button1),"clicked",G_CALLBACK(on_clicked1),NULL);
- gtk_widget_show(button1);
- button2=gtk_button_new_with_label("交运算"); //交运算按钮
- gtk_table_attach(GTK_TABLE(table),button2,0,1,2,3,(GtkAttachOptions) (GTK_FILL),(GtkAttachOptions) (GTK_FILL),0,0);
- g_signal_connect(G_OBJECT(button2),"clicked",G_CALLBACK(on_clicked2),NULL);
- gtk_widget_show(button2);
- button3=gtk_button_new_with_label("差运算"); //差运算按钮
- gtk_table_attach(GTK_TABLE(table),button3,0,1,3,4,(GtkAttachOptions) (GTK_FILL),(GtkAttachOptions) (GTK_FILL),0,0);
- g_signal_connect(G_OBJECT(button3),"clicked",G_CALLBACK(on_clicked3),NULL);
- gtk_widget_show(button3);
- label=gtk_label_new("答案"); //标签来显示答案
- gtk_table_attach(GTK_TABLE(table),label,1,2,2,3,(GtkAttachOptions) (GTK_FILL),(GtkAttachOptions) (GTK_FILL),0,0);
- gtk_widget_show(label);
- gtk_main();
- return 0;
- }
复制代码gcc collection.c -o collection `pkg-config --cflags --libs gtk+-2.0` //gcc编译
collection.c: 在函数‘CreateList’中:
collection.c:27: 警告: 隐式声明与内建函数‘strcpy’不兼容
collection.c:34: 警告: 比较指针和整数
运行
(collection:14233): Pango-WARNING **: Invalid UTF-8 string passed to pango_layout_set_text()
然后出现乱码,请大牛看看哪里出错了
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论