sandipan

C++ program to display a clock

Sep 6th, 2018
841
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.42 KB | None | 0 0
  1. /*
  2.  C/C++ code to display a clock on the DOS prompt
  3.  Sandipan Dey
  4.  BCSE, JU, Kolkata
  5.  2002
  6. */
  7.  
  8. #include <graphics.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <conio.h>
  12. #include <math.h>
  13. #include <dos.h>
  14. #define PI 3.14159
  15.  
  16. char* Roman(int r) {
  17.     switch(r) {
  18.         case 0:return "XII";break;
  19.         case 1:return "I";break;
  20.         case 2:return "II";break;
  21.         case 3:return "III";break;
  22.         case 4:return "IV";break;
  23.         case 5:return "V";break;
  24.         case 6:return "VI";break;
  25.         case 7:return "VII";break;
  26.         case 8:return "VIII";break;
  27.         case 9:return "IX";break;
  28.         case 10:return "X";break;
  29.         case 11:return "XI";break;
  30.     }
  31.     return "\0";
  32. }
  33.  
  34. class Clock {
  35.     int Cx,Cy,Hx,Hy,Mx,My,Sx,Sy;
  36.     struct time t;
  37.     char S[40];
  38.     public:
  39.     Clock();
  40.     Clock(int h,int m,int s);
  41.     void Draw(int=0);
  42.     void DrawCenter();
  43.     void SetHourArm();
  44.     void SetMinArm();
  45.     void SetSecArm();
  46.     void Name();
  47.     void SetTime();
  48.     void Digital();
  49.     void Time();
  50. };
  51.  
  52. Clock::Clock() {
  53.     Cx=300;
  54.     Cy=200;
  55.     settextstyle(DEFAULT_FONT,HORIZ_DIR,0);
  56.     Draw();
  57.     DrawCenter();
  58.     Time();
  59.     setcolor(getmaxcolor());
  60. }
  61.  
  62. Clock::Clock(int h,int m,int s) {
  63.     t.ti_hour=h;
  64.     t.ti_min=m;
  65.     t.ti_sec=s;
  66. }
  67.  
  68. void Clock::Draw(int t) {
  69.     setcolor(12);
  70.     for(int r=150;r<155;++r)
  71.         circle(Cx,Cy,r);
  72.     setcolor(14);
  73.     circle(Cx,Cy,149);
  74.     setcolor(1);
  75.     for(r=165;r<170;++r)
  76.         circle(Cx,Cy,r);
  77.     setcolor(1);
  78.     setcolor(getmaxcolor());
  79.     circle(Cx,Cy,171);
  80.     if(t==0) {
  81.         char s[3];
  82.         for(r=0;r<12;++r){
  83.             (r==0)?sprintf(s,"%d",12):sprintf(s,"%d",r);
  84.             outtextxy(Cx+140*sin(PI-PI*30*r/180),Cy+140*cos(PI-PI*30*r/180),s);
  85.         }
  86.     }
  87.     else
  88.         for(r=0;r<12;++r)
  89.             outtextxy(Cx+140*sin(PI-PI*30*r/180),Cy+140*cos(PI-PI*30*r/180),Roman(r));
  90.     setcolor(getmaxcolor());
  91. }
  92.  
  93. void Clock::SetHourArm() {
  94.     setcolor(7);
  95.     Hx=Cx+90*sin(PI-PI*(30*(t.ti_hour%12)+(t.ti_min/2))/180);
  96.     Hy=Cy+90*cos(PI-PI*(30*(t.ti_hour%12)+(t.ti_min/2))/180);
  97.     line(Cx-2,Cy,Hx-2,Hy+3);
  98.     line(Cx-1,Cy,Hx-1,Hy+1);
  99.     line(Cx,Cy,Hx,Hy);
  100.     line(Cx+1,Cy,Hx+1,Hy+1);
  101.     line(Cx+2,Cy,Hx+2,Hy+3);
  102. }
  103.  
  104. void Clock::SetMinArm() {
  105.     setcolor(10);
  106.     Mx=Cx+115*sin(PI-PI*(6*t.ti_min+(t.ti_sec/10))/180);
  107.     My=Cy+115*cos(PI-PI*(6*t.ti_min+(t.ti_sec/10))/180);
  108.     for(int i=-1;i<=1;++i)
  109.     line(Cx+i,Cy,Mx+i,My);
  110. }
  111.  
  112. void Clock::SetSecArm() {
  113.     setcolor(9);
  114.     Sx=Cx+125*sin(PI-PI*(6*t.ti_sec)/180);
  115.     Sy=Cy+125*cos(PI-PI*(6*t.ti_sec)/180);
  116.     for(int i=-1;i<=1;++i)
  117.     line(Cx+i,Cy,Sx+i,Sy);
  118.     sound(200);
  119.     Name();
  120.     delay(10);
  121.     nosound();
  122. }
  123.  
  124. void Clock::SetTime() {
  125.     setcolor(0);
  126.     for(int i=-2;i<=2;++i)
  127.     line(Cx+i,Cy,Hx+i,Hy);
  128.     for(i=-1;i<=2;++i)
  129.     line(Cx+i,Cy,Mx+i,My);
  130.     for(i=-1;i<=1;++i)
  131.     line(Cx+i,Cy,Sx+i,Sy);
  132.     outtextxy(225,425,S);
  133.     //DrawCenter();
  134.     setcolor(getmaxcolor());
  135.     //t.ti_hour=h;
  136.     //t.ti_min=m;
  137.     //t.ti_sec=s;
  138.     SetHourArm();
  139.     SetMinArm();
  140.     SetSecArm();
  141. }
  142.  
  143. void Clock::Name() {
  144.     settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
  145.     rectangle(515,375,630,465);
  146.     outtextxy(520,380,"SANDIPAN DEY");
  147.     outtextxy(520,390,"BCSE-IV");
  148.     outtextxy(520,400,"Roll-99710");
  149.     line(515,412,630,412);
  150.     outtextxy(520,415,"Press Keys:");
  151.     line(515,425,630,425);
  152.     outtextxy(520,432,"C-Change Time");
  153.     outtextxy(520,442,"R-Roman");
  154.     outtextxy(520,452,"X-Exit");
  155. }
  156.  
  157. void Clock::DrawCenter() {
  158.     setcolor(8);
  159.     putpixel(Cx,Cy,8);
  160.     for(int r=0;r<5;++r)
  161.     circle(Cx,Cy,r);
  162.     setcolor(getmaxcolor());
  163. }
  164.  
  165. void Clock::Digital() {
  166.     char* AMPM=(char*)malloc(5);
  167.     setcolor(12);
  168.     rectangle(210,415,440,455);
  169.     setcolor(13);
  170.     rectangle(215,420,435,450);
  171.     if(t.ti_hour>=12 && t.ti_hour<=23){
  172.         if(t.ti_hour>12)t.ti_hour=t.ti_hour%12;AMPM="PM";
  173.     }
  174.     else {
  175.         if(t.ti_hour==0)t.ti_hour=12;
  176.         AMPM="AM";
  177.     }
  178.     sprintf(S,"%d: %d: %d %s",t.ti_hour,t.ti_min,t.ti_sec,AMPM);
  179.     setcolor(14);
  180.     settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
  181.     //outtextxy(100,10,"Press C to Change Time");
  182.     outtextxy(225,425,S);
  183. }
  184.  
  185. void Clock::Time() {
  186.     gettime(&t);
  187.     char c;
  188.     int chk;
  189.     do {
  190.         while(!kbhit()) {
  191.             chk=0;
  192.             SetTime();
  193.             //SetHourArm();
  194.             //SetMinArm();
  195.             //SetSecArm();
  196.             DrawCenter();
  197.             Digital();
  198.             delay(980);
  199.             t.ti_sec=t.ti_sec+1;
  200.             if(t.ti_sec==60){t.ti_min=t.ti_min+1;t.ti_sec=0;chk=1;}
  201.             if(t.ti_min==60){t.ti_hour=t.ti_hour+1;t.ti_min=0;chk=1;}
  202.             if(!chk)delay(4);
  203.         }
  204.         c=getch();
  205.         if(c=='c' || c=='C') {
  206.             int h,m,s;
  207.             outtextxy(500,300,"New Time (hh:mm:ss):" );
  208.             outtextxy(500,320,"hh:");
  209.             fflush(stdin);
  210.             gotoxy(70,21);
  211.             scanf("%d",&h);
  212.             outtextxy(500,335,"mm:");
  213.             gotoxy(70,22);
  214.             scanf("%d",&m);
  215.             outtextxy(500,350,"ss:");
  216.             gotoxy(70,23);
  217.             scanf("%d",&s);
  218.             if((h>0 && h<=12) && (m>=0 && m<60) && (s>=0 && s<60)) {
  219.                 t.ti_hour=h;
  220.                 t.ti_min=m;
  221.                 t.ti_sec=s;
  222.                 cleardevice();
  223.                 settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
  224.                 Draw();
  225.             }
  226.             else {
  227.                 outtextxy(500,370,"Error!");
  228.                 getch();
  229.                 setfillstyle(SOLID_FILL,0);
  230.                 bar(500,300,650,400);
  231.             }
  232.         }
  233.         else if(c=='r' || c=='R') {
  234.             setcolor(15);
  235.             settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
  236.             cleardevice();
  237.             Draw(1);
  238.         }
  239.         else if(c=='n' || c=='N') {
  240.             setcolor(15);
  241.             settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
  242.             cleardevice();
  243.             Draw();
  244.         }
  245.         fflush(stdin);
  246.     } while(c!='x' && c!='X');
  247.     //{int h,m,s;cleardevice();outtextxy(100,100,"Enter New Time:");moveto(400,100);scanf("%d%d%d",&h,&m,&s);t.ti_hour=(unsigned char)h;t.ti_min=(unsigned char)m;t.ti_sec=(unsigned char)s;cleardevice();Clock(1);}
  248. }
  249.  
  250. void main(void) {
  251.    int gd=DETECT,gm;
  252.    initgraph(&gd,&gm,"");
  253.    Clock c;
  254.    //getch();
  255.    closegraph();
  256. }
Add Comment
Please, Sign In to add comment