#include "stdlib.h"
/ {0 c. h' V, T4 n6 {#include "string.h"$ |' ~" r% V( Z+ B8 O) g; n6 L
#include "stdio.h" #define MAXSIZE 400
+ U- q% V2 K) @! Q l- Hchar a[MAXSIZE]; typedef struct
; G1 F8 e- A+ p* o{
1 I/ m+ _1 s3 f! O$ N char elem[MAXSIZE];# `+ R: K; k' |, j' z- o$ D2 Q
int top;
7 o: ^! q) R' }) Q6 [4 g}SqStack; SqStack S; typedef struct poly
1 v4 A' I( P4 `{, K. l& { d" E7 `0 Z
int coef; /*xishu*/
6 F6 @) q4 N: |" ` int exp; /*zhishu*/
% c* S6 w G7 b+ x+ r struct poly *next;! H5 h7 L% m- G- x U
}Lpoly; Lpoly *pa; int cf(int i) /*10^i*/
& A: [. D0 m8 f. w! v{3 b- g5 N7 h" x2 J
int c = 1, j;# R: S0 w$ K; Y6 l# ?3 f
for(j = 1; j <= i; j ++)
Q+ v& J) I$ x7 o8 F- E5 H c = c * 10;& _/ Y2 |0 x9 _8 N4 M* D4 F
return c;
+ l9 h# b, Q9 F9 `" y. N} void input(char a[],int j)5 y( H3 R a6 ]& }$ ^" ?+ J' \
{
" r/ r' D9 q* r! [. ]" x Lpoly *p, *h, *ptr;
1 k- ?- u9 {6 ?9 c/ ` int i, k, flag = 0, x = 0;# R! P+ M/ v9 ~1 L
S.top = -1;
* N* n7 `3 {: X8 F, M$ n pa = (Lpoly*)malloc(sizeof(Lpoly));
3 f/ K( K" k& C# N! v) u! ^. B pa->coef = 0;
" l5 d) ?1 n3 p5 S$ \% J9 @ pa->exp = 0;
5 s, G w, t' K1 j& [/ P$ V5 n h = pa;
5 S0 n2 P" w# f h->next = NULL;/ }; j- W( o( H# e. O; m
p = h; /*jianlipa*/# \* E( I5 h \/ [0 L: @# O
) S0 |3 ^7 z$ H: L for (i = 0; i <= j - 1; i ++)6 P0 h% A X: U; ?
{
5 Z5 X/ ]; S5 ]& X A- U. ~ ptr = (Lpoly*)malloc(sizeof(Lpoly));9 H) u, m6 B* m/ o- }/ X
if(S.top)! N z+ h: Z: C5 [
{
8 |8 ~9 R5 a1 O* H S.top ++;4 `; v* b. Z( a4 \
S.elem[S.top] = a;
: Y/ }9 i: }2 l" G }) j" X$ }4 s) e/ F( Q0 e' I. j2 |
if (a == '-')
, U3 x* l* ^. V3 d' N$ T5 Z flag=1;* j! X- d( E& v+ \' {! {
4 M O. I: v! }( T9 ^, `3 P
if(a == 'x') /*fenlixishu*/
0 G8 L* o& u, r {
% A) K9 o1 w2 ` if (S.top==0)
! c) p& t' `$ N {& l Z8 \+ d) n7 x- x+ U' O
if (flag == 1): n/ r& x3 P A7 s+ V9 y; }
{
. h% L0 S2 _2 t' S ptr->coef = -1;
* R+ ~! z. b+ Z5 M/ P" B) ` ptr->next = NULL;: w1 Q# g7 f4 d* j
}& _; _* V, J- W( i& d: A( f
else
2 T5 d8 o+ F+ n& n! J {; A0 y! @) `. h1 U% j8 C& Z
ptr->coef = 1;
" V8 q+ J3 X0 S! a2 l, r ptr->next = NULL;
# C D3 f7 B$ X6 D6 M }5 N& u+ I8 t+ \1 h2 f
}
+ T2 ^; S; B) O2 ^# v else/ ^ g3 g3 `2 v5 X# j; _
{
+ {: N! ^- o8 r9 E for (k=S.top - 1; k >= 0; k --)- z# `7 _0 w+ D# y: T7 {! j2 G
x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);
5 G) X0 v) K, S0 Z# i ptr->coef = x;2 A! x! S n( S _; W. e# D: N, N% j
ptr->next = NULL;* W8 h5 z' m; }: P6 \1 s
x = 0;
( W: o0 I/ t6 W }& f# s- j; |+ |
S.top = -1;" R0 O, ~; c; M' a
flag = 0; /*clear*/3 ?. v2 H1 ?/ V
}
6 d5 j: v) W N- l9 {, H if (a == '+' || a == '-') /*fenlizhishu*/( _; p5 `, J+ g
{
( ] C1 }4 l3 g( A3 T if(S.top == 0)
7 o' p3 J, i2 t' u: b) O {- ~$ `: D; ?# ?8 [% `
ptr->exp = 1;) n/ n; @& N" f
ptr->next = NULL;
5 h0 Z7 ?" `3 I1 l }! `' Q) r% D# K/ X
else
$ F& p3 `, r4 `0 g8 Z. m {
7 t: M- [7 a4 W5 e for (k = S.top - 1; k >= 0; k --) a3 G) ]; `2 e% `- w+ o
x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);
" x, u/ c5 F" `8 ~5 u ptr->exp = x;
9 O$ O: s Y$ e+ i, ^ ptr->next = NULL;" X9 g( y1 Y A2 w
x = 0;
# |3 ]2 @' r+ C- L1 l% h# X }/*tiquzhishu*/" |; E7 m2 S/ w
S.top = -1;
/ c5 u, I* A& N9 H& |0 `+ _% e. t }0 X& V5 h$ `, K6 Z# H
if (ptr->coef != 0 && ptr->exp != 0)8 j& n9 F& \ u
{6 _. V; _7 t' g8 e! l6 S+ k0 w
p->next = ptr;
* O3 q. Y5 h, r" u! p) u( b- o p = ptr;! k) n0 G+ N2 e0 O9 e; a
}
- M) w, R& k2 n0 b* C if (i == j - 1)
4 Y4 X6 J' E' F0 g: k) p {' I7 p" g: U8 Q- n
if (ptr->coef == 0)
+ F4 g) t W# Q0 d* t3 x# l {
6 ?2 m4 X5 z; H0 y# [7 l for (k = S.top; k >= 0; k --)
) }1 {" R) m4 [9 t5 K9 k x = x + (S.elem[k] - '0') * cf(S.top - k);% p7 x, _, u- r% \4 z( t \4 V
ptr->coef = x;
- A$ K5 A! s2 U/ s/ T" e ptr->next = NULL;
, U3 t! F) }$ ` }
, q- M1 u% W! P, a* v1 _) ~ else4 N4 ~/ ~& |; N8 n1 d$ U7 \/ R4 E* c
{
, v: r& T8 M& f7 Z7 S9 c for (k = S.top; k >= 0; k --)+ c- u0 z8 {: u' ]) R' D
x = x + (S.elem[k] - '0') * cf(S.top - k);
# {6 S4 `/ |8 c$ y/ Y: x ptr->exp = x;# [' X; o( ^; s9 E0 P2 W
ptr->next = NULL;9 x- ^4 {6 l; Y9 B6 J& W1 r
}
: g! b2 ?: o# t/ G* f" N p->next = ptr;& f: l- H* |+ I1 J
ptr->next = NULL;( z& y5 ^8 n) m- ?
}
4 _5 S8 T9 Y3 Q2 Q }
' y& j1 n( z% v' d8 @3 A( ^} void stinsort(Lpoly *pa)
; ~: ]8 \1 a4 n; b# m% }) y+ v{: a/ o2 H, ?6 s: a) k6 l2 H2 U+ v
Lpoly *p, *h, *q, *k;# ~% `; {& x$ Q# L) F, H' x8 F' W; Z
h = pa;
0 K0 V# g( B: I- Q+ l p = k = h->next;4 ^# p5 U: j& O! R3 `1 u6 z ~
q = p->next;& T% V' d- I; K5 g( L
while (p->next != NULL)
( S# b+ Y" g' u9 t! ^) i {
8 `! M2 B: H$ e! j7 C' y9 | if (q->exp)
, k K: J% [) L- z! E, m {
2 l4 Y+ o' C- k3 g: }" o' d while(q->exp > k->exp)
& M R$ K+ G: V! W" }1 F* W {7 i1 \( \" c6 Q8 {3 i t
k = k->next;
% V- S. T0 @5 I7 i h = h->next;# J+ j" C- F- e( c
}6 s: P# `( S! I2 j& k9 g' g* Q Q R
if(q->exp)
* D5 P5 e- t2 M. Y0 {4 x3 ] {) D( |0 `. A" f! O0 ]
p->next = q->next; A: a3 t' U- ]) @1 L U
h->next = q;. |" S( G- H* I
q->next = k;" i: z% N4 Z+ g X# ?- `
q = p->next;$ l2 G: c6 p2 k& s) |0 @' V
}
) X+ E$ F6 R0 Y# k+ S: y# o if (q->exp = k->exp)
# G" n6 H8 e T6 v {8 E- d( U2 Q8 Z3 c4 A2 [
k->coef = k->coef + q->coef;" D6 Z# [# F' A4 L
p->next = q->next;
" m. \' C l1 f q = p->next;$ Q: f: |, e" ]
}
# `' |6 U6 g# }4 D6 A h=pa;2 [+ P9 q1 U( g3 ?
k = h->next;
: I4 D) W' g H! ?- z, y" P1 O }
* b+ O% A8 e2 q% \8 ^
4 L2 U% e! L" z' R% g$ a" n/ i2 [# j if (q->exp = p->exp)
+ J0 m* y0 c( `& Z {
B- I. C4 L1 p* |; X2 X" y3 ~! H* b p->coef = p->coef + q->coef;
; I4 i+ {7 }9 u' ?/ ~1 q p->next = q->next;/ P; U1 Z! A) B! |$ e9 M3 S# _
q = p->next;) {6 [, S7 i# x U1 `3 ~" ~! v# l
} J9 s' j. v" S
if (q->exp > p->exp)- k3 R6 T7 M' Y9 L
{
, c: M/ `" y7 R. i# @( m q = q->next;
* S3 }* S( t( E% @+ W* U p = p->next;5 X$ R) ]- ]$ X
}
4 {% a9 L7 b& B5 n4 J( s }
! z6 Q+ F7 g9 p' {' T6 y7 `} main()9 L# I! P4 O6 W/ H
{, M8 p1 d2 r) D' g/ F) s8 S8 ]
int i, j = 0;
4 N/ g, ^% _" g9 o! Z6 v9 C printf("a=");
+ E" [6 n: G1 W' T scanf("%s", a);/ g/ g( y3 \! r' A
j = strlen(a); input(a, j);5 Z1 d; p% i4 s$ D9 R; B
stinsort(pa);
8 i. U7 q3 b9 v( @+ [ printf("a=");
9 {/ R& W9 c) Q. k. q for (i = 0; i <= j + 10; i ++)
, P# Q- }! ~6 E% ` {
' U# _! a1 H9 q5 Q* [ printf("%dx%d+", pa->coef, pa->exp);. J5 u) O9 ]( z9 B
pa = pa->next;( j+ ^' u9 q3 f3 E2 _7 {
}6 o& k5 O& v3 H. B( u
}
/ _2 i# z6 _5 Q: u4 S. Z 我把楼猪的代码弄了下,把里面大小写错误排除了,这段代码会有死循环,谁有空谁研究去。 |