代入以下代码,得:11,9,4,2,1,6 滑稽~
4 u: M% X2 ^. Q: J" E
G% ~& \& B9 y4 ?; V2 b#include <iostream>
( b& j& y1 ~5 k' c/ ` m5 Q#include <string.h>
( e3 [$ b) W5 d* C2 x& uusing namespace std;4 B6 }' _# f( a' }/ p; h& Z& B' S
template <class T>
9 R& I" Z- p6 R/ Hclass Node
+ A: n' ^; c, B) O{! x# ^' E- c/ H& g
public:/ C: j" v- l# i6 v( D; K/ w
T data;# _; ~" o q. ]
Node<T> *next;$ t% X& A, {. r0 R! n
Node()! P$ _& }% z v( L8 z+ t( c
{
% I& t# e! l' s3 F; A! |# o1 p this->next = NULL;3 m$ g( i$ {2 ?) e2 L2 b6 d" J# O$ F
}/ D5 X. G, ~8 U2 ~# B
Node(T data,Node<T> *next=NULL)
- j: p- \+ O8 N- T; e {
7 V3 ^4 z4 F& _' ^& q X% ^( Y this->data = data;
* a* O( [8 F' U- Y! q this->next = next;/ M o5 j$ l8 e8 G2 R+ c8 Y0 B4 ~' W
}, `8 T# y. M, ^5 M8 k
};
: c5 w) ^" g2 B$ U& v
. T. n9 o( l D% r* U0 ?template <class T>2 F8 j8 V* u- Z, ]6 e3 s$ ], G" i
class LinkedStack, S# ? d# _8 o9 V
{7 M0 ^) ]0 k& d c
private:! Y* X J% }2 J& ]
Node<T> *top;1 U- y4 j% p7 {1 z) H4 P
public:
4 s4 O* F5 V# M& D# @ LinkedStack();
" C9 G( `+ [* v* ~( l# N; M# r) ` ~LinkedStack();, N! G, j4 n+ v% P
bool isEmpty();
! E9 r5 N! d- g1 y void push(T x);' l) K. S& I9 S* E* w
T pop();
# J: {* H1 [2 M1 Y6 e; f T get();
8 j# C5 `+ t! d: P3 P% ^ D};
2 ^% ]+ B2 w6 B3 M9 r6 Y$ ?1 b; Y, d* L, X1 O
template <class T>, k7 `9 u0 B3 D: {9 m
LinkedStack<T>::LinkedStack()
+ }+ g9 k4 z V{
4 r: p! h9 v9 d* M: A; \/ h top = NULL;, r3 a( W- U% b) y' _
}! {1 |' x" ~5 J. d. ?( ], H
i- a2 M! [: }. L! w$ g: D
template <class T># @) n& Z1 l; \/ c
LinkedStack<T>::~LinkedStack() M) |' T+ E- O5 y; E" p, }& a8 _
{
% p! Y2 ^( L# R4 I( L8 N Node<T> *p = top;3 |2 r+ _6 y0 X) _: K6 y
Node<T> *q;) K2 U( O8 p1 y- d
while(p!=top)
& ~- n7 _1 B# p. i% d) U7 N, c" x {$ ~' H) O% U& U2 b
q = p;
! N7 e+ |8 g. i2 K% _2 F p = p->next;
# B/ `7 K& R* t; j& d2 [ delete p;) W( q/ \ h) W: P( y2 A
}
! B* l( s9 s. V& m. _- d top = NULL;3 u& q, _$ R" L! V9 l! t5 R( ~4 l
}+ a: R4 z4 U! y- @; C' m+ u
( T- c/ M( ?: s' t, n) etemplate <class T>2 e' J& Z7 f0 @* ^
bool LinkedStack<T>::isEmpty()
9 Z, { t u o: s6 a2 @{! \6 M( D7 e( K7 f% R5 p! S4 @* S
return top == NULL;% w: U& L: P/ w, ]6 o4 K
}
+ D: w& W7 M) I6 d' ~7 j8 j# d3 I# ^" L! x) E
template <class T>! C3 R( R% N5 p+ a R" J( l
void LinkedStack<T>::push(T x)) x) v( ^& H) r Z+ i* }
{
4 h1 M0 l( R% Q# Y top = new Node<T>(x,top);) d# r2 ?3 X5 ~" v6 D \2 Z' W7 p
}
2 h$ R/ q. v# O1 F9 D" B$ c( t* J. g7 o/ P) ?1 A! Y0 R
template <class T>9 B6 H( `5 O7 A* I% ~- N
T LinkedStack<T>::pop()
3 Y6 e, o' ~1 W& N9 e4 G{" r% Q5 S& \& e4 i
if(!isEmpty())
- S$ V( a5 W5 X. S$ T& V9 c" t {
! g' T0 f. j ]" p5 Y8 F6 G T x = top->data;
* Y G/ X; H& K) G Node<T> *p = top;
7 a' A/ c4 ~* i; c- t! e: O: X top = top->next;
$ k: X$ L& x2 M3 f' j0 I; ? delete p;
3 N! H; @3 b4 m3 v0 T0 A! E return x;
/ X5 \; j; J7 [3 ^; d5 h+ o m }7 b4 ~0 [2 N! C; M# d
throw "空栈,不能执行出栈操作";
b2 }8 E$ x0 e0 J}
* r0 d$ @4 `% z% J6 K2 v2 `2 i7 X
; S/ @5 ^, g$ T$ m3 ^template <class T>9 e" v) C; P3 R( s2 [
T LinkedStack<T>::get()
* P& u$ Y* L, A" a3 `$ T; S{
- Y# K) _7 h+ h3 }5 f$ F if(!isEmpty())% P0 |3 b* j: M1 d8 ^) V ^2 J
{
! A( `) d5 F* I' G return top->data;4 H- U: ^$ U' t3 j& d, ?
}
3 X. H8 X9 B2 V# p( W$ [ throw "空栈,不能获得栈顶元素";* X7 e% S7 r$ c5 A5 B) P0 t6 K0 _
}% D5 ]9 C8 ?7 w* O. S
. L! V. U: K& e
char * toPostfix(char *expstr)8 d9 @8 y4 H0 v5 T k, ?
{
" T; ?, N( D! g( R6 j2 ?; f8 E% b LinkedStack<char> stack;3 w2 t9 x6 M/ \4 F& S) E
char *postfix = new char[strlen(expstr)*2];
6 p& p3 _' M; s3 f" W& u3 A! V int i=0;, y: v Y* A! Q# c
int j=0;
+ E( A" N( @4 v F8 e8 n/ y8 }8 a char out ;
0 u7 E4 k/ \) g7 e while(expstr[i]!='\0')
# C( `1 p$ G+ M* @ {
& J/ {0 Q/ z6 U" R- } switch(expstr[i])
8 d# Z" _! L% { {
4 I8 d, p5 E+ @+ k case'+':
* q$ s a" W7 H1 |+ n case'-':! N! h$ y# M) R" `1 C( B
while(!stack.isEmpty()&&stack.get()!='(')
* D: U+ c, W/ B2 P3 J) I/ J {8 w! x8 s! m8 ^: l9 ~+ s
postfix[j++] = stack.pop();) `7 u) j5 W9 R; ?2 J
}
6 u, [ ]8 {" A3 U stack.push(expstr[i++]);; v( q3 h! y; t* ]# m( O
break;
, i- o( D4 D9 ~% Z5 [7 n case'*':
M+ ^0 g, G, Y1 a/ R case'/':
, c" m6 O4 z4 p while(!stack.isEmpty()&&(stack.get()=='*'||stack.get()=='/'))% H& I! ]8 F3 W' _: e. x; C
{" Y1 v5 C$ i. Q- p1 ?0 E& u, @1 T
postfix[j++] = stack.pop();- j0 q1 s" f, X u4 e* Q4 U6 I
}) @) M; Q* A* x- g# A; h* j
stack.push(expstr[i++]);! I3 R+ w' f; \ [4 [7 w3 u
break;6 O( ~* B# j! @9 R c) p0 N! D+ @: u
case'(':stack.push(expstr[i++]);: z) M5 y; N8 I. j; i
break;
2 {% X: E2 w5 E8 e9 m5 q case')':out = stack.pop();2 Q& C! K, M7 Q1 h3 ^ }) o
while(!stack.isEmpty()&&out!='(')+ S) @5 a6 E8 m% E; C
{$ Z5 U" t' w/ m; z, {
postfix[j++] = out;/ y& h; t0 w: I- h
out = stack.pop();& Q2 P5 L6 h( j; ]: g
}: A& J) t/ T) d
i++;
9 s$ V s( R, ]) }) I% t; c5 K o; z2 \ break;! X* h& |* v2 K6 h; g
default:
/ n# i+ I: h7 f' ]7 Q" E/ w while(expstr[i]>='0'&&expstr[i]<='9'&&expstr[i]!='\0')) N# \4 d n/ g5 l0 i5 D5 y/ X2 ]
{
6 t9 x' v: |5 p/ o/ E postfix[j++] = expstr[i++];. b6 y# A- Q/ G) S9 Q! g! F
}; B. I2 q+ f0 u3 B; k! |0 `& m B
postfix[j++]=' ';4 z$ ?9 h& \6 y3 h
break;+ G2 K7 K$ F4 s( h
}8 ^: q9 ^1 Q3 [7 g0 i& i
}
9 o" P1 [, l, p `" ]# L) R while(!stack.isEmpty())1 z- @0 i, V4 L6 H. M4 i
{
5 O# u& \" Q( @ postfix[j++]=stack.pop();4 w& C; t& g6 p- `
}
% @3 | o* `+ `; Y$ t! h$ k postfix[j]='\0';
+ T) G% X/ l9 x. k( r9 c6 W return postfix;
# W: n- n+ m }8 }}- A2 F/ p4 S2 H5 T( \7 e3 w( ~5 \
9 |4 o$ X( x, b# N3 rint value(char *postfix)" E1 B# `* M: X( Z) [# N
{: y- N* y8 |0 V( \; g5 k3 V+ Y0 M
LinkedStack<int> stack;
( H: y* `; y& g* Z& u int i=0;( l9 y" I: X( T) y
int result = 0;" q* I& u( S v7 }; x( z+ l$ l
while(postfix[i]!='\0')2 m0 |2 g) F3 n1 D. Q+ f
{
3 e& M) J2 V' |1 [ if(postfix[i]>='0'&&postfix[i]<='9')
. F6 `8 M0 u( x+ E* Y8 z0 N {8 U7 J2 h x4 [! P; L
result = 0;3 B. \( h& {* _- o' g
while(postfix[i]!=' ')
* |9 Y$ R3 p5 k0 g6 m {$ X" h7 _; J% d- w9 B* V
result = result*10+postfix[i++]-'0';
, ?. ]; W2 z- D; O }
8 i6 x/ @+ c/ P. L) I i++;* ]0 Q# c3 h" E- R6 [
stack.push(result);
) {: Y6 r& _+ n1 V* [& y4 w }. L' T' y0 ]1 s! U% q
else f& K! P- T6 Q9 H$ k
{! A. ^" T% q8 E. k: w, {
if(postfix[i]!=' ')
- x, Z% n5 g Y. Q3 s {+ c' [! R; |( D1 r8 f) G$ H
int y = stack.pop();2 K$ C/ u+ ?" z7 S, F
int x = stack.pop();9 t; f5 ~* a5 x; I: a7 Y: F. h
switch(postfix[i])
+ [+ Q0 `! g" q' D+ O4 Y' o {# y% p; s5 ?, C- E, k4 i. x2 S
case'+':result = x + y;
6 {$ k: u: a2 Q* W9 e; T+ ? break;' i. }: E7 s3 O( K, B
case'-':result = x - y;
' h8 `6 Q' c0 V* W break;: q# n' O2 c; H. t
case'*':result = x *y;
: L0 n. [ \. d6 M% X( u, s, E break;
1 j) ]# K/ b* ?9 p case'/':result = x / y;% _+ x. P8 ?9 @" P2 V, r
break;+ e" a" b3 A6 \% k3 a v
}
- X B9 [2 e4 ^3 N1 _8 p stack.push(result);
" ?. `+ F) H* H! Y5 a9 m }
; W9 W- b x7 F( z i++;" i' ?) V9 u! E x8 h6 w( d
}
- e& j6 l$ p R: B- e- N( M }
* Y) g) ?+ P; v$ N! a return stack.pop();
2 x/ E1 H( ^2 e8 j$ ]}
' o4 F6 f- v- M9 K9 \1 R: r% ~* L( s( g- B0 A; B: ]: o
int main()
7 T5 p0 d" }- W{$ ?/ k" I: A, b- S0 \
//char *expstr = "121+10*(52-49+20)/((35-25)*2+10)";+ P! x9 L2 i: r: d/ a
cout << "请输入表达式:";9 ^5 o- ^6 B$ w8 w n h
//char *a ;
1 x0 T8 u# {" t) M) R/ E //cin >> *a;8 l# p9 W$ `$ W) t* P
char expstr[20]={0};
1 K* R8 m: X; N( h6 [9 [" E while(1)
/ E' l; t2 r/ j9 V+ S9 \6 d b5 U {
* s. v5 o% s E4 _7 M" R cin>>expstr;
2 n' k& s5 ]" l+ r char *postfix = toPostfix(expstr);- Y# t4 Y/ X3 y& O1 m1 L$ V
cout << "expstr= "<<expstr << endl;, a0 V0 Z4 A7 O: a3 t1 b
cout << "postfix= "<<postfix<<endl;7 S) E- A+ M0 Q2 f6 P/ N! B
cout << "value= "<<value(postfix) << endl;
: i$ m7 O6 w! h5 L }
* S4 A# x6 y+ t# d- j& Y return 0;2 M& D' S) t5 b' {+ c
} |