Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- C/C++ code to display a clock on the DOS prompt
- Sandipan Dey
- BCSE, JU, Kolkata
- 2002
- */
- #include <graphics.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <conio.h>
- #include <math.h>
- #include <dos.h>
- #define PI 3.14159
- char* Roman(int r) {
- switch(r) {
- case 0:return "XII";break;
- case 1:return "I";break;
- case 2:return "II";break;
- case 3:return "III";break;
- case 4:return "IV";break;
- case 5:return "V";break;
- case 6:return "VI";break;
- case 7:return "VII";break;
- case 8:return "VIII";break;
- case 9:return "IX";break;
- case 10:return "X";break;
- case 11:return "XI";break;
- }
- return "\0";
- }
- class Clock {
- int Cx,Cy,Hx,Hy,Mx,My,Sx,Sy;
- struct time t;
- char S[40];
- public:
- Clock();
- Clock(int h,int m,int s);
- void Draw(int=0);
- void DrawCenter();
- void SetHourArm();
- void SetMinArm();
- void SetSecArm();
- void Name();
- void SetTime();
- void Digital();
- void Time();
- };
- Clock::Clock() {
- Cx=300;
- Cy=200;
- settextstyle(DEFAULT_FONT,HORIZ_DIR,0);
- Draw();
- DrawCenter();
- Time();
- setcolor(getmaxcolor());
- }
- Clock::Clock(int h,int m,int s) {
- t.ti_hour=h;
- t.ti_min=m;
- t.ti_sec=s;
- }
- void Clock::Draw(int t) {
- setcolor(12);
- for(int r=150;r<155;++r)
- circle(Cx,Cy,r);
- setcolor(14);
- circle(Cx,Cy,149);
- setcolor(1);
- for(r=165;r<170;++r)
- circle(Cx,Cy,r);
- setcolor(1);
- setcolor(getmaxcolor());
- circle(Cx,Cy,171);
- if(t==0) {
- char s[3];
- for(r=0;r<12;++r){
- (r==0)?sprintf(s,"%d",12):sprintf(s,"%d",r);
- outtextxy(Cx+140*sin(PI-PI*30*r/180),Cy+140*cos(PI-PI*30*r/180),s);
- }
- }
- else
- for(r=0;r<12;++r)
- outtextxy(Cx+140*sin(PI-PI*30*r/180),Cy+140*cos(PI-PI*30*r/180),Roman(r));
- setcolor(getmaxcolor());
- }
- void Clock::SetHourArm() {
- setcolor(7);
- Hx=Cx+90*sin(PI-PI*(30*(t.ti_hour%12)+(t.ti_min/2))/180);
- Hy=Cy+90*cos(PI-PI*(30*(t.ti_hour%12)+(t.ti_min/2))/180);
- line(Cx-2,Cy,Hx-2,Hy+3);
- line(Cx-1,Cy,Hx-1,Hy+1);
- line(Cx,Cy,Hx,Hy);
- line(Cx+1,Cy,Hx+1,Hy+1);
- line(Cx+2,Cy,Hx+2,Hy+3);
- }
- void Clock::SetMinArm() {
- setcolor(10);
- Mx=Cx+115*sin(PI-PI*(6*t.ti_min+(t.ti_sec/10))/180);
- My=Cy+115*cos(PI-PI*(6*t.ti_min+(t.ti_sec/10))/180);
- for(int i=-1;i<=1;++i)
- line(Cx+i,Cy,Mx+i,My);
- }
- void Clock::SetSecArm() {
- setcolor(9);
- Sx=Cx+125*sin(PI-PI*(6*t.ti_sec)/180);
- Sy=Cy+125*cos(PI-PI*(6*t.ti_sec)/180);
- for(int i=-1;i<=1;++i)
- line(Cx+i,Cy,Sx+i,Sy);
- sound(200);
- Name();
- delay(10);
- nosound();
- }
- void Clock::SetTime() {
- setcolor(0);
- for(int i=-2;i<=2;++i)
- line(Cx+i,Cy,Hx+i,Hy);
- for(i=-1;i<=2;++i)
- line(Cx+i,Cy,Mx+i,My);
- for(i=-1;i<=1;++i)
- line(Cx+i,Cy,Sx+i,Sy);
- outtextxy(225,425,S);
- //DrawCenter();
- setcolor(getmaxcolor());
- //t.ti_hour=h;
- //t.ti_min=m;
- //t.ti_sec=s;
- SetHourArm();
- SetMinArm();
- SetSecArm();
- }
- void Clock::Name() {
- settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
- rectangle(515,375,630,465);
- outtextxy(520,380,"SANDIPAN DEY");
- outtextxy(520,390,"BCSE-IV");
- outtextxy(520,400,"Roll-99710");
- line(515,412,630,412);
- outtextxy(520,415,"Press Keys:");
- line(515,425,630,425);
- outtextxy(520,432,"C-Change Time");
- outtextxy(520,442,"R-Roman");
- outtextxy(520,452,"X-Exit");
- }
- void Clock::DrawCenter() {
- setcolor(8);
- putpixel(Cx,Cy,8);
- for(int r=0;r<5;++r)
- circle(Cx,Cy,r);
- setcolor(getmaxcolor());
- }
- void Clock::Digital() {
- char* AMPM=(char*)malloc(5);
- setcolor(12);
- rectangle(210,415,440,455);
- setcolor(13);
- rectangle(215,420,435,450);
- if(t.ti_hour>=12 && t.ti_hour<=23){
- if(t.ti_hour>12)t.ti_hour=t.ti_hour%12;AMPM="PM";
- }
- else {
- if(t.ti_hour==0)t.ti_hour=12;
- AMPM="AM";
- }
- sprintf(S,"%d: %d: %d %s",t.ti_hour,t.ti_min,t.ti_sec,AMPM);
- setcolor(14);
- settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
- //outtextxy(100,10,"Press C to Change Time");
- outtextxy(225,425,S);
- }
- void Clock::Time() {
- gettime(&t);
- char c;
- int chk;
- do {
- while(!kbhit()) {
- chk=0;
- SetTime();
- //SetHourArm();
- //SetMinArm();
- //SetSecArm();
- DrawCenter();
- Digital();
- delay(980);
- t.ti_sec=t.ti_sec+1;
- if(t.ti_sec==60){t.ti_min=t.ti_min+1;t.ti_sec=0;chk=1;}
- if(t.ti_min==60){t.ti_hour=t.ti_hour+1;t.ti_min=0;chk=1;}
- if(!chk)delay(4);
- }
- c=getch();
- if(c=='c' || c=='C') {
- int h,m,s;
- outtextxy(500,300,"New Time (hh:mm:ss):" );
- outtextxy(500,320,"hh:");
- fflush(stdin);
- gotoxy(70,21);
- scanf("%d",&h);
- outtextxy(500,335,"mm:");
- gotoxy(70,22);
- scanf("%d",&m);
- outtextxy(500,350,"ss:");
- gotoxy(70,23);
- scanf("%d",&s);
- if((h>0 && h<=12) && (m>=0 && m<60) && (s>=0 && s<60)) {
- t.ti_hour=h;
- t.ti_min=m;
- t.ti_sec=s;
- cleardevice();
- settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
- Draw();
- }
- else {
- outtextxy(500,370,"Error!");
- getch();
- setfillstyle(SOLID_FILL,0);
- bar(500,300,650,400);
- }
- }
- else if(c=='r' || c=='R') {
- setcolor(15);
- settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
- cleardevice();
- Draw(1);
- }
- else if(c=='n' || c=='N') {
- setcolor(15);
- settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
- cleardevice();
- Draw();
- }
- fflush(stdin);
- } while(c!='x' && c!='X');
- //{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);}
- }
- void main(void) {
- int gd=DETECT,gm;
- initgraph(&gd,&gm,"");
- Clock c;
- //getch();
- closegraph();
- }
Add Comment
Please, Sign In to add comment