首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

想做时钟加日历,该如何解决

2012-02-24 
想做时钟加日历#includestdio.h #includegraphics.h #includemath.h #includedos.h #definepi3.14

想做时钟加日历
#include   "stdio.h "
#include   "graphics.h "
#include   "math.h "
#include   "dos.h "
#define   pi   3.1415926
#define   mid_x   160
#define   mid_y   240
#define   X(a,b,c,d,e)   x=a*sin(pi-((b*c+360/e*d/60)*pi/180))+160
#define   Y(a,b,c,d,e)   y=a*cos(pi-((b*c+360/e*d/60)*pi/180))+240
#define   L(a,b,c,d,e)   X(a,b,c,d,e);Y(a,b,c,d,e);line(160,240,x,y)
void   intdraw()
{
int   m,l,x1,x2,y1,y2;
setcolor(2);
        line(20,80,620,80);
      line(20,80,20,400);
      line(20,400,620,400);
      line(620,400,620,80);
      setcolor(3);
      circle(mid_x,mid_y,130);
        circle(mid_x,mid_y,134);
circle(mid_x,mid_y,1);
for(m=0;m <60;m++)
{
if(m%5==0)   l=10;
else   l=4;
x1=130*cos(m*6*pi/180)+mid_x;
y1=130*sin(m*6*pi/180)+mid_y;
x2=(130-l)*cos(m*6*pi/180)+mid_x;
y2=(130-l)*sin(m*6*pi/180)+mid_y;
        line(x1,y1,x2,y2);
}
}

main()
{
int   graphdriver=DETECT,graphmode;
struct   time   curtime;
int   x,y;
double   h,m,s;
initgraph(&graphdriver,&graphmode, "c:\\tc ");
setbkcolor(0);
while(!kbhit(0))
      {
        intdraw();
        gettime(&curtime);
        h=curtime.ti_hour;
        m=curtime.ti_min;
        s=curtime.ti_sec;
        setcolor(10);
        L(70,h,30,m,12.0);
        setcolor(12);
        L(90,m,6,s,60.0);
        setcolor(8);
        L(120,s,6,0,1.0);
        sleep(1);
        clrscr();
  }
  getch();
  closegraph();
}时钟
void   clear(int   x1,int   y1,int   x2,int   y2,int   c);
void   show_mouse()      
    {      
    union   REGS   ireg;      
    ireg.x.ax=1;      
    int86(0x33,&ireg,&ireg);      
    }      
       
    void   hid_mouse()      
    {      
    union   REGS   ireg;      
    ireg.x.ax=2;      
    int86(0x33,&ireg,&ireg);      
    }      
void   main()
{
int   y,m,d,k,r,i,p,h,l,tj=1;
int   a[]={31,28,31,30,31,30,31,31,30,31,30,31};
char   *b[]={ "Sun ", "Mon ", "Tue ", "Wed ", "Thr ", "Fri ", "Sat "};
char   buff[960];
struct   date   sysdate;
getdate(&sysdate);
y=sysdate.da_year;
m=sysdate.da_mon;
l=wherex();
h=wherey();
gettext(21,1,59,11,buff);
hid_mouse();
while(tj)
{
    clear(21,1,59,11,BLUE);
    gotoxy(34,2);
    textcolor(WHITE);


    cprintf( "%dYEAR   %   dMON   date ",y,m);
    a[1]=((y%4==0&&y%100!=0)||(y%400==0))?29:28;
    d=y+(y-1)/4-(y-1)/100+(y-1)/400;
    if(m> 1)
    {
      for(i=1;i <=m-1;++i)
        d=d+a[i-1];
    }
    k=d%7;
    clear(21,3,59,3,GREEN);
    for(i=0;i <=6;++i)
    {
      gotoxy(5*i+24,3);
      if(i==0||i==6)
        textcolor(RED);
      cputs(b[i]);
      textcolor(WHITE);
    }
    textbackground(BLUE);
    r=4;
    for(i=1;i <=a[m-1];++i)
    {
      gotoxy(5*k+25,r);
      if(k==0||k==6)
        textcolor(RED);
      cprintf( "%2d ",i);
      textcolor(WHITE);
      ++k;
      if(k> 6)
      {
        k=0;
        ++r;
      }
    }
    clear(21,10,59,10,GREEN);
    gotoxy(22,10);
    cprintf( "up/down:YEAR   left/right:MON   ESC:LEAVE ");
    p=getch();
    switch(p)
    {
      case   UP:--y;
                      break;
      case   DOWN:++y;
                          if(y <1840)
                            y=1840;
                          break;
      case   PGUP:y-=20;
                          break;
      case   PGDN:y+=20;
                          if(y <1840)
                            y=1840;
                          break;
      case   RIGHT:++m;
                            if(m> 12)
                              m=1;
                            break;
      case   LEFT:--m;
                          if(m <1)
                            m=12;
                          break;
      case   DEL:y=sysdate.da_year;
                        m=sysdate.da_mon;
                        break;


      case   ESC:tj=0;
    }
    }
puttext(21,1,59,11,buff);
gotoxy(l,h);
show_mouse();
}

