随笔 - 62  文章 - 257  trackbacks - 0
<2024年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

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

常用链接

留言簿(7)

随笔分类(64)

随笔档案(62)

文章分类(11)

文章档案(11)

相册

BlOoD

FriEnds

搞起的人们

搜索

  •  

积分与排名

  • 积分 - 114717
  • 排名 - 216

最新评论

阅读排行榜

评论排行榜

    用double写的中值表达式的计值,可以算小数的……

/*
test case:
2*(3+4)-8/2
2*(3.25+4.75)-8/2
(2*3+(2+5*2-10))/(8/2)
15+10*(50*2-5)/2
15+10*(50*2-5)/3
15+10*(5.5*2-5)/2
output:
10
12
2
490
331.667
45
*/

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

#define  N 1000

char  c,ostack[N];
int  n =- 1 ,o,isnum = 0 ,isfraction = 0 ,endflag;
double  nstack[N],frapoint = 0 ;

void  count()
{
    
while ((c = getchar()) != EOF && c != ' \n ' )
    
{
        
switch (c)
        
{
        
case   ' ( ' :
            isnum
= isfraction = 0 ;
            ostack[
++ o] = ' ( ' ;
            
break ;
        
case   ' ) ' :
            isnum
= 0 ;
            
while (ostack[o] != ' ( ' )
            
{
                
if (o >= 0 && ostack[o] == ' + ' ) nstack[n - 1 ] = nstack[n - 1 ] + nstack[n];
                
else   if (o >= 0 && ostack[o] == ' - ' ) nstack[n - 1 ] = nstack[n - 1 ] - nstack[n];
                
else   if (o >= 0 && ostack[o] == ' * ' ) nstack[n - 1 ] = nstack[n - 1 ] * nstack[n];
                
else   if (o >= 0 && ostack[o] == ' / ' ) nstack[n - 1 ] = nstack[n - 1 ] / nstack[n];
                nstack[n
-- ] = 0 ;
                ostack[o
-- ] = 0 ;
            }

            ostack[o
-- ] = 0 ;
            
break ;
        
case   ' * ' :
            isnum
= isfraction = 0 ;
            
if (o >= 0 && ostack[o] == ' * ' {nstack[n - 1 ] = nstack[n - 1 ] * nstack[n];nstack[n -- ] = 0 ;}
            
else   if (o >= 0 && ostack[o] == ' / ' {nstack[n - 1 ] = nstack[n - 1 ] / nstack[n];nstack[n -- ] = 0 ;}
            
if (o >= 0 && (ostack[o] == ' + ' || ostack[o] == ' - ' || ostack[o] == ' ( ' )) ostack[ ++ o] = ' * ' ;
            
else  ostack[o] = ' * ' ;
            
break ;
        
case   ' / ' :
            isnum
= isfraction = 0 ;
            
if (o >= 0 && ostack[o] == ' * ' {nstack[n - 1 ] = nstack[n - 1 ] * nstack[n];nstack[n -- ] = 0 ;}
            
else   if (o >= 0 && ostack[o] == ' / ' {nstack[n - 1 ] = nstack[n - 1 ] / nstack[n];nstack[n -- ] = 0 ;}
            
if (o >= 0 && (ostack[o] == ' + ' || ostack[o] == ' - ' || ostack[o] == ' ( ' )) ostack[ ++ o] = ' / ' ;
            
else  ostack[o] = ' / ' ;
            
break ;
        
case   ' + ' :
            isnum
= isfraction = 0 ;
            
if (o >= 0 && ostack[o] == ' * ' {nstack[n - 1 ] = nstack[n - 1 ] * nstack[n];nstack[n -- ] = 0 ;}
            
else  
                
if (o >= 0 && ostack[o] == ' / ' {nstack[n - 1 ] = nstack[n - 1 ] /- nstack[n];nstack[n -- ] = 0 ;}
                
else  
                    
if (o >= 0 && ostack[o] == ' + ' {nstack[n - 1 ] = nstack[n - 1 ] + nstack[n];nstack[n -- ] = 0 ;}
                    
else  
                        
if (o >= 0 && ostack[o] == ' - ' {nstack[n - 1 ] = nstack[n - 1 ] - nstack[n];nstack[n -- ] = 0 ;}
            
if (o >= 0 && ostack[o] == ' ( ' ) ostack[ ++ o] = ' + ' ;
            
else  ostack[o] = ' + ' ;
            
break ;
        
case   ' - ' :
            isnum
= isfraction = 0 ;
            
if (o >= 0 && ostack[o] == ' * ' {nstack[n - 1 ] = nstack[n - 1 ] * nstack[n];nstack[n -- ] = 0 ;}
            
else  
                
if (o >= 0 && ostack[o] == ' / ' {nstack[n - 1 ] = nstack[n - 1 ] / nstack[n];nstack[n -- ] = 0 ;}
                
else  
                    
if (o >= 0 && ostack[o] == ' + ' {nstack[n - 1 ] = nstack[n - 1 ] + nstack[n];nstack[n -- ] = 0 ;}
                    
else
                        
if (o >= 0 && ostack[o] == ' - ' {nstack[n - 1 ] = nstack[n - 1 ] - nstack[n];nstack[n -- ] = 0 ;}
            
if (o >= 0 && ostack[o] == ' ( ' ) ostack[ ++ o] = ' - ' ;
            
else  ostack[o] = ' - ' ;
            
break ;
        
case   ' . ' :
            isfraction
= 1 ;
            frapoint
= 0.1 ;
            
break ;
        
default :
            
if (isnum && isfraction == 0 ) nstack[n] = nstack[n] * 10 + c - ' 0 ' ;
            
else   if (isnum && isfraction == 1 )
            
{
                nstack[n]
= nstack[n] + (c - ' 0 ' ) * frapoint;
                frapoint
/= 10 ;
            }

            
else
                nstack[
++ n] = c - ' 0 ' ;
            isnum
= 1 ;
        }

    }

    
while (n >= 1 )
    
{
        
if (ostack[o] == ' + ' ) nstack[n - 1 ] = nstack[n - 1 ] + nstack[n];
        
else   if (ostack[o] == ' - ' ) nstack[n - 1 ] = nstack[n - 1 ] - nstack[n];
        
else   if (ostack[o] == ' * ' ) nstack[n - 1 ] = nstack[n - 1 ] * nstack[n];
        
else   if (ostack[o] == ' / ' ) nstack[n - 1 ] = nstack[n - 1 ] / nstack[n];
        nstack[n
-- ] = 0 ;
        ostack[o
-- ] = 0 ;
    }

    
if (c != ' \n ' ) endflag = 1 ;
}


int  main()
{
    
while (endflag == 0 )
    
{
        memset(nstack,
0 , sizeof (nstack));
        memset(ostack,
0 , sizeof (ostack));
        n
=- 1 ;
        o
= isnum = 0 ;
        c
= ' \0 ' ;
        count();
        printf(
" %g\n " ,nstack[ 0 ]);
    }

    
return   0 ;
}

posted on 2006-10-25 20:10 Asp 阅读(604) 评论(3)  编辑 收藏 引用 所属分类: Ar!thmEt!c.Self

FeedBack:
# re: 中值表达式的计值 2006-10-26 23:34 Asp
原来只能输入整数,现在改好了,可以输入小数了,不过精度实在有限哦……  回复  更多评论
  
# re: 中值表达式的计值 2006-11-06 23:20 pengkuny
我猜你在读大二吧,我大三,建议你学学C++,比C表达要简洁多了  回复  更多评论
  
# re: 中值表达式的计值 2006-11-06 23:31 Asp
恩,谢谢……
我确实是在读大二……  回复  更多评论
  

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