1. /**********************************
  2. * Name : timeDisplay.cpp
  3. * Purpose: Display digital clock according to current system time.
  4. * Author : feicaixian
  5. ***********************************/
  6.  
  7. #include <stdio.h>
  8. #include <time.h>
  9. #include <stdlib.h>
  10. void segDisp(int n);
  11. void numToArr(int n, int *a);
  12.  
  13. /***********************
  14. * 7段码笔画编号,编号可以随意,不同编号方案对应不同7段码
  15. * _0
  16. * 5|_6| 1
  17. * 4|_3| 2
  18. *
  19. ***********************/
  20. //将0~9的7段码存入数组,0表示不显示笔画,1表示显示笔画
  21. const int segments[][] = {
  22. {, , , , , , },//
  23. {, , , , , , },//
  24. {, , , , , , },//
  25. {, , , , , , },//
  26. {, , , , , , },//
  27. {, , , , , , },//
  28. {, , , , , , },//
  29. {, , , , , , },//
  30. {, , , , , , },//
  31. {, , , , , , } //
  32. };
  33.  
  34. int main()
  35. {
  36. /*
  37. Use time() to get system time.The function prototype is:
  38. #include <time.h>
  39. time_t time(time_t *);
  40.  
  41. function type is time_t, function name is time, parameter is pointer, whose type is time_t.
  42. time(time_t *timer) returns
  43. the number of seconds
  44. between January 1, 1970 and the current system time
  45. to timer, which can be print as long int.
  46. */
  47. time_t t;//define a time_t type variable
  48.  
  49. time(&t);//Pass time_t type pointer to time()
  50.  
  51. printf("The number of seconds: %d\n",t);//print the seconds from January 1, 1970 to now.
  52.  
  53. /*Use functions followed to convert seconds to a structure:
  54. struct tm *gmtime(const time_t *timer); //convert seconds to Greenwich Mean Time(GMT)
  55. struct tm *localtime(const time_t * timer); //convert seconds to localtime
  56.  
  57. struct tm * means that the type of return value is a struct tm type pointer.
  58.  
  59. struct tm
  60. {
  61. int tm_sec;//seconds 0-61
  62. int tm_min;//minutes 1-59
  63. int tm_hour;//hours 0-23
  64. int tm_mday;//day of the month 1-31
  65. int tm_mon;//months since jan 0-11, so add 1 to get the real months.
  66. int tm_year;//years from 1900, add 1900 to get the real years.
  67. int tm_wday;//days since Sunday, 0-6
  68. int tm_yday;//days since Jan 1, 0-365
  69. int tm_isdst;//Daylight Saving time indicator
  70. };
  71.  
  72. */
  73. struct tm *p = localtime(&t);//不一定要把当前系统时间传给指针,你也可以自定义各成员的值。
  74. int oldSeconds;
  75.  
  76. while() {
  77. system("cls");//clear screen.
  78.  
  79. /*
  80. 方法一:
  81. char *asctime(const struct tm *timeptr) 返回一个表示当地时间的字符串,它代表了timeptr的日期和时间。
  82. */
  83. printf("%s\n\n",asctime(p));//输出结果像这样:Sat Mar 25 06:10:10 1989
  84.  
  85. /*
  86. 方法二:
  87. char *ctime(const time_t *timer) 返回一个表示当地时间的字符串,当地时间是基于参数 timer。
  88. */
  89. printf("%s\n\n",ctime(&t));//输出结果和asctime一样
  90.  
  91. /*
  92. 方法三:
  93. 也可以自己输出日期和时间,自定义格式
  94. */
  95. int a[];
  96. int timeToInt = p->tm_hour * + p->tm_min * + p->tm_sec;
  97. printf("%d/%d/%d ",+p->tm_year,p->tm_mon + , p->tm_mday);//date
  98. numToArr(timeToInt, a);
  99. printf("%d%d:%d%d:%d%d\n",a[],a[],a[],a[],a[],a[]);
  100.  
  101. //使用7段码显示时间
  102.  
  103. segDisp(timeToInt);
  104.  
  105. //不断获取当前时间,直到秒钟刷新,跳出循环,更新显示
  106. oldSeconds = p->tm_sec;
  107. do {
  108. time(&t);
  109. p = localtime(&t);
  110. }while (p->tm_sec == oldSeconds);
  111. }
  112. return ;
  113. }
  114.  
  115. void numToArr(int n, int *a)
  116. {
  117. int k = ; //数组a[]的下标
  118.  
  119. do {
  120. a[k++] = n % ;
  121. n /= ;
  122. }while (n != );
  123. }
  124.  
  125. void segDisp(int n)
  126. {
  127. int i; //打印7段码时形参n的各位的序号
  128. int k = ; //数组a[]的下标
  129. int a[]; //存储形参的各个位的数字
  130. const int num = ;//时间显示的所需数字个数
  131.  
  132. //将形参n的各位存入数组a[]
  133. numToArr(n, a);
  134.  
  135. //-------------------------------------
  136. //--- 按行显示数字的7段码,共3行。
  137. //--- 第一行显示编号0,第二行显示编号5 6 1,第三行显示编号4 3 2.
  138. //-------------------------------------
  139.  
  140. //显示编号0
  141. for (i = num-; i >= ; i--) {
  142. if (i == | i == ){
  143. //在第二个数字和第四个数字后面要显示冒号,如12:34:43。由于使用中文下的·号,采用全角模式,占两个字符宽度,
  144. //所以额外增加两个空格。
  145. if (segments[a[i]][])
  146. printf(" _ ");
  147. else
  148. printf(" ");
  149. }
  150. else {
  151. if (segments[a[i]][])
  152. printf(" _ ");
  153. //附加两个空格,显示下一个数字的笔画。
  154. //第一个空格是因为本数字右边还有一个笔画,第二个空格是因为两个数字之间应有间隔。
  155. else
  156. printf(" ");//用空格代替下划线
  157. }
  158. }
  159. printf("\n");//在下一行打印
  160.  
  161. //显示编号5 6 1
  162. for (i = num-; i >= ; i--) {
  163. if (segments[a[i]][])
  164. printf("|");
  165. else
  166. printf(" ");
  167. if (segments[a[i]][])
  168. printf("_");
  169. else
  170. printf(" ");
  171. if (segments[a[i]][])
  172. printf("| ");
  173. else
  174. printf(" ");
  175. if (i == | i == )
  176. printf("·");//显示冒号
  177. }
  178. printf("\n");//在下一行打印
  179.  
  180. //显示编号4 3 2
  181. for (i = num-; i >= ; i--) {
  182. if (segments[a[i]][])
  183. printf("|");
  184. else
  185. printf(" ");
  186. if (segments[a[i]][])
  187. printf("_");
  188. else
  189. printf(" ");
  190. if (segments[a[i]][])
  191. printf("| ");
  192. else
  193. printf(" ");
  194. if (i == | i == )
  195. printf("·");
  196. }
  197. }

