可以用C++编写钟表程序吗?每隔一秒自动走的那种,要代码!!!

可以用C++编写钟表程序吗?每隔一秒自动走的那种,要代码!!!

#include"stdio.h"
struct clock
{
int hour;
int minute;
int second;
};
typedef struct clock CLOCK;
void update (CLOCK *t)
{
t->second++;
if(t->second==60)
{
t->second=0;
t->minute++;
}
if(t->minute==60)
{
t->minute=0;
t->hour++;
}
if(t->hour==24)
t->hour=0;
}
void display(CLOCK *t)
{
printf("%02d:%02d:%02d\r",t->hour,t->minute,t->second);
}
void deplay()
{
int t;
for(t=0;t<10000;t++);
}
main()
{
CLOCK a;
CLOCK *time=&a;
time->hour=time->minute=time->second=0;
for(;;)
{
update(time);
display(time);
deplay();
}
} #include"graphics.h"
#define PI 3.1416
#include"math.h"
#include"dos.h"
main()
{
int x0=320,y0=240,r0=150;
void init_sceen();
void sec();
init_sceen(x0,y0,r0);
sec();
closegraph();
}
void init_sceen(int x0,int y0,int r0)/********************************************/
{
int i,x,y,graphdriver,graphmode;
char s[10];
float alpha,a0=90;

graphdriver=DETECT;
initgraph(&graphdriver,&graphmode,"");
setbkcolor(3);
setcolor(2);
circle(x0,y0,r0);
circle(x0,y0,r0+30);
setfillstyle(SOLID_FILL,10);
floodfill(x0-r0-10,y0,2);
/*please input the time*/
for(i=12;i>=1;i--)
{
alpha=(a0+30*(11-i)*PI/180);
x=x0+cos(alpha)*r0-16;
y=y0-sin(alpha)*r0;
sprintf(s,"%2d",i);
setcolor(4);
settextstyle(0,0,2);
outtextxy(x,y,s);
}
/*input second*/
for(i=60;i>=1;i--)
{
alpha=(a0+6*(60-i)*PI/180);
x=x0+cos(alpha)*(r0-20);
y=y0-sin(alpha)*(r0-20);
setcolor(14);
if(i%5==0)
circle(x,y,5);
else circle(x,y,2);
floodfill(x,y,14);
}
setlinestyle(0,0,3);
}
void sec(void) /******************************************************************/
{
int x,y,i,j,k,xj,yj,xk,yk,xi,yi,x0=320,y0=240,r0=150;
union REGS r;

unsigned char *shijie="";
unsigned char *daa="";
struct time tim;
struct date dat;
float alphai,alphak,alphaj,a0=90;
xi=x0;yi=y0;xj=x0;yj=y0;xk=x0;yk=y0;
do
{
/*intput the time*/
x=38;y=12;
gettime(&tim);
sprintf(shijie,"%02d:%02d:%02d",tim.ti_hour,tim.ti_min,tim.ti_sec);
setfillstyle(SOLID_FILL,0);
bar(245,190,375,210);
setcolor(15);
outtextxy(245,190,shijie);
/*input the date*/ /*****************************************************/
getdate(&dat);
sprintf(daa,"%02d--%02d--%02d",dat.da_year,dat.da_mon,dat.da_day);
/*setfillstyle(SOLID_FILL,3);*/
bar(225,290,395,310);
setcolor(BLUE);
outtextxy(225,290,daa);
x=190;y=430;
setcolor(BLUE);
outtextxy(x-26,y,"Designed by Xing Lei");
setcolor(LIGHTRED);
outtextxy(x+60,y0+20,"xing loving");
setlinestyle(0,0,3);
k=tim.ti_hour;
j=tim.ti_min;
i=tim.ti_sec;
alphak=(a0+30*(12-k)-j*5/60.*6)*PI/180;
alphaj=(a0-6*j)*PI/180;
/*write second hand*/
alphai=(a0+6*(60-i))*PI/180;
x=x0+cos(alphai)*(r0-32);
y=y0-sin(alphai)*(r0-32);
setcolor(BLACK);
line(x0,y0,xi,yi);
setcolor(YELLOW);
line(x0,y0,x,y);
xi=x;
yi=y;
/*write minute hand*/
x=x0+cos(alphaj)*(r0-60);
y=y0-sin(alphaj)*(r0-60);
setcolor(BLACK);
line(x0,y0,xj,yj);
setcolor(BLUE);
line(x0,y0,x,y);
xj=x;
yj=y;
/*write hour hand*/
x=x0+cos(alphak)*(r0-99);
y=y0-sin(alphak)*(r0-99);
setcolor(BLACK);
line(x0,y0,xk,yk);
setcolor(RED);
line(x0,y0,x,y);
xk=x;
yk=y;
delay(10000);
}
while(!kbhit());
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-07-23
void CClockView::OnDraw(CDC* pDC)
{
CClockDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);

// Get the client rectangle so that we
// can dynamically fit the clock to
// the window.
RECT Rect;
GetClientRect( &Rect );

// For easier calculations, get the
// x and y coordinates of the
// window center.
int nCenterX = Rect.right / 2;
int nCenterY = Rect.bottom / 2;

// Get the current time.
CTime Time = CTime::GetCurrentTime();

CString strDigits;
int i, x, y;
CSize size;

switch( m_nClockType ){
case CLOCK_ANALOG:
{
// Create a yellow pen with which we'll
// draw the ellipse.
CPen Pen( PS_SOLID, 5, RGB( 255, 255, 0 ) );

// Select the new pen into the DC and
// remember the pen that was selected out.
CPen *pOldPen = pDC->SelectObject( &Pen );

// Draw the clock face border with
// the Ellipse function.
pDC->Ellipse( 5, 5, Rect.right - 5,
Rect.bottom - 5 );

double Radians;

// Out text color will be red.
pDC->SetTextColor( RGB( 255, 0, 0 ) );

for( i=1; i<=12; i++ ){

// Format the digit CString object
// for the current clock number.
strDigits.Format( "%d", i );

// Get the text extent of the
// text so that we can center
// it about a point.
size =
pDC->GetTextExtent( strDigits,
strDigits.GetLength() );

// Calculate the number of radians
// for the current clock number.
Radians = (double) i * 6.28 / 12.0;

// Calculate the x coordinate. We
// use the text extent to center
// the text about a point.
x = nCenterX -
( size.cx / 2 ) +
(int) ( (double) ( nCenterX - 20 ) *
sin( Radians ) );

// Calculate the y coordinate. We
// use the text extent to center
// the text about a point.
y = nCenterY -
( size.cy / 2 ) -
(int) ( (double) ( nCenterY - 20 ) *
cos( Radians ) );

// Draw the text.
pDC->TextOut( x, y, strDigits );

}

// Calculate the radians for the hour hand.
Radians = (double) Time.GetHour() +
(double) Time.GetMinute() / 60.0 +
(double) Time.GetSecond() / 3600.0;
Radians *= 6.28 / 12.0;

// Create a pen for the hour hand that's five
// pixels wide with a green color.
CPen HourPen( PS_SOLID, 5, RGB( 0, 255, 0 ) );

// Select the newly-created CPen object
// into the DC.
pDC->SelectObject( &HourPen );

// Move to the center of the clock, then
// draw the hour hand line.
pDC->MoveTo( nCenterX, nCenterY );
pDC->LineTo(
nCenterX + (int) ( (double) ( nCenterX / 3 ) *
sin( Radians ) ),
nCenterY - (int) ( (double) ( nCenterY / 3 ) *
cos( Radians ) ) );

// Calculate the radians for the minute hand.
Radians = (double) Time.GetMinute() +
(double) Time.GetSecond() / 60.0;
Radians *= 6.28 / 60.0;

// Create a pen for the minute hand that's three
// pixels wide with a blue color.
CPen MinutePen( PS_SOLID, 3, RGB( 0, 0, 255 ) );

// Select the newly-created CPen object
// into the DC.
pDC->SelectObject( &MinutePen );

// Move the to center of the clock, then
// draw the minute hand line.
pDC->MoveTo( nCenterX, nCenterY );
pDC->LineTo(
nCenterX + (int) ( (double) (
( nCenterX * 2 ) / 3 ) * sin( Radians ) ),
nCenterY - (int) ( (double) (
( nCenterY * 2 ) / 3 ) * cos( Radians ) ) );

// Calculate the radians for the second hand.
Radians = (double) Time.GetSecond();
Radians *= 6.28 / 60.0;

// Create a pen for the second hand that's one
// pixels wide with a cyan color.
CPen SecondPen( PS_SOLID, 1, RGB( 0, 255, 255 ) );

// Select the newly-created CPen object
// into the DC.
pDC->SelectObject( &SecondPen );

// Move the to center of the clock, then
// draw the second hand line.
pDC->MoveTo( nCenterX, nCenterY );
pDC->LineTo(
nCenterX + (int) ( (double) (
( nCenterX * 4 ) / 5 ) * sin( Radians ) ),
nCenterY - (int) ( (double) (
( nCenterY * 4 ) / 5 ) * cos( Radians ) ) );

// Select the old CPen object back into
// the DC.
pDC->SelectObject( pOldPen );
}
break;
case CLOCK_DIGITAL:
{

// Begin by creating the text string
// that represents the digital time.
strDigits.Format( "%d:%02d:%02d",
Time.GetHour(),
Time.GetMinute(),
Time.GetSecond() );

CFont Font, *pOldFont;
int nWidth = 100;
int nHeight = 140;

do{

// We may have already created
// a font in the CFont object. Here
// we make sure it's deleted.
if( Font.GetSafeHandle() != NULL )
Font.DeleteObject();

// Create the font with mostly default
// values. Use a Times New Roman font.
Font.CreateFont( nHeight, nWidth, 0, 0,
FW_DONTCARE, 0, 0, 0, ANSI_CHARSET,
OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
"Times New Roman" );

// Select the newly-created CFont object into
// the DC and remember the CFont object that
// was selected out.
pOldFont = pDC->SelectObject( &Font );

// Get the text extent so we can decide if
// this font is too large.
size =
pDC->GetTextExtent( strDigits,
strDigits.GetLength() );

// Select the old font back into the DC.
pDC->SelectObject( pOldFont );

// If the text extent is too wide,
// reduce the font width.
if( size.cx > Rect.right )
nWidth -= 5;

// If the text extent is too high,
// reduce the font height.
if( size.cy > Rect.bottom )
nHeight -= 5;

} while( size.cx > Rect.right ||
size.cy > Rect.bottom );

// Select the final font into the
// DC and remember the CFont object
// that's selected out.
pOldFont = pDC->SelectObject( &Font );

// Draw the text.
pDC->TextOut( nCenterX - ( size.cx / 2 ),
nCenterY - ( size.cy / 2 ),
strDigits );

// Select the old font back into the DC.
pDC->SelectObject( pOldFont );

}
break;
}

}

void CClockView::OnTimer(UINT nIDEvent)
{

// The timer just causes a redraw to occur.
// In the OnDraw() function the current
// time is obtained.
InvalidateRect( NULL, TRUE );
UpdateWindow();

CView::OnTimer(nIDEvent);
}
相似回答