/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

								########									####		   ####
									 ##										  ##  ##	 ##	 ##
									 ##    								  ##    ####	 ##
									 ##									    ##	     		 ##
									 ##									    ##					 ##
								## ##								      ##					 ##
								#####								      ##					 ##
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <time.h>
#include <math.h>

int intHour=0,intMinute=10,intSec=0;
int xs[61],ys[61];//声明秒针端地址
int intMinuteX[61],intMinuteY[61];//声明分针端地址
int intHourX[61],intHourY[61];//时针端地址
//初始化指针端地址
void initPoint()
{
	double pi=3.14159265;
	double angle=0;
	int i;
	for (angle=0,i=60;angle<360;angle+=6,i--)
	{
		xs[i]=(int)(cos(angle/180*pi)*120);
		ys[i]=(int)(sin(angle/180*pi)*(-120));
		intMinuteX[i]=(int)(cos(angle/180*pi)*90);
		intMinuteY[i]=(int)(sin(angle/180*pi)*(-90));
		intHourX[i]=(int)(cos(angle/180*pi)*60);
		intHourY[i]=(int)(sin(angle/180*pi)*(-60));
	}
}
//秒针运动程序
void MovSec(int i)
{
	setcolor(13);
	line(xs[i]+320,ys[i]+240,320,240);
}
//清除上一个秒针
void ClrSec(int i)
{
	setcolor(getbkcolor());
	line (xs[i]+320,ys[i]+240,320,240);
}

void MovMin(int i)
{
	setcolor(12);
	line (intMinuteX[i]+320,intMinuteY[i]+240,320,240);
	line (intMinuteX[i]+319,intMinuteY[i]+240,319,240);
	line (intMinuteX[i]+320,intMinuteY[i]+239,320,239);
	line (intMinuteX[i]+319,intMinuteY[i]+239,319,239);
}

void ClrMin(int i)
{
	setcolor(getbkcolor());
	line(intMinuteX[i]+320,intMinuteY[i]+240,320,240);
	line (intMinuteX[i]+319,intMinuteY[i]+240,319,240);
	line (intMinuteX[i]+320,intMinuteY[i]+239,320,239);
	line (intMinuteX[i]+319,intMinuteY[i]+239,319,239);
}

void MovHour(int i)
{
	setcolor(9);
	
	line (intHourX[i]+320,intHourY[i]+240,320,240);
	line (intHourX[i]+319,intHourY[i]+240,319,240);
	line (intHourX[i]+320,intHourY[i]+239,320,239);
	line (intHourX[i]+319,intHourY[i]+239,319,239);
	line (intHourX[i]+321,intHourY[i]+240,321,240);
	line (intHourX[i]+321,intHourY[i]+239,321,239);
	line (intHourX[i]+321,intHourY[i]+241,321,241);
	line (intHourX[i]+320,intHourY[i]+241,320,241);
	line (intHourX[i]+319,intHourY[i]+241,319,241);
}

void ClrHour(int i)
{
	setcolor(getbkcolor());
	line (intHourX[i]+320,intHourY[i]+240,320,240);
	line (intHourX[i]+319,intHourY[i]+240,319,240);
	line (intHourX[i]+320,intHourY[i]+239,320,239);
	line (intHourX[i]+319,intHourY[i]+239,319,239);
	line (intHourX[i]+321,intHourY[i]+240,321,240);
	line (intHourX[i]+321,intHourY[i]+239,321,239);
	line (intHourX[i]+321,intHourY[i]+241,321,241);
	line (intHourX[i]+320,intHourY[i]+241,320,241);
	line (intHourX[i]+319,intHourY[i]+241,319,241);
}
	
int ChangeToTime(char charTimeNow[5])
{
	int i=0;
	int j=0;
	int intError=0;
	while (charTimeNow[i]!=58)
	{
		i++;
		if(i>8)
		{
			intError=1;
		}
	}
	j=i;
	switch(j)
	{
		case 1:
			intHour=(int)(charTimeNow[0])-48;
			break;
		case 2:
			intHour=(((int)(charTimeNow[0])-48)*10)+((int)(charTimeNow[1])-48);
			break;
		default:
			intError=1;
			break;
	}
	if (intHour>24)
	{
		intError=1;
	}
	else
	{
		if(intHour>12)
		{
			intHour-=12;
		}
	}
	//printf("Hour=%d\n",intHour);
	i++;
	while (charTimeNow[i]!=58)
	{
		i++;
		if(i>8)
		{
			intError=1;
		}
	}
	switch(i-j)
	{
		case 2:
			intMinute=(int)(charTimeNow[j+1])-48;
			break;
		case 3:
			intMinute=(((int)(charTimeNow[j+1])-48)*10)+((int)(charTimeNow[j+2])-48);
			break;
		default:
			intError=1;
			break;
	}

	if (intMinute>60)
	{
		intError=1;
	}
	i++;
	j=i;
	while (charTimeNow[i]!='\0')
	{
		i++;
		if(i>8)
		{
			intError=0;
		}
	}
	switch(i-j)
	{
		case 1:
			intSec=(int)(charTimeNow[j])-48;
			break;
		case 2:
			intSec=(((int)(charTimeNow[j])-48)*10)+((int)(charTimeNow[j+1])-48);
			break;
		default:
			intError=1;
			break;
		}
	if (intSec>60)
	{
		intError=1;
	}
	//printf("Second=%d\nEnd\n",intSec);
	return intError;
}
//画表盘
void ClockFace()
{
	int i;
	int intR=150;
	int CenterX=320;
	int CenterY=240;
	int x1,x2,y1,y2;
	double angle;
	double pi=3.14159265;
	setbkcolor(8);
	setcolor(1);
	for (i=0;i<10;i++)
	{
		circle(CenterX,CenterY,intR+i);
	}
	setcolor(15);
	bar(458,237,470,243);
	bar(317,90,323,102);
	bar(170,237,182,243);
	bar(317,378,323,390);
	for (angle=0;angle<360;angle+=30)
	{
		x1=(int)(cos(angle/180*pi)*150);
		y1=(int)(sin(angle/180*pi)*(-150));
		x2=(int)(cos(angle/180*pi)*140);
		y2=(int)(sin(angle/180*pi)*(-140));
		line (x1+CenterX,y1+CenterY,x2+CenterX,y2+CenterY);
	}
	printf("Press any key to exit.");
}
//将正常的时刻转换为指针(分,秒,小时)数组位置
int ChangeToReal(int i)
{
	if(i>=0&&i<=15)
	{
		i+=45;
	}
	else
	{
		i-=15;
	}
	return i;
}

int main()
{
	int gdriver,gmode;
	int i=0;
	

	time_t t;
	
	char charTimeNow[9];
	t = time(NULL);
	charTimeNow[8]='\0';
	clrscr();
	printf("Do you want to set time?");
	scanf("%s",charTimeNow);

	if(charTimeNow[0]!=78)
	{
		if(ChangeToTime(charTimeNow)==0)
		{
			ChangeToTime(charTimeNow);
		}
		else
		{
			printf("Time Input Error!");
			return;
		}

	}
	else
	{
		for (i=0;i<8;i++)
		{
			charTimeNow[i]=ctime(&t)[11+i];

		}

		
		ChangeToTime(charTimeNow);
	}
	printf("Time is %s\n",charTimeNow);
	getch();
	
	gdriver=9;
	gmode=2;
	initPoint();

	initgraph(&gdriver,&gmode,"f:\\borlandc\\bgi");
	ClockFace();
	intHour=intHour*5+((int)(intMinute/12));
	while(!kbhit())
	{
		intSec++;
		MovSec(ChangeToReal(intSec%60));
		delay(1000);
		ClrSec(ChangeToReal(intSec%60));
		MovHour(ChangeToReal(intHour));
		MovMin(ChangeToReal(intMinute));
		nosound();
		if(intSec%60==0)
		{
			intMinute++;
			MovMin(ChangeToReal(intMinute));
			ClrMin(ChangeToReal(intMinute-1));
			if(intMinute%12==0)
			{
				intHour++;
				MovHour(ChangeToReal(intHour));
				ClrHour(ChangeToReal(intHour-1));
			}

			sound(5000);
		}
	}

	getch();
	closegraph();

	return 0;
}
