免费注册 查看新帖 |

Chinaunix

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

现阶段写出的一段代码,大家帮忙看看 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-01-02 15:00 |只看该作者 |倒序浏览
大家畅所欲言,想知道自己现在处在什么一个位置。。。谢谢大家

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>   



  5. int callsystem( void )
  6. {
  7.         char ch[ 256 ];
  8.         int i = 0;
  9.         do {
  10.                 printf( "请输入一个系统命令 : " );
  11.                 for ( i = 0; i < 256; i++ ) {
  12.                        
  13.                         ch[ i ] = getchar();
  14.                         if ( ch[ i ] == '\n' ) {
  15.                                 ch[ i + 1 ] = '\0';
  16.                                 system( ch );
  17.                                 printf( "%s\n", ch );
  18.                                 break;
  19.                         }
  20.                 }

  21.         } while( ch[0] != 'e' && ch[1] != 'x' && ch[2] != 'i' && ch[3] != 't' );
  22.         return 0;   
  23. }
  24. void binary_to_ascii( unsigned int value )
  25. {
  26.         unsigned int quotient;
  27.         quotient = value / 10;
  28.         if ( quotient != 0 )
  29.                 binary_to_ascii( quotient );
  30.         putchar( value % 10 + '0' );
  31.        
  32. }

  33. size_t len( char * string )
  34. {
  35.         int length = 0;
  36.         while ( * string++ != '\0' )
  37.                 length += 1;
  38.         return length;
  39. }

  40. char * find_char( char * * strings, char value )
  41. {
  42.         char * string;
  43.         while ( ( string = * strings++ ) != NULL ) {
  44.                 while ( * string != '\0' ) {
  45.                         if ( * string++ == value )
  46.                                 return string;
  47.                 }
  48.         }
  49.         return NULL;
  50. }
  51.      
  52. int   timeexample( void )
  53. {
  54.         time_t now;    /* define 'now'. time_t is probably
  55.                         *
  56.                         * a typedef */
  57.    
  58.         /* Calender time is the number of
  59.          * seconds since 1/1/1970    */
  60.        
  61.         now = time( (time_t *)NULL );  /* Get the system time and put it
  62.                                       * into 'now' as 'calender time' */
  63.        
  64.         printf( "%s", ctime(&now) );  /* Format data in 'now'
  65.                                      * NOTE that 'ctime' inserts a
  66.                                      * '\n' */
  67.        
  68.         /*********************************************************************/
  69.        
  70.         /* Here is another way to extract the time/date information */

  71.         time( &now );
  72.        
  73.         printf( "%s", ctime(&now) );  /* Format data in 'now'  */
  74.        
  75.         /*********************************************************************/
  76.         {
  77.                 struct tm * l_time;
  78.                 l_time = localtime( &now );  /* Convert 'calender time' to
  79.                                             * 'local time' - return a pointer
  80.                                             * to the 'tm' structure. localtime
  81.                                             * reserves the storage for us. */
  82.                 printf( "%s", asctime(l_time) );
  83.         }
  84.        
  85.         /*********************************************************************/
  86.        
  87.         time( &now );
  88.         printf( "%s", asctime( localtime( &now ) ) );
  89.        
  90.         /*********************************************************************/
  91.        
  92.         {
  93.                 struct tm * l_time;
  94.                 char string[20];
  95.         
  96.                 time( &now );
  97.                 l_time = localtime( &now );
  98.                 strftime( string, sizeof string, "%d-%b-%y\n", l_time );
  99.                 printf( "%s", string );
  100.         }
  101. }

  102. int rand1( void )  
  103. {  
  104.         int i;  
  105.         printf( "Ten  random numbers from 0 to 99\n\n" );  
  106.         for( i = 0; i < 10; i++ )  
  107.                 printf( "%d\n", rand() % 100 );  
  108.         return 0;  
  109. }

  110. int rand2( void )
  111. {
  112.         int i;
  113.        
  114.         /* Seed the random-number generator with current time so that
  115.          * the numbers will be different every time we run. */
  116.         srand( (unsigned)time( NULL ) );
  117.        
  118.         /* Display 10 numbers. */
  119.         for( i = 0;   i < 10; i++ )
  120.                 printf( "  %6d\n", rand() );
  121. }


  122. int createfile( void )
  123. {
  124.         FILE  * fp;
  125.         char  Line[256];
  126.         /* ... Open a file for output. */
  127.         printf( "请输入你想要创建的文件名 :" );
  128.         gets( Line );
  129.         if ( len( Line ) <= 0 ) {
  130.                 return 0;                       
  131.         }                                                               
  132.         fp = fopen( Line, "w" );
  133.         printf( "开始输入内容, 输入 exit 保存退出 :" );
  134.         while( gets( Line ) )  /* Get data from stdin */
  135.         {
  136.                 if ( strcmp( Line, "exit" ) == 0 )
  137.                         break;
  138.                 fprintf( fp, "%s\n", Line ); /* Send data to file.  */
  139.         }
  140.        
  141.         fclose( fp );
  142. }

  143. void showfile( char * fname )
  144. {
  145.         int c;    /* Character read from the file. */
  146.         FILE * ptr;   /* Pointer to the file. FILE is a
  147.                          structure  defined in <stdio.h> */
  148.        
  149.         /* Open the file - no error checking done */
  150.         if ( ( ptr = fopen( fname, "r" ) ) == NULL ) {
  151.                 printf( "\n\t\t----------没有这个文件---------\n\n" );
  152.                 printf( "\a" );
  153.                 return;
  154.         }
  155.         /* Read one character at a time, checking
  156.            for the End of File. EOF is defined
  157.            in <stdio.h>  as -1    */
  158.         while ( ( c = fgetc( ptr ) ) != EOF )
  159.         {
  160.                 printf( "%c", c );  /* O/P the character to the screen */
  161.         }
  162.         fclose( ptr );   /* Close the file.   */
  163. }

  164. int openfile( int s, char * Line )
  165. {
  166.        
  167.         printf( "\n请选择 1.新建一个文件  2.显示已经存在的文件 3.退出 : " );
  168.         scanf( "%d", &s );
  169.         getchar();
  170.         if ( s == 1 ) {
  171.                 createfile();
  172.         }
  173.         if ( s == 2 ) {
  174.                 printf( "\t请输入你想要查看的文件名 :" );
  175.                 gets( Line );
  176.                 if ( strlen( Line ) <= 0 ) {
  177.                         return 0;                       
  178.                 }
  179.                 showfile( Line );
  180.                 free( Line );
  181.                 }
  182.         else {
  183.                 int l = 0;
  184.                 l = len( Line );
  185.                 printf( "Line's length : %d\n", l );
  186.                 return 0;
  187.         }
  188. }

  189. int main( int argc, char * argv[] )
  190. {
  191.         char Line[256];
  192.         int s;
  193.        
  194.         do {
  195.                 printf( "\n\n请选择你要做的操作 1.文件操作  2.系统时间 3.随机数 4.数字转换字符\n\n\t\t   5.系统调用  6.退出 : " );
  196.                 scanf( "%d", &s );
  197.                 getchar();
  198.                 switch( s ) {
  199.                 case 1: openfile( s, Line);
  200.                         break;
  201.                 case 2: timeexample();
  202.                         break;
  203.                 case 3: rand1(); rand2();
  204.                         break;
  205.                 case 4: printf( "请输入一个无符号数 : " );
  206.                         scanf( "%d", &s );
  207.                         getchar();
  208.                         binary_to_ascii( s );
  209.                 break;
  210.                 case 5: callsystem();
  211.                         break;
  212.                 case 6: return 0;
  213.                 }
  214.         } while( s != 6 );
  215.         getch();
  216. }
