1. 求個c語言小代碼,很簡單的分鍾倒計時程序
//有很多種方法,我這里用的是其中一種:
#include<stdio.h>
#include<Windows.h>
intmain(void){
intminutes;
printf("請輸入分鍾數: ");
scanf("minutes");
for(;minutes>=0;--minutes){
for(inti=60;i>=0;--i){
printf("還剩%d分鍾%d秒結束 ",minutes,i);//輸出剩餘時間
Sleep(1000);//延時函數延時一秒
};
};
printf("計時結束 ");
system("PAUSE");
return0;
};
//我沒上編譯器測試,但是具體架構就是這樣
2. c語言編程,怎麼計算時間
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main()
{
unsigned char time1[] = {10, 8, 31, 9, 26 };
unsigned char time2[] = { 10, 8, 31, 9, 50 };
struct tm t1 = {0};
struct tm t2 = {0};
time_t _t1;
time_t _t2;
double diff;
t1.tm_year = time1[0] + 100;
t1.tm_mon = time1[1];
t1.tm_mday = time1[2];
t1.tm_hour = time1[3];
t1.tm_min = time1[4];
t2.tm_year = time2[0] + 100;
t2.tm_mon = time2[1];
t2.tm_mday = time2[2];
t2.tm_hour = time2[3];
t2.tm_min = time2[4];
_t1 = _mkgmtime( &t1 );
_t2 = _mkgmtime( &t2 );
diff = difftime(_t2, _t1 );
printf( "相差 %.0f 分鍾
", diff / 60 );
}
(2)c語言編寫一個兩分鍾的時間擴展閱讀:
C語言中有兩個相關的函數用來計算時間差,分別是:
time_t time( time_t *t) 與 clock_t clock(void)
頭文件: time.h
計算的時間單位分別為: s , ms
time_t 和 clock_t 是函數庫time.h 中定義的用來保存時間的數據結構
返回值:
1、time : 返回從公元1970年1月1號的UTC時間從0時0分0秒算起到現在所經過的秒數。如果參數 t 非空指針的話,返回的時間會保存在 t 所指向的內存。
2、clock:返回從「開啟這個程序進程」到「程序中調用clock()函數」時之間的CPU時鍾計時單元(clock tick)數。 1單元 = 1 ms。
所以我們可以根據具體情況需求,判斷採用哪一個函數。
具體用法如下例子:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
time_t c_start, t_start, c_end, t_end;
c_start = clock(); //!< 單位為ms
t_start = time(NULL); //!< 單位為s
system("pause");
c_end = clock();
t_end = time(NULL);
//!<difftime(time_t, time_t)返回兩個time_t變數間的時間間隔,即時間差
printf("The pause used %f ms by clock() ",difftime(c_end,c_start));
printf("The pause used %f s by time() ",difftime(t_end,t_start));
system("pause");
return 0;
}
因此,要計算某一函數塊的佔用時間時,只需要在執行該函數塊之前和執行完該函數塊之後調用同一個時間計算函數。再調用函數difftime()計算兩者的差,即可得到耗費時間。
3. c語言怎麼樣編寫一個時鍾程序
c語言時鍾程序代碼如下:
#include<windows.h>
#include<math.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;i<iNum;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;iAngle<360;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;i<3;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);
}
4. c語言時鍾代碼
#include<graphics.h> /* 引入graphic.h */
#include<math.h> /* 引入math.h */
#include<dos.h> /* 引入dos.h */
#define pi 3.1415926 /*定義pi=3.14159*/
#define X(a,b,c) x=a*cos(b*c*pi/180-pi/2)+300;
#define Y(a,b,c) y=a*sin(b*c*pi/180-pi/2)+240;
#define d(a,b,c) X(a,b,c);Y(a,b,c);line(300,240,x,y) /*定義……*/
void init() /*初始化程序*/
{int i,l,x1,x2,y1,y2; /*定義……*/
setbkcolor(1); /*設置顏色*/
circle(300,240,200); /*作園*/
circle(300,240,205);
circle(300,240,5);
for(i=0;i<60;i++) /*循環(算時間)*/
{if(i%5==0) l=15;
else l=5;
x1=200*cos(i*6*pi/180)+300;
y1=200*sin(i*6*pi/180)+240;
x2=(200-l)*cos(i*6*pi/180)+300;
y2=(200-l)*sin(i*6*pi/180)+240;
line(x1,y1,x2,y2);
}
}
main()
{
int x,y;
int gd=VGA,gm=2;
unsigned char h,m,s; /*定義*/
struct time t[1];
initgraph(&gd,&gm,"d:\\tc");
init();
setwritemode(1);
gettime(t);
h=t[0].ti_hour;
m=t[0].ti_min;
s=t[0].ti_sec; /*定義時分秒*/
setcolor(7); /*設置顏色*/
d(150,h,30);
setcolor(14);
d(170,m,6);
setcolor(4);
d(190,s,6);
while(!kbhit()) /*獲取鍵盤相應*/
{while(t[0].ti_sec==s)
gettime(t); /*C語言中得到時間的函數*/
sound(400); /*計算時間……*/
delay(70);
sound(200);
delay(30);
nosound();
setcolor(4);
d(190,s,6);
s=t[0].ti_sec;
d(190,s,6);
if (t[0].ti_min!=m)
{
setcolor(14);
d(170,m,6);
m=t[0].ti_min;
d(170,m,6);
}
if (t[0].ti_hour!=h)
{ setcolor(7);
d(150,h,30);
h=t[0].ti_hour;
d(150,h,30);
sound(1000);
delay(240);
nosound();
delay(140);
sound(2000);
delay(240);
nosound();
}
}
getch(); /*設置空格後退出*/
closegraph();
}
具體的。。就是套用用幾個函數算時間。。
不要對這種很長的東西害怕,其實大部分都是在畫這個鍾~
加油哦~
5. 怎麼用C語言定義時間
樓主,我這段代碼是讀取時間的,不知道是不是你想要的:
#include <stdio.h>
#include <time.h>
int main(void)
{time_t now = time(NULL);
tm *t = localtime(&now);
printf("%d-%d-%d %d:%d:%d\n", t->tm_year+1900, t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
getchar();
return 0;}
6. 用c語言,求兩個時間的分鍾數
意思如下:
A時間為15:35
B時間為18:22
把A時間換算成分鍾 15*60+35=935
把B時間換算成分鍾 18*60+22=1102
1102-935=167 間隔167分鍾