void   clear(int   x1,int   y1,int   x2,int   y2,int   c)
{
int   p,q;
textbackground(c);
for(p=y1;p <=y2;++p)
{
    for(q=x1;q <=x2;++q)
    {
      gotoxy(q,p);
      cprintf( "   ");
    }
}
}日历
请问怎样帮两程序合起来!!!谢谢




[解决办法]
做成两个类,然后一个main来调用
或者把两个main里面的东东做成函数,然后用一个main来调用。

[解决办法]
难度关键在graphics.h!
[解决办法]
和成撒?
[解决办法]
帮顶
[解决办法]
给你合了一下,TC2下正常运行。先显示时钟,按任意键显示日历。 在日历里按 "ESC "返回时钟,按 "Q "退出程序,由于这两个部分都是等待键盘输入和刷屏所以想同时显示日历和时钟需要对程序结构进行很大的调整,比较麻烦。估计现在这个样子也能符合你的要求了。注意:CLOCL函数里TC的路径我改动了,运行前你记得改回你本地机器上的路径。


#include "stdio.h "
#include "graphics.h "
#include "math.h "
#include "dos.h "

#define pi 3.1415926
#define mid_x 160
#define mid_y 240
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
#define PGDN 81
#define PGUP 73
#define ESC 27
#define DEL 83

#define X(a,b,c,d,e) x=a*sin(pi-((b*c+360/e*d/60)*pi/180))+160
#define Y(a,b,c,d,e) y=a*cos(pi-((b*c+360/e*d/60)*pi/180))+240
#define L(a,b,c,d,e) X(a,b,c,d,e);Y(a,b,c,d,e);line(160,240,x,y)

void clear(int x1,int y1,int x2,int y2,int c);
void intdraw();
void clock();
void show_mouse();
void date();

void main(void)
{

while(1)
{
clock();
date();
}


}


void intdraw()
{
int m,l,x1,x2,y1,y2;
setcolor(2);
line(20,80,620,80);
line(20,80,20,400);
line(20,400,620,400);
line(620,400,620,80);
setcolor(3);
circle(mid_x,mid_y,130);
circle(mid_x,mid_y,134);
circle(mid_x,mid_y,1);
for(m=0;m <60;m++)
{
if(m%5==0) l=10;
else l=4;
x1=130*cos(m*6*pi/180)+mid_x;
y1=130*sin(m*6*pi/180)+mid_y;
x2=(130-l)*cos(m*6*pi/180)+mid_x;
y2=(130-l)*sin(m*6*pi/180)+mid_y;
line(x1,y1,x2,y2);
}
}

void clock()
{
int graphdriver=DETECT,graphmode;
struct time curtime;
int x,y;
double h,m,s;
initgraph(&graphdriver,&graphmode, "c:\\TURBOC2 ");
setbkcolor(0);
while(!kbhit(0))
{
intdraw();
gettime(&curtime);
h=curtime.ti_hour;
m=curtime.ti_min;
s=curtime.ti_sec;
setcolor(10);
L(70,h,30,m,12.0);
setcolor(12);
L(90,m,6,s,60.0);
setcolor(8);
L(120,s,6,0,1.0);
sleep(1);
clrscr();
}

closegraph();
}
/*-----------------------------时钟*/