复制代码

[ 本帖最后由 ngzyl 于 2007-1-2 15:17 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2007-01-02 15:13 |只看该作者
中规中矩。很好,继续。

论坛徽章:
0
3 [报告]
发表于 2007-01-02 15:30 |只看该作者
原帖由 langue 于 2007-1-2 15:13 发表
中规中矩。很好,继续。



不明白这句话的意思。。。。。。。。

论坛徽章:
0
4 [报告]
发表于 2007-01-02 15:31 |只看该作者
处在迷茫阶段,进步可能是最快的。

论坛徽章:
0
5 [报告]
发表于 2007-01-02 15:38 |只看该作者
我现在白天8小时工作   有闲暇时间就上CU和看一些编程有关的pdf书籍,并做一些例子,并思考后加些自己的东西,晚上回家吃了饭,到12点都是在和电子书、C及linux打交道,我会坚持下去,虽然迷茫,但总比把这些时间花在喝酒、上舞厅好的多。。。


但是总感觉进步不大,所以很迷茫,也很恐惧。。。自己这样的活着值不值得。。

[ 本帖最后由 ngzyl 于 2007-1-2 15:39 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2007-01-02 16:19 |只看该作者
我的看法: (a == 0)这样就可以了, 我开始和你一样( a == 0 ). 不过因为一般要求每行80字符, 所以(a == 0)就能省下2个字符的位置.
有些注释让代码混乱了. 特别是某些定义出自一些系统头文件的, 最好不要写, 一个是平台实现有差异, 另一个是如果写错了, 让看代码的混乱(水平高的当然不会). 减少不必要的注释. 个别常量可以考虑enum来定义.
char *p就可以了, *和变量名之间不需要再多个空格^_^
函数的定义时候, 把返回值单独放在一行, 对于一些自动处理代码的脚本来说可能更加方便.

PS: 有个地方 fopen 以后, 没有检查返回值.

[ 本帖最后由 Edengundam 于 2007-1-2 16:23 编辑 ]

论坛徽章:
0
7 [报告]
发表于 2007-01-02 16:31 |只看该作者
原帖由 Edengundam 于 2007-1-2 16:19 发表
我的看法: (a == 0)这样就可以了, 我开始和你一样( a == 0 ). 不过因为一般要求每行80字符, 所以(a == 0)就能省下2个字符的位置.
有些注释让代码混乱了. 特别是某些定义出自一些系统头文件的, 最好不要写, 一个 ...



呵呵,这位仁兄看的够仔细。。。。谢谢

论坛徽章:
0
8 [报告]
发表于 2007-01-02 18:06 |只看该作者
可以找个支持语法高亮的编辑器。Windows 或者 GTK 的用户可以使用 SciTE,终端用户用 JOE、VIM、Emacs 应该不错。

论坛徽章:
0
9 [报告]
发表于 2007-01-02 18:51 |只看该作者
原帖由 ngzyl 于 2007-1-2 15:38 发表
我现在白天8小时工作   有闲暇时间就上CU和看一些编程有关的pdf书籍,并做一些例子,并思考后加些自己的东西,晚上回家吃了饭,到12点都是在和电子书、C及linux打交道,我会坚持下去,虽然迷茫,但总比把这些时间 ...


厉害
成家后看书的时间就少多了

学习lz

论坛徽章:
0
10 [报告]
发表于 2007-01-02 18:56 |只看该作者
呵呵 差不多水平 大家一起加油
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP