944,099 Members | Top Members by Rank

Ad:
  • C++ Code Snippet
  • Views: 5308
  • C++ RSS
-1

Digital Clock in C++

by on Dec 10th, 2008
Here is simple Digital Clock coded in C++ and compiler used is TurboC.

A class DIGIT is designed to draw digits. Follwoing are the details for this class:

Consructor: DIDGT(); for default settings
DIGITE(int s,int c,int st); for user define settings
s -> is size of the digit
c -> color of the digit
st -> fill style of the digit

Methods: void DIGITE:rawDig(int x,int y,int n)
x,y -> x and y coordinates of the screen
n -> interge value (<10) to be display

void DIGITE:utDig(int x,int y,int n)
x,y -> x and y coordinates of the screen
n -> interge value (<100) to be display

void DIGITE::MakeBars()
internal function used to draw horizontal and vertical bars for the digits in the memory.

thats it!
C++ Code Snippet (Toggle Plain Text)
  1. /*
  2.   Developed by: Vivek Kumar
  3.   Email: vivek_kumar_bt@yahoo.co.in
  4.  
  5.   A Simple Digital Clock
  6.  
  7. */
  8.  
  9. #include<iostream.h>
  10. #include<stdio.h>
  11. #include<conio.h>
  12. #include<graphics.h>
  13. #include<dos.h>
  14. #include<stdlib.h>
  15. #define DOTCOLOR 14
  16. #define DIGCOLOR 4
  17. class DIGITE
  18. {
  19. private:
  20. int X,Y;
  21. int SIZE;
  22. int COLOR;
  23. int HIGHT,WIDTH;
  24. void *VER,*HOR;
  25. public:
  26. int style;
  27. DIGITE();
  28. DIGITE(int s,int c,int st);
  29. void MakeBars();
  30. void PutDig(int x,int y,int n);
  31. void DrawDig(int x,int y,int n);
  32. ~DIGITE();
  33. };
  34. DIGITE::DIGITE()
  35. {
  36. style=1;
  37. SIZE=1;
  38. HIGHT=1;
  39. WIDTH=12;
  40. COLOR=15;
  41. MakeBars();
  42. }
  43. DIGITE::DIGITE(int s,int c,int st)
  44. {
  45. style=st;
  46. SIZE=s;
  47. COLOR=c;
  48. HIGHT=1;
  49. WIDTH=6;
  50. MakeBars();
  51. }
  52. void DIGITE::MakeBars()
  53. {
  54. int x,y,w,h;
  55. int area;
  56. x=10;
  57. y=getmaxy()/2;
  58. w=WIDTH*SIZE;
  59. h=HIGHT*SIZE;
  60. setfillstyle(style,COLOR);
  61. bar(x+h,y,x+w-h,y+h);
  62. setcolor(COLOR);
  63. line(x,y+h/2,x+h,y);
  64. line(x,y+h/2,x+h,y+h);
  65. floodfill(x+h/2,y+h/2,COLOR);
  66. setcolor(COLOR);
  67. line(x+w-h,y,x+w+-h+h,y+h/2);
  68. line(x+w-h,y+h,x+w-h+h,y+h/2);
  69. floodfill(x+w-h+h/2,y+h/2,COLOR);
  70. area=imagesize(x,y,x+w,y+h);
  71. HOR=malloc(area);
  72. if(HOR==NULL)
  73. {
  74. printf("Low Memory(Horizontal)...\nPress Any Key");
  75. getch();
  76. exit(0);
  77. }
  78. getimage(x,y,x+w,y+h,HOR);
  79. putimage(x,y,HOR,XOR_PUT);
  80. x=getmaxx()/2;
  81. y=10;
  82. h=SIZE*WIDTH;
  83. w=HIGHT*SIZE;
  84. bar(x,y+w,x+w,y+h-w);
  85. setcolor(COLOR);
  86. line(x,y+w,x+w/2,y);
  87. line(x+w,y+w,x+w/2,y);
  88. floodfill(x+w/2,y+w/2,COLOR);
  89. setcolor(COLOR);
  90. line(x,y+h-w,x+w/2,y+h);
  91. line(x+w,y+h-w,x+w/2,y+h);
  92. floodfill(x+w/2,y+h-w/2,COLOR);
  93. area=imagesize(x,y,x+w,y+h);
  94. VER=malloc(area);
  95. if(VER==NULL)
  96. {
  97. printf("Low Memory(Vertical)...\nPress Any Key");
  98. getch();
  99. exit(0);
  100. }
  101. getimage(x,y,x+w,y+h,VER);
  102. putimage(x,y,VER,XOR_PUT);
  103. }
  104. void DIGITE::PutDig(int x,int y,int n)
  105. {
  106. int num,gap,qu,rem;
  107. gap=(HIGHT*SIZE);
  108. num=n;
  109. if(num>=0&&num<10)
  110. {
  111. DrawDig(x,y,0);
  112. DrawDig(x+WIDTH*SIZE+HIGHT*SIZE+gap,y,n);
  113. }
  114. if(num>=10&&num<100)
  115. {
  116. qu=num/10;
  117. rem=num%10;
  118. DrawDig(x,y,qu);
  119. DrawDig(x+WIDTH*SIZE+HIGHT*SIZE+gap,y,rem);
  120. }
  121. }
  122. void DIGITE::DrawDig(int x,int y,int n)
  123. {
  124. int H[10][3]={1,0,1,
  125. 0,0,0,
  126. 1,1,1,
  127. 1,1,1,
  128. 0,1,0,
  129. 1,1,1,
  130. 1,1,1,
  131. 1,0,0,
  132. 1,1,1,
  133. 1,1,1};
  134. int V[10][4]={1,1,1,1,
  135. 0,1,0,1,
  136. 0,1,1,0,
  137. 0,1,0,1,
  138. 1,1,0,1,
  139. 1,0,0,1,
  140. 1,0,1,1,
  141. 0,1,0,1,
  142. 1,1,1,1,
  143. 1,1,0,1};
  144. int HX[3]={x,x,x};
  145. int HY[3]={y,y+WIDTH*SIZE,y+2*WIDTH*SIZE};
  146. int VX[4]={x-(HIGHT*SIZE)/2,x+WIDTH*SIZE-(HIGHT*SIZE)/2,x-(HIGHT*SIZE)/2,x+WIDTH*SIZE-(HIGHT*SIZE)/2};
  147. int VY[4]={y+(HIGHT*SIZE)/2,y+(HIGHT*SIZE)/2,y+WIDTH*SIZE+(HIGHT*SIZE)/2,y+WIDTH*SIZE+(HIGHT*SIZE)/2};
  148. int i;
  149. setfillstyle(1,getpixel(x-1,y-1));
  150. bar(x-(SIZE*HIGHT)/2,y,x+WIDTH*SIZE+(SIZE*HIGHT)/2,y+2*SIZE*WIDTH+HIGHT*SIZE);
  151. for(i=0;i<3;i++)
  152. {
  153. if(H[n][i]==1)
  154. putimage(HX[i],HY[i],HOR,XOR_PUT);
  155. }
  156. for(i=0;i<4;i++)
  157. {
  158. if(V[n][i]==1)
  159. putimage(VX[i],VY[i],VER,XOR_PUT);
  160. }
  161. }
  162.  
  163. DIGITE::~DIGITE()
  164. {
  165. delete VER;
  166. delete HOR;
  167. }
  168.  
  169. void drawdot();
  170. int color=DOTCOLOR;
  171.  
  172. void main()
  173. {
  174. int gd=DETECT,gm,i;
  175. initgraph(&gd,&gm,"");
  176. struct time t;
  177. int th=t.ti_hour;
  178. int tm=t.ti_min;
  179. int ts=t.ti_sec;
  180. int tms=t.ti_hund;
  181. //setbkcolor(15);
  182. gettime(&t);
  183. DIGITE h(8,DIGCOLOR,1);
  184. DIGITE m(8,DIGCOLOR,1);
  185. DIGITE s(8,DIGCOLOR,1);
  186. DIGITE ms(2,DIGCOLOR,1);
  187. int y=200;
  188. int flag=1;
  189. h.PutDig(20,y,t.ti_hour);
  190. m.PutDig(220,y,t.ti_min);
  191. s.PutDig(380,y,t.ti_sec);
  192. ms.PutDig(500,y,t.ti_hund);
  193.  
  194. while(!kbhit())
  195. {
  196. gettime(&t);
  197. if(th!=t.ti_hour)
  198. {
  199. th=t.ti_hour;
  200. h.PutDig(20,y,t.ti_hour);
  201. }
  202. if(tm!=t.ti_min)
  203. {
  204. tm=t.ti_min;
  205. m.PutDig(220,y,t.ti_min);
  206. }
  207. if(ts!=t.ti_sec)
  208. {
  209. ts=t.ti_sec;
  210. s.PutDig(380,y,t.ti_sec);
  211.  
  212. drawdot();
  213. color=color-DOTCOLOR;
  214. if(color<0)
  215. color=DOTCOLOR;
  216. }
  217. if(tms!=t.ti_hund)
  218. {
  219. tms=t.ti_hund;
  220. ms.PutDig(500,y,t.ti_hund);
  221. }
  222. }
  223. getch();
  224. }
  225.  
  226. void drawdot()
  227. {
  228. int x,y,gap,size,c;
  229. c=color;
  230. gap=10;
  231. size=15;
  232. x=170;
  233. y=200;
  234. setfillstyle(1,color);
  235. bar(x+gap,y+gap,x+gap+size,y+gap+size);
  236. bar(x+gap,y+8*gap,x+gap+size,y+8*gap+size);
  237. //color=15;
  238. delay(0);
  239. }
Message:
Previous Thread in C++ Forum Timeline: Win32API Movefile() Pathname problem
Next Thread in C++ Forum Timeline: I want your guys opinion





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC