Chinaunix首页 | 论坛 | 博客
  • 博客访问: 128638
  • 博文数量: 55
  • 博客积分: 1870
  • 博客等级: 上尉
  • 技术积分: 540
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-21 20:51
文章分类

全部博文(55)

文章存档

2011年(27)

2009年(3)

2008年(25)

我的朋友

分类: LINUX

2008-04-22 10:26:32

/*
 * =====================================================================================
 *
 * Filename: t_str_m.c
 *
 * Descriptian: hello world China <--> China world hello
 *
 * Version: 1.0
 * Created: 04/21/08 20:25:55
 * Revision: 0.0.1
 * Compiler: gcc
 *
 * Author: Engelbert
 * Company: WAVETEK
 *
 * =====================================================================================
 */




/*-----------------------------------------------------------------------------
 * Include
 *-----------------------------------------------------------------------------*/

#include    <stdio.h>
#include    <string.h>
#include    <stdlib.h>

/*-----------------------------------------------------------------------------
 * Macro
 *-----------------------------------------------------------------------------*/

#define MAXVAL 80

/*-----------------------------------------------------------------------------
 * Variable
 *-----------------------------------------------------------------------------*/

char *value[MAXVAL];
int sp=0;
/*-----------------------------------------------------------------------------
 * Function
 *-----------------------------------------------------------------------------*/

void push (char *f);
char * pop ( void );

/*
 * === FUNCTION ======================================================================
 * Name: main
 * Description:
 * =====================================================================================
 */

int main ( int argc, char *argv[] )
{
    char *buf;
    char *delim=" \n";
    char *p;
    int i=0;
    int num=0;
    
    buf    = (char*)malloc ( sizeof(char) * 80 );

    if ( buf==NULL ) {
        fprintf ( stderr, "\ndynamic memory allocation failed\n" );
        exit (EXIT_FAILURE);
    }

    fgets(buf,80,stdin);
 
    p=strtok(buf,delim);

    while(p != NULL){
        push(p);

        p=strtok(NULL,delim);
        
        num++;
    }

    for(i=0;i<num;i++){
        printf ( "%s ", pop() );
    }

    free (buf);

    return EXIT_SUCCESS;
}                /* ---------- end of function main ---------- */


/*
 * === FUNCTION ======================================================================
 * Name: pop
 * Description: stack pop ,return stack value
 * =====================================================================================
 */

char * pop ( void )
{
    if(sp > 0)
        return (value[--sp]);
    else {
        printf ( "error:stack empty\n" );
        exit(1);
    }
}        /* ----- end of function pop ----- */

/*
 * === FUNCTION ======================================================================
 * Name: push
 * Description:
 * =====================================================================================
 */

void push (char *f)
{

    if ( sp < MAXVAL ) {
        value[sp++] = f;
    }        
    else
        printf ( "error:stack full,can't push %s\n", f );

}        /* ----- end of function push ----- */

阅读(594) | 评论(0) | 转发(0) |
0

上一篇:阻抗匹配的理解

下一篇:字符串逆序

给主人留下些什么吧!~~