digital clock based C的更多相关文章

  1. UVALive - 6269 Digital Clock 模拟

    UVALive - 6269 Digital Clock 题意:时钟坏了,给你一段连续的时间,问你现在可能的时间是多少. 思路:直接模拟,他妈的居然这场就跪在了这题,卧槽,他妈的就在111行,居然多打 ...

  2. UVALive 6269 Digital Clock --枚举,模拟

    题意:说不清楚,自己看吧,太恶心. 这题真是SB了,当时看了一下以为乱搞就好了,于是开始动手拍,结果拍了好几个小时都没拍出来,而且越想越想不通,直接把自己绕进去了,结果gg了. 总结:甭管什么题,想清 ...

  3. UVa 579 Clock Hands

    水题.. 求任意时刻时针和分针的夹角,其结果在0°到180°之间. 这里又一次用到了sscanf()函数,确实很方便. 思路:我们分别求出时针和分针转过的角度,然后大的减小的,如果结果ans大于180 ...

  4. 40个新鲜的 jQuery 插件,使您的网站用户友好

    作为最流行的 JavaScript 开发框架,jQuery 在现在的 Web 开发项目中扮演着重要角色,它简化了 HTML 文档遍历,事件处理,动画以及 Ajax 交互,这篇文章特别收集了40个新鲜的 ...

  5. Design Patterns Example Code (in C++)

    Overview Design patterns are ways to reuse design solutions that other software developers have crea ...

  6. angularJS 杂

    慎用ng-repeat 中的 $index http://web.jobbole.com/82470/ 服务provider,公共代码的抽象 (语法糖)分为: constant常量:constant初 ...

  7. Qt 之 数字钟

    本例用来展示 QTimer 的使用,如何定时的更新一个窗口部件. 1  QLCDNumber 类 QLCDNumber 是一种可将数字显示为类似 LCD 形式的窗口部件,它同 QLabel 一样,都继 ...

  8. RAC的QA

    RAC: Frequently Asked Questions [ID 220970.1]   修改时间 13-JAN-2011     类型 FAQ     状态 PUBLISHED   Appli ...

  9. FPGA相关术语(一)

    参考资料: 1. 数字时钟管理单元DCM 2. RS-232 知识点: ● Xilinx) Digital Clock Manager(DCM) primitive用于实现延迟锁相环(delay lo ...

随机推荐

  1. Arcgis CreateFishnet工具,生成到FileGDB中要素类的格网大小不一致

    我的第一篇博客!哈哈 最近在做一些关于创建渔网的工作,发现一些问题,做个总结. 1.问题描述:如图1,设置好渔网的必要参数,输出目录为gdb里的矢量图层,(行列数比较大,渔网的地理范围较小),输出的格 ...

  2. 关于Apache本地能访问外网不能访问的问题

    title: 关于Apache本地能访问外网不能访问的问题 date: 2018-08-05 19:22:12 tags: web --- 在配置apache和tomcat时,把它们都配置好,放到服务 ...

  3. error: exportArchive: The data couldn’t be read because it isn’t in the correct format.

    在执行ios 打包的时候,我们通过执行下面的指令来打包ipa: mkdir arch archive_path=arch/${app_name}.xcarchive workspace_name=HP ...

  4. Linux命令——trap

    简介 trap是shell内置命令,它对硬件信号和其他事件做出响应.trap定义并激活信号处理过程,信号处理过程是当shell接收信号或其他特殊条件时要运行的处理过程. 语法 trap [-lp] [ ...

  5. 201871010128-杨丽霞《面向对象程序设计(java)》第六-七周学习总结

    201871010128-杨丽霞<面向对象程序设计(java)>第六-七周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ ...

  6. robotframework-post request请求携带上一个请求返回的cookie

    公司的接口服务需要先登录,获取服务端的cookie后,在后续的请求中携带这个cookie才能够访问 在尝试用RF工具进行自动化接口测试时,发现先访问登录接口之后,接着请求其他接口时没有自动携带上次请求 ...

  7. Cookie 技术

    Cookie 学习: 问题: HTTP 协议是没有记忆功能的,一次请求结束后,相关数据会被销毁.如果第二次的请求需要使用相同的请求数据怎么办呢?难道是让用户再次请求书写吗? 解决:使用 Cookie ...

  8. Java System.getProperty vs System.getenv

    转自:https://www.baeldung.com/java-system-get-property-vs-system-getenv 1. Introduction The package ja ...

  9. 数据结构or算法

    其实长久以来 mrxfyxj一直纠结着数据结构和算法到底有什么区别 只要学了一个算法就在惋惜她为什么不能是数据结构 产生这种想法的原因是mrxf觉得他blog里数据结构的东西很少 而mrxf自身又有一 ...

  10. 【转】jsp 页面 按回车键 触发事件

    转载: https://blog.csdn.net/ludongshun2016/article/details/59536779. 第一种: <script type="text/J ...