首页 > 编程知识 正文

包含c语言改系统时钟的词条

时间:2024-03-25 09:50:19 阅读:333227 作者:JOOV

本文目录一览:

c语言怎么样编写一个时钟程序

c语言时钟程序代码如下:

#includewindows.h

#includemath.h

#define ID_TIMER 1//计时器ID

#define TWOPI (2*3.14159)

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR szCmdLine,int iCmdShow)

{

static TCHAR szAppName[]=TEXT("Clock");

HWND hwnd;

MSG msg;

WNDCLASS wndclass;

wndclass.cbClsExtra=0;

wndclass.cbWndExtra=0;

wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);

wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);

wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);

wndclass.hInstance=hInstance;

wndclass.lpfnWndProc=WndProc;

wndclass.lpszClassName=szAppName;

wndclass.lpszMenuName=NULL;

wndclass.style=CS_HREDRAW|CS_VREDRAW;

if(!RegisterClass(wndclass))

{

MessageBox(NULL,TEXT("This program requires Windows

T"),szAppName,MB_ICONERROR);

return 0;

}

hwnd=CreateWindow(szAppName,TEXT("Analog Clock"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);

ShowWindow(hwnd,iCmdShow);

UpdateWindow(hwnd);

while(GetMessage(msg,NULL,0,0))

{

TranslateMessage(msg);

DispatchMessage(msg);

}

return msg.wParam;

}

void Setsotropic(HDC hdc,int cxClient,int cyClient)

{

SetMapMode(hdc,MM_ISOTROPIC);

SetWindowExtEx(hdc,1000,1000,NULL);

SetViewportExtEx(hdc,cxClient/2,-cyClient/2,NULL);

SetViewportOrgEx(hdc,cxClient/2,cyClient/2,NULL);

}

void RotatePoint(POINT pt[],int iNum,int iAngle)

{

int i;

POINT ptTemp;

for(i=0;iiNum;i++)

{

ptTemp.x=(int)(pt[i].x*cos(TWOPI*iAngle/360)+pt[i].y*sin(TWOPI*iAngle/360));

ptTemp.y=(int)(pt[i].y*cos(TWOPI*iAngle/360)+pt[i].x*sin(TWOPI*iAngle/360));

pt[i]=ptTemp;

}

}

void DrawClock(HDC hdc)

{

int iAngle;

POINT pt[3];

for(iAngle=0;iAngle360;iAngle+=6)

{

pt[0].x=0;

pt[0].y=900;

RotatePoint(pt,1,iAngle);

pt[2].x=pt[2].y=iAngle%5?33:100;

pt[0].x-=pt[2].x/2;

pt[0].y-=pt[2].y/2;

pt[1].x=pt[0].x+pt[2].x;

pt[1].y=pt[0].y+pt[2].y;

SelectObject(hdc,GetStockObject(BLACK_BRUSH));

Ellipse(hdc,pt[0].x,pt[0].y,pt[1].x,pt[1].y );

}

}

void DrawHands(HDC hdc,SYSTEMTIME *pst,BOOL fChange)

{

static POINT pt[3][5]={0,-150,100,0,0,600,-100,0,0,-150, 0,-200,50,0,0,800,-50,0,0,-200, 0,0,0,0,0,0,0,0,0,800 };

int i,iAngle[3];

POINT ptTemp[3][5];

iAngle[0]=(pst-wHour*30)%360+pst-wMinute/2;

iAngle[1]=pst-wMinute*6;

iAngle[2]=pst-wSecond*6;

memcpy(ptTemp,pt,sizeof(pt));

for(i=fChange?0:2;i3;i++)

{

RotatePoint(ptTemp[i],5,iAngle[i]);

Polyline(hdc,ptTemp[i],5);

}

}

LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)

{

static int cxClient,cyClient;

static SYSTEMTIME stPrevious;

BOOL fChange;

HDC hdc;

PAINTSTRUCT ps;

SYSTEMTIME st;

switch(message)

{

case WM_CREATE:

SetTimer(hwnd,ID_TIMER,1000,NULL);

GetLocalTime(st);

stPrevious=st;

return 0;

case WM_SIZE:

cxClient=LOWORD(lParam);

cyClient=HIWORD(lParam);

return 0;

case WM_TIMER:

GetLocalTime(st);

fChange=st.wHour!=stPrevious.wHour||st.wMinute!=stPrevious.wMinute;

hdc=GetDC(hwnd);

Setsotropic(hdc,cxClient,cyClient);

SelectObject(hdc,GetStockObject(WHITE_PEN));

DrawHands(hdc,stPrevious,fChange);

SelectObject(hdc,GetStockObject(BLACK_PEN));

DrawHands(hdc,st,TRUE);

stPrevious=st;

return 0;

case WM_PAINT:

hdc=BeginPaint(hwnd,ps);

Setsotropic(hdc,cxClient,cyClient);

DrawClock(hdc);

DrawHands(hdc,stPrevious,TRUE);

EndPaint(hwnd,ps);

return 0;

case WM_DESTROY:

KillTimer(hwnd,ID_TIMER);

PostQuitMessage(0);

return 0;

}

return DefWindowProc(hwnd,message,wParam,lParam);

}

C语言是一门通用计算机编程语言,应用广泛。C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。尽管C语言提供了许多低级处理的功能,但仍然保持着良好跨平台的特性,以一个标准规格写出的C语言程序可在许多电脑平台上进行编译,甚至包含一些嵌入式处理器(单片机或称MCU)以及超级电脑等作业平台。

C语言是由UNIX的研制者丹尼斯·里奇(Dennis Ritchie)于1970年 由 肯·汤普逊(Ken Thompson)所研制出的B语言的基础上发展和完善起来的。目前,C语言编译器普遍存在于各种不同的操作系统中,例如UNIX、MS-DOS、Microsoft Windows及Linux等。C语言的设计影响了许多后来的编程语言,例如C++、Objective-C、Java、C#等。

C语言如何调用系统时间

方法一,#includetime.h

int main()

{

time_t timep;

struct tm *p;

time (timep);

p=gmtime(timep);

printf("%dn",p-tm_sec); /*获取当前秒*/

printf("%dn",p-tm_min); /*获取当前分*/

printf("%dn",8+p-tm_hour);/*获取当前时,这里获取西方的时间,刚好相差八个小时*/

printf("%dn",p-tm_mday);/*获取当前月份日数,范围是1-31*/

printf("%dn",1+p-tm_mon);/*获取当前月份,范围是0-11,所以要加1*/

printf("%dn",1900+p-tm_year);/*获取当前年份,从1900开始,所以要加1900*/

printf("%dn",p-tm_yday); /*从今年1月1日算起至今的天数,范围为0-365*/

}

方法二.#include stdio.h

#include time.h

int main ()

{

time_t t

struct tm * lt;    time (t);//获取Unix时间戳。

lt = localtime (t);//转为时间结构。

printf ( "%d/%d/%d %d:%d:%dn",lt-tm_year+1900, lt-tm_mon, lt-tm_mday,

lt-tm_hour, lt-tm_min, lt-tm_sec);//输出结果

return 0;}

扩展资料

1、CTimeSpan类

如果想计算两段时间的差值,可以使用CTimeSpan类,具体使用方法如下:

CTime t1( 1999, 3, 19, 22, 15, 0 );

CTime t = CTime::GetCurrentTime();

CTimeSpan span=t-t1; //计算当前系统时间与时间t1的间隔

int iDay=span.GetDays(); //获取这段时间间隔共有多少天

int iHour=span.GetTotalHours(); //获取总共有多少小时

int iMin=span.GetTotalMinutes();//获取总共有多少分钟

int iSec=span.GetTotalSeconds();//获取总共有多少秒

2、timeb()函数

_timeb定义在SYSTIMEB.H,有四个fields

dstflag

millitm

time

timezone

void _ftime( struct _timeb *timeptr );

struct _timeb timebuffer;

_ftime( timebuffer );

参考资料来源:百度百科:time函数

c语言 更改系统时间(settime)程序

函数名: settime

功 能: 设置系统时间

用 法: void settime(struct time *timep);

程序例:

#include stdio.h

#include dos.h

int main(void)

{

struct time t;

gettime(t);

printf("The current minute is: %dn", t.ti_min);

printf("The current hour is: %dn", t.ti_hour);

printf("The current hundredth of a second is: %dn", t.ti_hund);

printf("The current second is: %dn", t.ti_sec);

/* Add one to the minutes struct element and then call settime */

t.ti_min++; //在这个地方就可以控制你要改的时间的分,以此同!

settime(t);

return 0;

}

C语言编写由键盘输入设定计算机时钟当前时间的程序

我同意楼上说的  计算机时钟和当前时间不是一个概念

但是我猜你应该是想修改当前时间,代码如下

#includestdio.h

#includestdlib.h

#includestring.h

#define time_len 20

int main ()

{

    char set_time[time_len], cmd[100];

    printf("请输入想设置的时间(格式XX:xx):");

    scanf("%s", set_time); //输入将时间修改为XX:XX,其实可以不设置分,直接XX,则默认XX:00

    strcpy(cmd, "time ");

    strcat(cmd, set_time);

    system(cmd);

}

C语言中调用系统时间

#include stdio.h 

#include time.h 

int main()

time_t rawtime; 

struct tm * timeinfo; 

time ( rawtime ); 

timeinfo = localtime ( rawtime ); 

printf ( "当前系统时间: %s", asctime (timeinfo) ); 

return 0;

}

说明:

time_t // 时间类型(time.h 定义) 

struct tm { // 时间结构,time.h 定义如下: 

int tm_sec; 

int tm_min; 

int tm_hour; 

int tm_mday; 

int tm_mon; 

int tm_year; 

int tm_wday; 

int tm_yday; 

int tm_isdst; 

time ( rawtime ); // 获取时间,以秒计,从1970年1月一日起算,存于rawtime 

localtime ( rawtime ); //转为当地时间,tm 时间结构 

asctime() // 转为标准ASCII时间格式: 

//就是直接打印tm,tm_year 从1900年计算,所以要加1900,月tm_mon,从0计算,所以要加1

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。