#include "stdlib.h"8 [; l4 U3 M3 C, a- s' ]
#include "string.h"7 v+ H& ?4 [ r4 r
#include "stdio.h" #define MAXSIZE 4005 w& h% k# z: }3 @4 K5 `2 m# \
char a[MAXSIZE]; typedef struct( D g) z" v- k; g* q0 g" ?
{
4 X9 O4 h: |1 w+ ^' ~ char elem[MAXSIZE];
2 a( j* g4 W: N" p int top;
: C: s4 n1 I5 g/ p9 x* d/ z; Q4 Y}SqStack; SqStack S; typedef struct poly/ Y5 L6 ^. P. C C
{
5 r: ~3 ~. O @4 O o int coef; /*xishu*/- f) l. f' {2 h0 b" M1 q
int exp; /*zhishu*/# b( R8 L+ g- ^/ z1 I
struct poly *next;. a" g4 p. |- U% e# O H
}Lpoly; Lpoly *pa; int cf(int i) /*10^i*/$ W2 A) J) x% b+ C3 ^3 r3 u
{
, x- m$ g5 N( l Q int c = 1, j;
`1 K* U% ?9 e7 U5 S/ w: q) C for(j = 1; j <= i; j ++)
* {7 Y/ U3 i7 {1 V# x& r c = c * 10; c, ?! ^5 g8 n6 q, n
return c;
8 t( g5 {* X$ f8 z% D} void input(char a[],int j)
; ^- w& w1 A$ _9 s/ }! g- ?{' l- b6 g v, o3 j. I
Lpoly *p, *h, *ptr;
0 q8 v) q( U( L. ^4 X int i, k, flag = 0, x = 0;0 L) t2 B: Q( d; t
S.top = -1;
* o6 y' y; j! K/ e pa = (Lpoly*)malloc(sizeof(Lpoly));5 b8 j: z4 @( o( x/ k9 h4 T3 G
pa->coef = 0;2 n9 Q7 V! h$ l! o* g
pa->exp = 0;5 [% s0 ?0 l+ A5 U: F
h = pa;7 x% l! @2 F# o% b' V
h->next = NULL;
$ f) n3 \) n* [- @6 R+ @ p = h; /*jianlipa*/
0 @2 t+ \+ i& W; |/ P 4 Y6 B4 X) T5 G! c. i1 l
for (i = 0; i <= j - 1; i ++)- _% o% V$ i% o
{
$ E1 D: R/ k1 v6 H ptr = (Lpoly*)malloc(sizeof(Lpoly));3 b2 a) b: K- ~4 {
if(S.top)
. E- {, P G6 W N# x" L. T {
1 K/ h4 v' B' U. s6 C S.top ++;
! D; [+ {/ q8 P- z S.elem[S.top] = a;
3 \: T0 F+ N S, B, n }
, i2 g& I: l# I f if (a == '-'). ^- E# Y0 ]1 K# F
flag=1;
& S% {( {% `- \% B; H: n
( V' l$ I; O4 Z4 m/ E if(a == 'x') /*fenlixishu*/
" Q/ i" d6 Q- y/ n* i- ]5 v {
1 P$ ^; I5 u) B' l if (S.top==0)( s0 A$ v. s7 C! W) A
{0 }4 f7 |- ~/ d% N! x+ ~1 l, x
if (flag == 1)
% T- p8 J: `4 |/ A2 I& ]7 b: q {
$ f' D: B7 n% @ ptr->coef = -1;6 G; }1 u1 O( n, b) ]
ptr->next = NULL;& u' a2 s/ f( y3 ~7 u. `
}
1 s1 f" s# I6 M% [& }1 P1 Y9 j else `! v( S S' `4 d2 H0 o" l
{
1 L' @# w" C9 T/ p: y ptr->coef = 1;
7 Z3 z! J. ^) h2 ~1 T# W5 Z. o( b ptr->next = NULL;: h9 N0 b X& H! i5 C
}1 X) [* a2 D) t6 W2 G) V, n. A
}
& ~2 R w: s9 G8 Y: P; l1 j) @" R5 F" C else% ~+ v' J$ t0 A1 T+ ?* H
{
# M, R2 |- E, m for (k=S.top - 1; k >= 0; k --)% P: u! j9 l C+ A" A' d. B- k: O" t5 P
x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);8 L V5 |1 v9 I) w6 I" x( M! d
ptr->coef = x;
k- F7 h% ~; P7 U6 Y+ h" } ptr->next = NULL;
9 b/ p5 k$ P# _5 x* c x = 0;
/ G6 X& m, V) i$ w! W$ d' E }( f1 L' t# W3 T, ~
S.top = -1;
: I* R' A1 ~- |) g# t3 q3 T5 a; | flag = 0; /*clear*/
+ c* t0 E* y/ h2 ^% E+ @2 n }9 o: {* c$ r5 t& F$ e& b8 v
if (a == '+' || a == '-') /*fenlizhishu*/
" \0 d% a) v# d" G {
' g/ a* M& U3 G |3 [8 h3 `6 v6 j: T if(S.top == 0)! l: K! |# }+ v
{
7 l* H! E5 y0 {9 V ptr->exp = 1;
" G; @+ ~( @7 P$ v) y. P4 _ ptr->next = NULL;
$ a$ q) _/ L# ^! C }
* h4 C0 _. B8 ~# } D+ t else
7 S& \/ `# E; k; ~ {
: C: B9 g2 k, m. ^- d for (k = S.top - 1; k >= 0; k --)
& k: l2 D0 i2 ]; [4 @ x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);* |" Q8 O4 S3 S" {
ptr->exp = x;* ~- q! M5 f! x9 ^# C' {
ptr->next = NULL;4 n: S3 s* j/ b- M7 U! w/ p( a# }# k, ~
x = 0;* O3 g) E4 x, V2 a$ e
}/*tiquzhishu*/
& w0 N( z! V1 w% s+ B, `5 a/ B( d: B S.top = -1;
) M) X' F$ E5 q' f }3 z: `# I: E. J' E8 T- m) T9 r
if (ptr->coef != 0 && ptr->exp != 0)
3 T: Q& Z& ?% l# ^ {
4 S1 [; p: G. h/ @! } p->next = ptr;
& K1 `) H4 y6 q$ P+ B p = ptr;
( \, ~9 b6 @0 w+ u4 | }" E2 V6 W4 r6 S5 ?4 K# P7 k! a4 a' l
if (i == j - 1)5 z4 B$ W( q. d" n# J! a8 [* y" y
{0 z/ j# L- s2 n m$ w7 R
if (ptr->coef == 0)
% m0 Y- ]- D0 o {
; z# l# x: q4 L9 f% T for (k = S.top; k >= 0; k --)
8 [+ X3 d. |) h o& o g+ | x = x + (S.elem[k] - '0') * cf(S.top - k);/ V! D p6 s& z3 O: k5 w% q1 q1 I; }
ptr->coef = x;0 E" \& m- y4 r4 g$ d/ _
ptr->next = NULL;5 F; {2 Y" e4 [8 v& m# b, g# w# L
}
$ W: h$ \; z6 z( ^5 \/ E/ s else: y" d+ u4 h/ u
{. `& C$ W$ V+ G* h2 J
for (k = S.top; k >= 0; k --)
8 M/ ~, O; a5 f9 q" E/ \% v; w x = x + (S.elem[k] - '0') * cf(S.top - k);
/ l) @/ r5 o: ?5 y ptr->exp = x;" n- j V1 ^- }0 H9 [2 i
ptr->next = NULL;
3 V8 i; m. f% E9 e# i }
# M% Q, @" q6 X- r/ Y- n7 I p->next = ptr;: N# ~3 o; m3 U# S/ q8 i. C9 M
ptr->next = NULL;# I3 w9 V, O9 A1 u
}
6 A- k8 o1 C0 d0 H# r* N' G }
( ?4 p2 j# Z8 e' F- d} void stinsort(Lpoly *pa)- D$ u" E9 P" J: {" a6 N2 a p8 V
{8 }9 X* E: m- O4 T8 `
Lpoly *p, *h, *q, *k;) P) y3 K3 @# [; h
h = pa;
* R1 G# F2 W" t9 u1 ? p = k = h->next;* S9 F/ u i1 G3 j, `6 k( m& j
q = p->next;; N& X0 [+ @4 ?8 |: B3 ~$ |
while (p->next != NULL)1 w, I, }! d0 f/ y8 N% h6 w
{7 O& c2 b. h; O! b
if (q->exp)
- n# l3 b) n: Q5 j/ {+ h5 ^ {
! G7 i, I4 U: R& c! S1 w while(q->exp > k->exp)8 k i! o: F! P- u1 ]
{
+ H9 ~; i6 l. [7 @& y* X k = k->next;2 l7 u; i+ E( ?/ X
h = h->next;
q! g+ N) `& s z }5 O8 x# R% a; [4 u' _2 L
if(q->exp)
+ z# l8 g: g" ?4 i7 t {: g9 s" D; j7 y0 P. s: l' [# H
p->next = q->next;2 _! E \; J* L* K2 Y0 r
h->next = q;# }+ H7 R: g( ?# R. y% I
q->next = k;! L3 x* f! ~) @) z/ ^
q = p->next;) N0 [! s5 S7 ^) G1 u
}
# ?6 I) P2 K- M6 [. } if (q->exp = k->exp)/ Y$ u3 R( o9 D
{
F [$ W! n# i4 K4 e- k: u& { k->coef = k->coef + q->coef;
! G/ J7 E9 R9 U p->next = q->next;
, i( m5 f( |9 R) e, J q = p->next;+ e% x0 p1 G! k4 ?0 c8 e$ r* a
}
: m; t$ ~, Z1 p2 a h=pa;
2 Q. M5 ]5 P0 }8 _$ H- P. w k = h->next;8 L; M. B% w( U+ I3 D
}
/ u! U# _7 w) f' L$ d; a4 Y( S * t8 ?( f) k; I) s4 D. `
if (q->exp = p->exp)4 s6 l+ k1 T5 Z9 u
{
9 L, y) l& s( w; d- n6 G8 x p->coef = p->coef + q->coef;
% e, X( I3 ]3 a4 {0 y9 T p->next = q->next;
. d5 R2 p, M1 X5 G8 f0 P q = p->next;9 E/ C9 a/ y- v9 u! g7 e
}
% ?' I9 M! k1 v2 t7 `# k if (q->exp > p->exp)$ A4 \' ?8 H6 e+ L
{% L% }+ V# `. a* g
q = q->next;3 T5 E, Q# Q1 ~$ a
p = p->next;
, I9 ^; P* R+ e" @) o }5 B7 r8 d8 O4 D/ w- Y+ k2 d" l
}
/ O) X$ p8 K( X& X6 G; K' b} main()4 D9 q/ w3 b5 ]" h5 y" v
{1 C& M6 Z; M. S& V
int i, j = 0;; ]3 d# ]) l: ?+ Q9 E4 v
printf("a=");, e0 v; q! r+ Q5 i8 R* X
scanf("%s", a);. H" w O' y g4 ]7 _
j = strlen(a); input(a, j);
/ s4 T# j: `/ }- |, |# m' a stinsort(pa);
8 d2 y3 l$ o2 k% v4 A printf("a=");6 l" C! U4 c: T* F2 g
for (i = 0; i <= j + 10; i ++)0 Y1 p/ c3 K" `% I% {& J: ~
{0 I9 W9 Z1 L4 u- w2 A
printf("%dx%d+", pa->coef, pa->exp);
6 C6 A+ O0 V3 Z. C) P pa = pa->next;
" k# b% b( O3 ]2 W* x% G }# \+ G& y# O/ O8 e$ [/ ?
}7 n& G) P# u/ X% p8 W5 m, p
我把楼猪的代码弄了下,把里面大小写错误排除了,这段代码会有死循环,谁有空谁研究去。 |