用C编程一个简单的时钟

时间:2011-11-28 11:16:35

标签: c

  

编写程序以模拟简单的数字时钟显示

     

HOURS(s):MINUTES(s):SECOND(s)

  • 如何获取本地系统时间?

  • 如何将本地系统时间分成小时,分钟和秒?

编辑1:

这是我尝试过的,但它远非完美(dosnt甚至使用time.h)

     #include <stdio.h>
     #include <conio.h>
     #include <dos.h> void main() { int h,m,s; h=0; m=0; s=0; while(1) {

     if(s>59) {m=m+1; s=0; } 
      if(m>59) { h=h+1; m=0; } 
      if(h>11) { h=0; m=0; s=0; } 
      delay(1000); 
      s=s+1; 
      clrscr(); 
      printf("\n DIGITAL CLOCK");
      printf("\n HOUR:MINUTE:SECOND"); 
      printf("\n%d:%d:%d",h,m,s); 
}

}

5 个答案:

答案 0 :(得分:7)

  

如何获得本地系统时间?   如何将本地系统时间分成小时,分钟和秒?

time.h

#include <stdio.h>
#include <time.h>

int main(void) {

    time_t rawtime;
    struct tm*  time_;

    time(&rawtime);
    time_ = localtime(&rawtime);

    printf("%i:%i:%i %i %i %i\n", time_->tm_hour, time_->tm_min, 
            time_->tm_sec, time_->tm_mday, time_->tm_mon+1,
            time_->tm_year+1900);

    return 0;
}

我为什么要+1900+1您可以阅读here

答案 1 :(得分:3)

来自维基百科的

C date and time functions

答案 2 :(得分:1)

做一个“man localtime()”。尝试找出传递给函数的结构和返回值。直到你写自己,你不会学习。不要从上面复制粘贴并提交你的作业:)

答案 3 :(得分:1)

您可以使用这个最简单的代码来完成上述问题。

#include <stdio.h>
int main()
{
 printf("%s %s",__TIME__,__DATE__);
 return 0;
 } 

答案 4 :(得分:0)

#include<stdio.h>
#include<conio.h>
int main(void)

{
    clrscr();
    int h = 0, m = 0, s = 0;
    printf("Digi_Clock \n");
    printf("Enter time (hh : mm : ss) \n");
    scanf(" %d %d %d", &h, &m, &s);

start:;

    for (h; h <= 24; h++) {
        for (m; m <= 60; m++) {
            for (s; s <= 60; s++) {
                clrscr();

                printf(" %d : %d : %d \n", h, m, s);

                for (double i = 0; i <= 6699000; i++) {
                    i++;
                    i--;
                }
            }
            s = 0;
        }
        m = 0;
    }

    goto start;
    getch();
}
相关问题