void show_mouse()
{
union REGS ireg;
ireg.x.ax=1;
int86(0x33,&ireg,&ireg);
}

void hid_mouse()
{
union REGS ireg;
ireg.x.ax=2;
int86(0x33,&ireg,&ireg);


}


void date()
{
int y,m,d,k,r,i,p,h,l,tj=1;
int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
char *b[]={ "Sun ", "Mon ", "Tue ", "Wed ", "Thr ", "Fri ", "Sat "};
char buff[960];
struct date sysdate;
getdate(&sysdate);
y=sysdate.da_year;
m=sysdate.da_mon;
l=wherex();
h=wherey();
gettext(21,1,59,11,buff);
hid_mouse();
while(tj)
{

clear(21,1,59,11,BLUE);
gotoxy(34,2);
textcolor(WHITE);
cprintf( "%dYEAR % dMON date ",y,m);
a[1]=((y%4==0&&y%100!=0)||(y%400==0))?29:28;
d=y+(y-1)/4-(y-1)/100+(y-1)/400;
if(m> 1)
{
for(i=1;i <=m-1;++i)
d=d+a[i-1];
}
k=d%7;
clear(21,3,59,3,GREEN);
for(i=0;i <=6;++i)
{
gotoxy(5*i+24,3);
if(i==0||i==6)
textcolor(RED);
cputs(b[i]);
textcolor(WHITE);
}
textbackground(BLUE);
r=4;
for(i=1;i <=a[m-1];++i)
{
gotoxy(5*k+25,r);
if(k==0||k==6)
textcolor(RED);
cprintf( "%2d ",i);
textcolor(WHITE);
++k;
if(k> 6)
{
k=0;
++r;
}
}
clear(21,10,59,10,GREEN);
gotoxy(22,10);
cprintf( "up/down:YEAR left/right:MON ESC:RETURN Q:QUIT ");
p=getch();
switch(p)
{
case UP:--y;
break;
case DOWN:++y;
if(y <1840)
y=1840;
break;
case PGUP:y-=20;
break;
case PGDN:y+=20;
if(y <1840)
y=1840;
break;
case RIGHT:++m;
if(m> 12)
m=1;
break;
case LEFT:--m;
if(m <1)
m=12;
break;
case DEL:y=sysdate.da_year;
m=sysdate.da_mon;
break;
case ESC:tj=0;
break;
case 'q ':
exit(0);
break;
}
}
puttext(21,1,59,11,buff);
gotoxy(l,h);
show_mouse();
}

void clear(int x1,int y1,int x2,int y2,int c)
{
int p,q;
textbackground(c);
for(p=y1;p <=y2;++p)
{
for(q=x1;q <=x2;++q)
{
gotoxy(q,p);
cprintf( " ");
}
}
}/*日历*/
[解决办法]
这就是c语言作为中级语言的一个重要体现,这是一个C中调用bios系统调用和dos系统调用的函数接口,第一个参数是软中断号,第二个是作为参数的寄存器组,第三个是作为返回值的寄存器组,往往后两个参数用同一个寄存器组就可以了。
该函数的函数原形如下
int _Cdecl int86 (int intno, union REGS *inregs, union REGS *outregs);
还有一个类似的函数,这个函数多了一个参数,作为传递给系统调用的段寄存器组。 如下
int _Cdecl int86x (int intno, union REGS *inregs, union REGS *outregs,
struct SREGS *segregs);

C语言中的 REGS 结构如下:

union REGS
{
struct WORDREGS x;
struct BYTEREGS h;
};

明显地,REGS 是由两个另外的结构封装而成,WORDREGS 结构及 BYTEREGS 结构。

struct WORDREGS
{
unsigned int ax, bx, cx, dx, si, di, cflag, flags;
};

unsigned int 一般在16位字长CPU下的编译器被定义尺寸是16位(2字节)的。

BYTEREGS 如下:
struct BYTEREGS
{
unsigned char al, ah, bl, bh, cl, ch, dl, dh;
};

分别是各寄存器的高位字节与低位字节的描述,以 unsigned char 类型来声明是因为,高位字节与低位字节都是8位宽。


上面的内容如果理解困难的话,你就理解为调用了一个系统中断好了,每个的具体意思不太清楚也没什么大关系。

热点排行