C语言 闹钟程序

Python012

C语言 闹钟程序,第1张

用C语言实现的闹钟程序

#include

#include

#include

#include

#include

void

main()

{

int

gd=DETECT,gm

int

x=320,y=240,r=200,i,h,m,s,thetamin,thetasec

struct

time

t

char

n[12][3]={"3","2","1","12","11","10","9","8","7","6","5","4"}

initgraph(&gd,&gm,"g:\\tc\\bgi")//图形驱动器路径,根据自己的系统更换。

circle(x,y,210)

setcolor(4)

settextstyle(4,0,5)

for(i=0i<12i++)

{

if(i!=3)

outtextxy(x+(r-14)*cos(M_PI/6*i)-10,y-(r-14)*sin(M_PI/6*i)-26,n[i])

else

outtextxy(x+(r-14)*cos(M_PI/6*i)-20,y-(r-14)*sin(M_PI/6*i)-26,n[i])

}

gettime(&t)

printf("The

current

time

is:

%2d:%02d:%02d.%02d\n",t.ti_hour,

t.ti_min,

t.ti_sec,

t.ti_hund)

while(!kbhit())

{

setcolor(5)

setfillstyle(1,5)

circle(x,y,10)

floodfill(x,y,5)

gettime(&t)

if(t.ti_min!=m)

{

setcolor(0)

line(x,y,x+(r-60)*cos(thetamin*(M_PI/180)),y-(r-60)*sin(thetamin*(M_PI/180

)))

circle(x+(r-80)*cos(thetamin*(M_PI/180)),y-(r-80)*sin(thetamin*(M_PI/180))

,10)

line(x,y,x+(r-110)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-110)*sin(M_PI/6*h

-((m/2)*(M_PI/180))))

circle(x+(r-130)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-130)*sin(M_PI/6*h-(

(m/2)*(M_PI/180))),10)

}

if(t.ti_hour>12)

t.ti_hour=t.ti_hour-12

if(t.ti_hour<4)

h=abs(t.ti_hour-3)

else&n

bsp

h=15-t.ti_hour

m=t.ti_min

if(t.ti_min<=15)

thetamin=(15-t.ti_min)*6

else

thetamin=450-t.ti_min*6

if(t.ti_sec<=15)

thetasec=(15-t.ti_sec)*6

else

thetasec=450-t.ti_sec*6

setcolor(4)

line(x,y,x+(r-110)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-110)*sin(M_PI/6*h

-((m/2)*(M_PI/180))))

circle(x+(r-130)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-130)*sin(M_PI/6*h-(

(m/2)*(M_PI/180))),10)

line(x,y,x+(r-60)*cos(thetamin*(M_PI/180)),y-(r-60)*sin(thetamin*(M_PI/180

)))

circle(x+(r-80)*cos(thetamin*(M_PI/180)),y-(r-80)*sin(thetamin*(M_PI/180))

,10)

setcolor(15)

line(x,y,x+(r-70)*cos(thetasec*(M_PI/180)),y-(r-70)*sin(thetasec*(M_PI/180

)))

delay(1000)

setcolor(0)

line(x,y,x+(r-70)*cos(thetasec*(M_PI/180)),y-(r-70)*sin(thetasec*(M_PI/180

)))

}

}

该程序已在Turbo

C++3.0中通过编译.

C语言中,可以使用atoi函数将字符串转换为数字,如atoi("123")可以得到数字123。

atoi (表示 ascii to integer)是把字符串转换成整型数的一个函数,应用在计算机程序和办公软件中。int atoi(const char *nptr) 函数会扫描参数 nptr字符串,会跳过前面的空白字符(例如空格,tab缩进)等。

如果 nptr不能转换成 int 或者 nptr为空字符串,那么将返回0。特别注意,该函数要求被转换的字符串是按十进制数理解的。atoi输入的字符串对应数字存在大小限制(与int类型大小有关),若其过大可能报错-1。

C语言中数字转化为字符串的方案:

使用sprintf函数来实现,如sprintf("%d", 123)可以得到字符串"123"。

sprintf指的是字符串格式化命令,主要功能是把格式化的数据写入某个字符串中。sprintf 是个变参函数。使用sprintf 对于写入buffer的字符数是没有限制的,这就存在了buffer溢出的可能性。解决这个问题,可以考虑使用 snprintf函数,该函数可对写入字符数做出限制。