免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
1234下一页
最近访问板块 发新帖
查看: 9432 | 回复: 32
打印 上一主题 下一主题

给初学者玩的一个时钟的小程序 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-09-27 10:52 |只看该作者 |倒序浏览
同事问起一个在终端显示时间嘀哒走的程序,反正没什么事就写了一个,非常简单,给初学者玩玩

  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <unistd.h>

  4. int main ()
  5. {
  6.         while (1) {
  7.                 time_t sec  = time(NULL);
  8.                 struct tm t = *localtime(&sec);

  9.                 printf("\x1b[2J");      /* clear screen and home cursor */
  10.                 printf("\x1b[31;40m");  /* red foreground, black background */
  11.                 printf("\x1b[11;29H");  /* moves cursor to line 11, column 29 */
  12.                 printf("+-----^--^-----+\n");
  13.                 printf("\x1b[12;29H");
  14.                 printf("|\t%02d:%02d:%02d:  |\n", t.tm_hour, t.tm_min, t.tm_sec);
  15.                 printf("\x1b[13;29H");
  16.                 printf("+-------V------+\n");
  17.                 sleep(1);
  18.         }

  19.         return 0;
  20. }
  21. /* reference: ANSI Escape Sequences
  22. * compile:   gcc mytime.c -o mytime
  23. * usage:     ./mytime
  24. */

复制代码

论坛徽章:
0
2 [报告]
发表于 2006-09-27 11:13 |只看该作者
真棒,就是不懂那些printf是干什么用的

论坛徽章:
0
3 [报告]
发表于 2006-09-27 11:18 |只看该作者
有简单注释
搜索一下ANSI Escape Sequences 内容不是很多
有时候调试程序时的不同输出信息加入颜色还是很有用的

论坛徽章:
0
4 [报告]
发表于 2006-09-27 13:25 |只看该作者
楼主这些printf在dos下也可以用吗?

论坛徽章:
0
5 [报告]
发表于 2006-09-27 13:45 |只看该作者
dos下也可以
跟ansi.sys相关

论坛徽章:
0
6 [报告]
发表于 2006-09-27 14:53 |只看该作者
嗯。不错。以前记过觉得很好玩,就是有时偶尔想用了,又找不到这些控制字了,呵呵!
个人比较喜欢这东东。

论坛徽章:
0
7 [报告]
发表于 2006-09-27 15:17 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
8 [报告]
发表于 2006-09-27 15:50 |只看该作者
不懂printf("\x1b[2J");里面的   1b[2J  是什么东西啊,高手指点指点

论坛徽章:
0
9 [报告]
发表于 2006-09-27 16:22 |只看该作者
1B是ESC的十六进制

  1. ANSI Escape Sequences

  2. What ANSI escape sequences can be used?

  3. ANSI escape sequences provide cursor and screen control in OS/2 character
  4. mode sessions.  By default ANSI support is turned ON (although it may be
  5. turned off with the command ANSI OFF).  ANSI support is also available in
  6. DOS sessions if the device driver ANSI.SYS is loaded.  See the online
  7. Command Reference for details.

  8. The following ANSI escape sequences are available:

  9. Key      
  10. ESC       Refers to ASCII code 27 (i.e. the Escape key)
  11. #         Replace with the appropriate number
  12. ....      Replace with additional attributes, if desired


  13. Escape Code Sequence          Function
  14. Cursor Controls               
  15. ESC[#;#H or ESC[#;#f          Moves cursor to line #, column #
  16. ESC[#A                        Moves cursor up # lines
  17. ESC[#B                        Moves cursor down # lines
  18. ESC[#C                        Moves cursor forward # spaces
  19. ESC[#D                        Moves cursor back # spaces
  20. ESC[#;#R                      Reports current cursor line and column
  21. ESC[s                         Saves cursor position for recall later
  22. ESC[u                         Return to saved cursor position
  23. Erase Functions               
  24. ESC[2J                        Clear screen and home cursor
  25. ESC[K                         Clear to end of line
  26. Set Graphics Rendition        
  27. ESC[#;#;....;#m               Set display attributes where # is
  28.                                0 for normal display
  29.                                1 bold on
  30.                                4 underline (mono only)
  31.                                5 blink on
  32.                                7 reverse video on
  33.                                8 nondisplayed (invisible)
  34.                                30 black foreground
  35.                                31 red foreground
  36.                                32 green foreground
  37.                                33 yellow foreground
  38.                                34 blue foreground
  39.                                35 magenta foreground
  40.                                36 cyan foreground
  41.                                37 white foreground
  42.                                40 black background
  43.                                41 red background
  44.                                42 green background
  45.                                43 yellow background
  46.                                44 blue background
  47.                                45 magenta background
  48.                                46 cyan background
  49.                                47 white background
  50. ESC[=#;7h                     Put screen in indicated mode where # is
  51.                                0 for 40x25 black and white
  52.                                1 40x25 color
  53.                                2 80x25 black and white
  54.                                3 80x25 color
  55.                                4 320x200 color graphics
  56.                                5 320x200 black and white graphics
  57.                                6 640x200 black and white graphics
  58.                                7 to wrap at end of line
  59. ESC[=#;7l                     Resets mode # set with above command
  60. Keyboard Reassignments        
  61. ESC[#;#;....#p                The first ASCII code defines what is to be
  62.                                changed; the remaining codes define what it
  63.                                is to be changed to; strings are permitted.
  64.                               
  65.                                Examples:
  66.                                ESC[65;81p - A becomes Q
  67.                                ESC[81;65p - Q becomes A
  68.                                ESC[0;68;"dir";13p - Assign the F10 key to
  69.                                a DIR command.
  70.                                The 0;68 portion is the extended ASCII code
  71.                                for the F10 key and 13 is the ASCII code
  72.                                for a carriage return.
  73.                                Other function key codes: F1=59, F2=60,
  74.                                F3=61, ... F10=68.


  75. You can use ANSI escape sequences in the PROMPT environment variable to
  76. create complex command line prompts.  See the online Command Reference
  77. (under PROMPT) for details.

  78. For example, if you have a color monitor, try editing your CONFIG.SYS
  79. file so that

  80. SET PROMPT=$e[32;40m$e[1m[$P]$e[0m
  81.   

  82. to obtain a more colorful OS/2 command line prompt.  (Case is significant
  83. in the example given.)  You can do the same for your DOS sessions if you
  84. edit PROMPT in AUTOEXEC.BAT, assuming you have ANSI.SYS loaded.  Note
  85. that the $i portion of your PROMPT will enable the help line at the top
  86. of the window or screen.  It is not included in the example above.

复制代码

论坛徽章:
0
10 [报告]
发表于 2006-09-27 16:58 |只看该作者

回复 9楼 mike_chen 的帖子

你真棒,懂的真多,我平常都用不到这些东西,我也不清楚这是做什么用的,什么时候该用噢?为啥要用十6进制呢,咋不用其它进制呢,嘻嘻...ANSI Escape Sequences是谁做的,什么时候要用到这个东西呢?

[ 本帖最后由 蜗大牛牛 于 2006-9-27 17:04 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP