该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#include
#include
#include
#include
#include
GtkWidget *entry_value;
GtkWidget *entry_file_path;
GtkWidget *label_error;
int get_value(char *String )//get the world's value
{
int i,value=0,length = strlen(String);
for(i=0;i
{
value=value+String[i]-64; //'A'=1
}
return value;
}
void button_ok_clicked(GtkWidget *widget,gpointer data) //the event action when //button_on was clicked
{
const char *input_file;//text of entry_file_path
FILE *source,*result;//pointer of source file and result file
char temp,array[100][100]; //source words
int i,j,length;
input_file=gtk_entry_get_text(GTK_ENTRY((GtkWidget *)data));//get file path
g_print("%s\n",input_file);
source=fopen(input_file,"r");
if(!source)
{
g_print("The file is not exist!!!");
return;
gtk_label_set_text(GTK_LABEL((GtkWidget *)label_error),"error");
}//if file can not be found show error message
else
{
g_print("success!!!");
gtk_label_set_text(GTK_LABEL((GtkWidget *)label_error),"success");
}
for(i=0;;i++)
{
for(j=0;;j++)
{
temp = fgetc(source);
if(temp==10){array[i][j] = '\0';break;}
else if(temp==EOF)break;
else array[i][j]=temp;
}
if(temp==EOF)break;
}
length = i;
result=fopen("/root/result.txt","w+");
for(i=0;i
{
g_print("%s\n",array[i]);
fprintf(result,"%d\n",get_value(array[i]));
}
fclose(result);
fclose(source);
}
void on_delete(GtkWidget *window,GdkEvent *event,gpointer data) //quit from main
{
gtk_main_quit();
}
void button_computing_clicked(GtkWidget *widget,gpointer data) //compute the value of thd word that was input in data
{
const char *input_text;
const char *output_text;
input_text = gtk_entry_get_text(GTK_ENTRY((GtkWidget *)data));//get the word
sprintf(output_text,"%d",get_value(input_text));//get value of word and chang to String format
gtk_entry_set_text(GTK_ENTRY((GtkWidget*)entry_value),output_text); //show the value on entry_value
}
int main(int argc,char * argv[])
{
GtkWidget *window;
GtkWidget *label;
GtkWidget *label_input;
GtkWidget *label_value;
GtkWidget *label_file_path;
GtkWidget *entry_input;
GtkWidget *button_computing;
GtkWidget *button_ok;
GtkWidget *vbox;
GtkWidget *hbox_1;
GtkWidget *hbox_2;
GtkWidget *hbox_3;
GtkWidget *hbox_4;
GtkWidget *hbox_5;
gtk_init(&argc,&argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window),"Value of Word!");
gtk_container_set_border_width(GTK_CONTAINER(window),10);
g_signal_connect(G_OBJECT(window),"delete_event",G_CALLBACK(on_delete),NULL);
label_input = gtk_label_new(g_convert("要请输入单词:",-1,"UTF-8","GB2312",NULL,NULL,NULL));
label_value = gtk_label_new(g_convert("该单词的值",-1,"UTF-8","GB2312",NULL,NULL,NULL));
label_file_path = gtk_label_new(g_convert("请输入文件路径:",-1,"UTF-8","GB2312",NULL,NULL,NULL));
label_error = gtk_label_new("");
button_computing = gtk_button_new_with_label(g_convert("该计算",-1,"UTF-8","GB2312",NULL,NULL,NULL));
button_ok = gtk_button_new_with_label(g_convert("确定",-1,"UTF-8","GB2312",NULL,NULL,NULL));
entry_input = gtk_entry_new();
entry_value = gtk_entry_new();
entry_file_path = gtk_entry_new();
vbox = gtk_vbox_new(FALSE,5);
hbox_1 = gtk_hbox_new(TRUE,2);
hbox_2 = gtk_hbox_new(TRUE,2);
hbox_3 = gtk_hbox_new(TRUE,2);
hbox_4 = gtk_hbox_new(TRUE,2);
hbox_5 = gtk_hbox_new(TRUE,2);
gtk_box_pack_start(GTK_BOX(hbox_1),label_input,TRUE,TRUE,0);
gtk_box_pack_start(GTK_BOX(hbox_1),gtk_label_new(""),TRUE,FALSE,0);
gtk_box_pack_start(GTK_BOX(hbox_2),entry_input,TRUE,FALSE,0);
gtk_box_pack_start(GTK_BOX(hbox_2),button_computing,TRUE,TRUE,0);
gtk_box_pack_start(GTK_BOX(hbox_3),label_value,TRUE,FALSE,0);
gtk_box_pack_start(GTK_BOX(hbox_3),entry_value,TRUE,FALSE,0);
gtk_box_pack_start(GTK_BOX(hbox_4),label_file_path,TRUE,FALSE,0);
gtk_box_pack_start(GTK_BOX(hbox_4),gtk_label_new(""),TRUE,FALSE,0);
gtk_box_pack_start(GTK_BOX(hbox_5),entry_file_path,TRUE,FALSE,0);
gtk_box_pack_start(GTK_BOX(hbox_5),button_ok,TRUE,TRUE,0);
gtk_box_pack_start(GTK_BOX(vbox),hbox_1,TRUE,FALSE,0);
gtk_box_pack_start(GTK_BOX(vbox),hbox_2,TRUE,FALSE,0);
gtk_box_pack_start(GTK_BOX(vbox),hbox_3,TRUE,FALSE,0);
gtk_box_pack_start(GTK_BOX(vbox),gtk_label_new(""),TRUE,FALSE,0);
gtk_box_pack_start(GTK_BOX(vbox),hbox_4,TRUE,FALSE,0);
gtk_box_pack_start(GTK_BOX(vbox),hbox_5,TRUE,FALSE,0);
gtk_box_pack_start(GTK_BOX(vbox),label_error,TRUE,FALSE,0);
g_signal_connect(G_OBJECT(button_computing),"clicked",G_CALLBACK(button_computing_clicked),entry_input);
g_signal_connect(G_OBJECT(button_ok),"clicked",G_CALLBACK(button_ok_clicked),entry_file_path);
gtk_container_add(GTK_CONTAINER(window),vbox);
gtk_widget_show_all(window);
gtk_main();
return 0;
}