用double写的中值表达式的计值,可以算小数的……
/*Sample Test Cases*/
/*
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 阅读(601)
评论(3) 编辑 收藏 引用 所属分类:
Ar!thmEt!c.Self