#include "stdlib.h"
1 a& ^2 V% h# U#include "string.h": R9 R& Y; J9 n3 Z, n& q1 x
#include "stdio.h" #define MAXSIZE 400
$ `9 I; u0 A! \char a[MAXSIZE]; typedef struct" l! j1 S3 W2 _
{
6 C0 N$ D i7 x char elem[MAXSIZE];
5 O0 V$ W: I. | int top;5 u: s9 ~- S b/ |3 }( i5 n
}SqStack; SqStack S; typedef struct poly* I- G1 t6 ^' e/ O! e1 V
{, H, g5 a3 t% [* c
int coef; /*xishu*/
" k' P+ p- a$ O6 p: u8 x% g int exp; /*zhishu*/
1 a1 c0 d6 ?0 n* j0 [ struct poly *next;$ x( t7 f; s) U9 a" r8 z
}Lpoly; Lpoly *pa; int cf(int i) /*10^i*/
" G, ?/ L3 [- ?{+ _9 v3 ]& q6 ?3 Z: J0 d" a. z# l. c
int c = 1, j;2 |6 I5 F" z& o8 H2 b4 w5 T
for(j = 1; j <= i; j ++)3 j7 h% |# \2 Q" Q( r
c = c * 10;5 d' m6 Z% O3 V8 }' W! a3 ~1 r- q* ~
return c; M D7 H2 D, B- a, K
} void input(char a[],int j)- Z) t3 G- D8 \$ P9 E
{
, U% Q" N4 N- V& X4 K Lpoly *p, *h, *ptr;+ |9 H6 f( Q, j4 s( F
int i, k, flag = 0, x = 0;; n6 } U0 j' V! @
S.top = -1;
! ^0 |' }) E8 K8 U0 _ pa = (Lpoly*)malloc(sizeof(Lpoly));
. ]; A- X/ c7 u pa->coef = 0;2 N6 v. w' m5 y* g _- b
pa->exp = 0;1 D9 H& y8 m3 A# O7 h3 W( d
h = pa;
7 P6 i& T) C& @0 ?: g- H! g h->next = NULL;
6 K( x/ r2 S+ C# _& U p = h; /*jianlipa*/6 w* \% s& f+ U5 n
N# E/ C) o/ e2 X) W {. {& ?7 h3 j
for (i = 0; i <= j - 1; i ++)
$ \9 |+ \4 S7 g/ v {+ ~6 U# |+ X+ j# M) G3 \
ptr = (Lpoly*)malloc(sizeof(Lpoly));- \; D7 p" S; [7 e
if(S.top)+ I! a! ?: Q4 @
{
' u/ w2 c5 x# [. G3 e S.top ++;
8 h' ]: H1 G( z! O) ~- L- X S.elem[S.top] = a;
9 ]9 b, @ P4 e: d8 J. j6 ~ }
) i j! O, F- f if (a == '-')6 v% }" _* H/ E/ q
flag=1;, Q! a8 ^4 h9 B" \
! L1 {4 R0 g) ]" t/ Z" \/ x; ^1 s
if(a == 'x') /*fenlixishu*/
0 J4 }# [9 G( C' Y8 i {
# Y3 G8 N$ a5 D) y) x! C8 L if (S.top==0)
: x, U& |; d/ y) G {3 g! ` {! h1 e8 M( B9 q; u" Q0 Y- ]
if (flag == 1)5 x9 e* u" O" k' ~
{) c$ e; K2 r. Z
ptr->coef = -1;
$ n$ |- f% L( p! h; n5 D ptr->next = NULL;* Q$ g* i! X! v9 D
}# e, i; L' B& A5 h. I6 u& s1 v) v3 D
else/ C/ s5 `$ i8 ?2 H% A( `9 n3 |
{" Q, Y' s. s4 t0 F4 E7 H
ptr->coef = 1;
8 a7 o( I( l+ o8 u6 j! Q9 K* S ptr->next = NULL;3 O) l/ E& {# r/ s2 n* K5 G: t' a
}
( W, m% l- v n" j" S3 \ }
9 e p- ?3 A- @4 o; g9 O- u0 i else4 a' D9 L+ t( t& @% B) Z; A
{
# @( h$ ?$ j! N; g [" C2 S5 Q for (k=S.top - 1; k >= 0; k --)4 F6 M6 Q7 F0 z) o' I
x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);
6 N" d7 Y$ s- d) q! H) V4 Q ptr->coef = x;% W e3 K9 B) n3 W, j: D7 [
ptr->next = NULL;
/ {7 p& c: V% o. @ x = 0;
+ T6 V, d" u4 e! t }4 O' T1 T; T. d+ x: ~; t) s/ _
S.top = -1;" V5 g# s) [5 k7 g; h9 b
flag = 0; /*clear*/
% ^' @7 Y: ~4 a% C }: a. a& x7 n8 u5 p' q7 Z
if (a == '+' || a == '-') /*fenlizhishu*/- }5 d8 j. d1 T, q9 Y: D' ~
{
/ }# a! y, w7 q" e: ?2 t. d if(S.top == 0)3 E; J S& a A& K8 G% R
{
+ m: s. m6 C4 N2 } N ptr->exp = 1;1 p: ~1 c: p. t! U, ~4 o8 j* T) Q
ptr->next = NULL;8 z+ y7 h$ ]- X; P! ]0 {3 y# M d
}; d( S- E$ |( _6 E4 ~2 ?( ]
else
8 O. @& w9 P+ L) q! } {
- k: i7 U7 U6 A, ^0 c for (k = S.top - 1; k >= 0; k --)
; f$ H9 y# c: s( ]: h4 i. D6 s x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);
7 E+ n D/ R6 |2 w, X1 u ptr->exp = x; ]& o G" z$ H6 M, F5 A- d3 P
ptr->next = NULL;
! ]* S1 D1 l# T* \6 ~7 o. @7 A( Y x = 0;0 |+ }4 A* r% K H0 Q7 Z1 a
}/*tiquzhishu*/
{) _/ k4 D! ~+ s) ?: N3 z6 A. Q S.top = -1;
( t/ n/ v9 n1 A; r, S) b }
" L* K% K$ D" {* e# I$ W" Y' q if (ptr->coef != 0 && ptr->exp != 0)/ v8 a" `6 I5 u: [$ ^
{" X- {: S h& d! u; b1 `) T0 w3 n
p->next = ptr;
S6 C5 `& x+ D$ ?3 U! s p = ptr;2 O3 V; O' s+ Q8 w8 L( k
}! v7 E. y% ^5 V" v
if (i == j - 1)% ?, b2 X* _ P, g7 I" P
{
/ w/ V% T+ Q( |0 ?- P. a( ? if (ptr->coef == 0)
h3 B( D o! z* z {+ L3 @6 @ e4 x, z. a! r4 A
for (k = S.top; k >= 0; k --)
0 [; H6 Y) [" I! V3 q x = x + (S.elem[k] - '0') * cf(S.top - k);/ m$ c6 h5 A& W$ T9 f; Q" R3 w) Y C
ptr->coef = x;
) A' q: O& L! \! x8 L ptr->next = NULL;5 T, G; e- K$ s6 z* a2 F0 b. u$ n7 X
}
" y% J9 E6 A! z' l; P0 \ else( n; A' B/ I1 p! x, M* T
{+ b! {4 H7 g- t6 t8 j# ~, M: H1 ~
for (k = S.top; k >= 0; k --)9 @, ?* }; U/ z+ _* Y4 P- I. O$ \
x = x + (S.elem[k] - '0') * cf(S.top - k);9 y( N9 N2 Y6 Q5 B
ptr->exp = x;
2 b2 p6 d8 t7 x& n; h ptr->next = NULL;+ c/ b O5 ]$ w; o
}
% o" j" ~# l9 F i6 o p->next = ptr;
, [* O* m" |( `7 A# Z$ n1 q" X ptr->next = NULL;/ d( S3 i3 e1 \! a4 q+ G" y+ k. v- K: J
}
) B+ s3 K+ e G7 _ O2 R/ h) c }: j" e/ H: g" J7 q$ Q! I3 K
} void stinsort(Lpoly *pa)% S. ]' X5 q- g+ I
{
. c! D+ G! c2 Z& Q' E9 R. U7 ] Lpoly *p, *h, *q, *k;2 {/ w' F( Q( A; K/ K5 r7 w3 N
h = pa;
" {: I1 n& s& y7 h6 e/ Y- R p = k = h->next;
. ~+ V+ O9 }1 ?7 X! e3 [1 M q = p->next;
- [1 Q& n* @; `6 I' {4 B3 l while (p->next != NULL)! e$ v& U9 k. A
{* D, Y) \6 T1 ]" w
if (q->exp)# R. N, p% j1 [. K
{
& r" g' b0 z" Y; ?+ t while(q->exp > k->exp)
3 @. {/ G! K. A# R! U {) Q. A, [$ E: x, a
k = k->next;
# ]9 o% [ x$ ?/ C' u/ `6 o1 F h = h->next;
3 h9 a2 E. T/ b6 i. C2 W }
, W1 c$ Z. ?5 y( ~ if(q->exp)9 g) N+ ~) a% |* C; ?# t" M
{
- l( q0 K9 U3 D# R6 L; N$ ~ p->next = q->next;
$ P/ Q: s' E6 n3 j% r+ [1 z# g h->next = q;7 k# g8 ?+ Z3 a
q->next = k;8 k$ n2 v- A. ]% P
q = p->next;- d5 u8 q1 f- p' w* U0 y' s! i
}, x, y& \: f3 ^* ~; x8 B
if (q->exp = k->exp)
! B) j2 _* c* L0 R {
& k7 M: F# D8 B" S0 H8 D, s( F k->coef = k->coef + q->coef;4 ] ^& ~4 \; T6 K5 S r& ~5 Z
p->next = q->next;9 k& b$ n" u S L2 ?( o
q = p->next; C) @* E g5 K7 g1 d1 v/ D. a2 C
}
& ]9 s8 _3 X+ V9 {$ p0 f h=pa;0 ]! @6 u& F* z; Y: P, e
k = h->next;5 j3 K. e) H# {$ n
}; U/ L( Q9 j8 P$ n) ~- }
1 ]! [* b: m0 p, b, G$ I! o# `
if (q->exp = p->exp)
4 m+ u. M8 }$ [: _& t, r {
' Z% o3 k/ f, j2 q3 m p->coef = p->coef + q->coef;% x% i- Q9 x. [8 m) a
p->next = q->next;) n. a8 x6 }' e* K& w$ e
q = p->next;" ^- [, h$ c4 m# B6 N
}
: Y: R Y1 P: e if (q->exp > p->exp)# N3 r r# C5 k
{
9 ^- K) z$ k; g5 z( c% H* n0 @0 c- L q = q->next;
4 o0 z9 d5 b5 W) Q) \ p = p->next;
6 u, D" f' V1 H }
1 ?5 z+ L( A% K& { }
; {; t( ]+ ^7 s} main()
# Y7 M1 V. C9 b0 T7 |( ~2 L, t{. F& g! i, j& e2 {! ?/ U3 U( h
int i, j = 0;' J2 r, M, `/ N- A
printf("a=");5 J) m6 R$ ^3 L7 Y! L6 t0 J
scanf("%s", a);
+ t3 b; F* J$ _3 h/ [ j = strlen(a); input(a, j);0 @& i4 m" @1 S% ?3 s6 e. A
stinsort(pa);
* J3 R* m; k0 Y) L3 B printf("a=");6 R4 M' a8 q' |# T- T/ @! }
for (i = 0; i <= j + 10; i ++)2 h2 @6 w+ o J
{) J$ ?2 R' T& x, n0 a; B6 r/ g
printf("%dx%d+", pa->coef, pa->exp);# U; x: b9 L3 \* r
pa = pa->next;1 H! s) g8 Q6 e' Q" n9 G3 \
}
5 o2 P! \6 T. h: @ r}9 w7 u b C# E9 C7 w% Q
我把楼猪的代码弄了下,把里面大小写错误排除了,这段代码会有死循环,谁有空谁研究去。 |