秒錶源碼
❶ 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();
}
http://..com/question/121241211.html
❷ 如何用C語言編程,使出現一個界面幾秒後自動跳到另一個界面;求高手解答。
你的界面指什麼界面???控制台還是應用程序界面??
給個代碼你參考下吧,是秒錶計時器程序的代碼
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <stdlib.h>
struct tm //定義時間結構體,包括時分秒和10毫秒
{
int hours,minutes,seconds;
int hscd;
}time,tmp,total; //time用以計時顯示,tmp用以存儲上一階段時間,total記總時間
int cnt;
FILE* fout;
//每次調用update函數,相當於時間過了10ms
void update(struct tm *t)
{
(*t).hscd++; //10ms單位時間加1
cnt++;
if ((*t).hscd==100) //計時滿1s,進位
{
(*t).hscd=0;
(*t).seconds++;
}
if ((*t).seconds==60) //計時滿一分,進位
{
(*t).seconds=0;
(*t).minutes++;
}
if ((*t).minutes==60) //計時滿一小時,進位
{
(*t).minutes=0;
(*t).hours++;
}
if((*t).hours==24) (*t).hours=0;
//delay();
Sleep(10); //Sleep是windows提供的函數,作用是暫停程序,單位毫秒,所以此處暫停10ms
}
void display(struct tm *t)
{
//此處輸出計時結果,\r為回車不換行,既一直在同一行更新時間
printf("%d:",(*t).hours);
printf("%d:",(*t).minutes);
printf("%d:",(*t).seconds);
printf("%d\r",(*t).hscd);
//printf("Now, press 『e』 key to stop the clock…");
}
void time_init() //初始化時間
{
time.hours=time.minutes=time.seconds=time.hscd=0;
}
void get_total() //計算總時間
{
total.hscd = cnt % 100;
cnt /= 100;
total.seconds = cnt % 60;
cnt /= 60;
total.minutes = cnt % 60;
cnt /= 60;
total.hours = cnt;
}
int main()
{
char m;
time_init();
cnt = 0;
fout = fopen("timeout.txt","w");
printf("按回車鍵開始計時!\n");
while(1)
{
m = getch();
if(m != 『\r』) //讀入一個輸入,如果是回車,那麼跳出次循環
printf("輸入錯誤,僅能輸入回車鍵!\n");
else
break;
}
printf("已經開始計時,但是你可以按回車鍵以分段計時!\n");
while(1)
{
if(kbhit()) //此處檢查是否有鍵盤輸入
{
m=getch();
if(m == 『\r』) //如果等於回車,那麼計時結束,跳出循環
break;
else if(m == 『 『) //如果等於空格,顯示此次計時,初始化計時器
{
tmp = time; //記錄上一段計時器結果
fprintf(fout,"%d:%d:%d:%d\n",tmp.hours,tmp.minutes,tmp.seconds,tmp.hscd); //寫入文件
time_init();
printf("\n");
}
else
{
printf("輸入錯誤,僅支持輸入回車鍵或者空格鍵!\n");
}
}
update(&time); //更新計時器
display(&time); //顯示計時器時間
}
tmp = time; //輸出最後一次即使結果,寫入文件
fprintf(fout,"%d:%d:%d:%d\n",tmp.hours,tmp.minutes,tmp.seconds,tmp.hscd);
get_total(); //計算總的時間,顯示,並寫入文件
printf("\n總時間:%d:%d:%d:%d\n",total.hours,total.minutes,total.seconds,total.hscd);
fprintf(fout,"統計時間:%d:%d:%d:%d\n",total.hours,total.minutes,total.seconds,total.hscd);
fclose(fout);
printf("已經保存到當前目錄下的timeout.txt文件中按任意鍵結束!");
getch();
}
❸ 用Delphi實現的秒錶程序源碼
控制項 button1 開始計時按鈕
button2 置零按鈕
edit1 和timer1結合,顯示從比賽開始到現在的時間,時刻變化
timer1 用來計時,interval=10
memo1 用來存儲每個人的時間的記錄
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Edit1: TEdit;
Button2: TButton;
Timer1: TTimer;
procere Button1Click(Sender: TObject);
procere Button2Click(Sender: TObject);
procere FormShow(Sender: TObject);
procere Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
i:integer;
time:tdatetime;
s:string;
implementation
{$R *.dfm}
procere TForm1.Button1Click(Sender: TObject);//計時開始按鈕
begin
if i=0 then
begin
i:=1;
time:=now;
s:='00:00:00 000';
timer1.Enabled :=true;
exit;
end;
s:= FormatDateTime('hh:nn:ss:zzz',now-time);
memo1.Lines.Add(s);
end;
procere TForm1.Button2Click(Sender: TObject); //置零按鈕
begin
timer1.Enabled :=false;
s:='00:00:00 000';
memo1.Clear;
edit1.text:='00:00:00 000';
i:=0;//判斷是否是開始計時的標志 i=1 是在計時,i=0 沒有開始計時
end;
procere TForm1.FormShow(Sender: TObject);
begin
i:=0;
edit1.text:='00:00:00 000';
end;
procere TForm1.Timer1Timer(Sender: TObject);
begin
edit1.Text:=FormatDateTime('hh:nn:ss:zzz',now-time);
end;
end.
測試通過,和秒錶的功能一樣!