#include "stdlib.h", O/ i, _5 D' w( ]
#include "string.h"" ^- Y2 t: D) C) e2 q
#include "stdio.h" #define MAXSIZE 4002 G% H- C* Z* }2 M% N) z, T
char a[MAXSIZE]; typedef struct
9 e, h. j" J2 C2 f: p; p{* ~0 @3 F# H [$ w$ \, e
char elem[MAXSIZE];+ A1 n, _. [! I- D9 y) A/ l* n1 q
int top;
+ z4 T9 a3 X6 q1 Q}SqStack; SqStack S; typedef struct poly
" \4 J) G: h0 L4 n6 h2 [' \4 l{( C! R$ S- s/ D4 I" j; i1 N
int coef; /*xishu*/
) e" `: ?5 K6 R. x9 J+ K3 [: K int exp; /*zhishu*/$ ]# f1 q; G/ o
struct poly *next;+ R7 d$ E' t2 o0 I
}Lpoly; Lpoly *pa; int cf(int i) /*10^i*/
8 d5 {2 z, w, X3 |1 k9 `; @{: R4 L& ^ s& p# b0 x
int c = 1, j;
$ Q9 T9 _. }/ g for(j = 1; j <= i; j ++)
* j& o( F& g* L: R }5 d; {& @ c = c * 10;* `' v& a) f& b
return c;8 K' q5 K$ F, M; k/ [* @
} void input(char a[],int j), A: C# l- v0 S {
{
0 T# M; P+ |4 E Lpoly *p, *h, *ptr;
- a2 h, U- R+ J0 X; S: H, T6 x int i, k, flag = 0, x = 0;8 a0 w2 W; g1 W/ J
S.top = -1;0 ]" g- `* t+ K. _/ F, @9 \: E) W
pa = (Lpoly*)malloc(sizeof(Lpoly));0 z ^5 z* v0 l2 N7 K" F. Q
pa->coef = 0;
/ }0 E5 g- Q4 T; w5 ?$ E5 W pa->exp = 0;
2 V4 P1 X, H6 f. T h = pa;6 W+ N/ ~& W3 n, y' Y
h->next = NULL;. M$ |$ S$ L) B. y+ l
p = h; /*jianlipa*/
. k' T9 ]5 I: P7 O* G% x9 y8 ?5 N
p4 A V/ j" E* O( G! U) p for (i = 0; i <= j - 1; i ++)
+ i {$ f0 K' C9 p% |) | {
8 Y$ [' h; N! Z6 {) ~( C& U, A ptr = (Lpoly*)malloc(sizeof(Lpoly));' g; x* ?3 B6 Z4 B0 a
if(S.top)
$ i" c. E1 B7 j F+ m1 h {& e% y B& U1 J+ C- p
S.top ++;
; a A, U; \9 W1 Q0 Q/ l) J& I b S.elem[S.top] = a;
5 ]3 Q3 B$ M( F; @% P- {+ S }
- b4 U/ G% B( \ V/ @3 B5 b; T6 e7 \ if (a == '-')$ E( p; J }" N5 Y4 q# r+ x3 I
flag=1;: v; g* k# \$ b4 P3 J; [; ?9 y
* {+ T5 i/ ? q4 b! l( R
if(a == 'x') /*fenlixishu*/
( {. q7 s& w6 q; q; K {2 ?6 \4 B, Q6 W, C0 |2 x5 q7 Y+ {/ B9 _
if (S.top==0)8 p2 [) U n6 n4 r- t0 n, s
{; c9 q& V8 ]$ c4 N8 j( o
if (flag == 1)" `( o/ L. G8 X/ \) a3 g1 J1 P7 K
{/ R- T& G; R j2 ?+ e
ptr->coef = -1;( @1 [7 g1 j2 S0 z/ I' D
ptr->next = NULL;) x2 A! a) U1 X, S% \
}$ w V8 k2 z q s
else$ y! E% b* V8 \% ?
{
. S' o: }3 U$ K& p& n ptr->coef = 1;
1 {# q0 w9 j7 v" C# V! K ptr->next = NULL;- D0 v$ H' {( V+ j- `
}
5 L: g! C! o% N, u" n }
3 U; ~7 k; r$ J, V+ r6 [5 ^ else
" C6 Q8 u8 T" o3 V/ { {( ^" _1 g4 y+ N6 @6 F% [. s
for (k=S.top - 1; k >= 0; k --), L2 y! I( U$ F8 J
x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);1 l6 s( \* i* W0 F O
ptr->coef = x;
9 Y+ U/ d% l) i. ]$ _4 j% a ptr->next = NULL;
8 L' |, {7 f t _- z. R+ L9 m x = 0;: G" `. i, T6 J8 n# ^5 b9 _: n
}2 a9 W) L: [/ M" N' o' D) s
S.top = -1;
F. m: G# ]4 B3 Q7 r flag = 0; /*clear*/' W+ R; K7 f5 o. J# o5 I
}
* n) i4 w" w+ O1 J0 [ if (a == '+' || a == '-') /*fenlizhishu*/
2 V7 w, T% P: y! R; ~ {, ~' ^: w+ l3 y- q5 z! z u
if(S.top == 0)
7 E) G8 d9 t# B; @' Z {
; L y$ ~( l2 V7 n1 g g3 Y' [ ptr->exp = 1;
; D8 Z' T1 ^7 C Y ptr->next = NULL;
6 w6 }/ J1 k5 o4 _0 G# _) C) X. y }7 I* d9 e0 l$ {
else+ N z9 }/ f, J' W2 p( [
{; I7 ?4 c: h R+ P
for (k = S.top - 1; k >= 0; k --)0 Q6 n1 _! `9 }9 e0 f \2 B8 g7 e7 V
x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);
) ?" W6 o) Y4 ^$ S! B ptr->exp = x;7 c3 q6 Q; A8 e: p# y1 N
ptr->next = NULL;/ b, G0 F s% B' I& ^3 R
x = 0;3 C% f6 w. _' {
}/*tiquzhishu*/
7 A2 S+ Z5 t0 U+ d5 n5 v4 A S.top = -1;
; m: A& d( e. M8 h7 M }5 u: ^0 @- ~! q0 @
if (ptr->coef != 0 && ptr->exp != 0)
# q) v N6 Q& J3 | {, w! F# t7 q3 W3 `5 Z2 u
p->next = ptr;
# \* g. q' K$ ]0 D* i2 { p = ptr;
2 O3 {0 p5 Y+ {* g }$ H7 e) @1 O2 P/ M( O L
if (i == j - 1)
% V& l. S! O, h0 x/ R8 g0 X4 w {
, V5 [& a" K" |0 z if (ptr->coef == 0): n' C4 s+ t) ^! p" g4 V+ Y9 D
{. U& q* D6 Y" V5 @$ T/ T
for (k = S.top; k >= 0; k --) S3 v* v8 S" u
x = x + (S.elem[k] - '0') * cf(S.top - k);
7 L2 z" j4 u5 a ptr->coef = x;
" i; _( ?9 {9 \7 D ptr->next = NULL;
$ u( R" B- l A }3 S4 K$ U. F* @
else
0 r7 w7 d9 F9 s/ F) @$ u+ J" g {# k% w4 b5 X/ H6 L
for (k = S.top; k >= 0; k --)$ [" y2 N$ }1 y* \: J
x = x + (S.elem[k] - '0') * cf(S.top - k);6 z$ `6 ~# L% G0 \
ptr->exp = x;4 @& f% E% P/ z3 Q
ptr->next = NULL;
+ e1 I. `1 L0 {5 k }6 J; G8 {$ h1 W: \) ~) p
p->next = ptr;( `1 b3 H; O4 d/ C
ptr->next = NULL;
7 K. n# s* J5 L2 _* ~ }. a! a+ ]# x, U, N- b8 G
}8 n) \6 k$ b8 p6 f- l2 {
} void stinsort(Lpoly *pa)* _/ K) l/ }0 k; o' g
{; }6 a) ?5 I# M, f. C7 R1 @
Lpoly *p, *h, *q, *k;+ e5 J$ r. c9 Q5 X$ b" U
h = pa;5 @# l6 R; `% P- V* V T1 v
p = k = h->next;
. u0 \# d7 F% W W+ A$ ?8 K q = p->next;. l4 A/ w! @$ @) n! X& g2 T
while (p->next != NULL)
/ S6 L0 v& Y5 B* X# a {( `- Z& s' w/ [; M" X" h
if (q->exp)# W" i! ?) n6 B
{# ?* _) h7 m R: ]" ~5 f
while(q->exp > k->exp)" u$ C; D# F7 Q; k# `
{& K. o! R4 Q. o2 {% A
k = k->next;7 U1 K% M. d9 g3 h2 V- Y6 F8 M
h = h->next;
; B, q: J v# Q- i- ? }* {6 Y4 e+ |" {) H. l; M8 a
if(q->exp)- R. k# R' Y* w
{
' S' p" ?# p* n' o p->next = q->next;8 G. k P: l2 k) h5 H1 a" {
h->next = q;+ Y4 E+ F8 E+ k; j
q->next = k;
0 E6 _, Q1 S1 O8 Q7 Y9 @: Y3 v q = p->next;
# P" W: z& i9 V }, l! ^+ X4 f3 c% U+ e# N. J" m
if (q->exp = k->exp)) Q+ D; x4 B1 F' L- ~ R' ^' z1 }
{
8 t& M2 d- l) S) \ k->coef = k->coef + q->coef;
/ Z7 h4 X$ r# K" d. V2 O' h9 G p->next = q->next;9 y- z, }% B& u
q = p->next;
E* ^: P5 k; i5 \) b }2 E9 A4 \. g! I
h=pa;
$ I. j& W3 [6 r k = h->next;
; a6 r' t, e; ^/ | }: w1 X3 G) G& S5 x& Z% u7 V
2 m& V( X& O) n; Z; Y- a/ b if (q->exp = p->exp)
. u0 w9 H; j/ O) {- N2 `6 ]/ n {
1 ^6 }8 {6 V$ Z p->coef = p->coef + q->coef;$ K7 ]# h3 I6 Q4 u+ E3 L3 N$ a- @
p->next = q->next;
- m, ? Y/ ~' @9 S7 X% j) P q = p->next;5 M9 k2 g4 x3 g* l6 y0 c! W& @
}6 H. r- T' C8 O; g
if (q->exp > p->exp)- O7 l3 s' S+ K7 z, A% `: E
{- d' y& w) D$ T
q = q->next;
+ J# R% V! Q3 b! j1 N5 Y) F p = p->next;) G7 R" N# a( @2 Y" d0 y+ t; ]
}/ g! _# h( C1 s6 Z
}# Q% G T3 ]1 M5 e
} main()
$ W8 {- q0 W& I& J{
5 N' _' Y# ]1 E: ~ int i, j = 0;
" W. ~. a- `! s/ q printf("a=");
5 b# e4 ~! h, P. S, {/ F. C scanf("%s", a);% \$ L1 p2 _* T7 u
j = strlen(a); input(a, j);
0 m' z2 t7 j) A* \ stinsort(pa);
) X! F3 ~* R6 n2 M& g. J printf("a=");2 x' V9 y3 u1 R! M2 [
for (i = 0; i <= j + 10; i ++), V( P+ I% X; Q0 i2 ]5 p
{
+ @3 I& S/ b2 @) E printf("%dx%d+", pa->coef, pa->exp);
u% F9 K/ b E. P pa = pa->next;
3 D( w: {7 w' h( k/ ~8 U }
( }, Q* B' }! a}# L r, W: l% U, M( R) z/ f0 f4 y% @
我把楼猪的代码弄了下,把里面大小写错误排除了,这段代码会有死循环,谁有空谁研究去。 |