#include "stdlib.h"
8 y0 D. }) u% k8 G* P#include "string.h"
' [( |' o. W, L3 Z; G#include "stdio.h" #define MAXSIZE 400
8 k+ Y1 Y- R% |. f% o0 ~- ychar a[MAXSIZE]; typedef struct
3 T( M& C5 S8 j{2 F( o: s1 z; r- [; ?0 R
char elem[MAXSIZE]; H3 d: ~' H! n9 ?
int top;4 p4 O/ }, `. r J H4 {. \0 G
}SqStack; SqStack S; typedef struct poly
. n( r3 b7 i: n/ ?+ }: N5 l2 O8 X2 w{8 ?* k( X. X! [
int coef; /*xishu*/% }7 z4 P% g. w+ B4 Z
int exp; /*zhishu*/
! z: O/ } T, S# e- ]7 \. x struct poly *next;- }+ V7 H' q2 e
}Lpoly; Lpoly *pa; int cf(int i) /*10^i*/* Z7 _6 R! b7 C$ y1 M, W
{% C3 Y- A# r( _0 @4 u n
int c = 1, j;; W( ]* v) {# d4 ?! R/ n! g
for(j = 1; j <= i; j ++)* \6 `' }1 P) I6 _0 i
c = c * 10;
" z' z) y0 ?, r" j( O return c;3 U y8 t- s% U0 w5 ?0 O9 h6 e
} void input(char a[],int j)/ g" s0 i2 g! D% w N; ]
{1 Y6 d5 e# Q5 g! g. H9 s
Lpoly *p, *h, *ptr;
; x" S' L a6 o" Q$ k int i, k, flag = 0, x = 0;# Z3 r" B: T/ g) P
S.top = -1;4 ^1 L1 H6 |9 Z' y7 ?' \
pa = (Lpoly*)malloc(sizeof(Lpoly));: o# Y. M/ e) B; F! u& p) ?" y3 D9 I
pa->coef = 0;
4 A: `* y7 {8 |, m- e1 A" i pa->exp = 0;
& P0 o- P" x9 R# ^8 ]1 x8 P2 b h = pa;
& C) F/ B* @ x: ]" _ h->next = NULL;4 @" P" D; P3 g+ m8 P
p = h; /*jianlipa*/
2 h1 B, o( ]# u
7 ]* {2 h8 O% r; I4 b0 B1 | for (i = 0; i <= j - 1; i ++)3 ?* e8 G/ h7 d& Z E# j! q
{+ _) [1 Y. I8 h/ r
ptr = (Lpoly*)malloc(sizeof(Lpoly));
0 X! d+ D& ]! \) P$ r1 p if(S.top)
( T. ?- A% s, X( q- L8 \$ p {
5 t x3 ^7 v ^/ K5 U) t S.top ++;
. R6 M; I d- f4 k( E S.elem[S.top] = a; q ^$ F" T3 Y4 \
}
1 e6 F) [+ I0 ? if (a == '-')
6 v/ v! ?7 B) F& T: N" q flag=1;
8 W# F- w3 q1 g0 I! l+ n/ E
6 R o. u/ y+ [ if(a == 'x') /*fenlixishu*/
6 _5 j( Q/ Z! F4 ]$ O {
/ F. K& R5 w; [ x# V, E! J: j9 Z if (S.top==0)3 _0 @% C1 U3 z7 o! R/ A6 D
{$ [: z0 g; \2 Q6 P9 r
if (flag == 1)
% p- R; W/ i5 p2 D2 I1 ~ {6 ~3 v) _/ h1 E7 G. y2 l
ptr->coef = -1;& s# I; [; {0 ?) q$ ~
ptr->next = NULL;1 [/ _ N* e2 T! P+ B( i' i
}
! c# S0 v4 H5 J9 F1 M7 G3 [+ C else1 y( h5 @1 _( K2 G
{# j+ d0 e4 E. I' K) @: D2 N
ptr->coef = 1;0 S- r" t" N$ P
ptr->next = NULL;
2 Q- }3 [1 P' M/ y }
7 n- a0 F) D& \$ E. d& @) f }
' W D9 f9 d" `: o0 M, [( Y& E else
8 G5 F7 d& M) T/ W# S8 w# r {
0 D- g/ c% y: R, x P+ T B* T for (k=S.top - 1; k >= 0; k --)! D+ e* q) O$ a# _5 I( Y# g
x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);
7 ^2 H3 e+ i* S9 } ptr->coef = x;+ u8 @. @) j4 ^+ l
ptr->next = NULL;' e! v) V! Y/ @$ S3 w8 Q
x = 0;
- V* s. {- L4 o# u7 s: t8 O1 B }
p$ `9 G9 x7 N3 U S.top = -1;
% v: G, B% N# u3 l* b) C flag = 0; /*clear*/! ]/ B! u' A. G6 v% K% t
}
|- Q* G) [4 C8 c# {. s if (a == '+' || a == '-') /*fenlizhishu*/
) e( E+ j/ ]* b {) B, `& q s- z. r
if(S.top == 0)1 @7 A* f! ]1 r' V) F' W
{- N- V. ?3 O% s6 Q7 }) b4 m
ptr->exp = 1;1 h& A) i0 r7 J
ptr->next = NULL;, H6 K0 Q, i% ^
}
1 _* d! i" |' ? else
, X1 [- J% w: b {
' E# Q0 ?+ P- `+ [# }( y5 ~* c for (k = S.top - 1; k >= 0; k --)% W5 p+ L9 O6 ^' C) X/ P
x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);
3 z; M( _) H2 V: I ptr->exp = x;
3 d; @" K, y4 ~: b+ ` ptr->next = NULL;8 U+ S, l+ @+ t7 d( q
x = 0;7 K( {: f9 c z" Y. k/ E
}/*tiquzhishu*/% ~6 P$ W4 j" q
S.top = -1;
3 f+ a3 B9 ?' F. [ }1 }9 D" ~* [* Q" ~! V
if (ptr->coef != 0 && ptr->exp != 0)! }' i0 [5 b: B4 f v
{
L8 Y2 h5 k) n/ d1 Y p->next = ptr;
E9 g, {+ E. X, g" I. K- ] p = ptr;# h i( s% J6 ~2 r
}) C+ S3 E/ j* S' z: A) \9 V& B
if (i == j - 1)0 u6 S" u, C8 v
{
; N* P7 x1 M' ~: t0 E8 m4 s S* h if (ptr->coef == 0)2 A, g( B& e: Z
{$ g) h$ K! p0 z- s3 P
for (k = S.top; k >= 0; k --)
& g, j" L9 f5 d) B3 }7 y x = x + (S.elem[k] - '0') * cf(S.top - k);( a1 F. e# G) q, x$ M
ptr->coef = x;
; H& x+ g6 \! J9 Z0 y ptr->next = NULL;% M* H5 _9 Y. K
}3 T' X/ I Y3 r0 ^3 ~# X
else
: X, S' G5 X& _ { J' L: X1 k8 m0 Z$ [- a0 p
for (k = S.top; k >= 0; k --)
. i/ t; \5 X# m: J% O& k N x = x + (S.elem[k] - '0') * cf(S.top - k);0 t V0 y8 [/ m2 U/ x
ptr->exp = x;( D3 i4 S" c$ L( q( i1 D# ] E
ptr->next = NULL;
3 h5 Z3 x z8 Q" \3 y0 d }
" f3 o" C7 E" I5 x# w9 v, Y p->next = ptr;
K, \- P- R! q( X" n8 z: F0 _& Q4 g7 v ptr->next = NULL;
3 D4 U: C/ v- c! {$ ] }9 V- d, B _4 p7 }; U
}
* [2 [7 f) m7 |; G- m+ @} void stinsort(Lpoly *pa)
N2 _ N* ]. V! b4 \" F. l8 ?5 {8 e0 H{2 c0 c) B5 M, r6 @
Lpoly *p, *h, *q, *k;. b4 g: j4 R. |: ^0 ]9 G
h = pa;0 ^' g: l3 `+ | q* [+ h
p = k = h->next;
4 q! ?; x# A: N' L7 M. z q = p->next;% F1 R+ T- _ k
while (p->next != NULL)
0 g7 R$ ^! Y9 c& ^ {2 A3 C( i: e0 q
if (q->exp)
* j- w* p% w! u o7 F( N {1 W0 r: w$ A1 S6 X
while(q->exp > k->exp)4 q d# L6 f( y' C# n
{
9 r5 P7 a3 t. e& u k = k->next;6 V) ~2 n0 l8 M$ o
h = h->next;
5 O- ^! a* j2 C* S }& e* b( H3 Z4 u. b8 R: z- i
if(q->exp)
4 l' Z: ?3 B) d% b% I6 \/ t {
! }; z, j- B* `3 _4 c" ~ p->next = q->next;% R2 v' K9 p( h0 l8 C
h->next = q;
( D0 J* I8 x) U q->next = k;
7 {6 w; S$ D/ S4 o% x q = p->next;6 S& n* ?5 I8 Z4 h
}' F1 Z" h; M/ h, z
if (q->exp = k->exp)/ `5 ^4 s) m- M p6 w/ b
{$ l! K9 Y5 I$ ]! c
k->coef = k->coef + q->coef;6 L% b& _5 @' Z0 z. j9 n* U
p->next = q->next;1 j2 L) B0 P: f4 [4 X
q = p->next;
' [- {% P( [8 ^* a }7 K+ a) c; e: U
h=pa;
* C) n' {7 b1 Y7 s k = h->next;- w/ k9 J+ L; Z0 ?
}
5 s1 a4 `5 x' |0 L, e
9 Z! C G& @/ ~ if (q->exp = p->exp); F5 j! |7 s/ E5 B
{
: w. }6 @, J. l' k. R- P: {0 @3 u p->coef = p->coef + q->coef;9 H- ~. f& C! i2 s5 H2 l
p->next = q->next;
% I" f" {5 d( F6 y( _ q = p->next;
0 A5 o/ |3 |' x6 v$ b- j* D }
( [* P0 l. ` k+ E m: A if (q->exp > p->exp)) |. ^, I4 ?. O" r5 \
{7 o+ R; X$ `4 U# `3 u
q = q->next;
1 e: l2 _+ I I; i p = p->next;
: R, S, I- d2 r( l7 Z3 } }" K4 W1 Z% \4 [# u
}
. p; a: P% A: B/ E+ b} main()
8 G+ y) x& _! O- C3 _) @' N# @" J{
$ W' ]0 f- H1 C8 y$ l1 I. P int i, j = 0;
- n5 \2 s: q3 Y; W: D printf("a=");& ^4 d. F: k! F6 w
scanf("%s", a);! M7 z5 d4 N/ y# l' O
j = strlen(a); input(a, j);2 q/ O+ e: q, \3 D$ I
stinsort(pa);, M P) [3 k9 `
printf("a=");
+ N! Z, X6 t" B% l5 | for (i = 0; i <= j + 10; i ++) w3 H. B& u/ E" P
{
& X5 j8 d! _& J# y' z printf("%dx%d+", pa->coef, pa->exp);" N" G4 t$ m) r$ E0 Q, l. b
pa = pa->next;
* X; N8 t& N% L% C$ l" l } S5 M# L0 H/ A0 j1 C- N( `- z3 [1 U7 V+ b
}
9 A) L- o( F# Z/ n! T9 P 我把楼猪的代码弄了下,把里面大小写错误排除了,这段代码会有死循环,谁有空谁研究去。 |