论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > C语言程序设计教程
Tag:新手,函数,指针,数据类型,对象,Turbo,入门,运算符,数组,结构,二级,,tc,游戏,试题,问答,编译,视频教程

模拟windows 日期/时间的C程序

文章类别:C语言程序设计 | 发表日期:2008-9-24 14:43:10

#include<graphics.h>
#include<stdio.h>
#include<math.h>
#include<dos.h>
#define PI 3.1415926
#define UP 0x4800
#define DOWN 0x5000
#define ESC 0x11b
#define TAB 0xf09
clock();
date(int,int);
int digitsec(double s)/*digital clock second*/
{int i;
 for(i=0;i<=59;i++) if(s==i) return i;
 }
int digithour(double h)/*digital clock hour*/
{int i;
 for(i=0;i<=23;i++) if(h==i) return i;
 }
int digitmin(double m)/*digital clock minute*/
{int i;
 for(i=0;i<=59;i++) if(m==i) return i;
 }
digitclock(int x,int y,int clock)/*整个digital clock*/
{char buffer1[10];
 setfillstyle(0,2);
 bar(x,y,x+15,328);
 if(clock==60) clock=0;
 sprintf(buffer1,"%d",clock);
 outtextxy(x,y,buffer1);
 }
cursor(int count)    /*画一个光标函数*/
{switch(count)
 {case 1:line(156,108,156,118);break;
  case 2:line(280,108,280,118);break;
  case 3:line(424,315,424,325);break;
  case 4:line(465,315,465,325);break;
  case 5:line(505,315,505,325);break;
  }
 }
clear(int count)    /*清除光标函数*/
{switch(count)
 {case 2:line(156,108,156,118);break;
  case 3:line(280,108,280,118);break;
  case 4:line(424,315,424,325);break;
  case 5:line(465,315,465,325);break;
  case 1:line(505,315,505,325);break;
  }
 }
int key(int,int);
int dateupchange(int);
int timeupchange(int);
int timedownchange(int);
double h,m,s;
double x,x1,x2,y,y1,y2;
char buffer[80];
struct time t[1];
struct date da[1];
main()
{int driver, mode=0,i,j;
 int year,mon;
 char *month[]={"SUN","MON","TUE","WEN","THU","FRI","SAT"};
 driver=DETECT;
 initgraph(&driver, &mode, "");
 setlinestyle(0,0,3);
 setbkcolor(0);
 setcolor(7);
 line(82,430,558,430);
 line(70,62,70,418);
 line(82,50,558,50);
 line(570,62,570,418);
 line(70,62,570,62);
 line(76,56,297,56);
 line(340,56,564,56);  /*画主体框架*/
 setlinestyle(0,0,3);
 arc(82,62,90,180,12);
 setlinestyle(0,0,3);
 arc(558,62,0,90,12);
 setlinestyle(0,0,3);
 arc(82,418,180,279,12);
 setlinestyle(0,0,3);
 arc(558,418,270,360,12);   /*画边角*/
 setcolor(15);
 outtextxy(300,53,"CLOCK"); /*总标题*/
 setcolor(7);
 rectangle(80,72,332,360);  /*月历的框架*/
 rectangle(342,72,560,360); /*时钟的框架*/
 for(j=78;j<=84;j+=6)
 line(80,j,332,j);
 setwritemode(1);
 line(190,78,225,78);
 setwritemode(0);
 setcolor(15);
 outtextxy(193,75,"DATE");
 setcolor(7);
 rectangle(90,106,160,120);
 outtextxy(163,110,"(M)");  /*打印月份标题*/
 rectangle(190,106,290,120);
 outtextxy(293,110,"(Y)");  /*打印年标题*/
 for(i=0,j=90;i<7;i++,j+=35)
 outtextxy(j,145,month[i]);/*打印周一至周日*/
 rectangle(88,140,323,330);
 line(88,158,323,158);/*月份的框架*/
 for(j=78;j<=84;j+=6)
 line(342,j,560,j);
 setwritemode(1);
 line(431,78,465,78);
 setwritemode(0);
 setcolor(15);
 outtextxy(433,75,"TIME");/*时钟的标题*/
 setcolor(7);
 line(392,310,510,310);
 line(392,330,510,330);
 arc(392,320,90,270,10);
 arc(510,320,270,90,10);
 for(i=431;i<=470;i+=39)
 for(j=317;j<=324;j+=7){
  setlinestyle(0,0,3);
  circle(i,j,1);
  }                  /*DIGITAL CLOCK*/
 line(156,108,156,118);     /*cursor*/
 for(i=0,m=0,h=0;i<=11;i++,h++){                                   /*hour points*/
    x=100*sin((h*60+m)/360*PI)+451;
    y=200-100*cos((h*60+m)/360*PI);
    setlinestyle(0,0,3);
    circle(x,y,1);
    }
 for(i=0,m=0;i<=59;m++,i++){                                       /*minute points*/
       x=100*sin(m/30*PI)+451;
       y=200-100*cos(m/30*PI);
       setlinestyle(0,0,1);
       circle(x,y,1);
       }
 setcolor(9);
 outtextxy(83,380,"COLLEGE OF COMPUTER SCIENCE BEIJING UNIVERSITY OF TECHNOLOGY");
 outtextxy(400,400,"DESIGNED BY flowers");
 outtextxy(500,410,"2003.6");
 setcolor(15);
 outtextxy(81,380,"COLLEGE OF COMPUTER SCIENCE BEIJING UNIVERSITY OF TECHNOLOGY");
 outtextxy(398,400,"DESIGNED BY flowers");
 outtextxy(498,410,"2003.6");
 outtextxy(81,410,"Control:TAB,UP,DOWN,ESC");
 getdate(da);
 mon=da[0].da_mon;
 year=da[0].da_year;
 date(year,mon);
 clock();

 closegraph();
 return 0;
 }
