#include "stdlib.h"
' o5 D# I% p8 B, R2 I( c& h3 Z4 I" b#include "string.h"
) z2 S7 O1 `7 B( K0 i5 u! C( Y#include "stdio.h" #define MAXSIZE 400 t; ?" |5 U8 D' L, L- m t
char a[MAXSIZE]; typedef struct3 U$ o; V/ p' I) V
{- C- ]; z, X$ W: L2 `- c! |
char elem[MAXSIZE];
: M1 G' I: {9 }0 u2 e int top;) l% K0 H O+ t; U# O
}SqStack; SqStack S; typedef struct poly: F" }! ?' b& y) p) a; \2 B/ I
{( v$ g/ L: U# N' m/ O1 b
int coef; /*xishu*/0 k# H; t) ^$ [
int exp; /*zhishu*/
. i* y E T( W/ `2 ~ struct poly *next;9 A% [- W8 ^# @! ^
}Lpoly; Lpoly *pa; int cf(int i) /*10^i*/+ j# H6 z. q$ N; f
{, y1 g3 l" \8 o1 d6 I( j
int c = 1, j;7 t& C. c8 ~, ?% `
for(j = 1; j <= i; j ++)
( t0 t6 N( T- |& \* u' \6 m c = c * 10;6 F. u0 a5 [- \) B+ r" m9 Y |
return c;
: e, t/ P( t. M- r% }} void input(char a[],int j)" a8 {$ N, P6 @
{! m% F% d1 K1 w2 n0 o: j
Lpoly *p, *h, *ptr;
3 @5 S' q& G0 J V; F int i, k, flag = 0, x = 0;
' a3 |- H& f% E S.top = -1;
3 w9 ]! U [" X% V, ?" [3 c pa = (Lpoly*)malloc(sizeof(Lpoly));
5 q& C0 N0 W' D* Q' l; p' ^ t pa->coef = 0;
4 t6 @, O$ P- U. r) j5 B( | pa->exp = 0;& K$ ?/ m. C, E# F/ i( K
h = pa;
# x e+ `! X0 o+ L h->next = NULL;9 M3 Q) \$ J( n& S ]
p = h; /*jianlipa*/
; @9 p- e9 I- _3 L* v
( n) }/ \5 v6 J2 ~3 ]- m! g for (i = 0; i <= j - 1; i ++)3 S! C, s: j @2 e) Y, C! |
{; o; W: v; `+ c+ R; Q M$ z
ptr = (Lpoly*)malloc(sizeof(Lpoly));
) Q( q* m0 d4 S3 L% W9 O if(S.top)
! z6 i4 _9 f, o+ L( T {5 { E; [2 g% I% G
S.top ++;5 I+ w8 d( ?# W; q7 V0 M3 [$ n
S.elem[S.top] = a;
6 e; `* @6 s1 t/ n/ Q5 B- a }# m9 j6 F/ e8 d1 h
if (a == '-')- n }6 {/ W8 W
flag=1;
6 o: H! ^4 L1 U B4 i 7 w4 x! u! R. C j. @
if(a == 'x') /*fenlixishu*/2 i0 Q. k- }- s
{
: d1 V7 N4 q1 s, ` if (S.top==0)9 X O1 N! V9 a
{
" A' @- `5 L6 y8 t! L2 R' s6 U if (flag == 1); I8 D7 v2 Q$ X& ^9 P l' b& g
{0 `. Y+ j' n' h5 R5 g
ptr->coef = -1;
9 w/ S! G4 G5 _+ Q ptr->next = NULL;
: L# |, t$ `: c! g, I O3 f' @0 |; d }* r5 w; w5 U+ ~, s
else
! R& J# D$ \* n7 z/ x {
/ J$ h( {0 g6 }2 f ptr->coef = 1;
" a0 h& W2 C; t: ] ptr->next = NULL;
8 i% ]! }) p3 n5 i. V }
$ ?. C) S: p6 V }1 S# ^7 j1 ~) b' o
else/ r7 f. y4 P) j- X
{6 L& _$ y' @% X3 ?6 K( C8 q
for (k=S.top - 1; k >= 0; k --)
" @$ i" _( s/ @* h% k7 X! A x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);1 P# t! F9 `( C4 k/ x6 c/ S
ptr->coef = x;
! W( c7 ?7 \7 j7 q ptr->next = NULL;
8 B( I: ]& F* |- g6 m/ o0 r) \' E! I x = 0;: Z; o$ A" O* ~) g
}8 R5 s1 d2 n5 D/ x. N
S.top = -1;3 c2 n% r( `. c5 ?( P
flag = 0; /*clear*/& Z0 k3 n6 |# T6 C/ j' X
}/ m- D: d6 k6 y4 G) i$ V1 f
if (a == '+' || a == '-') /*fenlizhishu*/1 v% v- Q# k0 u
{. ^) _) R* B" I( \; E5 E9 S
if(S.top == 0)/ O. f$ a# G# V
{; a# _& _* N5 k4 D
ptr->exp = 1;
/ @2 W! ~! p& t ptr->next = NULL;
& X2 |" e- M1 z ?/ ?2 i }. `) ` ~$ x6 n
else6 o8 \$ Z. o$ I# l. p9 T, d* [
{* |) t5 ^, o8 g, A' j3 A
for (k = S.top - 1; k >= 0; k --)" C- M C; ^- M5 G
x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);
, Z8 A; y/ u, _# k7 k7 [ ptr->exp = x;
% A: e1 E( L. {4 M) l K& d- ] ptr->next = NULL;
% x* }+ F) L/ Z* |( i x = 0;! ?5 a# ^& g l/ {
}/*tiquzhishu*/
" W3 j# g C. t$ m! J9 f S.top = -1;
% a4 c& S+ i9 T9 y+ P' h* A0 [8 ] l2 i% y }/ [. X }/ B; V0 k! V' o! d
if (ptr->coef != 0 && ptr->exp != 0)/ v9 x( v) ~; f( x0 d6 }
{9 ~3 M# ?1 c' m7 u8 C; w s
p->next = ptr;0 r2 R$ S! B B r2 T* ]
p = ptr;5 y+ |6 X! k# f6 L7 e0 n
}
" n; y0 G. J: O5 ? if (i == j - 1); h; q$ R L, i0 B( U. l: R: g
{9 Y E# h/ T4 M1 c# w
if (ptr->coef == 0)
& U U& e$ a% q% d {
: L; _0 N1 O$ v, \" `) D for (k = S.top; k >= 0; k --)6 }0 J* H. Y- K
x = x + (S.elem[k] - '0') * cf(S.top - k);& S3 i4 Q5 U8 ~. ^3 n2 b
ptr->coef = x;6 U/ |) p5 L9 j* H
ptr->next = NULL;* I" W/ Z( W" f- f
}
* ~0 k# E9 j& m6 d else
1 M" z; a) y- }7 F1 M {
: F/ C; T; L ~- E for (k = S.top; k >= 0; k --)6 E5 D/ A- @9 C6 G/ L9 @
x = x + (S.elem[k] - '0') * cf(S.top - k);
# r' X; D5 F5 z. K ptr->exp = x;
) E# A0 M3 O# ]' }2 I$ q0 R ptr->next = NULL;& R8 l9 |+ k$ @0 B& q8 n
}) v9 A, u! u5 `3 _0 w, o, s
p->next = ptr;* R" q; e5 G- Y* d
ptr->next = NULL;" }9 i5 q& @/ ?
}
# B, Y1 U" j, ^* l! Z1 j! B8 A: h) D }# {; F( J% I( b0 q8 H
} void stinsort(Lpoly *pa)3 C2 k4 ?) h: ?% Y0 `4 J5 \$ u
{3 M6 H" v, I/ o9 V- c: l6 j
Lpoly *p, *h, *q, *k;
! F+ ~3 ]( z/ f/ ]; l2 Q h = pa;
7 z" S+ S+ Q, |+ Q9 R p = k = h->next;
. V* _/ J- G. d0 H4 o q = p->next;0 X0 X f% e1 T
while (p->next != NULL)
# E7 ]7 ~- X/ u2 j {
- w- n5 A6 `5 S if (q->exp)2 Q* r* a R/ Q: Z) X5 d) G! D
{
0 h5 x: ~7 E. |% W$ R while(q->exp > k->exp)
. s5 N. w. v) q+ d' y5 N l {
: o* d2 B+ m/ S2 H2 Y k = k->next;( X. L0 T- a& \3 F0 V% g- v
h = h->next;& `7 u4 ^- s: f2 i2 V7 j
}
+ L: _& P |8 R2 Z; h* l2 x if(q->exp)) {$ ~0 o" H+ H% x
{- Z+ h: Z6 B: I/ L6 z
p->next = q->next;
. e# b! U8 }$ C# n$ Y- X) H% v h->next = q;
( g. \- M1 l2 X; Q q->next = k;
* {( \$ A4 U, {: R q = p->next;
1 V: p' r% d7 T }
: O1 }' F* T5 B3 w& b: k3 C if (q->exp = k->exp)
6 b1 G9 S5 ^8 m1 A/ V3 v {
r. w0 M( I, F k->coef = k->coef + q->coef;
`5 F; s/ K& z9 t2 g! W4 ? p->next = q->next;2 r# {# V$ r' ]
q = p->next;
2 e. U( M+ D0 X5 @6 G' ^: Y9 k }( H9 c' z3 t) O$ z& x
h=pa;! H% z0 p* x- V
k = h->next;3 D D$ w/ e0 e/ ~3 k
}8 n; i" w0 V; H; x* f6 y3 j0 {
% `3 g2 ]7 d; o7 L$ j" N# l# W if (q->exp = p->exp)
l& b- s: t# \" B" n {
5 k( x2 e9 B) ~4 F8 J7 ^ p->coef = p->coef + q->coef;, C' z) i$ u2 M
p->next = q->next; P$ A" I# y8 j5 B
q = p->next;
. ` z# n7 \9 p2 u- j7 J* j }
, ^! r' q! P7 d0 X0 U if (q->exp > p->exp)
, j1 P- W6 J1 t9 U9 I {
" f* v4 y7 i% E$ ?' A q = q->next;0 i J* `( a7 c5 V! d- V
p = p->next;
* Q! ]! p1 I- J. K5 E6 K& t }
# V2 w* n( F/ L) Y" n) ?9 z }- N K/ B4 x2 c. a- {: ]
} main()
, w0 G2 Y$ y6 `) B{8 ~( d+ C+ r, [9 G5 X5 \3 _
int i, j = 0;
4 g4 l% B: k& N5 |+ C6 | printf("a=");! Z1 r7 T5 m$ ^; k- g# B8 ], v
scanf("%s", a);; ?5 E2 O6 _: d/ u6 X
j = strlen(a); input(a, j);6 c0 Q* [' ~& u: j* x V8 f5 _& {
stinsort(pa);
3 p' e6 T# D2 k/ D" r; v4 B- ^7 ? printf("a=");
' `- E. j% e3 k4 u3 @2 d for (i = 0; i <= j + 10; i ++)( U. u" V+ ?9 i+ b
{" Y$ f5 ^( w5 f$ l5 @$ D
printf("%dx%d+", pa->coef, pa->exp);
! Z$ ?* s* i6 ^1 c pa = pa->next;
; ~5 @9 m2 D9 u+ z+ ~% c }' C9 K: U- k5 M' I% d
}" T) N- b% l5 g6 _3 x! T! V& L
我把楼猪的代码弄了下,把里面大小写错误排除了,这段代码会有死循环,谁有空谁研究去。 |