#include "stdlib.h"
- q o5 A8 ?. j: N#include "string.h"
2 h. I5 g1 t; h1 O* M#include "stdio.h" #define MAXSIZE 400
" w6 w+ r$ t, o, V/ gchar a[MAXSIZE]; typedef struct
! ]9 ~$ M% D2 Y{
& C, ]2 |: B0 o char elem[MAXSIZE];/ N" v( I7 J/ _- a) i1 p0 z9 D7 r% `
int top;
5 @" L% }! W9 a3 i# Z9 c6 ` f}SqStack; SqStack S; typedef struct poly
) D* b% h6 J( s# D( e% {: v{; m! `* c: g/ q/ K
int coef; /*xishu*/: Z. v$ S3 [+ l X. B: B0 u& c
int exp; /*zhishu*/
2 I" C/ a* A6 M struct poly *next;' s0 Q' Y0 @0 j2 L2 d! A( O4 k2 x
}Lpoly; Lpoly *pa; int cf(int i) /*10^i*/4 g5 G" C9 @8 C- V& k
{
. ~3 T; {" c' A! q0 B2 e Z) Q$ ? int c = 1, j; {7 T& T3 ?2 N
for(j = 1; j <= i; j ++)3 M7 e4 ]# f: q3 G5 y3 w8 Y9 \
c = c * 10;5 Y. N8 h) W% X0 L
return c;; {- n; x$ Z& Y
} void input(char a[],int j)
( } l' @' ~2 @{% {: U( d5 ]6 M. H9 V
Lpoly *p, *h, *ptr;6 ~; c N) j% i" |) [
int i, k, flag = 0, x = 0;) Q7 H. |; v- L1 @. r6 ]- j
S.top = -1;
2 c( m9 ?6 R7 e pa = (Lpoly*)malloc(sizeof(Lpoly));- l% a( \1 j+ w
pa->coef = 0;% b/ w/ i, n) N
pa->exp = 0;
F8 P; y) g; ?, _5 ]" u8 P h = pa;' { G6 K7 n0 y
h->next = NULL;
6 [, v+ n* V6 ]3 g' W* X' z) M p = h; /*jianlipa*/
( K+ V+ H$ h- [# Q0 `2 _' N6 d
! ~ {& F7 d1 H. o W7 R# u* B) z3 T for (i = 0; i <= j - 1; i ++)3 S7 m$ U# u$ k7 j' r, y
{/ |% |- h$ C0 _8 @2 }' o8 g0 q
ptr = (Lpoly*)malloc(sizeof(Lpoly));! A+ s, D9 l" V' L/ a8 k
if(S.top)
$ c" {2 q) z4 Q {
4 C) x, d; Y I" d2 c S.top ++;# a @3 Q- P G
S.elem[S.top] = a;- ^7 X6 \9 K. P
}
* H5 Z; t9 q6 ]; j, F: w: P4 Z# c if (a == '-')
$ p P- B1 b9 a flag=1;) z/ n2 ?' J' u
* A; j4 b# f# s8 G7 E6 E
if(a == 'x') /*fenlixishu*/
% w, o& A- a5 o7 t {
! D0 ] i2 _& t8 s if (S.top==0)0 b% x6 G4 w$ j9 B1 T2 g
{4 f- c9 x- B( O* w. u9 ^, t# z0 M
if (flag == 1)+ G7 ?( p, V, n p5 ^4 c4 C
{5 S! D% a5 B6 S+ F: S6 A. l4 z. J9 m
ptr->coef = -1;
$ p% V5 z0 c( l/ q# J# K ptr->next = NULL;9 l. j" o7 R2 O7 p7 t8 W3 S
}. l D4 F4 [0 S3 P
else
7 r/ d [$ \5 n. _# N" r6 e J$ c {
/ E8 D6 h1 d# w7 O4 E9 [) M ptr->coef = 1;) H( F8 p/ S& @/ U: p
ptr->next = NULL;* L9 z2 m. L" A
}5 u8 |4 p6 t9 ~& |1 W
}
' V3 E4 z6 o7 i) C else% b7 K: L- G1 i
{5 G B8 o. R2 T
for (k=S.top - 1; k >= 0; k --) M8 L% _' T1 ]; r
x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);) C' R8 l0 v' o1 I
ptr->coef = x;
7 v6 r/ i+ |5 P4 p9 o ptr->next = NULL;" ~( h( ?$ @7 Z9 q% v
x = 0;
$ L+ t9 l9 w4 O; H9 t4 D }" r: P5 n3 V/ {
S.top = -1;3 o% X) D* p5 t, {0 E$ b# B# X
flag = 0; /*clear*/: {5 l$ w5 p& `2 @ r4 F+ R5 Q
}/ E) l, f" {3 G' v0 @6 n7 g: |
if (a == '+' || a == '-') /*fenlizhishu*/
) @# B: ^2 o; {- P L {: Y- O" I/ o- i! e2 B, c3 l9 ?
if(S.top == 0)/ q' q5 v6 o7 |/ ^3 t
{# b3 k# E+ a0 z T- y1 L
ptr->exp = 1;
( a: @( {/ p. o/ Q1 G! F ptr->next = NULL;
" n% m! s9 c- P0 u% o$ z9 u+ h }% ^1 f- ^/ a; h) c& G; A$ \! R
else
1 e7 P) ]3 R, p8 C1 Z* M {
* ~, H1 k$ p& E* C/ c0 p for (k = S.top - 1; k >= 0; k --)
' L1 t0 b9 @6 E E9 u x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);
^ }; Y2 l1 K8 N1 T } ptr->exp = x;- @/ L' U6 n: ^0 ?% P
ptr->next = NULL;
) b( _# @7 [! v: k- K x = 0;
. _7 A. G: L- q# B( U }/*tiquzhishu*/
; q, C+ o# Y; | @8 \, j S.top = -1;
# o* w" S3 O3 b6 D. ^* S+ z# B }
' T( B7 G; k# }; M9 f if (ptr->coef != 0 && ptr->exp != 0)
, g! l# E' @1 I5 f* o {
: w ?% p M6 @% m3 E. A, S+ J p->next = ptr;1 [: o, G8 w1 P2 D
p = ptr;
3 D) H: y; T9 F0 [2 M, x! \, w }2 O# _ ?: L5 C/ E( W8 w3 J
if (i == j - 1)
( W# W2 C: V/ s, j {
/ C: z+ r8 c- v, D( e- |# D9 O @ if (ptr->coef == 0)
# ]/ X! B/ I) u8 R D {
. Z0 Q* ?8 ~9 a7 G0 e for (k = S.top; k >= 0; k --)% R9 v( W$ ]/ [6 B1 i8 V
x = x + (S.elem[k] - '0') * cf(S.top - k);# G6 L u% x3 \7 o2 K
ptr->coef = x;! ^8 v0 y2 \' R+ O: e* Q% w7 e( [
ptr->next = NULL;+ x- H# x1 ^' `1 y- x( e7 Q
}1 z' P; I* u7 G
else: i5 ?3 `/ V9 [5 B6 _4 }4 c* r! E% {
{; D5 M1 y: `! R; m* Z' I5 ~2 q$ M
for (k = S.top; k >= 0; k --)
5 o. K( y+ |% A/ {) k, j# t x = x + (S.elem[k] - '0') * cf(S.top - k);
' M! T" p* _& x$ P: K" t1 f ptr->exp = x;* ~, m" E7 j; t' U8 _
ptr->next = NULL;
6 C- c: a7 Z1 s" e3 a) r7 o' Z }
O( Y5 c; P& l p->next = ptr;" `( w" e: d, Y3 n
ptr->next = NULL;
. q# F& K/ R& N/ ^ }4 U$ O8 ^5 N L
}
* D* k1 _$ M1 E# R6 o8 {} void stinsort(Lpoly *pa)8 d: Z! P9 Q' J6 q
{3 F. u$ M1 F& z2 I% J
Lpoly *p, *h, *q, *k;' a2 r+ n# h! Z( E
h = pa;
; f6 j+ O U: o) w! ~ p = k = h->next;
4 g% }. R) t. ^+ D' `8 T9 l q = p->next;
# q6 p" t7 m0 B- m" t while (p->next != NULL)& I; F, D* D3 s1 r' w
{+ [6 L) |( v& }$ d9 U$ ]* q# P
if (q->exp)
% Q% u4 I, X' j- ` {
# W; K* R# K9 V) h% R6 B+ ~2 M while(q->exp > k->exp)9 ^& `+ P. \% ]% t. p9 S% @
{7 r# G8 ]) m) V
k = k->next; G: y3 P: r+ l0 C7 f+ d
h = h->next;
, q. P" F1 x. G3 F, h }
4 O+ G' d( `' G7 m if(q->exp)
6 u) I' p- o. _' |, ? {
+ T$ C# y* W2 K1 E" X p->next = q->next;
4 e& y8 ?5 J8 U: G( G. a h->next = q;! o5 E2 E4 Z; F u5 j
q->next = k;. e, U8 H! m: v% O, u
q = p->next;
p9 }) T7 ~1 b Y2 p: s1 s }
, ]( U# n. f( Q1 K! w4 e if (q->exp = k->exp)
# x1 D7 K7 _3 M5 n+ j {+ Y* Z. w7 `+ v, h, `- B; {
k->coef = k->coef + q->coef;
* G# k/ o( n, N _& s& I5 Z) m& q p->next = q->next;1 k% ?& a, u, @' S
q = p->next;, M n; l& S% ]! P2 c1 z* I
}+ E' b) u8 N; u+ x2 h
h=pa;3 i) u0 Z! K- ?2 p5 ]# s
k = h->next;
) o: B$ F3 e; _" i: U }
6 y1 a( \, }, @
: U' k) ~- V+ d' v( w6 I" W. W if (q->exp = p->exp)
8 [) j1 o) F! L' m7 ~! z5 P- v) k/ N {
. L5 U4 u* t4 n5 t9 m8 F' { p->coef = p->coef + q->coef;
7 Y# b6 W4 f v8 p p->next = q->next;
( x- x/ {% L* |5 g q = p->next;: A) I8 }9 f* l
}8 V$ T; \- |- ^$ D
if (q->exp > p->exp)
* G' ? F! ]( r% m& X" ? {
3 s! ^9 L+ H4 @# S q = q->next;
& E/ D6 |! O L9 A, ~, D p = p->next;
~2 j( [- p: p( ]3 ~ I }7 w }( u: F, L' n U2 i' b' c2 K( j
}5 T" ~5 z% M: W7 q) _
} main() o( S, P+ o0 M- `7 _* ~1 J! C
{8 _% ?4 ?& L1 k2 S7 A
int i, j = 0;
/ t5 F0 S/ S5 \ printf("a=");) @6 E3 R0 }" ]5 }0 Z2 K! p1 C
scanf("%s", a);3 y( b, T5 F6 N7 E
j = strlen(a); input(a, j);
$ ]1 k& n. {: o1 g; m$ r stinsort(pa);
! D5 c3 `# b9 B printf("a=");4 \# S% K) u" A0 V
for (i = 0; i <= j + 10; i ++)
. Z ?+ x( r' p, m* H G" | {' O$ _8 p" m! L
printf("%dx%d+", pa->coef, pa->exp);
& {- e5 X* g0 t: v) {. c pa = pa->next;
6 t* x+ L( Q, ], w/ l9 T7 i }
4 D" ]; g2 |+ o}
; Z; c* A- S7 H2 l 我把楼猪的代码弄了下,把里面大小写错误排除了,这段代码会有死循环,谁有空谁研究去。 |