#include "stdlib.h". K7 t4 I0 ] n& ^+ w
#include "string.h"; \9 G5 u* b9 C) q) N
#include "stdio.h" #define MAXSIZE 400
- @) K2 I" p: G0 a& Xchar a[MAXSIZE]; typedef struct6 p7 P7 X- z( h5 y4 L
{
H5 M, c- F, A" t+ A% }) D char elem[MAXSIZE];! p" X& ?% B- f( ?5 \& }
int top;9 ^3 }! S' D3 [
}SqStack; SqStack S; typedef struct poly% M5 x: P9 [0 i3 {; L$ o% R
{2 z$ ]. `% ~2 E; S7 o' G; L" u8 ~1 Z
int coef; /*xishu*/# \9 H$ C8 g h+ J c
int exp; /*zhishu*/
, N% J, X# b6 G struct poly *next;; C- z) l/ e$ s: ~; W2 F
}Lpoly; Lpoly *pa; int cf(int i) /*10^i*/
! u; Y' @! H8 _{+ E" v F) ^! g9 s* b3 L
int c = 1, j;( h4 ~, _) y. L7 |9 M
for(j = 1; j <= i; j ++)! t* q& \% S' H
c = c * 10;. N" e4 C7 X3 e& z" ^' n9 d, _
return c;
/ h2 j8 v! D7 b* c9 F8 K# {* n} void input(char a[],int j)* F( g; F! d9 B+ q
{0 |* {& Y' E2 F- u: d3 f$ r
Lpoly *p, *h, *ptr;
3 v0 z1 U5 T' D: J$ [. Y/ A int i, k, flag = 0, x = 0;; p7 ?, |6 W6 e* Z( k, j
S.top = -1;% [8 \* C [3 P( D6 T3 S
pa = (Lpoly*)malloc(sizeof(Lpoly));
) E4 X3 K* [2 T2 t! Y6 N9 } pa->coef = 0;
, h+ ]4 c- _, N. g9 E pa->exp = 0;& {' X4 R/ v9 u
h = pa;
3 d; u' [0 D; R h->next = NULL;
, ~8 N9 N$ E2 @! e& t. ? p = h; /*jianlipa*/& p* F/ |# Y) P0 L8 o4 I7 {
% B5 W% Y1 g. Z for (i = 0; i <= j - 1; i ++)
1 [, T; |! d& p0 _+ { {
) I! C! F* X4 J' ]" q ptr = (Lpoly*)malloc(sizeof(Lpoly));
# B- y6 _! s1 L$ ?3 {2 O if(S.top)
% D$ ?9 N6 j5 s3 \ {
. f& W( p( R6 E S.top ++;
& x) e2 k ^' A S.elem[S.top] = a;# |5 A' Q. L. T( {4 B* c
}+ L# h+ S4 e9 v
if (a == '-'), u4 \2 Y! k3 t1 P! H; R
flag=1;
6 A; D9 O$ j1 H+ g& {2 V' ? 2 A( E* f2 K" c( ]4 Q2 J
if(a == 'x') /*fenlixishu*/5 t* H- x2 k, U# X/ l
{/ T2 t8 [) h( B8 N* Y6 o: W
if (S.top==0)
0 ?4 W5 c [0 f4 X {% @+ E- V2 \: L3 ?. E
if (flag == 1)7 e3 C: X! s, ~
{
( ^4 C% i) Q4 }; L; w ptr->coef = -1;
9 C3 R, `% l8 s* Q" ^ ptr->next = NULL;. e( _# L0 e# a. j; ]
}! |' i; _) d8 N6 A- z/ V+ v
else
* ~& o6 m; J( ^( y {
; ?# Y9 u) p- r& M ptr->coef = 1;. z( }, b* I$ E# z
ptr->next = NULL;6 e! n `; w8 _7 S( n; _, U
} {% s: v9 n" b7 A6 |" ^
}# U: ?# L- P1 r, ~( g& W% t
else* \' o7 N" I9 `$ X* i8 R* }5 |4 x" m
{! {1 y, G3 C# l$ G8 o8 b: b5 c
for (k=S.top - 1; k >= 0; k --)4 v! M7 u/ q8 R: K8 s
x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);
7 y: w( C- T/ Z# d# U1 r5 r' R8 O/ [ ptr->coef = x;4 [, h9 c x% E% W& B( Z5 Z6 P+ q
ptr->next = NULL;
! s- W* K# s& F `# O0 x x = 0;
, _3 O3 b' W: S- u }! x6 W" p# V% q8 B0 h; v
S.top = -1;' G, J' t, A. C ~1 ^) ^4 C- |, Q
flag = 0; /*clear*/
* Q+ [: t `4 t2 v' C$ m }
3 d) y; D6 k$ O S if (a == '+' || a == '-') /*fenlizhishu*/
' o9 f5 A8 x1 H( q2 ~3 h {
3 F1 a3 k' n7 k! H, W! z1 ?- S if(S.top == 0)
+ e7 v+ F, s- r {
6 q, {8 B. W0 E# N2 Z8 x! D6 ^ ptr->exp = 1;
$ {6 d, B% v$ X. s) s ptr->next = NULL;! _/ b6 ]9 Y: w, F6 l+ L
}+ d8 I- ]0 D3 Y6 K7 S- T/ W! F* G
else) S2 g& d" A+ X5 ~% L- z- I
{
; e8 w3 n& b2 A for (k = S.top - 1; k >= 0; k --)
$ n! @4 ^6 I5 @% }, r( s) ` x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);" [1 R$ x) v2 ]1 P
ptr->exp = x;% B8 b; L) h+ T9 t3 m' h- `
ptr->next = NULL;" T! J$ Y! A+ Q& s) O- D6 v
x = 0;
% n- ]% d n- |) O }/*tiquzhishu*/
7 G N5 A9 W, u S.top = -1;+ I/ }$ Y! F9 a. X( s* m
}
& T4 _9 ^) _7 [6 J8 |& v4 q! i5 M7 z if (ptr->coef != 0 && ptr->exp != 0)& V- v: W9 d( F* _; q8 d
{! | F. x$ i0 j' l7 p% U& k+ \( f; ?
p->next = ptr;
0 x/ n B# h4 V p = ptr;
. A) _$ v5 f" v4 o" w6 h }# L* W" {, B5 K7 N- H
if (i == j - 1)
! X1 e5 a- ?2 w0 K {1 W# C9 z6 |1 D) v
if (ptr->coef == 0)' W* m V% p$ i% ?
{
) o4 z/ C' t3 q+ I' }; |. ?8 A for (k = S.top; k >= 0; k --)
( V0 d# X' R! a# P1 E% T x = x + (S.elem[k] - '0') * cf(S.top - k);
( R/ ?8 d. {& F+ Z$ T ptr->coef = x;: |0 N% _, q+ M: e
ptr->next = NULL;, b0 G) N/ P; ~2 X4 a
}
, j- x; p; X3 I5 p else+ v4 n( K$ i$ b# j. ^. w
{
. I+ Q8 g* N$ R for (k = S.top; k >= 0; k --)
1 ]* T, H3 D. _4 b4 h x = x + (S.elem[k] - '0') * cf(S.top - k);
4 s& ?+ D+ P2 ^6 ~3 a7 W ptr->exp = x;& ] c' \* q+ |
ptr->next = NULL;
# F. ]0 Q: G0 t! `1 X& R# Z }+ d$ [2 F9 L5 N7 w9 W, L* h& k' z
p->next = ptr;
' w% o3 j5 M& s; {1 [ W' f$ z ptr->next = NULL;+ i( E( C" Q7 T: F% ^& t
}
m# J6 E# a3 j! {& N: j- @: {& i }
5 L5 T0 x! V! _& A2 p. @} void stinsort(Lpoly *pa)
( g' s% K9 W/ ^( K{# a! B. {' a6 B3 T; i4 K5 D
Lpoly *p, *h, *q, *k;
+ c6 n8 P1 h. O* i, ]$ p9 c7 f2 Z h = pa;
4 H# R' o5 y2 X$ } p = k = h->next;
0 N6 L7 _6 Y8 } q = p->next;/ t! x) D! R, |: G% N$ |4 _
while (p->next != NULL)
) [1 q$ y8 K5 x1 m1 z) h5 J" P9 t; Q% X {9 ?/ q2 m) ]4 {* f/ {- N
if (q->exp)
5 P8 L1 V9 n. f! C, K0 W+ u7 L {
8 L y( y+ @7 m+ _4 v5 S while(q->exp > k->exp)7 H, ]+ E3 c( m
{/ k% j' Y( G9 V
k = k->next;" |; e; R* P6 z2 ?+ x4 x- |
h = h->next;
- z. U, g% L/ {: ~; X m0 w }
' r& n6 m" v% L if(q->exp)
. b; m. ]) e7 X {
, ^- [$ s3 G9 n6 n. M( ] p->next = q->next;
" l; l, a! q! Q% e h->next = q;3 ?& f; U4 Q/ ?9 k
q->next = k;+ I2 A. x( r" }2 ^$ D' q
q = p->next;; a1 x+ X- ?" S" N& B' m! i
}
9 e8 S1 R [1 J. y if (q->exp = k->exp)4 _" ]8 _8 ~5 f- e" Q" C
{- }0 W! V7 y' K4 v
k->coef = k->coef + q->coef;
0 i) H7 z# d( f- [6 Z% h p->next = q->next;
+ I- N/ f; B2 p q = p->next;
: c O4 L$ _9 O$ {% z. l" @ }
. ^ F) \6 Y2 F/ T4 a5 w h=pa;, t: M6 l/ c! R+ F
k = h->next;
: C, f ?" |5 j" l' g }
1 ~& Y1 S% k1 N ( v& [) W, T" n$ M' I* x
if (q->exp = p->exp)
5 {+ P- Y$ g f* N {& v- @# X% }" _/ L. l. q$ \
p->coef = p->coef + q->coef;
: t: z( ~9 g! O2 ~1 y p->next = q->next;
2 I3 s* y' \9 R% Q q = p->next;$ [+ z( j6 n9 R1 O" l* K1 w
}
0 F6 \' f( U3 b7 u( f' X if (q->exp > p->exp); i* z, B$ [1 ~! D1 `
{
- ]) A8 a' x* f( b) M q = q->next;
$ M F$ x3 ~* f- Q8 B: k p = p->next;5 J P- J+ V# B5 l1 J) y R
}
3 G8 V- A o e) c, W2 e }. s% }8 W: q! q! l, k
} main()
+ z5 }2 x" f. |! J{/ X+ A2 O6 |* K- _0 d$ s# f
int i, j = 0;
& a9 Q) I0 D5 Y; p0 s/ }2 Q' N printf("a=");7 ^6 z0 @5 ]2 a) |
scanf("%s", a);
r2 }6 `9 s# H0 \7 F: F8 Z j = strlen(a); input(a, j);. }/ r8 e" v2 K3 I; s
stinsort(pa);# q7 P4 X" Z9 e2 B. a( k2 o
printf("a=");8 h5 s+ L+ }& n2 [. u5 a
for (i = 0; i <= j + 10; i ++)
9 Q! V% Y. d( {9 C( j {( |# j! A5 |- Y2 ^# e
printf("%dx%d+", pa->coef, pa->exp);
+ p. g( U4 ]+ R& }; P pa = pa->next;3 r p$ O9 [- D0 e( o
}+ a3 G9 \6 ~$ ~0 z5 Q; z( i
}
8 C6 V. l' \& a$ \$ L 我把楼猪的代码弄了下,把里面大小写错误排除了,这段代码会有死循环,谁有空谁研究去。 |