clock(){
 int k=0,count;
 setcolor(15);
 gettime(t);
 h=t[0].ti_hour;
 m=t[0].ti_min;
 x=50*sin((h*60+m)/360*PI)+451;
 y=200-50*cos((h*60+m)/360*PI);
 line(452,200,x,y);
 x1=80*sin(m/30*PI)+451;
 y1=200-80*cos(m/30*PI);
 line(451,200,x1,y1);
 digitclock(408,318,digithour(h));
 digitclock(446,318,digitmin(m));
 setwritemode(1);
 for(count=2;k!=ESC;){                            /*second hand*/
   setcolor(12);
   sound(500);
   delay(70);
   sound(200);
   delay(30);
   nosound();
   s=t[0].ti_sec;
   x2=98*sin(s/30*PI)+451;
   y2=200-98*cos(s/30*PI);
   line(451,200,x2,y2);
   s=t[0].ti_sec;
   while(t[0].ti_sec==s)  {
     gettime(t);/*让时间静止一秒钟!!*/
     if(bioskey(1)!=0){
     k=bioskey(0);
     count=key(k,count);
     if(count==6) count=1;
     } }
   setcolor(15);
   digitclock(485,318,digitsec(s)+1);
   setcolor(12);
   x2=98*sin(s/30*PI)+451;
   y2=200-98*cos(s/30*PI);
   line(451,200,x2,y2);
   if(t[0].ti_min!=m){                         /*minute hand*/
         setcolor(15);
         x1=80*sin(m/30*PI)+451;
         y1=200-80*cos(m/30*PI);
         line(451,200,x1,y1);
         m=t[0].ti_min;
         digitclock(446,318,digitmin(m));
         x1=80*sin(m/30*PI)+451;
         y1=200-80*cos(m/30*PI);
         line(451,200,x1,y1);
        }
  if(t[0].ti_hour!=h){                        /*hour hand*/
          setcolor(15);
          x=50*sin((h*60+m)/360*PI)+451;
          y=200-50*cos((h*60+m)/360*PI);
          line(452,200,x,y);
          h=t[0].ti_hour;
          digitclock(408,318,digithour(h));
          x=50*sin((h*60+m)/360*PI)+451;
          y=200-50*cos((h*60+m)/360*PI);
          line(452,200,x,y);
          }
   }
 }
