- 论坛徽章:
- 0
|
大家畅所欲言,想知道自己现在处在什么一个位置。。。谢谢大家
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <string.h>
- int callsystem( void )
- {
- char ch[ 256 ];
- int i = 0;
- do {
- printf( "请输入一个系统命令 : " );
- for ( i = 0; i < 256; i++ ) {
-
- ch[ i ] = getchar();
- if ( ch[ i ] == '\n' ) {
- ch[ i + 1 ] = '\0';
- system( ch );
- printf( "%s\n", ch );
- break;
- }
- }
- } while( ch[0] != 'e' && ch[1] != 'x' && ch[2] != 'i' && ch[3] != 't' );
- return 0;
- }
- void binary_to_ascii( unsigned int value )
- {
- unsigned int quotient;
- quotient = value / 10;
- if ( quotient != 0 )
- binary_to_ascii( quotient );
- putchar( value % 10 + '0' );
-
- }
- size_t len( char * string )
- {
- int length = 0;
- while ( * string++ != '\0' )
- length += 1;
- return length;
- }
- char * find_char( char * * strings, char value )
- {
- char * string;
- while ( ( string = * strings++ ) != NULL ) {
- while ( * string != '\0' ) {
- if ( * string++ == value )
- return string;
- }
- }
- return NULL;
- }
-
- int timeexample( void )
- {
- time_t now; /* define 'now'. time_t is probably
- *
- * a typedef */
-
- /* Calender time is the number of
- * seconds since 1/1/1970 */
-
- now = time( (time_t *)NULL ); /* Get the system time and put it
- * into 'now' as 'calender time' */
-
- printf( "%s", ctime(&now) ); /* Format data in 'now'
- * NOTE that 'ctime' inserts a
- * '\n' */
-
- /*********************************************************************/
-
- /* Here is another way to extract the time/date information */
-
- time( &now );
-
- printf( "%s", ctime(&now) ); /* Format data in 'now' */
-
- /*********************************************************************/
- {
- struct tm * l_time;
- l_time = localtime( &now ); /* Convert 'calender time' to
- * 'local time' - return a pointer
- * to the 'tm' structure. localtime
- * reserves the storage for us. */
- printf( "%s", asctime(l_time) );
- }
-
- /*********************************************************************/
-
- time( &now );
- printf( "%s", asctime( localtime( &now ) ) );
-
- /*********************************************************************/
-
- {
- struct tm * l_time;
- char string[20];
-
- time( &now );
- l_time = localtime( &now );
- strftime( string, sizeof string, "%d-%b-%y\n", l_time );
- printf( "%s", string );
- }
- }
- int rand1( void )
- {
- int i;
- printf( "Ten random numbers from 0 to 99\n\n" );
- for( i = 0; i < 10; i++ )
- printf( "%d\n", rand() % 100 );
- return 0;
- }
- int rand2( void )
- {
- int i;
-
- /* Seed the random-number generator with current time so that
- * the numbers will be different every time we run. */
- srand( (unsigned)time( NULL ) );
-
- /* Display 10 numbers. */
- for( i = 0; i < 10; i++ )
- printf( " %6d\n", rand() );
- }
- int createfile( void )
- {
- FILE * fp;
- char Line[256];
- /* ... Open a file for output. */
- printf( "请输入你想要创建的文件名 :" );
- gets( Line );
- if ( len( Line ) <= 0 ) {
- return 0;
- }
- fp = fopen( Line, "w" );
- printf( "开始输入内容, 输入 exit 保存退出 :" );
- while( gets( Line ) ) /* Get data from stdin */
- {
- if ( strcmp( Line, "exit" ) == 0 )
- break;
- fprintf( fp, "%s\n", Line ); /* Send data to file. */
- }
-
- fclose( fp );
- }
- void showfile( char * fname )
- {
- int c; /* Character read from the file. */
- FILE * ptr; /* Pointer to the file. FILE is a
- structure defined in <stdio.h> */
-
- /* Open the file - no error checking done */
- if ( ( ptr = fopen( fname, "r" ) ) == NULL ) {
- printf( "\n\t\t----------没有这个文件---------\n\n" );
- printf( "\a" );
- return;
- }
- /* Read one character at a time, checking
- for the End of File. EOF is defined
- in <stdio.h> as -1 */
- while ( ( c = fgetc( ptr ) ) != EOF )
- {
- printf( "%c", c ); /* O/P the character to the screen */
- }
- fclose( ptr ); /* Close the file. */
- }
- int openfile( int s, char * Line )
- {
-
- printf( "\n请选择 1.新建一个文件 2.显示已经存在的文件 3.退出 : " );
- scanf( "%d", &s );
- getchar();
- if ( s == 1 ) {
- createfile();
- }
- if ( s == 2 ) {
- printf( "\t请输入你想要查看的文件名 :" );
- gets( Line );
- if ( strlen( Line ) <= 0 ) {
- return 0;
- }
- showfile( Line );
- free( Line );
- }
- else {
- int l = 0;
- l = len( Line );
- printf( "Line's length : %d\n", l );
- return 0;
- }
- }
- int main( int argc, char * argv[] )
- {
- char Line[256];
- int s;
-
- do {
- printf( "\n\n请选择你要做的操作 1.文件操作 2.系统时间 3.随机数 4.数字转换字符\n\n\t\t 5.系统调用 6.退出 : " );
- scanf( "%d", &s );
- getchar();
- switch( s ) {
- case 1: openfile( s, Line);
- break;
- case 2: timeexample();
- break;
- case 3: rand1(); rand2();
- break;
- case 4: printf( "请输入一个无符号数 : " );
- scanf( "%d", &s );
- getchar();
- binary_to_ascii( s );
- break;
- case 5: callsystem();
- break;
- case 6: return 0;
- }
- } while( s != 6 );
- getch();
- }
复制代码
[ 本帖最后由 ngzyl 于 2007-1-2 15:17 编辑 ] |
|