C语言写的一个电子时钟

运行环境:windows XP上的winTC。
说明:需要支持***graphics.h***才能运行。

=以下是源代码===

#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
#include<time.h>
//一些键值的定义
#define PI 3.141592653
#define UP 0x4800
#define DOWN 0x5000
#define ESC 0x11b
#define TAB 0xf09

int keyhandle(int,int);/*键盘按键判断*/
int timeupchange(int);/*处理上移按键*/
int timedownchange(int);/*处理下移按键*/
int digithour(double);/*double hour to int*/
int digitsec(double);
int digitmin(double);
void digitclock(int,int,int);/*在指定位置显示时钟或分钟或秒钟*/
void drawcursor(int);/*绘制一个光标*/
void clearcursor(int);/*消除一个光标*/
void clockhandle();/*时钟处理*/
void digitAclock();/*闹钟数字显示*/

double h,m,s;
double x,x1,x2,y,y1,y2;
struct time t[1];
struct AlarmClock/*定义闹钟结构体变量*/
{
    int hc;
    int mc;
    int sc;
}Alarm;

int main()
{   int driver ,mode=0,i,j;
    driver=DETECT;
    initgraph(&driver,&mode,"");
    setlinestyle(0,0,3);
    setbkcolor(8);
    setcolor(RED);
    /*时钟外围主体框架*/
    rectangle(52,11,583,431);
    rectangle(44,28,592,413);
    setwritemode(0);
    /*时钟标题*/
    setcolor(YELLOW);
    setlinestyle(0,0,1);
    outtextxy(280,15,"MyColck");
    /*画一个光标*/
    line(136,269,136,285);

    /*shu zi shi zhong kuang jia*/
    line(95,262,250,262);
    line(95,297,250,297);
    arc(95,280,90,270,17);
    arc(250,280,270,90,17);
    setcolor(5);
    /*作时分秒之间的分隔符*/
    for(i=146;i<=197;i+=51)
      for(j=274;j<=281;j+=7)
      {
         setlinestyle(0,0,3);
         circle(i,j,1);
      }
    /*在时钟中心画一个圆作为时钟的转轴*/
    setcolor(YELLOW);
    circle(178,151,2);


    /*==================================================*/
    /*小时点数*/
    for(i=0,m=0,h=0;i<=11;i++,h++)
    {
        x=88*sin((h*60+m)/360*PI)+178;
        y=151-88*cos((h*60+m)/360*PI);
        setlinestyle(0,0,3);
        setcolor(14);
        circle(x,y,1);
    }
    /*分或秒点数*/
    for(i=0,m=0;i<=59;m++,i++)
    {
        x=88*sin(m/30*PI)+178;
        y=151-88*cos(m/30*PI);
        setlinestyle(0,0,1);
        setcolor(3);
        circle(x,y,1);
    }
    /*在时钟上打印12,3,6,9数字*/
    outtextxy(98,148,"9");
    outtextxy(250,148,"3");
    outtextxy(170,70,"12");
    outtextxy(175,225,"6");


    /*帮助界面*/
    setcolor(GREEN);
    outtextxy(131,310,"H E L P");
    outtextxy(84,328,"Tab :Cursor move");
    outtextxy(84,348,"Up  :Time ++");
    outtextxy(84,368,"Down:Time --");
    outtextxy(84,388,"Esc :Quit system!");
    outtextxy(312,418,"Designed by Futuroscope&Joyce");

    /*绘制note的框架和字样*/
    rectangle(315,101,484,381);
    rectangle(322,86,498,387);
    rectangle(326,244,481,279);/*闹钟数字显示框架*/
    line(378,244,378,279);
    line(430,244,430,279);
    setcolor(1);
    outtextxy(370,111,"N O T E");
    setcolor(YELLOW);
    outtextxy(330,135,"You can set an ala-");
    outtextxy(330,145,"rm colck here!");
    setcolor(8);
    outtextxy(354,298,"ATTENTION!");

    clockhandle();
    closegraph();
    return 0;
}


void clockhandle()
{
  int k=0,count;
  /*初始化闹钟*/
  Alarm.hc=0;
  Alarm.mc=0;
  Alarm.sc=0;
  setcolor(RED);
  digitAclock(336,254,Alarm.hc);
  digitAclock(388,254,Alarm.mc);
  digitAclock(440,254,Alarm.sc);
  setcolor(15);/*白色时针*/
  gettime(t);
  setwritemode(1);
  /*开始循环,直到按ESC*/
  for(count=2;k!=ESC;)
  {
  
  /*声音模拟*/
  sound(500);
  delay(700);
  sound(200);
  delay(300);
  nosound();

  s=t[0].ti_sec;
  m=t[0].ti_min;
  h=t[0].ti_hour;

  /*draw hour r=50 ,white*/
  setcolor(15);
  x=50*sin((h*60+m)/360*PI)+178;
  y=151-50*cos((h*60+m)/360*PI);
  line(178,151,x,y);

  /*draw minuite r=78 lightblue*/
  setcolor(9);
  x1=78*sin(m/30*PI)+178;
  y1=151-78*cos(m/30*PI);
  line(178,151,x1,y1);

  /*draw second r=85 lightred*/
  setcolor(12);
  x2=85*sin(s/30*PI)+178;
  y2=151-85*cos(s/30*PI);
  line(178,151,x2,y2);

  while(t[0].ti_sec==s&&t[0].ti_min==m&&t[0].ti_hour==h)
  {  gettime(t);/*get system's time*/
     if(bioskey(1)!=0)
     {
     k=bioskey(0);
     count=keyhandle(k,count);
     if(count==8)
         count=1;
     }
  }/*whlie end*/

     setcolor(15);
     /*在数字框中显示数字时、分、秒,white*/
     digitclock(105,275,digithour(h));
     digitclock(160,275,digitmin(m));
     digitclock(215,275,digitsec(s)+1);
     
     /*异或操作,在原来的地方用相同的颜色画相同的秒针来擦除*/
     setcolor(15);
     x=50*sin((h*60+m)/360*PI)+178;
     y=151-50*cos((h*60+m)/360*PI);
     line(178,151,x,y);
     
     setcolor(9);
     x1=78*sin(m/30*PI)+178;
     y1=151-78*cos(m/30*PI);
     line(178,151,x1,y1);
     
     setcolor(12);
     x2=85*sin(s/30*PI)+178;
     y2=151-85*cos(s/30*PI);
     line(178,151,x2,y2);

    /*Alarm colck*/
    if(Alarm.hc==h&&Alarm.mc==m&&Alarm.sc==s)
    {
     Alarm.sc++;
     setwritemode(1);
     setcolor(YELLOW);
     setlinestyle(0,0,3);
     rectangle(315,101,484,381);
     rectangle(322,86,498,387);
     setlinestyle(0,0,1);
     outtextxy(370,111,"N O T E");
     setcolor(RED);
     outtextxy(330,135,"You can set an ala-");
     outtextxy(330,145,"rm colck here!");
     outtextxy(254,298,"ATTENTION!");
     delay(2000);
    }
    
  }/*for end*/
}


int keyhandle(int key,int count)
{  switch(key)
   {
   case UP:timeupchange(count-1);break;
   case DOWN:timedownchange(count-1);break;
   case TAB:setcolor(YELLOW);drawcursor(count);clearcursor(count);count++;break;
   }
   return count;
}


int timeupchange(int count)/*上键处理*/
{
      if(count==1)
    {
        t[0].ti_hour++;
        if(t[0].ti_hour==24) t[0].ti_hour=0;
        settime(t);
    }
    if(count==2)
    {
        t[0].ti_min++;
        if(t[0].ti_min==60)
        {
            t[0].ti_hour++;
            if(t[0].ti_hour==24)
            t[0].ti_hour=0;
            t[0].ti_min=0;
        }
        settime(t);
    }
    if(count==3)
    {
        t[0].ti_sec++;
        if(t[0].ti_sec==60)
        {
            t[0].ti_min++;
            if(t[0].ti_min==60)
            {
                t[0].ti_hour++;
                if(t[0].ti_hour==24)
                t[0].ti_hour=0;
                t[0].ti_min=0;
            }
            t[0].ti_sec=0;
        }
        settime(t);
    }

     /*闹钟*/
     { 
     if(count==4)
     { Alarm.hc++;
       if(Alarm.hc==24)
         Alarm.hc=0;
     }

     if(count==5)
     {  Alarm.mc++;
        if(Alarm.mc==60)
          Alarm.mc=0;
     }

     if(count==6)
     {  Alarm.sc++;
        if(Alarm.sc==60)
          Alarm.sc=0;
      }
     setcolor(RED);
     digitAclock(336,254,Alarm.hc);
     digitAclock(388,254,Alarm.mc);
     digitAclock(440,254,Alarm.sc);
     }
}


