#include "stdlib.h"6 {- l5 e4 ?% r a: W8 o. n- R
#include "string.h"* |/ z+ g% o* M3 w6 W5 R/ m
#include "stdio.h" #define MAXSIZE 400
6 s" I8 |* u$ F ?1 Zchar a[MAXSIZE]; typedef struct
; ^* |9 A' J" d; H{
) x$ [: m1 |% g0 ~/ s* Q char elem[MAXSIZE];$ |9 Z+ t$ ~% n" I7 x
int top;. c0 K8 ~& U: Q; O& E0 {& p
}SqStack; SqStack S; typedef struct poly
6 f: M, x3 p2 g{
; o% d9 g* m- m6 R int coef; /*xishu*// |# e9 f- y$ I5 ?0 m+ V' g( j
int exp; /*zhishu*/
/ }5 a2 y0 n% s; R$ P struct poly *next;
0 f) {2 s. J% Y3 `9 q& y}Lpoly; Lpoly *pa; int cf(int i) /*10^i*/
, Q0 R6 A! f' g% H. E{( x) W& I# a b4 _, F3 ^$ j. c
int c = 1, j;' A. q3 N% K- S6 @! D1 M8 o
for(j = 1; j <= i; j ++)$ r; Z/ Y$ ?5 T# w
c = c * 10;0 f) {0 Z0 T5 ?
return c;
; p/ Z! g$ r2 l. z; ?4 [% g} void input(char a[],int j)
' M$ I d+ R. z! C8 a) j* w( ]{
( W4 r# S( {6 t$ T% U* Z, L Lpoly *p, *h, *ptr;9 M" r, e8 X+ c' s, y) P
int i, k, flag = 0, x = 0;
7 {$ `2 p# X' Y4 @- [7 ^. n S.top = -1;
, J/ X9 v- @- O pa = (Lpoly*)malloc(sizeof(Lpoly));
4 G" A9 w$ t9 w/ a | pa->coef = 0;
5 `- q1 v: D O' O {3 b# d9 S pa->exp = 0;0 G; e z! a2 O- I, X
h = pa;
5 [$ f7 H$ `: b, }! d h->next = NULL;1 I" E: k4 r4 F* A3 [% w
p = h; /*jianlipa*/
) i6 n8 g) W3 N, j }# G8 v & R) a' n3 X# n+ c" H
for (i = 0; i <= j - 1; i ++)
% m! k4 f9 g0 Q) _ z; U {
9 j0 t& S) Z& { G+ r3 x ptr = (Lpoly*)malloc(sizeof(Lpoly));! H) D5 Y% a, D' }2 _* f6 S& {0 S
if(S.top)8 R9 t* _# \$ _' ?0 P
{
3 J7 t9 R6 A9 h# Y/ b0 B2 B S.top ++;
4 Y' u" m/ q) y3 I S.elem[S.top] = a;
. d8 ^# O% O7 }8 |$ q( D* R) b }
1 S: f5 S) q' Z# r+ W( s if (a == '-')9 Z. @9 v" L$ l+ G
flag=1;
! J3 P" y) T2 b$ [0 Z) ]# A4 r
) I( ^8 q' K! b9 k7 E( { if(a == 'x') /*fenlixishu*/
7 G, Y3 V h, O; z1 }0 C: X {3 h" }; @+ l% r
if (S.top==0)* ^% Y& B+ n, p8 I- ?
{
7 H- i% o' _! g& \0 N- L if (flag == 1)0 U+ L" K; a* F" X' P ?
{& Y. A* D! b7 i
ptr->coef = -1;
' b% w* z- k3 E" F ptr->next = NULL;
; @$ E; p' {0 z" W }
. U# }) r0 u1 {4 c4 j else; D8 ~! |! [/ t+ p
{
8 ^ @3 ]8 f9 m: l& K ptr->coef = 1;- a R5 K/ u/ S7 h( |' l
ptr->next = NULL;
2 c# X3 A- {3 q, g- e& c8 D: D1 ` }# ~: { ]' j; D3 S* C; ]; g
}
! P3 k6 N7 W( B% _- w3 F( C+ u3 X# b3 U. @ else* q& l8 D) y4 S% D/ H2 w$ ?
{: D) ~! |( k2 l! d: S
for (k=S.top - 1; k >= 0; k --). n+ o# R: N( V# P6 @$ ]! I3 E
x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);
! x3 L& {. w2 @" ?+ N! ] ptr->coef = x;
" x0 y: p: Y1 g7 B( } ptr->next = NULL;$ O- Y( d) R# u# J. `. |% h
x = 0;
3 d; A4 w9 B( g1 `+ g$ g }
" h4 M. Q6 k# P# @& t. ^' [6 w8 x B S.top = -1;) g8 Q0 q/ p4 V8 p3 Q1 Q, Y" J
flag = 0; /*clear*/
- B9 N: G) P2 b4 T# ? }! x" s& d# v5 ^' J0 Z
if (a == '+' || a == '-') /*fenlizhishu*/0 l: N' c2 g+ s0 h O; h
{
% l: o7 [! v# F* L if(S.top == 0)
' r" O, ~" p0 t {
( Q* o% ]1 w, ]- ]% } ptr->exp = 1;
. }. c( W* b$ ? ptr->next = NULL;: H. L! g; Z/ n3 j; \4 b3 \! D
}: J; Z8 j1 ^9 E5 S
else8 V7 c7 p2 T* z5 k9 A! i* S
{3 e8 H7 ]( L- h& b
for (k = S.top - 1; k >= 0; k --)' Q A% h N. T5 I6 |
x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);: w' q# ~* g3 F7 G$ |$ m U
ptr->exp = x;& k6 m/ R: U# K- S% O6 X
ptr->next = NULL;' G0 Q$ K" c4 s* @2 d7 M8 b5 k5 b
x = 0;' y: p: ?$ M1 L4 P
}/*tiquzhishu*/
! G8 z, c& z1 w/ }; m S.top = -1;
0 F( T- x$ Q. N: t3 | }5 q. r. v5 V; q1 q) K5 N
if (ptr->coef != 0 && ptr->exp != 0)
h+ m. d4 @; Y: t {8 {/ {' X& _; }
p->next = ptr;5 J0 v: v3 l3 X5 I5 N
p = ptr;9 H2 C5 c) g8 P3 u- S# C9 q
}% W. W$ ?) n& i" ^" x
if (i == j - 1)
2 j5 w# \% g, f9 T- _' Q {
: W1 d$ W; B4 e v" e% N \ if (ptr->coef == 0) v/ P$ B/ Y& s6 ]1 {
{
% t7 [+ F, P% |5 x1 M" ^! Z7 ^1 w2 f for (k = S.top; k >= 0; k --)
; \2 v: G7 E5 q; L( F& | x = x + (S.elem[k] - '0') * cf(S.top - k);1 T' m: `9 z5 h. `$ p$ y
ptr->coef = x;+ m& q: s6 b0 r7 Q
ptr->next = NULL;/ g, B- L4 l4 M( o4 t6 {9 y" n
}& S( Y) \7 }2 @) y( X1 P' Q9 d) `
else
% Y0 B E P# {# c {9 C& I5 b8 ~: O- E
for (k = S.top; k >= 0; k --)" J: `) l, E6 F" x
x = x + (S.elem[k] - '0') * cf(S.top - k);
2 b$ @: K/ i/ \! M" |+ z ptr->exp = x;" K* F1 X2 x% T! o3 q, d; w
ptr->next = NULL;& z. M4 g+ L2 x1 I7 z
}, m( I) }4 a K P- n. v4 M# D
p->next = ptr;
4 F9 Z3 n3 F9 Q" K ptr->next = NULL;
3 m. d+ X4 Z" c6 ]1 D! j7 c }3 s' T+ t `$ r" I# m8 h
}/ @/ l8 x! J8 [7 G) E
} void stinsort(Lpoly *pa)
- a+ ^2 p. k6 D4 s! V{/ N! c4 I4 w$ O" @! y
Lpoly *p, *h, *q, *k;
! x4 N b8 s/ X. Y h = pa;. w9 U! S5 n% N S
p = k = h->next;
$ z7 S" D4 y! [6 D3 t* ? q = p->next; |8 s4 f+ y+ m% z
while (p->next != NULL) Q- r( v* F4 N8 T* Y! D
{
) }4 W2 B; o: ~+ m0 K9 M% Q* L4 f if (q->exp)
/ r5 o: s* ^, s) A {9 U d( |9 T9 M8 ~( d1 d. C' U
while(q->exp > k->exp)
6 a$ M8 I- u* L7 h {+ R5 W4 Z1 J X! g. {' i- ~& G
k = k->next;
' x# |% m' }/ K3 R/ Z h = h->next;* v! A8 e' j6 Q2 M1 U4 s
}3 [9 i# R/ x. B: Y, d7 ?7 q
if(q->exp)
6 {: M3 t Y4 M4 | {
# A. u! I5 y6 f2 q7 m p->next = q->next;0 |8 g+ Y4 c) }, t G2 e
h->next = q;( g( {/ i1 x. Z# ?; T) s2 v1 F( z
q->next = k;- A; l0 {; l( G, o0 ^2 I" s
q = p->next;
2 z% v5 ~, ~+ o: Z8 U$ a% ~4 A% \ }
5 z8 D+ f8 G" z8 p3 d if (q->exp = k->exp)
" I0 F/ h3 h) {8 V( V A" g4 c5 m {
) l Q3 Y. w# N6 T: C k->coef = k->coef + q->coef;
( j8 Q: y- V- y5 K* A q p->next = q->next;
* w H% h' f4 L4 h. W* \) H% V% l q = p->next;
$ }& D% q. \% D! e/ f }, l8 g7 r1 l, q
h=pa;0 e6 a5 o0 `) J4 C
k = h->next;
. a0 y, A4 z1 f5 Q3 J1 w8 }1 z5 C& P }5 `- H1 | z& o6 I
; j% J$ J4 v; F7 U' b" ?' ^( ~
if (q->exp = p->exp)
# l* h4 G9 s% q$ T# [ {
5 M6 Z0 O4 d6 P7 E0 Q: w+ @, ` p->coef = p->coef + q->coef;- D0 e: `1 o4 w7 E" x' }. w9 @* K
p->next = q->next;- c* `- _: a1 d: U6 M3 D9 g8 o
q = p->next;
8 N) P- Q1 z! I* Q' [" U }5 r) b, h3 s; F+ l- A. x
if (q->exp > p->exp)/ J8 r1 F- c: X% ]
{7 W! R5 y9 ~/ F, y2 h7 A
q = q->next;# S2 l2 I* c6 H
p = p->next;1 Y) {: r4 i4 O) d) k4 m
}
3 p, {" C+ v7 W9 m }
* ^7 O6 g% J, Y5 @} main()( @6 [. k$ D4 N, V( ^0 @
{
$ p6 n, p6 e1 M. X+ T/ Z+ P int i, j = 0;
& L; C4 C ^4 o$ \( I printf("a=");
& y B, a, l' a- E- c! @% w scanf("%s", a);3 h) H4 @9 r$ A% d- I3 T$ t7 T5 u
j = strlen(a); input(a, j);5 p7 K! m6 H. `" i4 f) f
stinsort(pa);
. p# l* }+ f# O& m) |7 O; ] printf("a=");6 P1 U8 \, v4 b) v" P' q7 k( _
for (i = 0; i <= j + 10; i ++)
. r/ D, Q; `: E {
' i! ]8 f, |$ h# p* ~; ] printf("%dx%d+", pa->coef, pa->exp);; E {# X5 B1 |+ V, t
pa = pa->next;. D2 ~# X, w% q. Z7 u0 b+ y
}
7 j2 v' h( ?) T5 I" A}; C! B% U# X( g# t
我把楼猪的代码弄了下,把里面大小写错误排除了,这段代码会有死循环,谁有空谁研究去。 |