|
发表于 2004-3-11 18:19:17
|
显示全部楼层
偶刚学GTK,顺便写了一下,希望大家指点指点.
下面的程序支持从古到今到将来的任意一天(不过公元前1年输入0,前二年输入-1,以次类推),用到了一点天文的小知识--儒略日,有兴趣的话可以去看看.
[code:1]
#include<gtk/gtk.h>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
const gchar *yyear;
const gchar *mmonth;
const gchar *ddate;
int year;
int month;
int date;
int xingqi(int yy,int mm,int dd)
{
int y,m,A,B,xingqi;
int temp1,temp2,temp3;
float JD;
if((mm==1)||(mm==2))
{
y=yy-1;
m=mm+12;
}
else
{
y=yy;
m=mm;
}
if((yy<=1582)&&(mm<=10)&&(dd<15))
{
A=B=0;
}
else
{
A=y/100;
B=2-A+(A/4);
}
temp1=365.25*y;
temp2=30.6001*(m+1);
JD=temp1+temp2+1720994.5+dd+B;
temp3=JD+1.5;
xingqi=temp3%7;
return xingqi;
}
void qingkong(GtkWidget *widget,GtkWidget *entry)
{
gtk_entry_set_text(GTK_ENTRY(entry),"");
}
void getyear(GtkWidget *widget,GtkWidget *entry)
{
yyear=gtk_entry_get_text(GTK_ENTRY(entry));
}
void getmonth(GtkWidget *widget,GtkWidget *entry)
{
mmonth=gtk_entry_get_text(GTK_ENTRY(entry));
}
void getdate(GtkWidget *widget,GtkWidget *entry)
{
ddate=gtk_entry_get_text(GTK_ENTRY(entry));
}
void jishuan(GtkWidget *widget,GtkWidget *entry)
{
int xingqishu;
year=atoi(yyear);
month=atoi(mmonth);
if(month<=0||month>12)
{
gtk_entry_set_text(GTK_ENTRY(entry),"Wrong month");
}
else
{
date=atoi(ddate);
if(date<=0||date>31)
{
gtk_entry_set_text(GTK_ENTRY(entry),"wrong date");
}
else
{
xingqishu=xingqi(year,month,date);
switch(xingqishu)
{
case 0:gtk_entry_set_text(GTK_ENTRY(entry),"The day is Sunday");break;
case 1:gtk_entry_set_text(GTK_ENTRY(entry),"The day is Monday");break;
case 2:gtk_entry_set_text(GTK_ENTRY(entry),"The day is Tuesday");break;
case 3:gtk_entry_set_text(GTK_ENTRY(entry),"The day is Wednesday");break;
case 4:gtk_entry_set_text(GTK_ENTRY(entry),"The day is Thirstday");break;
case 5:gtk_entry_set_text(GTK_ENTRY(entry),"The day is Friday");break;
case 6:gtk_entry_set_text(GTK_ENTRY(entry),"The day is Saturday");break;
default:break;
}
}
}
}
int main(int argc,char *argv[])
{
GtkWidget *window;
GtkWidget *vbox,*hbox;
GtkWidget *entry_year,*entry_month,*entry_date,*entry_xingqi;
GtkWidget *label;
GtkWidget *button;
GtkWidget *separator;
gtk_init(&argc,&argv);
window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),NULL);
vbox=gtk_vbox_new(FALSE,0);
gtk_container_add(GTK_CONTAINER(window),vbox);
gtk_widget_show(vbox);
hbox=gtk_hbox_new(FALSE,0);
gtk_container_add(GTK_CONTAINER(vbox),hbox);
gtk_widget_show(hbox);
label=gtk_label_new("What day is it ?");
gtk_box_pack_start(GTK_BOX(hbox),label,TRUE,TRUE,0);
gtk_widget_show(label);
separator=gtk_hseparator_new();
gtk_box_pack_start(GTK_BOX(vbox),separator,FALSE,TRUE,10);
gtk_widget_show(separator);
entry_xingqi=gtk_entry_new();
gtk_editable_set_editable(GTK_EDITABLE(entry_xingqi),FALSE);
gtk_box_pack_start(GTK_BOX(vbox),entry_xingqi,FALSE,FALSE,0);
gtk_widget_show(entry_xingqi);
hbox=gtk_hbox_new(FALSE,0);
gtk_container_add(GTK_CONTAINER(vbox),hbox);
gtk_widget_show(hbox);
entry_year=gtk_entry_new();
g_signal_connect(G_OBJECT(entry_year),"changed",
G_CALLBACK(qingkong),entry_xingqi);
gtk_box_pack_start(GTK_BOX(hbox),entry_year,FALSE,FALSE,5);
gtk_widget_show(entry_year);
label=gtk_label_new("year");
gtk_box_pack_start(GTK_BOX(hbox),label,FALSE,FALSE,5);
gtk_widget_show(label);
hbox=gtk_hbox_new(FALSE,0);
gtk_container_add(GTK_CONTAINER(vbox),hbox);
gtk_widget_show(hbox);
entry_month=gtk_entry_new();
gtk_entry_set_max_length(GTK_ENTRY(entry_month),2);
g_signal_connect(G_OBJECT(entry_month),"changed",
G_CALLBACK(qingkong),entry_xingqi);
gtk_box_pack_start(GTK_BOX(hbox),entry_month,FALSE,FALSE,5);
gtk_widget_show(entry_month);
label=gtk_label_new("month");
gtk_box_pack_start(GTK_BOX(hbox),label,FALSE,FALSE,5);
gtk_widget_show(label);
hbox=gtk_hbox_new(FALSE,0);
gtk_container_add(GTK_CONTAINER(vbox),hbox);
gtk_widget_show(hbox);
entry_date=gtk_entry_new();
gtk_entry_set_max_length(GTK_ENTRY(entry_date),2);
g_signal_connect(G_OBJECT(entry_date),"changed",
G_CALLBACK(qingkong),entry_xingqi);
gtk_box_pack_start(GTK_BOX(hbox),entry_date,FALSE,FALSE,5);
gtk_widget_show(entry_date);
label=gtk_label_new("date");
gtk_box_pack_start(GTK_BOX(hbox),label,FALSE,FALSE,5);
gtk_widget_show(label);
button=gtk_button_new_with_label("check now");
g_signal_connect(G_OBJECT(button),"clicked",
G_CALLBACK(getyear),entry_year);
g_signal_connect(G_OBJECT(button),"clicked",
G_CALLBACK(getmonth),entry_month);
g_signal_connect(G_OBJECT(button),"clicked",
G_CALLBACK(getdate),entry_date);
g_signal_connect(G_OBJECT(button),"clicked",
G_CALLBACK(jishuan),entry_xingqi);
gtk_box_pack_start(GTK_BOX(vbox),button,FALSE,FALSE,0);
gtk_widget_show(button);
separator=gtk_hseparator_new();
gtk_box_pack_start(GTK_BOX(vbox),separator,FALSE,TRUE,10);
gtk_widget_show(separator);
button=gtk_button_new_with_label("quit");
g_signal_connect(G_OBJECT(button),"clicked",
G_CALLBACK(gtk_main_quit),NULL);
gtk_box_pack_start(GTK_BOX(vbox),button,TRUE,FALSE,0);
gtk_widget_show(button);
gtk_widget_show(window);
gtk_main();
return 0;
}
[/code:1]
[/code] |
|