int timedownchange(int count)/*下键处理*/
{
     if(count==1)
    {

        if(t[0].ti_hour==0) t[0].ti_hour=24;
        t[0].ti_hour--;
        settime(t);
    }
    if(count==2)
    {

        if(t[0].ti_min==0)
        {
            if(t[0].ti_hour==0)
            t[0].ti_hour=24;
            t[0].ti_hour--;
            t[0].ti_min=60;
        }
        t[0].ti_min--;
        settime(t);
    }
    if(count==3)
    {
        if(t[0].ti_sec==0)
        {
            if(t[0].ti_min==0)
            {
                if(t[0].ti_hour==0)
                t[0].ti_hour=24;
                t[0].ti_min=60;
                t[0].ti_hour--;
            }
            t[0].ti_sec=60;
            t[0].ti_min--;
        }
        t[0].ti_sec--;
        settime(t);
    }

     {
     if(count==4)
     { Alarm.hc--;
       if(Alarm.hc<0)
         Alarm.hc=23;
     }

     if(count==5)
     {  Alarm.mc--;
        if(Alarm.mc<0)
          Alarm.mc=59;
     }

     if(count==6)
     {  Alarm.sc--;
        if(Alarm.sc<0)
          Alarm.sc=59;
      }
      setcolor(RED);
      digitAclock(336,254,Alarm.hc);
      digitAclock(388,254,Alarm.mc);
      digitAclock(440,254,Alarm.sc);
     }
}


void digitclock(int x,int y,int clock)/*数字时钟*/
{
    char buffer1[10];
    setfillstyle(0,2);
    bar(x,y,x+15,290);
    if(clock>=60)
        clock=0;
    sprintf(buffer1,"%d",clock);
    outtextxy(x,y,buffer1);
}

void digitAclock(int x,int y,int clock)
{
    char buffer2[10];
    setfillstyle(0,2);
    bar(x,y,x+15,270);
    if(clock>=60)
        clock=0;
    sprintf(buffer2,"%d",clock);
    outtextxy(x,y,buffer2);
    
}


int digithour(double h)/*double hour to int*/
{
    int i;
    for(i=0;i<=23;i++)
    {
        if(h==i)
            return i;
    }
}

int digitmin(double m)/*double minuite to int*/
{
    int i;
    for(i=0;i<=59;i++)
    {
        if(m==i)
            return i;
    }
}

int digitsec(double s)/*double second to int*/
{
   int i;
    for(i=0;i<=59;i++)
    {
        if(s==i)
            return i;
    }
}


/*绘画光标*/
void drawcursor(int count)
{
    switch(count)
    {
        case 1:line(136,269,136,285);break;
        case 2:line(188,269,188,285);break;
        case 3:line(244,269,244,285);break;
        case 4:line(367,254,367,270);break;
        case 5:line(418,254,418,270);break;
        case 6:line(469,254,469,270);break;

    }
}

/*清除光标*/
void clearcursor(int count)
{
    switch(count)
    {
       case 2:line(136,269,136,285);break;
       case 3:line(188,269,188,285);break;
       case 4:line(244,269,244,285);break;
       case 5:line(367,254,367,270);break;
       case 6:line(418,254,418,270);break;
       case 1:line(469,254,469,270);break;
    }
}
==========================================================
运行后的界面
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-v5b0rilm-1570524531029)(https://img-blog.csdn.net/20170305145746668?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzQ0MDAyMzI=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]
  • 21
    点赞
  • 92
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
众所周知,人工智能是当前最热门的话题之一, 计算机技术与互联网技术的快速发展更是将对人工智能的研究推向一个新的高潮。 人工智能是研究模拟和扩展人类智能的理论与方法及其应用的一门新兴技术科学。 作为人工智能核心研究领域之一的机器学习, 其研究动机是为了使计算机系统具有人的学习能力以实现人工智能。 那么, 什么是机器学习呢? 机器学习 (Machine Learning) 是对研究问题进行模型假设,利用计算机从训练数据中学习得到模型参数,并最终对数据进行预测和分析的一门学科。 机器学习的用途 机器学习是一种通用的数据处理技术,其包含了大量的学习算法。不同的学习算法在不同的行业及应用中能够表现出不同的性能和优势。目前,机器学习已成功地应用于下列领域: 互联网领域----语音识别、搜索引擎、语言翻译、垃圾邮件过滤、自然语言处理等 生物领域----基因序列分析、DNA 序列预测、蛋白质结构预测等 自动化领域----人脸识别、无人驾驶技术、图像处理、信号处理等 金融领域----证券市场分析、信用卡欺诈检测等 医学领域----疾病鉴别/诊断、流行病爆发预测等 刑侦领域----潜在犯罪识别与预测、模拟人工智能侦探等 新闻领域----新闻推荐系统等 游戏领域----游戏战略规划等 从上述所列举的应用可知,机器学习正在成为各行各业都会经常使用到的分析工具,尤其是在各领域数据量爆炸的今天,各行业都希望通过数据处理与分析手段,得到数据中有价值的信息,以便明确客户的需求和指引企业的发展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值