#include "stdlib.h"$ g! S/ {! u+ ^ l/ M
#include "string.h"& n( Q/ U& } M
#include "stdio.h" #define MAXSIZE 400
; C! ~, c8 p0 K+ ?7 g' x3 s, Z/ zchar a[MAXSIZE]; typedef struct8 T5 O X) R% z0 u/ e- m6 U# ^+ \
{
# }# s2 E1 p3 P* u |2 S char elem[MAXSIZE];- T- C+ q$ T3 X+ V N X
int top;6 U1 a! c9 P2 h# U
}SqStack; SqStack S; typedef struct poly) t3 J- R4 |, `5 s5 k
{2 H# e; s( n5 d& P2 b
int coef; /*xishu*/
. k" y; l0 x( |0 q1 o6 C& u3 i int exp; /*zhishu*/
1 f% x0 n: E) i8 j& d struct poly *next;
! @- Q' K8 S( b1 S2 E) D, V, G}Lpoly; Lpoly *pa; int cf(int i) /*10^i*/' S% n0 E9 x$ A# ~' _. @
{) _3 U& W# Q) @$ j7 C) O
int c = 1, j;6 x& R$ c$ p$ [
for(j = 1; j <= i; j ++)
& A( j; _- e# j) U- E, ^ V c = c * 10;4 m9 C) g4 N C, k$ q$ j v
return c;
- x" q( f$ D& z! X& q8 I6 u7 C! Q} void input(char a[],int j): q7 b5 b" }9 o( g+ o* t
{" r! c' I& Z1 P/ N8 z& |, `$ A
Lpoly *p, *h, *ptr;4 @! [: Q- g1 @( ]1 e
int i, k, flag = 0, x = 0;
' u F; S, W7 B) ^ S.top = -1;
9 B5 k5 T. X! v' A% E pa = (Lpoly*)malloc(sizeof(Lpoly));9 q' p& w; J/ v$ [
pa->coef = 0;
( _/ h- b+ t$ g& b$ T2 ^ pa->exp = 0;1 b7 [8 [: t% d3 q9 K
h = pa;
; ~8 A# ?5 R/ }4 T( @( r$ c; H( t h->next = NULL;. ?! ~2 X; D( v9 z; W
p = h; /*jianlipa*/9 Q/ J V+ }: N9 T9 |' ~$ X, Y
8 v7 D0 A& T9 h2 A; h0 e: L for (i = 0; i <= j - 1; i ++)
$ m) h) ^' @- a$ i# B: d4 `* a: x {4 b& |) _+ x1 t. C( H
ptr = (Lpoly*)malloc(sizeof(Lpoly));
* {1 [$ j- V" P- \4 O if(S.top)
+ W2 [5 d: P: p) ~, ^, p2 ? {- T3 o' k: z8 Q4 Q8 [
S.top ++;
0 q) m2 D& k4 u8 |) k, j S.elem[S.top] = a;
% t4 z0 Z! n- H& z0 q; F }) K/ f( U( m; F5 G/ m0 o* @
if (a == '-')
' P) S. ?" [9 [0 M' _3 u flag=1;
& }: g6 ]* a4 g4 E4 f' ?% @
/ a' u7 l+ F! c if(a == 'x') /*fenlixishu*/) ~) K* }. Q0 J
{7 W+ x) g# z6 Q$ ?" Z
if (S.top==0)5 z3 t' {. {0 d V; [
{; X) y F& M t5 @. _9 w
if (flag == 1)1 x+ \& w8 N w) r
{
/ t4 x ~7 L) P2 r2 h ptr->coef = -1;9 h# Y* N, | e
ptr->next = NULL;
: l2 H7 N( A m/ N+ z! t0 `8 a }6 S: n8 ~/ u: O' q
else
0 l3 I8 w k8 T9 k" R5 S {( Y+ N9 s% ]8 `' l: ]0 C
ptr->coef = 1;9 \; y" F$ j$ @' Y) Z' P7 u1 a0 ?0 d! r
ptr->next = NULL;9 _; u, n3 N5 C! b" h
}( F+ F) O3 ~) \8 t' V
}
) Q- v' q* x8 m) U else
5 E# I& y n" J2 C; w4 m" p {
8 c' f7 \; Z2 a$ R4 i for (k=S.top - 1; k >= 0; k --): m; C3 H6 A8 |( O
x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);4 @3 ~5 r R$ b2 q. f% W U, \
ptr->coef = x;( c3 X& K% J* |& M0 ?# d
ptr->next = NULL;
+ H( N( i0 V( i7 l! c( ` x = 0;0 A: Q3 c" a- C3 Z
}
! q ?0 S* ~5 [1 {2 D S.top = -1;6 Z4 d, L' o" }
flag = 0; /*clear*/
+ C- n [- L: b3 k2 P% \# Q }7 e H4 r. n+ a! }! c" T+ i! ^1 z
if (a == '+' || a == '-') /*fenlizhishu*/- X2 _5 u1 ]! c7 y8 v) @% r: @
{
6 e L0 H, Y9 \0 J# R2 Z5 Y) F if(S.top == 0)
8 D' [% b, m8 i T2 W {
Y7 {7 I: B, [" l' Z, M ptr->exp = 1;
/ o) U! U. W! j$ u& s5 k ptr->next = NULL;
8 u1 _8 x6 U' k+ u }
# T& u* V/ Q; b4 m7 [ else. d* K; P5 [$ A$ b( Y. G6 @ Y
{8 F/ y- B0 O/ F$ l
for (k = S.top - 1; k >= 0; k --)* A% r7 ^* h' U' ^1 Y
x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);
: F: H2 C! R- H2 j3 \& J ptr->exp = x;
~) q' R7 M/ {: m ptr->next = NULL;6 q) R! [' m4 f6 D! c
x = 0;
# E, M, h: v5 N1 E( H9 |9 e }/*tiquzhishu*/
0 }+ t& e* L3 |; ^ S.top = -1; H @9 E0 F9 u' K
}
4 c. _0 |6 }7 T" W! A U N if (ptr->coef != 0 && ptr->exp != 0)" `; i0 L% @; `2 g; C5 x
{+ p6 C1 [ a% z; H: }8 B1 d/ o' c
p->next = ptr;
# U) ?: y; x$ V2 E' T p = ptr;
7 I* D+ x7 B% [2 R1 B+ P }
u# L& ~. X4 t: c$ V if (i == j - 1): k0 w6 c" X2 h w3 i. c; K, [* B0 D; @
{ s; e8 q# a( }3 M1 u
if (ptr->coef == 0)
/ O5 G0 X' n0 t# n9 P {
$ v0 @" s5 ?* Z! Q$ H5 W for (k = S.top; k >= 0; k --); [ g1 ^8 N7 P% `, F4 E9 F
x = x + (S.elem[k] - '0') * cf(S.top - k); v& |6 _6 o6 u( n) c" G
ptr->coef = x;
% ~% p% Y' c3 P. P ptr->next = NULL;
, k5 u3 \' d E9 M! H# B3 ~ }
9 e- W, F5 H5 K else
" r$ Y& F: `; I: S {/ J, f6 i. \$ [2 n% M- A, L
for (k = S.top; k >= 0; k --)
% ^2 Y0 R- ]& M; I/ {$ W! F x = x + (S.elem[k] - '0') * cf(S.top - k);6 z: ?, w7 O0 m o
ptr->exp = x;" C' D& K& R2 M# `' H% x8 |& H8 K" C
ptr->next = NULL;5 @7 q. P8 I& o; g+ Y6 L9 x- v
}' S7 ^. s3 r( y4 y/ f. z
p->next = ptr;0 s- L- M0 y* U
ptr->next = NULL;" C$ |" x y) V. [
}6 E* M) {8 F# H4 L
}
2 [/ @& q2 u5 q/ ~} void stinsort(Lpoly *pa)# E( ?& \0 m$ w, E
{* x! n2 i; N; a7 _4 z
Lpoly *p, *h, *q, *k;
; k; b! C. w. O3 A e h = pa;$ ~$ i, R0 W" G! S4 o0 M
p = k = h->next;: m3 D0 @1 [) {* U0 m( m* F# @
q = p->next;0 @( T" l* c6 k" S
while (p->next != NULL)
% @, {0 k' ~( [4 _% o, p {
# ?. l9 s+ W! ]: ?; k if (q->exp)
! D! t- B" R& a" x$ r {
$ c( L: V" r6 ?3 Q# x" U+ m" T while(q->exp > k->exp)1 [) |1 O9 D' h' L* X
{. F+ q! R# }' R( U) ?' d
k = k->next;
/ ^6 j1 f9 N+ R! d& t6 y3 O h = h->next;$ e# v4 m! W' Y, i. v; f
}8 W: t$ z) r% ` R0 J
if(q->exp)7 X- L7 m+ J) a' S/ Z7 o/ t
{
! q$ t! S$ o9 @# t/ w# K* A p->next = q->next; `" I, a1 V) r& ]$ G4 V
h->next = q;
' }* |% k: a# Y Q8 s& ?2 u4 P2 S q->next = k;* `. N! c+ [0 Q' T1 g! o5 h
q = p->next;
+ O0 G: M# {" U+ H }4 f0 d) O) x3 @1 }- M. d$ J' O9 O& {
if (q->exp = k->exp)
& N, t' c3 s# B7 d {5 J% z/ k& `; j: H( ^
k->coef = k->coef + q->coef;
/ l5 R% k$ U4 C9 j4 B% r! N p->next = q->next;
& }" O `/ \ ?9 Y: b$ G7 h e. [ q = p->next;( j. i- \/ j* g5 B7 h$ ]* e
}6 E! c7 X* ~: _$ c0 `
h=pa;' p2 \4 Y0 C* [) M: v$ x* R5 M
k = h->next;
4 @8 T' u* s( m6 W: p) w( U }
& @. Z: y$ ^; h) E4 M& S, w
6 l5 a+ y, n7 N if (q->exp = p->exp). ]; m. b( @0 r$ B
{
& f/ G, n: E: m, E- x9 G% g p->coef = p->coef + q->coef;( W A3 g: a0 L( x6 x
p->next = q->next;- A% X: f4 r) k& b7 P4 ]! D' g! B
q = p->next;
! j" i! P: t. O }. ?# ^) q% E& D' L
if (q->exp > p->exp)% Z- I+ `5 f6 p+ F% I9 Z4 D
{
" X/ o; `$ b0 x# M/ Z6 H4 r& G) D q = q->next;
! f$ V0 X1 j! M2 ]% x! p8 Y p = p->next;6 u- _" _: g. C
}& r# D4 g4 \$ n* k
}2 ~9 u+ \! y) ~4 p: Z( t% Z& E
} main()
% w% b' L5 W. `1 W+ ]6 g6 \$ j$ X+ o{- E3 Y' h; G$ [% T- l
int i, j = 0;
+ X: k o/ W4 h K+ { _ printf("a=");
8 J% a: s( T9 M scanf("%s", a);
; ^7 D1 Y& o( e( J6 K3 H j = strlen(a); input(a, j);" B& e6 c# q n5 T: v( q
stinsort(pa); j6 L4 M' {0 z- w$ h3 [
printf("a=");6 l- j9 I8 |" e% f4 c
for (i = 0; i <= j + 10; i ++)5 F0 @5 C5 x. e3 t: Y; N
{
# e- l( C( {3 Z5 i: \7 M printf("%dx%d+", pa->coef, pa->exp);5 n2 j$ h% g x! N3 s
pa = pa->next;4 {& ?1 K# g3 t8 g
}
3 V, |8 |# p: m- @}
) o, K' c/ d' G7 p/ _3 x0 x 我把楼猪的代码弄了下,把里面大小写错误排除了,这段代码会有死循环,谁有空谁研究去。 |