中缀表达式转后缀表达式……
/**/
/*
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