随笔 - 62  文章 - 257  trackbacks - 0
<2006年9月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

I Love Programming & Music.... CS Became CSed....

常用链接

留言簿(7)

随笔分类(64)

随笔档案(62)

文章分类(11)

文章档案(11)

相册

BlOoD

FriEnds

搞起的人们

搜索

  •  

积分与排名

  • 积分 - 114333
  • 排名 - 216

最新评论

阅读排行榜

评论排行榜

    中缀表达式转后缀表达式……

/*
test case:
2*(3+4)-8/2
(2*3+(2+5*2-10))/(8/2)
15+10*(50*2-5)/2
15+10*(50*2-5)/3
1.5*2.3*(3.7-2.13*3.2)-8.0
*/

#include 
<  stdio.h  >
#include 
<   string .h  >
#define  N 1000

char  b[N],c,stack[ 100 ];
int  bn,sn,isnum,endflag = 0 ;

void  midtoback()
{
    bn
= 0 ;
    sn
=- 1 ;
    isnum
= 0 ;
    
while ((c = getchar()) != EOF && c != ' \n ' )
    
{
        
switch (c)
        
{
        
case   ' ( ' :
            
if (isnum == 1 ) b[bn ++ ] = ' , ' ;
            isnum
= 2 ;
            stack[
++ sn] = ' ( ' ;
            
break ;
        
case   ' ) ' :
            
if (isnum == 1 ) b[bn ++ ] = ' , ' ;
            isnum
= 2 ;
            
while (sn >= 0 && stack[sn] != ' ( ' )
            
{
                b[bn
++ ] = stack[sn -- ];
                stack[sn
+ 1 ] = 0 ;
            }

            stack[sn
-- ] = 0 ;
            
break ;
        
case   ' + ' :
            
if (isnum == 1 ) b[bn ++ ] = ' , ' ;
            isnum
= 2 ;
            
while (sn >= 0 && stack[sn] != ' ( ' )
            
{
                b[bn
++ ] = stack[sn -- ];
                stack[sn
+ 1 ] = 0 ;
            }

            stack[
++ sn] = ' + ' ;
            
break ;
        
case   ' - ' :
            
if (isnum == 1 ) b[bn ++ ] = ' , ' ;
            isnum
= 2 ;
            
while (sn >= 0 && stack[sn] != ' ( ' )
            
{
                b[bn
++ ] = stack[sn -- ];
                stack[sn
+ 1 ] = 0 ;
            }

            stack[
++ sn] = ' - ' ;
            
break ;
        
case   ' * ' :
            
if (isnum == 1 ) b[bn ++ ] = ' , ' ;
            isnum
= 2 ;
            
while (sn >= 0 && stack[sn] != ' ( ' && stack[sn] != ' + ' && stack[sn] != ' - ' )
            
{
                b[bn
++ ] = stack[sn -- ];
                stack[sn
+ 1 ] = 0 ;
            }

            stack[
++ sn] = ' * ' ;
            
break ;
        
case   ' / ' :
            
if (isnum == 1 ) b[bn ++ ] = ' , ' ;
            isnum
= 2 ;
            
while (sn >= 0 && stack[sn] != ' ( ' && stack[sn] != ' + ' && stack[sn] != ' - ' )
            
{
                b[bn
++ ] = stack[sn -- ];
                stack[sn
+ 1 ] = 0 ;
            }

            stack[
++ sn] = ' / ' ;
            
break ;
        
default :
            isnum
= 1 ;
            b[bn
++ ] = c;
            
break ;
        }

    }

    
while (sn >= 0 )
        b[bn
++ ] = stack[sn -- ];
    
if (c != ' \n ' ) endflag = 1 ;
}


int  main()
{
    
while (endflag == 0 )
    
{
        memset(stack,
0 , sizeof (stack));
        memset(b,
0 , sizeof (b));
        midtoback();
        printf(
" %s\n " ,b);
    }

    
return   0 ;
}

        
posted on 2006-10-27 00:05 Asp 阅读(2521) 评论(0)  编辑 收藏 引用 所属分类: Ar!thmEt!c.Self

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理