時鐘語音c語言,c語言時鐘函數

本文目錄一覽:

怎麼用C語言編程數字時鐘

1、以下例程實現時鐘的實時顯示基本要求: 1) 自行設計界面,模擬錶盤式時鐘。要求界面美觀,清晰。2)數字同步顯示時間信息。

2、例程:

#includegraphics.h 

#includemath.h 

#includedos.h 

#define PI 3.1415926 

//屏幕中心的坐標(640X480模式下)

#define mid_x 320 

#define mid_y 240 

int main() 

{ int graphdriver=DETECT,graphmode; 

int end_x,end_y; 

struct time curtime; 

float th_hour,th_min,th_sec; 

initgraph(graphdriver,graphmode,”C:\\TC2″); //初始化VGA屏幕模式

setbkcolor(BLACK); //使用黑色的背景色

while(!kbhit(0)) //若有鍵盤輸入,則跳出,即是結束程序

{ setcolor(GREEN); //把畫筆設為綠色

circle(mid_x,mid_y,180); //鐘的外圓

circle(mid_x,mid_y,150); //鐘的內圓

circle(mid_x,mid_y,1); //畫出鐘的圓心

gettime(curtime); //取得系統當前時間

th_sec=(float)curtime.ti_sec*0.1047197551; //把秒針的角度化為弧度,為以後繪製時方便,下同

th_min=(float)curtime.ti_min*0.1047197551+th_sec/60.0; //分針的弧度

th_hour=(float)curtime.ti_hour*0.5235987755+th_min/12.0; //時度的弧度,注意整時是12等分的,所時乘的是3.14/180*5

//計算出時針的尾的坐標(時針長70)

end_x=mid_x+70*sin(th_hour); 

end_y=mid_y-70*cos(th_hour); 

setcolor(RED); 

line(mid_x,mid_y,end_x,end_y); //用紅色線畫出時針

//計算出分針坐標(分針長110)

end_x=mid_x+110*sin(th_min); 

end_y=mid_y-110*cos(th_min); 

setcolor(RED); 

line(mid_x,mid_y,end_x,end_y); //用紅色畫出分針

end_x=mid_x+140*sin(th_sec); 

end_y=mid_y-140*cos(th_sec); 

setcolor(RED); 

line(mid_x,mid_y,end_x,end_y); //同上,畫出秒針,長為140

//畫出鐘盤上的刻度,刻度長20

line(140,240,160,240); //9點對應的大刻度

line(320,60,320,80); //12點對應的大刻度

line(500,240,480,240); //3點的刻度

line(320,420,320,400); //6點的刻度

line(410,395.7,400,378.4); //5點

line(475.7,330,458.4,320); //4點

line(475.7,150,458.4,160); //2點

line(410,84.3,400,101.6); //1點

line(230,84.3,240,101.6); //11點

line(164.3,150,181.6,160); //10點

line(164.3,330,181.6,320); //8點

line(230,395.7,240,378.4); //7點

sleep(BLUE); //這裡應該是打錯,停止一秒,應為sleep(1000)

cleardevice(); //清除屏幕上的顯示

closegraph(); //關閉VGA屏幕,即返迴文本方式

return 0; 

}

用C語言做數字時鐘每走一秒響一次,求大神告訴源代碼

“響一次”需要牽涉到圖形編程中的音樂播放問題,需要自己下載圖形編程相關庫文件,具體實現請自己在TODO里添加播放音樂的代碼

數字時鐘的實現很簡單,運用time.h相關函數即可

#includestdio.h

#includestdlib.h

#includetime.h

time_t oldt=-1;

struct tm *p;

bool Printdate()

{

time_t t=time(NULL);

if(t!=oldt)

{

oldt=t;

p=localtime(t);

system(“cls”);

printf(“%d/%d/%d 周”,1900+p-tm_year,1+p-tm_mon,p-tm_mday,p-tm_hour,p-tm_min,p-tm_sec); 

switch(p-tm_wday)

{

       case 1:printf(“一”);break;

       case 2:printf(“二”);break;

       case 3:printf(“三”);break;

       case 4:printf(“四”);break;

       case 5:printf(“五”);break;

       case 6:printf(“六”);break;

       case 7:printf(“日”);break;

 }  

printf(” %d:%02d:%02d     “, p-tm_hour, p-tm_min,  p-tm_sec);

return 1;

}

return 0;

}

main()

{

while(1)

    if(Printdate())

    {

    /*************TODO*************/

    /**For example: printf(“\a”);**/

    }

return 0;

}

用 C語言 編寫一個 整點聲音報時時鐘:

(1)用數字邏輯集成塊實現;

(2)時間以24小時為一個周期,顯示時、分、秒;

(3)計時過程具有報時功能,當時間到達整點前5秒進行蜂鳴報時;

(4)為了保證計時的穩定及準確須由晶體振蕩器提供錶針時間基準信號。

c51單片機 晶振為11.0592MHz

#includereg52.h

#define HOUR1 1

#define HOUR0 0

#define MIN1 2

#define MIN0 8

#define SEC1 2

#define SEC0 0

#define uint unsigned int

#define ulint unsigned long int

#define uchar unsigned char

sbit dula=P2^6;

sbit wela=P2^7;

sbit beep=P2^3;

int i;

ulint

sharp,second,count=0,sec0=SEC0,sec1=SEC1,min0=MIN0,min1=MIN1,hour0=HOUR0,hour1=HOUR1;//秒計數全局變量

uchar code segment[]=;

uchar code time[]={

0x3f,0x06,0x5b,0x4f,

0x66,0x6d,0x7d,0x07,

0x7f,0x6f};

void delay(uint);//程序毫秒延時

void beeper(uchar);//開蜂鳴器毫秒

void init();//初始化函數

void display();//從數碼管上顯示

void counter();//計算進行過程中的時、分、秒值

void scan();//掃描鍵盤

void main()

{

init();

while(1)

{

scan();//掃描鍵盤看是否有鍵按下

for(i=6;i0;i–)//動態掃描6位數碼管

{

display();//顯示時、分、秒

}

}

}

void init()

{

second=hour1*36000+hour0*3600+min1*600+min0*60+sec1*10+sec0;

TMOD=0x01;

TH0=(65536-46080)/256;

TL0=(65536-46080)%256;

EA=1;

ET0=1;

TR0=1;

}

void delay(uint z)//程序毫秒延時

{

uint x=0,y=0;

for(x=z;x0;x–)

for(y=110;y0;y–);

}

void timer0() interrupt 1

{

TH0=(65536-46080)/256;

TL0=(65536-46080)%256;

count++;

if(count==20)//判斷是否到1秒

{

counter();//計算進行過程中的時、分、秒值

if(sharp!=hour0) beeper(1000);//判斷小時的值是否改變,變則啟動蜂鳴器

}

}

void beeper(uchar tt)

{

uchar t=tt;

count=0;

beep=0;//開蜂鳴器

delay(t);

beep=1;//關蜂鳴器

}

void display()

{

P0=0xff;//位消影(低電平選擇位)

//送位選信號

wela=1;

P0=segment[i-1];

wela=0;

P0=0x00;//段消影(高電平選擇段)

//送段選信號

dula=1;

switch(i)

{

case 6 : P0=time[sec0]; break;

case 5 : P0=time[sec1]; break;

case 4 : P0=time[min0]; break;

case 3 : P0=time[min1]; break;

case 2 : P0=time[hour0]; break;

case 1 : P0=time[hour1]; break;

}

delay(1);

P0=0x00; //配合上面用於消隱

dula=0;

}

void counter()

{

second++;

if(second==86400) second=0;

count=0;

sharp=hour0;//設置報時檢測KEY

sec0=second%10;

sec1=(second%60-sec0)/10;

min0=((second%3600-sec1*10-sec0)/60)%10;

min1=((second%3600-sec1*10-sec0)/60-min0)/10;

hour0=(second%36000-min1*600-min0*60-sec1*10-sec0)/3600;

hour1=second/36000;

}

void scan()

{

}

另外,團IDC網上有許多產品團購,便宜有口碑

用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;i60;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(s10) /*用字符的形式輸出時間*/

{ if(m10)

printf(“%u:0%u:0%u”,h,m,s);

else

printf(“%u:%u:0%u”,h,m,s);

}

else

{ if(m10)

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語言模擬時鐘

delay(240);

nosound();

delay(140);

sound(2000);

delay(240);

nosound();

}

c語言時鐘代碼

#includegraphics.h /* 引入graphic.h */

#includemath.h /* 引入math.h */

#includedos.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;i60;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();

}

具體的。。就是套用用幾個函數算時間。。

不要對這種很長的東西害怕,其實大部分都是在畫這個鐘~

加油哦~

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/257631.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-15 12:46
下一篇 2024-12-15 12:46

相關推薦

  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • Python中capitalize函數的使用

    在Python的字符串操作中,capitalize函數常常被用到,這個函數可以使字符串中的第一個單詞首字母大寫,其餘字母小寫。在本文中,我們將從以下幾個方面對capitalize函…

    編程 2025-04-29
  • Python中set函數的作用

    Python中set函數是一個有用的數據類型,可以被用於許多編程場景中。在這篇文章中,我們將學習Python中set函數的多個方面,從而深入了解這個函數在Python中的用途。 一…

    編程 2025-04-29
  • 三角函數用英語怎麼說

    三角函數,即三角比函數,是指在一個銳角三角形中某一角的對邊、鄰邊之比。在數學中,三角函數包括正弦、餘弦、正切等,它們在數學、物理、工程和計算機等領域都得到了廣泛的應用。 一、正弦函…

    編程 2025-04-29
  • 單片機打印函數

    單片機打印是指通過串口或並口將一些數據打印到終端設備上。在單片機應用中,打印非常重要。正確的打印數據可以讓我們知道單片機運行的狀態,方便我們進行調試;錯誤的打印數據可以幫助我們快速…

    編程 2025-04-29
  • Python3定義函數參數類型

    Python是一門動態類型語言,不需要在定義變量時顯示的指定變量類型,但是Python3中提供了函數參數類型的聲明功能,在函數定義時明確定義參數類型。在函數的形參後面加上冒號(:)…

    編程 2025-04-29
  • AES加密解密算法的C語言實現

    AES(Advanced Encryption Standard)是一種對稱加密算法,可用於對數據進行加密和解密。在本篇文章中,我們將介紹C語言中如何實現AES算法,並對實現過程進…

    編程 2025-04-29
  • 學習Python對學習C語言有幫助嗎?

    Python和C語言是兩種非常受歡迎的編程語言,在程序開發中都扮演着非常重要的角色。那麼,學習Python對學習C語言有幫助嗎?答案是肯定的。在本文中,我們將從多個角度探討Pyth…

    編程 2025-04-29
  • Python定義函數判斷奇偶數

    本文將從多個方面詳細闡述Python定義函數判斷奇偶數的方法,並提供完整的代碼示例。 一、初步了解Python函數 在介紹Python如何定義函數判斷奇偶數之前,我們先來了解一下P…

    編程 2025-04-29
  • Python實現計算階乘的函數

    本文將介紹如何使用Python定義函數fact(n),計算n的階乘。 一、什麼是階乘 階乘指從1乘到指定數之間所有整數的乘積。如:5! = 5 * 4 * 3 * 2 * 1 = …

    編程 2025-04-29

發表回復

登錄後才能評論