#include "stdlib.h"
! f+ c# v5 q! j% A$ X#include "string.h"0 f) `. M7 D7 q$ s! B5 D( }1 q
#include "stdio.h" #define MAXSIZE 4009 J5 _$ ~6 H9 T' t7 n9 X
char a[MAXSIZE]; typedef struct
- H7 `7 E% E$ P! t{
) b* Z0 q0 T( M5 | char elem[MAXSIZE];- ]9 z, q1 F4 Z6 [# v
int top;
: S$ y! W4 B8 w1 L& r}SqStack; SqStack S; typedef struct poly
+ l F. o( r0 m, x4 N4 ?{; J/ \* f; Q, e, _$ ] }) g
int coef; /*xishu*/
+ Y# B' S% g: y& v int exp; /*zhishu*/0 P( p. j& F" h" \9 W
struct poly *next;: M2 v8 Y7 T# v& a' T
}Lpoly; Lpoly *pa; int cf(int i) /*10^i*/ \6 h( ^3 H/ N5 ~! f$ q. ]
{+ v' p( w0 B/ o A
int c = 1, j;+ F4 ], Q h; r3 \9 U
for(j = 1; j <= i; j ++)
* w2 h6 M: h I8 j/ v3 k+ N9 W c = c * 10;
( ]2 c- W n+ F' v return c;% w" t! A5 U+ E( K9 ?! t
} void input(char a[],int j)
0 Y% Y8 `; U' j& ^+ C z1 a{' p# Q( h! _- `& `9 Y
Lpoly *p, *h, *ptr;
1 m c& ]( |6 g int i, k, flag = 0, x = 0;9 u3 O' w: V; k$ i
S.top = -1;
- x8 ]) L. M. V' c8 d- O! c pa = (Lpoly*)malloc(sizeof(Lpoly));
; o; D8 i' z1 G. D" I. { pa->coef = 0;0 F! L2 a0 X/ K! F( I
pa->exp = 0;
/ c1 o. s- x0 I; M' X5 O5 f5 j h = pa;% F) w) h( J: [# N- i A* [
h->next = NULL;0 r# o2 Z) B8 D' y4 t& V) p
p = h; /*jianlipa*/
$ X' e* Q6 s- h3 O( a& u$ I7 J+ c ! \2 ^, c7 v+ K$ V4 r& N
for (i = 0; i <= j - 1; i ++)
* b, k) w" c- J {0 O$ e3 n/ A0 {
ptr = (Lpoly*)malloc(sizeof(Lpoly));
_7 B5 c i) o8 \$ F f9 W2 V+ z { if(S.top)
" y. h3 n5 O4 o1 s' [) p8 t/ { {
4 D5 y0 p" _# q S.top ++;5 R# G3 j( N& D2 |5 C5 c; w
S.elem[S.top] = a;' ?% O& |; |1 Q. w
}
7 `! H; {4 h2 U+ C if (a == '-')0 [2 ?+ Z) A, x/ B
flag=1;, R; f9 n0 X M9 ^7 C" ~
3 A# Z) q! x! q! U& K if(a == 'x') /*fenlixishu*/
# W3 P4 ?3 y! _3 v {1 V0 y S# C* b/ J Z* \4 J% K
if (S.top==0): Q& \ s0 k$ k% ^3 H9 j0 k
{- L( v- r' Y& A* O0 M! f
if (flag == 1)
/ \7 q7 C' I; F5 M! [ {$ r$ Z J8 K" `/ ]' N8 l5 Y
ptr->coef = -1;
$ k& `) w- d- x8 w! { ptr->next = NULL;$ a, n4 X8 v6 A, V6 j
}3 |4 I6 c% X. [) W
else, i# D- R5 W9 p/ n4 x
{2 H/ |# F! M9 _: j. C
ptr->coef = 1;8 p, ?, j! I4 ^. S @, x5 [5 b# f
ptr->next = NULL; P2 ?$ T8 [, g2 O% N+ ^7 _
}
' A6 u+ N! X$ [, p }
s3 c8 v, X' x7 @ else1 T) c8 C3 W& O
{
! j8 a4 N' Y* a# a! g# F X) ] for (k=S.top - 1; k >= 0; k --)* X1 G. V& m5 {1 Z( \; R
x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);
+ c& T0 p4 |* k3 V ptr->coef = x; C( U7 R! q* E$ \: s
ptr->next = NULL;
6 V" Z X7 o( y$ S! b% e( V7 T. h1 D9 F x = 0;
. u: E) s" e2 w9 F }# P5 R k# ]- D0 S. ~
S.top = -1;" E% U, M# d2 o9 s
flag = 0; /*clear*/ X% h1 `" c! E4 B/ ~ E
}
/ ~8 o( B, }& O* }) n& [( p if (a == '+' || a == '-') /*fenlizhishu*/% e7 Z6 ^# p v
{! B. S7 r' u: [; W
if(S.top == 0)
; W* V1 R+ O* @0 p4 C8 S {
# S4 D3 f% j# p( L ptr->exp = 1;
1 W3 J) U% c& p& x% Q ptr->next = NULL;0 z3 F" K' O( [- o" t
}( h( v6 C* ^, Q3 Q
else% w: ^" Y9 a' W5 O# I6 o% ?$ z
{
2 V/ P5 R* A# a2 e/ k. p/ S for (k = S.top - 1; k >= 0; k --)
' G- ]5 n5 d! q1 o x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);' i# {! G! z1 N
ptr->exp = x;2 j' I1 ?8 M5 |- o" N5 T
ptr->next = NULL;
/ C/ f! a( P0 ?' K+ I& z x = 0;0 a3 p- r D: D" [
}/*tiquzhishu*/: }6 f; v1 X7 j J' l
S.top = -1; W) B4 O- c3 t' l4 E' b
}0 l) k. e G% X/ r+ E
if (ptr->coef != 0 && ptr->exp != 0)
' l! w! s" _) S {4 l: S x5 B" T* }' @$ @) `
p->next = ptr;: r3 I6 I+ y. [5 b5 B8 B0 r
p = ptr;( ^" j* Q, c! U& ?8 `! l' X0 W
}* ~6 t0 O B" N+ \1 g
if (i == j - 1)
' W( z/ h! m- f1 v3 E0 W4 U {
( o8 n( Y- u. B; d" s if (ptr->coef == 0)8 Y# M. a/ U, B8 P
{+ a$ S, S& C% U
for (k = S.top; k >= 0; k --)
* i$ J* @- D. ~: D. B x = x + (S.elem[k] - '0') * cf(S.top - k);
% F6 z9 N" j3 x+ [) C; I ptr->coef = x;
' q& I0 B$ V- F( Z2 U ptr->next = NULL;
; ~ t7 W5 X, Z/ i* ~# R- C" ?8 l }, Y, X" V" |% P; k
else
; h% F8 _1 S7 M% t% O! m {
* i2 W# o& ]# {# y e for (k = S.top; k >= 0; k --)
" `- t6 X) ?0 [4 }) z( r4 `: g' W x = x + (S.elem[k] - '0') * cf(S.top - k);
7 y/ y! q" V" k0 w1 | ptr->exp = x;
$ V4 s4 K* n* ~. G: ` ptr->next = NULL;+ y @2 L( w- j) S8 J1 n
}: N+ @0 ]: P7 y( b' X$ x0 U( }
p->next = ptr;
* p8 |" p7 a# b3 R# b" H/ @; b# S" M ptr->next = NULL;" ]) j1 o$ [# A2 i( X% t {; ^
}8 ]! U" r, B# h2 R! \9 m( F( O9 K
}0 j; A% B/ G: x+ U+ P9 ~$ t4 _
} void stinsort(Lpoly *pa), M* w u3 E X
{
7 y) h8 |* D: u3 t" `4 z Lpoly *p, *h, *q, *k;
0 E9 \4 Z9 P4 e4 h! U h = pa;, Z6 T/ R' F8 _- K1 c& V. J
p = k = h->next;8 C9 ?/ P ` M& e2 s4 x4 h/ k
q = p->next;6 M" Q8 `/ b+ m
while (p->next != NULL)
5 n* ]! E$ T4 }- K; T {; w& v( ]" H# b% ?* f& n6 X, r# q
if (q->exp)
( d: S. }+ ]! h {
- U( z7 X2 W" d; g# J7 H while(q->exp > k->exp)
* r) W1 o9 b, q+ G. U: b {
b3 _8 U; D) u k = k->next;
& x9 i1 n8 E( J/ Z- m1 D h = h->next;
0 H8 h, ]" a8 x: a9 y6 ` }5 G( G6 s$ T' E! w
if(q->exp)3 e0 U" m2 T, D% E: b( X
{
4 s7 `6 U+ P" h2 z4 H/ | p->next = q->next;
& f. {, M% x7 u h->next = q;
0 U+ ]# P/ J! F7 r- K q->next = k;* ~) h' \+ [ B: h# ]& G$ C
q = p->next;
6 X0 {- B4 z+ K } Q9 K, p& s5 Q1 G+ f
if (q->exp = k->exp)- l# X! l; P; N1 B
{" J2 u$ _5 [. {- ?/ {# K
k->coef = k->coef + q->coef;* z O" r. O# w# }
p->next = q->next;0 v6 G8 A. D; H5 s( w
q = p->next;
2 V5 o+ U( s! L0 Y; V2 v9 o# ` }
% X' O) u! G7 z6 h5 `, { D h=pa;& c6 g1 y. x* j
k = h->next;
, |' J. X- R+ B P }4 j; n m3 n$ O
- S! i: J! ~9 T if (q->exp = p->exp)
0 p5 R/ u/ ?4 F; t$ z {- _6 j' O$ D" M4 ~
p->coef = p->coef + q->coef;
8 Q# x6 s( F4 v) w$ B6 l p->next = q->next;
1 S* j* @2 A2 A7 h q = p->next;
" l6 i t# @: L: Y" S }" P& i7 y' p. R8 T- Z& i
if (q->exp > p->exp)
2 h i( Y* ]9 c {0 m/ |; P m+ i0 v6 b
q = q->next;, w8 z/ b" `- y% B, z
p = p->next;$ C; s4 L; o- B* K$ \; D) a4 @
}/ o& s9 d) w5 q5 ~
}
5 j% w& ~; R; `# D5 I# h} main()
% n# v. C1 E L$ y1 [{9 y* I% K4 l% ^
int i, j = 0;9 e; c$ Z5 [ U- z( P) x7 C/ m6 H
printf("a=");
" W# f3 ~7 ^# b4 N) ]# }/ h' u scanf("%s", a);& u/ v/ O% i/ z, m+ k4 [
j = strlen(a); input(a, j);
w$ G- ^" ]6 C) w/ o stinsort(pa);7 K: ^0 t( F( j0 L% x" {# h
printf("a=");
* I4 H( p2 q5 \, r% } for (i = 0; i <= j + 10; i ++)
3 h: h9 Y" k9 r6 R( b, g( T {- @6 ^) U }2 v: F
printf("%dx%d+", pa->coef, pa->exp);6 {$ u' b7 N& l% T/ R
pa = pa->next;4 e4 m4 [" L% Z7 T0 H8 t
}
, q8 B& n: o& [; j% _, ]}& M( C1 R9 y' ?- b# b6 e/ E
我把楼猪的代码弄了下,把里面大小写错误排除了,这段代码会有死循环,谁有空谁研究去。 |