date(int year,int month)                /*万年历函数*/
{char buffer2[10];
 int a,b,c,d,e,judge,l;
 int year3;
 setcolor(15);
 year3=year;
 if(year>=2000) for(a=0,year--;year>=2000;year--)                        /*计算除当年外有几个闰年*/
        if((year%4==0&&year%100!=0)||(year%100==0&&year%400==0))
   a++ ;
 year=2000;
 if(year<2000) for(a=0,year++;year<=2000;year++)
        if((year%4==0&&year%100!=0)||(year%100==0&&year%400==0))
   a++;
 year=year3;
 if((year%4==0&&year%100!=0)||(year%100==0&&year%400==0)) judge=1;/*判定当年是否为闰年*/
 else judge=0;
        if(month==1) d=0;
        if(month==2) d=31;
        if(month==3&&judge==0) {b=1;
       d=59+judge;
       }
        if(month==3&&judge==1) {b=0;
       d=59;
       }
        if(month==4) d=90+judge;
        if(month==5) d=120+judge;
        if(month==6) d=151+judge;
        if(month==7) d=181+judge;
        if(month==8) d=212+judge;
        if(month==9) d=243+judge;
        if(month==10) d=273+judge;
        if(month==11) d=304+judge;
        if(month==12) d=334+judge;  /*计算月份的天数差*/
   /*计算当月一日是星期几*/
 if(year<2000) c=((2000-year)*365+a+d)%7;
 else c=((year-2000)*365+a+d)%7;
 if(c==0) c=7;

 if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)/*判定每月的天数*/
 e=31;
 if(month==4||month==6||month==9||month==11)
 e=30;
 if(month==2&&judge==0) e=29;
 if(month==2&&judge==1) e=28;
 setfillstyle(1,0);
 bar(90,160,321,328);/*清空前一个月份*/
 for(l=1,y=162;l<=e;l++,c++){                              /*画出月历*/
         if(c>7) {
           y+=32;
           c=1;
           }
         sprintf(buffer2,"%d",l);
         outtextxy((c-1)*35+92,y,buffer2);
         }

 setfillstyle(1,0);
 bar(140,110,155,118);
 sprintf(buffer2,"%d",month);
 outtextxy(140,110,buffer2);
 setfillstyle(1,0);
 bar(250,110,279,118);
 sprintf(buffer2,"%d",year);
 outtextxy(250,110,buffer2);
 }
int key(int key,int count)   /*键盘控制 */
{switch(key)
 {case UP:dateupchange(count-1);
   date(da[0].da_year,da[0].da_mon);
   timeupchange(count-1);
   break;
  case DOWN:datedownchange(count-1);
     date(da[0].da_year,da[0].da_mon);
     timedownchange(count-1);
     break;
  case TAB:setcolor(15);
    clear(count);
    cursor(count);
    count++;
    break;
  }
  return count;
 }
int dateupchange(int count)
{if(count==1) {da[0].da_mon++;
        if(da[0].da_mon==13)
    da[0].da_mon=1;
        }
 if(count==2) da[0].da_year++;
 setdate(da);
 }
int datedownchange(int count)
{if(count==1) {da[0].da_mon--;
        if(da[0].da_mon==0)
    da[0].da_mon=12;
        }
 if(count==2) da[0].da_year--;
 setdate(da);
 }
int timeupchange(int count)
{if(count==3) {t[0].ti_hour++;

        settime(t);
        }
 if(count==4) {t[0].ti_min++;
        settime(t);
        }
 if(count==5) {t[0].ti_sec++;
        settime(t);
        }
 }
int timedownchange(int count)
{if(count==3) {t[0].ti_hour--;
        settime(t);
        }
 if(count==4) {t[0].ti_min--;
        settime(t);
        }
 if(count==5) {t[0].ti_sec--;
        settime(t);
        }
 }

视频教程列表
文章教程搜索
 
C语言程序设计推荐教程
C语言程序设计热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058