#include "stdlib.h"
( S* u! L) c& E# d$ }0 A#include "string.h"
! n( V# ~$ s4 E4 L$ ~#include "stdio.h" #define MAXSIZE 400
) U9 i7 Y% D! |; w& l7 [char a[MAXSIZE]; typedef struct
' x# Y/ o% y# @ `{/ D5 _' q+ U/ V+ c
char elem[MAXSIZE];( Y2 X) ?- `1 c% k) S/ |
int top;: P. i( \! q4 o' u3 s0 {8 U, r0 z
}SqStack; SqStack S; typedef struct poly! B w" U" {& y3 U4 I c
{2 _( n/ t7 T0 Z- l) A( m
int coef; /*xishu*/- M0 K# _! a+ Q( h
int exp; /*zhishu*/# h1 {4 y1 h7 |. W9 F) \. O" N
struct poly *next;+ O& _6 f- `2 ?" i. O
}Lpoly; Lpoly *pa; int cf(int i) /*10^i*/
3 V% _7 R0 H% @{! N( `5 U& E# v* k
int c = 1, j;* f/ z7 p* Z* H7 Q0 H. L
for(j = 1; j <= i; j ++)4 H5 {8 m( n2 L% k; m3 S
c = c * 10;
' F5 n; x$ L0 K return c;% F2 A3 z, g5 ?) r) D
} void input(char a[],int j)
: c% o5 @8 k! z0 B4 x4 |{$ ^3 p" g4 E* o* t( Q4 d
Lpoly *p, *h, *ptr;
3 Y" ]2 [; v( x. q5 i int i, k, flag = 0, x = 0;
* Z0 ]( ~' y1 m: Q S.top = -1;0 S. M4 e ~9 B6 h& [
pa = (Lpoly*)malloc(sizeof(Lpoly));
" @2 N% t0 J2 s: k pa->coef = 0;# K% X# S% f! m; g# E9 G% ~# A& r
pa->exp = 0;
# u5 k2 f& a7 {9 n, ]# Z: A h = pa;+ [; a7 @& Y3 \4 ^% t% `* W% j
h->next = NULL;& E' {4 H1 z1 h& x6 V5 y. `
p = h; /*jianlipa*/
9 W" j. h4 V+ N0 E- v" R! j
5 _5 Z0 @% H8 H4 j7 \% H for (i = 0; i <= j - 1; i ++)
' @3 ]2 |# h6 ] [ s8 ? {
. F* z/ e3 u) \5 x ptr = (Lpoly*)malloc(sizeof(Lpoly));3 R2 |) u' _. g& P$ o
if(S.top)$ |* H f5 \6 v* ^
{
. u( [4 g6 x0 n+ Z0 V S.top ++;
! I; n2 _, t+ |1 b) B2 x S.elem[S.top] = a;/ v2 C. P5 [" j5 e& h! v [% b
}
& B8 p1 i. M( R7 n if (a == '-') B6 L( m( [1 o& p
flag=1;! ~+ V( }7 \8 |) b9 s
5 j. |( C. ?5 d& }
if(a == 'x') /*fenlixishu*/
' R/ I. u; @ I {' f$ @; W$ Z$ |% ?$ X$ R) }: \
if (S.top==0)
, |2 _7 G7 q# h2 V$ ` {9 C# r. J. W. C2 e( n% ^
if (flag == 1)" V2 |0 T& G% }, \9 F6 n
{+ b8 y. Q! T$ X
ptr->coef = -1;; s7 D9 H. e$ ^8 I1 F
ptr->next = NULL;
$ B# t' g4 o; a }3 a: q8 S8 s8 A7 z. y/ D
else3 Y" Z% a- o$ L
{3 K7 {9 Z {' K0 m3 P
ptr->coef = 1;
" @! `5 r; o R, f ptr->next = NULL;0 r. h. w8 v! X
}" _9 } C1 ]/ D) i* X9 H6 A. g9 c# N; ^
}
( L; q. s/ N& J+ b else* W: A3 d$ U2 T
{& x* V4 K E; O* Z" U
for (k=S.top - 1; k >= 0; k --)" X4 M# G% R- Q
x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);
5 I$ K+ G1 j1 ?3 K1 `. m1 } ptr->coef = x;; x: y6 J/ k6 {5 `. p& a( J
ptr->next = NULL;
* E" o6 h5 M1 D. I1 a) r# S. i0 {8 k x = 0;7 T, d) K+ ] M7 |4 J- P/ {% d5 J
}
# ?9 T; B3 `5 [) s- l S.top = -1;& _! M6 B2 v* n1 T- a$ m
flag = 0; /*clear*/
/ n% w$ h0 }; Z v1 R8 L' _+ g }: S% O( s; |' m g2 `
if (a == '+' || a == '-') /*fenlizhishu*/
9 s5 Z: N4 d, r {9 c+ [5 a% Z8 t1 U) s
if(S.top == 0)
' e: L9 W' e Y {6 E, P {- Z8 c+ U1 ]1 ~! H* H
ptr->exp = 1;
; Q( `- U/ V2 o0 C ptr->next = NULL;5 |: j! l( l" B: Z3 E W) N# [5 b0 r
}5 D N' P1 J. t( ]2 J0 c" L
else
# d. ?0 }6 \% W- {* Y/ `: u- v {2 S# s% R; P# \ T) {' j( Q
for (k = S.top - 1; k >= 0; k --)
% Z/ G2 k8 n' o) j6 K x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);' l- M# k5 M; c" t: l# N. @
ptr->exp = x;
( P. s3 S- o2 I: |# w r( F4 N4 V5 @3 N9 Q( R ptr->next = NULL;1 \& J" A) B3 |8 A/ k
x = 0;3 `% X6 {7 k- _9 D2 R
}/*tiquzhishu*/. X" b& |- ^3 h9 W R
S.top = -1;
* t; ]" f$ h5 G; N }
) I/ J, U2 v* `. k" U3 d1 O; D if (ptr->coef != 0 && ptr->exp != 0)- @7 _2 l6 J# S4 d' q. L
{! K/ d. o7 d& R) |. k5 n# Q6 ]* W
p->next = ptr;/ u: U. U+ W( m7 z
p = ptr;/ e& l5 P$ Y) v6 e. K
}
+ w6 M* H; R y# b if (i == j - 1)" A/ x/ I0 f5 e1 i' V6 P
{3 z( p7 W2 @/ Z$ K
if (ptr->coef == 0)1 ~* Y! l4 Y5 L) Y/ Z& [8 X0 f
{
: u7 q M/ N( x' Q* \ for (k = S.top; k >= 0; k --)
: ~' g8 z+ u! R/ |6 u* E/ M x = x + (S.elem[k] - '0') * cf(S.top - k);. t0 R% X" z& Q) W# U0 p9 g: ?/ B
ptr->coef = x;
' V3 B5 P2 S) q8 ?! \8 a ptr->next = NULL;$ g1 o# i. j* B6 R0 _
}
; |: Y( m+ g1 P4 t( C7 v" A else( F- U/ ^8 [( j5 E4 T, r7 I
{
; R2 `. G5 V" }8 Z ^" U for (k = S.top; k >= 0; k --)0 ^ u5 f0 V! C7 p2 N8 N2 w
x = x + (S.elem[k] - '0') * cf(S.top - k);
0 \7 h; p4 o+ T$ K$ O0 Q- V) n2 s ptr->exp = x;0 ]) x v( u' F; {# o4 r
ptr->next = NULL;
& I9 d$ i8 _ X& F8 O4 C }( X3 s% c/ ]# O! R2 S& l! S
p->next = ptr;
1 U g+ H# f, A( }8 p; @3 h' Z ptr->next = NULL;
8 q2 w+ Y1 Q+ f, d+ ]( S }
+ a& N% Q6 t3 L: u2 o; F7 v }6 W* b$ X5 |8 H# O) N; q# a
} void stinsort(Lpoly *pa)
. M1 Z {2 G( }4 T' h6 V{
% q+ U4 s+ l* @% @0 J Lpoly *p, *h, *q, *k;
0 j+ l( m4 B# _9 {0 Q' f h = pa;, F; c* U s9 W
p = k = h->next;
) J6 j, r/ S5 O, O% i q = p->next;! c, u& A* U0 x: j
while (p->next != NULL)$ }6 P0 ?5 i0 O# ?' Q9 ?& f
{8 G, R$ N' Y; S. q
if (q->exp)
0 `% x% g4 Y/ d( O) A+ u$ Y! Z0 A {+ b' Q" `% n7 C0 Y9 }; U% [& `
while(q->exp > k->exp)) v4 G5 z( E' ^$ b2 ?: Y* y8 s
{/ n7 G) \1 h: i8 F* A* c2 }- g
k = k->next;
8 R2 }1 Q( `& z. X h = h->next;8 F0 e" d5 u5 v5 y$ g4 Z
}, j+ D) p+ e5 E4 B) \/ W1 [9 e
if(q->exp)( i6 P( F1 o/ _/ M; m4 T' [. }; o
{
; K( E. U% ?4 U9 B p->next = q->next;
0 Z1 r) `+ g1 q! V; O+ b h->next = q;
/ B! \' s3 w5 S4 t5 E- Z B2 J0 J6 V: S q->next = k;
% ~- N/ Y3 f& b. |: m q = p->next;/ W/ p% A y- w. Q
}
% }/ @8 \( E8 H2 k% b+ T if (q->exp = k->exp)- S8 f8 X+ ^- C
{0 i" Z- ]/ M" w0 B
k->coef = k->coef + q->coef;9 q5 F# P5 [% _5 @
p->next = q->next;& A4 S k; v d( V1 ^' D
q = p->next;
4 E3 |4 P+ d3 L }
& [& U5 f2 _! q* c6 c/ r* F: ?9 R h=pa;0 r4 _' O6 E6 |
k = h->next;+ b1 {, ?( Y6 W, H1 l. W
}
1 ~9 A5 T4 Z G8 P 1 Y$ t6 U' N- c# r
if (q->exp = p->exp)1 A [, S9 _9 d$ \! A, W! v% I
{+ b- j$ [* H$ J$ C
p->coef = p->coef + q->coef;6 v0 l6 @% V/ ^4 j% U$ N2 P
p->next = q->next;- X! z m9 p( g& Z" a% B
q = p->next;
& f5 U" k2 O/ r x9 _8 K }8 |. I2 ~4 W7 c! K6 V
if (q->exp > p->exp)
9 }9 O9 a3 d, Y8 H6 M* f. y {7 V6 W5 ~% G* o8 }8 b
q = q->next;
9 Y6 w0 z2 t7 O% S- {" p! U p = p->next;
' [9 T" u* w2 q }
3 t/ W' _7 m/ \5 O }1 d/ i; d1 w( ^2 r
} main()
/ @5 l2 J( y5 {7 d/ t% O1 y{& I6 o& |& q( w2 H
int i, j = 0;
3 y8 c2 d- ]0 u9 K# U; p1 X! t printf("a=");( O, V8 h" s" u
scanf("%s", a);
/ F+ S6 K e2 U) g8 Z9 J. { j = strlen(a); input(a, j);& I; I' C0 x+ d8 P( O
stinsort(pa);
* T y8 {, n4 k printf("a=");
, D8 L: C6 V% P% i; [- e for (i = 0; i <= j + 10; i ++)
$ K5 H, v! W' E* E z {
$ L; C9 t# }1 Q6 _5 O8 p8 I printf("%dx%d+", pa->coef, pa->exp);
9 g* e% n( m2 J" C pa = pa->next;& _$ d. X+ p) \- {
}5 @; b! {0 ], R- D9 C6 B
}2 G( p# F/ H% g0 g8 a3 o, C
我把楼猪的代码弄了下,把里面大小写错误排除了,这段代码会有死循环,谁有空谁研究去。 |