#include "stdlib.h". h9 L. W. L) r3 p2 s8 M
#include "string.h"* I6 o3 P f7 m; A1 b' R+ r
#include "stdio.h" #define MAXSIZE 400
/ M2 Q7 s1 c: u) achar a[MAXSIZE]; typedef struct$ j" ?: S( k; t% v- G3 a. C/ O( i
{
/ c) i y9 M/ O+ L4 n2 _$ ?: N char elem[MAXSIZE];" z& m Z8 \3 L
int top;' X: V' G, t2 P* |
}SqStack; SqStack S; typedef struct poly
9 j" ~, {! a/ H9 \1 Q{' t( G# v( V6 d2 @2 E
int coef; /*xishu*/; a3 b" r7 T- b- \
int exp; /*zhishu*/
6 v. q6 n% A/ m1 s& D. i- C struct poly *next;$ @6 p9 q3 ~$ h+ B& V
}Lpoly; Lpoly *pa; int cf(int i) /*10^i*/. p, V3 q! U% D* H
{
. k5 |3 a0 P) d- |; { int c = 1, j;4 \5 j3 m8 I% Z
for(j = 1; j <= i; j ++)
; S2 I) m5 b& ^: D: m% B c = c * 10;
`% j7 e% ^* A8 Z return c;) v1 W0 V2 U; H2 i4 h: I6 \
} void input(char a[],int j)
* U d0 m1 q$ ~1 r{
# U0 A( X/ J8 }6 G# D. L1 d Lpoly *p, *h, *ptr;
, Z- _6 j) Z3 c, l) A" g1 G) j int i, k, flag = 0, x = 0;- i+ y: ~- O) p
S.top = -1;
0 H$ A. [! O3 @ ] pa = (Lpoly*)malloc(sizeof(Lpoly));
- A4 z& Y6 _- m: S pa->coef = 0;
1 r y5 @/ X+ S- E" {4 m pa->exp = 0;
% l! ~! G. H! p h = pa;% y1 z5 [3 o( d) g
h->next = NULL;
0 O/ Z, v6 ~; S7 o p {3 L; z p = h; /*jianlipa*/+ Q2 N! o, T e# f. r( A
; N5 r: ]0 O0 d/ r, ^% q7 l5 C( T# f
for (i = 0; i <= j - 1; i ++)& i$ @- Z% W+ E6 a' a
{! x2 P5 l. I) V7 u& a0 Q( n# a0 s
ptr = (Lpoly*)malloc(sizeof(Lpoly));# a' A' g; D# L% X& Z% |
if(S.top)
~- n; H0 ]! p: a {% U0 g0 ]# L: P: ]
S.top ++;/ G; p i6 Z! [$ \0 p& I) u5 z" ^
S.elem[S.top] = a;
( ]5 g r: ~ w0 X }7 X* }8 A" _) e+ i
if (a == '-')+ B: p' `# R8 b5 x! b3 Q
flag=1;
9 D9 g0 E* f0 }9 {0 H1 n Q" \# R/ c0 X% M
if(a == 'x') /*fenlixishu*/6 W$ D" Z& U/ O9 m' z1 X0 D
{
7 Q* ^% Z3 o1 L if (S.top==0)
7 [. e# B6 ~; O5 | {
# t8 ~* F1 s+ h) i if (flag == 1)- D" p( E' O; s( l
{
# K0 A2 K* J6 N( S5 j ptr->coef = -1;
5 D6 Z* ?" k$ w) h1 W5 Y; ^ ptr->next = NULL;/ z9 d6 ^( J! B- P7 I0 V
}+ q6 t. |7 s; Y7 Q* I% A
else) [ Z2 o6 G- X, C( ~5 s
{
9 O4 i! ?8 \) Z* I* ] ptr->coef = 1;
* z8 l3 P' g/ i# T+ i ptr->next = NULL;9 @0 E, V' O0 |2 e
}
- P5 @0 P5 F7 A+ J3 L' { }
3 E$ n; T3 [) `# S; [! y1 L else: w4 `7 O) |* C6 r/ f4 i) K2 U) s
{# o& p8 x }) X9 G+ _ z
for (k=S.top - 1; k >= 0; k --): z/ ~! q* z; q4 f& h: X$ G
x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);8 ?, o# D% r& {8 C; Q8 |
ptr->coef = x;
: {0 }2 l, u! x" p2 \# J3 _ ptr->next = NULL;
8 b7 T1 ?1 G( k- o1 _$ Q x = 0;
1 P3 K% J: j6 _' q1 a3 ^4 e4 g }
( c& Q# C: W$ H5 g5 H3 l- c S.top = -1;
' i! l9 r# W/ z+ F/ U# F+ A flag = 0; /*clear*/) `/ K: t1 _' ^6 i! W
}
- A: {# i ?. k& c if (a == '+' || a == '-') /*fenlizhishu*/+ E3 e$ U* D+ M3 r
{
) w3 X; p0 w, l, i# m2 ] if(S.top == 0)
& R3 j8 W/ c1 h4 b& W* u6 [, t: z {
' n y4 K8 O3 @: m ptr->exp = 1;) S- z. L- R* W( w+ a7 X
ptr->next = NULL;" u. r; v+ e$ u% Y& y
}+ {% X( {' p8 E7 D6 E
else- z) [" S4 b* x6 u2 t- n
{. g7 N6 {6 n' b$ y+ l. I. b
for (k = S.top - 1; k >= 0; k --)/ \3 c. l/ l6 c1 `" \2 ]6 V5 A" }
x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);; N6 V9 X* p! n2 O" }. W
ptr->exp = x;6 n9 @3 l( [+ Y* U) s( x
ptr->next = NULL;2 I: D, T& x( `, K, j! Z2 }' `5 p
x = 0;& X- N' { |; q$ _6 G8 T ]
}/*tiquzhishu*/1 m1 v7 {8 R7 J3 I8 }4 }, m7 P" |
S.top = -1;
- w, O' k6 s0 Z6 d* F" ^; W }4 X1 a% c g' S5 b3 d1 b8 l* p
if (ptr->coef != 0 && ptr->exp != 0)+ @8 M9 w/ X' ^) B+ [1 m8 ]
{
2 s3 T8 P/ w0 x& T/ v* G p->next = ptr;- h# o6 n/ o" z5 l: B2 u0 g. v
p = ptr;6 G8 Z; F* o( V4 b% [' z1 k( H
}
' K0 c8 a, ^% o& T7 a if (i == j - 1)
- ?( n% _$ T6 N" s( x7 y% _ {
. I6 h8 s5 S/ V8 M' o2 n9 ?' z: M if (ptr->coef == 0)$ G9 |4 D: p% v& ^% I, v/ {
{
( G: K6 ~- P+ x! n* j for (k = S.top; k >= 0; k --)! u; R" |3 b, ~. N2 H' p2 r
x = x + (S.elem[k] - '0') * cf(S.top - k);
" {0 g( J$ V$ V" ?5 v+ k ptr->coef = x;4 e4 L+ s9 M- s" S
ptr->next = NULL;$ m8 j, f$ O( U: d* l
}
! ~- z/ k4 {' k6 U* X* c6 w2 p$ ~ else$ g" F$ X- H- g/ m1 g
{! k- I1 }; s* m2 d9 W" ?8 U! q
for (k = S.top; k >= 0; k --)% o4 t4 _) x$ p& b* j1 a' ?
x = x + (S.elem[k] - '0') * cf(S.top - k);6 P+ ?2 d& Z5 A) W
ptr->exp = x;* o9 P6 w/ s. m' G6 f. ?4 _
ptr->next = NULL;8 d. G6 V4 g8 R* u5 d% U3 r
}
" Q8 u6 M3 r |/ U p->next = ptr;
& l9 Q8 \' c, c: A8 p1 `& I0 L6 L ptr->next = NULL;5 X# j6 _" g4 u$ M/ `
}
: A* f8 t' c5 h% z* f& r5 Q6 Z }9 s k4 n# E' `8 r1 d5 ~ z
} void stinsort(Lpoly *pa)
' _' j1 Z8 f( y S$ Q& a9 B{3 s$ m* Z3 ]7 t
Lpoly *p, *h, *q, *k;
. w; S2 W/ |- e8 d. ] h = pa;" y9 M" l4 k$ D, v
p = k = h->next;
1 V( f$ Q8 Y/ S8 `( I q = p->next;
+ R& O) Z2 r! }* Y) I: M( l while (p->next != NULL)6 v( ?6 l) B" z; H) e% T, V2 F+ ^
{
: i3 C- B4 r5 Y0 K if (q->exp)
# ~' b, Q8 [+ d {
3 c5 ^- A# c, B5 P- T( \" D$ p# t while(q->exp > k->exp)! T3 u4 F: X3 M% |' G0 s6 U8 X; Y( q
{ \0 x9 `: {6 a( Y) n, n9 A5 k
k = k->next; U! ` D& T+ v
h = h->next;
4 n+ F4 Z0 c" I" e" y5 H0 N0 q }
: i# V# y( B. t if(q->exp)( \1 |$ b% G* [
{/ m9 r$ k, b( S
p->next = q->next;- f+ `2 w7 h! P* E1 o
h->next = q;5 S1 A, v5 {% V1 ~: j# q }6 m
q->next = k;
7 U' u. H, L; o% B q = p->next;* ` Y5 L; [ _! b3 O! T! `
}+ m9 z" X* |# f5 b2 r
if (q->exp = k->exp)0 {+ j' e$ w) q# p* ~
{
& o, M/ L" l/ ? k->coef = k->coef + q->coef;8 U/ T/ G0 j; n, P& T
p->next = q->next;
3 w4 c5 x3 S1 V! l( I) e q = p->next;
! i- e1 S4 A0 g% G% P8 _' Z }
* n, q- Z: P) F" n7 o h=pa;
: C, | L( z# _( |; t& X4 k" R( y k = h->next;+ i( M; n" w. {: T( c& q) H; _& j
}& l9 ?7 ^% ]) ~2 z* H( _
1 Q+ [/ N- d* X/ H5 E
if (q->exp = p->exp)
5 b" \7 U$ g- h {9 P' @$ [4 O" B" ~( h9 u( n; X! k
p->coef = p->coef + q->coef;) g! O8 s) x/ b) D, n: o
p->next = q->next;
0 V/ ~1 S$ j( M" @% X( B q = p->next;$ Y, K0 q: v5 z
}
3 c, w$ W: z7 v" {0 D1 O: W if (q->exp > p->exp)
. N4 ]4 T! _# H5 e/ G! M# n {4 O, z' o7 y& ?! r
q = q->next;
$ M0 z4 f9 d! D/ { p = p->next;! R; z! E# H, ?
}% z! Z# E$ B3 j9 }6 l
}
0 w0 J, ]4 n& n6 p7 ~} main()
3 @9 M$ q I' F- D+ M4 O3 ^- E6 ]{8 e( z# r {% K* m/ F$ N) ?
int i, j = 0;
, U3 ]0 ^$ f* P# S/ e3 t' z6 t; ~ printf("a=");
3 k S+ `$ E, A scanf("%s", a);4 C9 P* u4 H9 \7 v9 F
j = strlen(a); input(a, j);' P4 R; i4 ^3 f9 ?1 {
stinsort(pa);
3 T' V1 b" e, p- c printf("a=");" H2 n B, ?% C2 k2 G4 c. {+ \% u
for (i = 0; i <= j + 10; i ++)
2 R6 l; o) U& P9 r {3 C3 R k0 q3 b5 O# N( K7 @
printf("%dx%d+", pa->coef, pa->exp);8 n7 ~" x% G! G3 @+ v! j
pa = pa->next;$ K0 i: f3 Q# n2 m
}, ^/ }# H+ q8 c0 D5 ?! w: \! J* Y
}2 ?; n+ p/ s/ W5 x6 k
我把楼猪的代码弄了下,把里面大小写错误排除了,这段代码会有死循环,谁有空谁研究去。 |