『壹』 c語言模擬時鍾的代碼,在vc++環境中運行。
/**
給你一個用圖形庫實現的案例,如果你不會用圖形庫或者不用圖形庫也無所謂,看看思路也是
很好的。你說到鍾表怎麼轉,那必然不是顯示數字那麼簡單,所以這個小案例會對你有點幫助。
*/
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
#definePI3.141592653
voidDrawDial();
voidDrawHand(inthour,intmin,intsec);
intmain()
{
//創建一個窗口
initgraph(640,480);
SYSTEMTIMEt;
while(!kbhit())
{
DrawDial();
GetLocalTime(&t); //獲取當地時間
DrawHand(t.wHour,t.wMinute,t.wSecond);
Sleep(1000); //大寫的S!
BeginBatchDraw();
cleardevice();
EndBatchDraw();
}
getchar();
return0;
}
//靜態的表盤
voidDrawDial()
{
circle(320,240,2);
circle(320,240,60);
circle(320,240,160);
circle(405,210,30);
outtextxy(290,315,L"電子時鍾");
//繪制刻度
intx,y;
for(inti=0;i<60;i++)
{
x=320+int(145*sin(PI*2*i/60)); //半徑*sinA得出x軸偏移量
y=240+int(145*cos(PI*2*i/60));
/*
PI*2-->360°
i*60-->把360分成60分,佔60份中的i份
*/
if(i%15==0)
{
bar(x-5,y-5,x+5,y+5);//畫矩形
}
elseif(i%5==0)
{
circle(x,y,3);
}
else
putpixel(x,y,WHITE); //畫一像素(點)
}
for(inti=0;i<=11;i++)
{
x=405+int(28*sin(PI*2*i/12));
y=210+int(28*cos(PI*2*i/12));
putpixel(x,y,WHITE);
}
}
//動態表針
voidDrawHand(inthour,intmin,intsec)
{
doubleahour,amin,asec; //時分秒的弧度值
intxhour,yhour,xmin,ymin,xsec,ysec; //時分秒針的端點坐標
asec=sec*2*PI/60;
amin=min*2*PI/60+asec/60; //加上秒數帶來的偏移
ahour=hour*2*PI/12+amin/12;
/*
已走的格數與自己要走的格數的比例
+
下級已走的弧度與自己要走的格數的比例
*/
xsec=int(120*sin(asec)); //120為秒針長度
ysec=int(120*cos(asec));
xmin=int(100*sin(amin));
ymin=int(100*cos(amin));
xhour=int(70*sin(ahour));
yhour=int(70*cos(ahour));
//繪製表針
setlinestyle(PS_SOLID,2);
setcolor(RED);
line(320+xsec,240-ysec,320-xsec/3,240+ysec/3);
setlinestyle(PS_SOLID,5);
setcolor(YELLOW);
line(320+xmin,240-ymin,320-xmin/3,240+ymin/3);
setlinestyle(PS_SOLID,7);
setcolor(LIGHTGREEN);
line(320+xhour,240-yhour,320-xhour/3,240+yhour/3);
//自定義main函數時,手動將樣式置回初始:
setlinestyle(PS_SOLID,1);
setlinecolor(WHITE);
}
『貳』 C語言模擬時鍾
delay(240);
nosound();
delay(140);
sound(2000);
delay(240);
nosound();
}
『叄』 c語言 時鍾模擬
#include<math.h>
#include<dos.h>
#include<graphics.h>
#include<conio.h>
#include<time.h>
#define PI 3.141592653589793
int h,m,s,i,l,mon,y,d;
struct time t;
struct date data;
draw()
{
gettime(&t); //取得時間信息到t
s=t.ti_sec; //秒
h=t.ti_hour; //時
m=t.ti_min; //分
getdate(&data); //取得日期信息到data
y=data.da_year; //年
mon=data.da_mon; //月
d=data.da_day; //日
//畫出鍾的外圓(即是輪廓)
setcolor(11);
circle(300,200,152);
setcolor(3);
circle(300,200,157);
//畫出60個分鍾刻度
for(i=0;i<60;i+=1)
{
if(i%5==0) l=140;
else l=145;
line(300+150*sin(i*PI/30),200-150*cos(i*PI/30),
300+l*sin(i*PI/30),200-l*cos(i*PI/30));
}
//畫秒針
setcolor(19);
line(300,200,300+140*sin(s*PI/30),200-140*cos(s*PI/30));
//畫分針
setcolor(3);
line(300,200,300+110*sin(m*PI/30),200-110*cos(m*PI/30));
//畫時針
setcolor(11);
line(300,200,300+90*sin(((float)h+(float)m/60)*PI/6),200-90*cos(((float)h+(float)m/60)*PI/6));
//標注鍾盤上的"3"、"6"、"9"、"12"
settextstyle(3,0,2);
outtextxy(430,190,"3");
outtextxy(295,320,"6");
outtextxy(160,190,"9");
outtextxy(293,60,"12");
}
main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,""); //初始化屏幕模式
setbkcolor(8);
while(!kbhit()) //若有鍵盤輸入則跳出(結束程序)
{
draw(); //繪制鍾
settextstyle(3,0,5);
setcolor(9);
outtextxy(60,170,"my clock");
gotoxy(35,17);
//列印出數字形式的時間(hh:mm:ss)
if(h<10) printf("0");printf("%d:",h);
if(m<10) printf("0");printf("%d:",m);
if(s<10) printf("0");printf("%d",s);
gotoxy(33,18);
printf("%d:",y);
//列印出日期(mm:dd)
if(mon<10) printf("0");printf("%d:",mon);
if(d<10) printf("0");printf("%d",d);
sound(200); //讓喇叭以200HZ叫一聲
delay(70); //延時0.07秒,即是聲音延續0.07秒
nosound(); //停止聲音
sleep(1); //停止一秒
cleardevice(); //清屏
}
}
『肆』 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);
}
『伍』 c語言簡單數學時鍾(結構體,指針),求大佬幫助
至今為止,在中國歷史上有留下記載的四代計時器分別為:日晷、沙漏、機械鍾、石英鍾。在中國市場上石英鍾最熱銷。
時鍾一直以來都是國人鍾愛的商品之一。新中國成立以來,國家投入大量資金發展鍾表工業,使這一產業得以快速發展,此後,中國的改革開放以及經濟全球化發展給中國鍾表業帶來了繁榮。經過幾十年的發展,中國鍾表業經歷了進料組裝-外觀件製造-產品開發-創立品牌的發展過程,目前已形成配套齊全的鍾表製造工業,除高端機芯外的所有零配件均可加工生產。
現狀
從區域格局來看,全國已形成以廣州、深圳為龍頭的珠三角地區、福建、浙江、江蘇、山東、天津等6大鍾表主產區;從產量來看,我國已成為世界鍾表生產大國,鍾表產量穩居世界第一。監測數據顯示,2011年,我國鍾和表的產量分別達到1.59億只和1.3億只。
我國鍾錶行業發展雖然取得長足的進步,但國內鍾表企業及其品牌在國際市場上的信譽度和影響力還微不足道,產量佔比雖然已經達到80%以上,但是產值佔比不到30%,依然沒有話語權和定價權。統計數據顯示,2010年從金融危機陰影中走出來的中國鍾錶行業實現了全面的恢復和增長,全年規模以上企業工業總產值同比增長17.12%,銷售收入同比增長23.00%,均達到2008年以來的最高增速,同時利潤總額也大幅增長了55.63%。此外,我國鍾表出口形勢也得到好轉,全年共實現出口額30.49億美元,同比增長24.27%。
『陸』 用c語言編寫一個有滴答聲音的全屏模擬時鍾。
我在TC上調試通過了,不過時間好像過了額...
/*開發環境:turbo c 2.0模擬時鍾轉動程序代碼*/
#include"graphics.h"
#include"math.h"
#include"dos.h"
#define pi 3.1415926
#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*sin(i*6*pi/180)+300;
y1=200*cos(i*6*pi/180)+240;
x2=(200-l)*sin(i*6*pi/180)+300;
y2=(200-l)*cos(i*6*pi/180)+240;
line(x1,y1,x2,y2);
}
}
main()
{
int x,y,i,k=1;
int gdriver=9,gmode=2;
unsigned char h,m,s;
int o,p,q;
float n;
struct time t[1];
struct date d[1];
initgraph(&gdriver,&gmode,"c:\\tc");
initgraph(&gdriver,&gmode,"c:\\tc");
for(i=0;i<=6;i++)
{
settextstyle(TRIPLEX_FONT,HORIZ_DIR,i); /*控制輸出字元的字體,方向,大小*/
cleardevice();
settextjustify(1,1); /*在指定坐標上輸出字元串*/
outtextxy(300,80,"12") ;
outtextxy(300,390,"6");
outtextxy(140,230,"9");
outtextxy(460,230,"3");
outtextxy(380,100,"1");
outtextxy(220,100,"11");
outtextxy(430,160,"2");
outtextxy(430,310,"4");
outtextxy(380,370,"5");
outtextxy(220,370,"7");
outtextxy(160,160,"10");
outtextxy(160,310,"8");
}
init();
setwritemode(1); /*設置畫線的輸出模式*/
if(k!=0)
{
getdate(d); /*獲得系統日期函數*/
o=d[0].da_year;
p=d[0].da_mon;
q=d[0].da_day;
gettime(t); /*獲得系統時間函數*/
h=t[0].ti_hour;
m=t[0].ti_min;
s=t[0].ti_sec;
}
setcolor(7); /*設置時針顏色*/
n=(float)h+(float)m/60;
d(150,n,30); /*畫出時針*/
setcolor(14); /*設置分針顏色*/
d(170,m,6); /*畫出分針*/
setcolor(4); /*設置秒針顏色*/
d(190,s,6); /*畫出秒針*/
while(!kbhit()) /*控製程序按下任意鍵退出*/
{
while(t[0].ti_sec==s)
gettime(t);
gotoxy(44,18); /*使游標移動到指定坐標*/
printf("\b\b\b\b\b\b\b\b\b"); /*退格,使表示時間的字元串不斷變化*/
sound(400); /*按給定的頻率打開PC揚聲器*/
delay(70); /*中斷程序的執行,時間為70毫秒*/
sound(200);
delay(30);
nosound(); /*按給定的頻率關閉PC揚聲器*/
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();
}
if(s<10) /*用字元的形式輸出時間*/
{ if(m<10)
printf("%u:0%u:0%u",h,m,s);
else
printf("%u:%u:0%u",h,m,s);
}
else
{ if(m<10)
printf("%u:0%u:%u",h,m,s);
else
printf("%u:%u:%u",h,m,s);
}
gotoxy(34,19); /*在指定坐標上輸出日期*/
printf("%d年%d月%d日",o,p,q);
printf("\b\b\b\b\b\b\b\b\b");
}
getch();
closegraph();
}
『柒』 C語言 用devc++編寫一個模擬時鍾,最簡單那種就行,但要能在devc++上運行。
#include<iostream>
#include<windows.h>
usingnamespacestd;
classClock{
public:
Clock(shorth=0,shortm=0,shorts=0):h(h),m(m),s(s){
}
voiddisplayTime();
private:
shorth;
shortm;
shorts;
};voidClock::displayTime(){
while(true){
cout<<h<<':'<<m<<':'<<s<<"";
Sleep(1000);//一秒更新一次
cout<<' ';
if(!(s=++s%60))
if(!(m=++m%60))
h=++h%24;
}
}intmain()
{
ClockA(12,20,30);//初始時間
A.displayTime();//更新函數
return0;
}
/*
12:20:30
*/
『捌』 用vc++6.0C語言編寫模擬時鍾,大神,求助
可以參考一下下面的代碼:
#include<stdio.h>
#include <graphics.h>
#include <conio.h>
#include <math.h>
#define 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 Draw(int hour, int minute, int second)
{
double a_hour, a_min, a_sec; // 時、分、秒針的弧度值
int x_hour, y_hour, x_min, y_min, x_sec, y_sec; // 時、分、秒針的末端位置
// 計算時、分、秒針的弧度值
a_sec = second * 2 * PI / 60;
a_min = minute * 2 * PI / 60 + a_sec / 60;
a_hour= hour * 2 * PI / 12 + a_min / 12;
// 計算時、分、秒針的末端位置
x_sec = 320 + (int)(120 * sin(a_sec));
y_sec = 240 - (int)(120 * cos(a_sec));
x_min = 320 + (int)(100 * sin(a_min));
y_min = 240 - (int)(100 * cos(a_min));
x_hour= 320 + (int)(70 * sin(a_hour));
y_hour= 240 - (int)(70 * cos(a_hour));
// 畫時針
setlinestyle(PS_SOLID, NULL, 10);
setcolor(WHITE);
line(320, 240, x_hour, y_hour);
// 畫分針
setlinestyle(PS_SOLID, NULL, 6);
setcolor(LIGHTGRAY);
line(320, 240, x_min, y_min);
// 畫秒針
setlinestyle(PS_SOLID, NULL, 2);
setcolor(RED);
line(320, 240, x_sec, y_sec);
}
void main()
{
int i,l,x1,x2,y1,y2;
initgraph(640, 480); // 初始化 640 x 480 的繪圖窗口 // 繪制一個簡單的表盤
circle(320, 240, 2);
circle(320, 240, 160);
outtextxy(296, 300, "Quan");// 設置 XOR 繪圖模式
for(i=0;i<60;i++) /*劃鍾點上的短線*/
{
if(i%5==0)
l=15;
else
l=5;
x1=160*sin(i*6*PI/180)+320;
y1=160*cos(i*6*PI/180)+240;
x2=(160-l)*sin(i*6*PI/180)+320;
y2=(160-l)*cos(i*6*PI/180)+240;
line(x1,y1,x2,y2);
}
setwritemode(R2_XORPEN); // 設置 XOR 繪圖模式
// 繪製表針
SYSTEMTIME ti; // 定義變數保存當前時間
while(!kbhit()) // 按任意鍵退出鍾表程序
{
GetLocalTime(&ti);
Draw(ti.wHour, ti.wMinute, ti.wSecond); // 畫表針
Sleep(1000); // 延時 1 秒
Draw(ti.wHour, ti.wMinute, ti.wSecond); // 擦表針(擦表針和
}
closegraph();
}
『玖』 C語言模擬動態時鍾程序
#include "stdio.h"
#include "dos.h"
//#include <windows.h>
#include <time.h>
#define S2M_M2H 60
struct N
{
int hour;
int minute;
int second;
} mytime = {0,0,0};
int main()
{
time_t rt;
struct tm *t;
time ( &rt );
t = localtime ( &rt );
mytime.hour=t->tm_hour;
mytime.minute = t->tm_min;
mytime.second = t->tm_sec;
//1樓答得不錯,你不會自己合直來么,我幫你合起來了
//以後我白天不能上網,晚上來答一兩個就玩去了,這題如果可行,就採納1樓的,我沒功勞
for(; ; mytime.second++)
{
if(mytime.second==S2M_M2H)
{
mytime.minute++;
mytime.second=0;
if(mytime.minute==S2M_M2H)
{
mytime.hour++;
mytime.minute=0;
}
}
{
printf("\b\b\b\b\b\b\b\b%2d:%02d:%02d",mytime.hour,mytime.minute,mytime.second);
delay(1000);
}
}
return 0;
}
『拾』 C語言作業:結構體編程練習 在屏幕上模擬顯示一個數字式時鍾 源代碼能給我的話+50,感謝
#include <stdio.h>
struct clock {
int hour;
int minute;
int second;
};
typedef struct clock CLOCK;
/*
函數功能:時、分、秒時間的更新
函數參數:無
函數返回值:無
*/
void Update(CLOCK *myclock) {
myclock->second++;
if (myclock->second == 60) { /*若second值為60,表示已過1分鍾,則 minute值加1*/
myclock->second = 0;
myclock->minute++;
}
if (myclock->minute == 60){ /*若minute值為60,表示已過1小時,則 hour值加1*/
myclock->minute = 0;
myclock->hour++;
}
if (myclock->hour == 24) { /*若hour值為24,則hour的值從0開始計時*/
myclock->hour = 0;
}
}
/*
函數功能:時、分、秒時間的顯示
函數參數:無
函數返回值:無
*/
void Display(CLOCK *myclock) { /*用回車符'\r'控制時、分、秒顯示的位置*/
printf("%2d:%2d:%2d\r", myclock->hour, myclock->minute, myclock->second);
}
/*
函數功能:模擬延遲1秒的時間
函數參數:無
函數返回值:無
*/
void Delay(void) {
long t;
for (t = 0; t < 290000000; t++) {
/*循環體為空語句的循環,起延時作用*/
}
}
int main(){
CLOCK myclock;
long i;
myclock.hour = myclock.minute = myclock.second = 0; /*hour,minute,second賦初值0*/
for (i = 0; i < 100000; i++) { /*利用循環結構,控制時鍾運行的時間*/
Update(&myclock); /*時鍾更新*/
Display(&myclock); /*時間顯示*/
Delay(); /*模擬延時1秒*/
}
return 0;
}