#include "stdlib.h". ^" l8 p, q' v! Z I! Y
#include "string.h"5 G8 Z0 s( m+ Y0 D+ Q
#include "stdio.h" #define MAXSIZE 400
8 Y( p2 [ u9 p( l4 _char a[MAXSIZE]; typedef struct
7 \: T7 H4 N9 T- ~/ C" F{1 q% i( s0 `6 w& ~
char elem[MAXSIZE];1 C+ ~) Y* y; E6 q7 ]6 ?; V; F, K
int top;
& I& c5 B2 e2 c% m}SqStack; SqStack S; typedef struct poly' X5 a3 H5 B4 _$ d
{
6 V8 H3 L/ K9 J/ y0 t: D+ a int coef; /*xishu*/; \$ w( j, t$ n# S
int exp; /*zhishu*/
3 r/ L' X A) J5 C struct poly *next;5 M+ N4 I2 ^, m7 z" e' K
}Lpoly; Lpoly *pa; int cf(int i) /*10^i*/
7 a, F2 {. R- _2 ~{
; `$ P4 t& R: A' ?0 T) Z8 V int c = 1, j;" }; W' q0 j. `& K3 S6 U$ `
for(j = 1; j <= i; j ++)$ I) U; e6 \- S6 w. \
c = c * 10;6 g: P$ ?3 P0 n
return c;0 z, M+ f4 }, a$ ~
} void input(char a[],int j)' U2 S, ~. S6 e, a" e1 R6 A
{
. V, B7 s5 w4 I7 F5 q( v2 N Lpoly *p, *h, *ptr;
4 I% c8 _7 D, V. p; } int i, k, flag = 0, x = 0;
) I2 h6 I$ l( c5 c9 V/ c S.top = -1;, T" l/ ]( I! n( |9 q9 ]
pa = (Lpoly*)malloc(sizeof(Lpoly));2 n. w1 L$ \, P0 L9 Q" }) x
pa->coef = 0;2 }8 z2 l* _$ a+ R! t7 H# F/ k
pa->exp = 0;' {8 L! |$ Q- W. r
h = pa;) T( b* i$ ]+ |4 G( j
h->next = NULL;
0 ^" x! K, g, V$ k p = h; /*jianlipa*/
3 J/ g1 R9 x* `& T
+ d8 _! s! {. a- {5 n for (i = 0; i <= j - 1; i ++)& A$ s6 M) u* X
{
( y, z; J4 z& C: ]$ E ptr = (Lpoly*)malloc(sizeof(Lpoly));5 {& r; h" p; A
if(S.top)0 Y* l* P" L, |' n
{
$ a" e# O2 o$ O S.top ++;
) l* I0 L: q R S.elem[S.top] = a;
& D* `# U" J2 V. U }, b* Y* x4 c& A$ u
if (a == '-')
& O; e U' k, N; e9 V8 P/ D+ N1 _ flag=1;
' d- V6 @8 Z0 l- O3 H! b
6 X r- R, y8 K0 s% U if(a == 'x') /*fenlixishu*/- c! D& B, q! a' p* i% d$ t
{7 ^0 w$ t0 A. o! k
if (S.top==0)
( r( \2 Y2 ^1 K& M {; r6 C0 w3 J( y, |2 m3 G6 S5 j7 L
if (flag == 1)
4 @4 q: l( T! T3 i {0 R9 @/ J g4 c+ `2 T2 X7 u
ptr->coef = -1;
) V5 X% B# _& I! c1 }5 J ptr->next = NULL;
4 i% ? N# l/ c0 Q* P6 `, R }/ b+ A" ?; `5 M1 y( _% [
else2 T7 X* Q- y' B: A6 z7 L
{1 V4 A, L& P; l" A8 E# K
ptr->coef = 1;
* W- Y9 _9 u& ]2 x ptr->next = NULL;
3 Z( Z/ ]8 b8 Z4 e' X) j }
! w8 y, ~# a" H9 `# T }. a$ b6 c6 Y8 `
else
' d4 Z, ]/ z; O7 x5 Z {
7 |/ Z" F! f* f) ^. V( x5 r( o for (k=S.top - 1; k >= 0; k --)# p8 e6 O4 n, n; a* M$ ?/ A
x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);3 I. W: P9 y1 r6 j
ptr->coef = x;7 }' n. W; B) i, _
ptr->next = NULL;
4 |3 A# F+ I$ J" C \! K/ ? x = 0;- P b: D) f {* `, v
}
7 ?# c) E% N' f% l S.top = -1; i, a" c O( |
flag = 0; /*clear*/
% h1 ]) k7 p, u5 a7 E* \1 h' Y/ u0 G }* A7 L% v4 T" c# A
if (a == '+' || a == '-') /*fenlizhishu*/# D. ]' N, C7 C/ C$ r+ r6 }
{
! b- M# E. s' o5 h: Y4 f if(S.top == 0)+ Z" Q$ @# q. @
{
, F5 }2 }" c' [+ U7 e. [ ptr->exp = 1;
- a5 i9 y) |; A& u6 F$ V ptr->next = NULL;4 D9 d% P+ `, x4 ~* v
}
: q/ q4 }& @. I1 \ else5 ~! p5 o+ `8 v! O
{
- p3 ? E; M8 c/ N5 V, U for (k = S.top - 1; k >= 0; k --)
\5 L4 q+ ~8 I( m) O' m% G5 M) s8 o$ K x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);
8 _! d+ Z1 I# {* m7 y" j ptr->exp = x;: V, Y/ `' p/ o4 G+ J8 W; U% {( m1 x
ptr->next = NULL;+ M9 K4 s3 }; l @) i
x = 0;8 r2 O5 n+ X4 {: }7 G5 b* u
}/*tiquzhishu*/
6 j8 Q+ |! h; A7 O1 \% z& n G S.top = -1;
J; q, r: A! s. v }
5 C/ V7 B; J9 X, A% A8 l if (ptr->coef != 0 && ptr->exp != 0); Z& `; ?, v7 ^& f
{
( x( C" L6 ~) Q! s1 L p->next = ptr;' W- o9 Z) T3 U; g" {5 B R
p = ptr;
; f) L" a# v# l4 S' T }6 R* W4 K( }$ [0 v M* H
if (i == j - 1)
/ G$ L- x0 ]) J H {+ L6 Z& s: J/ |/ N, j0 ?& `
if (ptr->coef == 0)5 H- `2 o u# u$ H& p* {8 i6 {
{5 k5 d: O/ T; _- i( q
for (k = S.top; k >= 0; k --)
+ z$ x8 N# u5 q) g/ @& W& f; |) b# x x = x + (S.elem[k] - '0') * cf(S.top - k);
9 P8 O7 {* v8 w& } ptr->coef = x;
* ^+ d s' {3 C W; T& G ptr->next = NULL;
2 x" b+ w# j, {2 W ^1 B N" v }
4 ?! t0 ~1 m5 R7 Z else
* Z `5 D# e5 ^1 B0 Z$ g {
( s: `) ^& g2 M z9 D' G& r for (k = S.top; k >= 0; k --)
3 {9 _ y4 _* d+ q+ B# _ z x = x + (S.elem[k] - '0') * cf(S.top - k);
6 Z' Z3 h; p% _, J; T7 u ptr->exp = x;. P L t1 `! }1 p. `
ptr->next = NULL;
0 @' j9 v @3 q. `' t7 ?/ B }0 s# q) \7 [" U
p->next = ptr;+ Q$ I$ Z" d/ Q+ Y1 n- }* B
ptr->next = NULL;
: B4 Z/ k; Y( o* J: k/ N9 T5 G }
$ w# @- c9 _1 _$ Q* m$ p }; X) W% `9 Z$ v. B; \% Y5 l3 ~2 F% H
} void stinsort(Lpoly *pa)
% c% C; m6 `8 J K5 U{; `1 t( A: f" Y' C2 O& e; Q
Lpoly *p, *h, *q, *k;
$ F6 E$ S+ X6 C! Z7 c3 s h = pa;
: j2 h) i0 W5 c2 T p = k = h->next;
$ |8 s# Y( R( ]% {5 n q = p->next;: m* m& O+ L, I* x$ D# s) B
while (p->next != NULL)6 X2 B. q( ^7 ?) Y+ T
{+ q9 I' D1 Y, V+ C
if (q->exp)
; G. w: Z$ q% J' E {
3 H) _8 M2 [/ h @) R while(q->exp > k->exp)$ ` u% f/ i9 M, Q) ?5 _
{% {; J, l4 y( B2 E
k = k->next;
/ h$ a/ \8 P1 r! f" l4 a; C h = h->next;. i6 K% { q0 e: B7 O4 w/ O
}1 O* f1 ?$ E" Q2 K* M
if(q->exp)
- c, h. G) H/ h6 x4 P& @ {. `6 m# ?* g( m4 Z& Y6 }1 V: f, ?
p->next = q->next;) B" F* p- m+ c" w, C* K h; H
h->next = q; {9 H0 z1 M% @, L+ H q4 M
q->next = k;9 J+ }0 F! }# i! ~0 ^ v* D
q = p->next;) a! t4 B q' U
}
: m% e! G9 `& H" F if (q->exp = k->exp)
' K3 l5 p# F# H {
. m) g, v/ ]; b- z' k, @6 Z* |2 I k->coef = k->coef + q->coef;" C: Z8 G' G0 e9 E6 C0 B- m2 i
p->next = q->next;, P6 L+ [* U6 a' @# r; W, ~* u
q = p->next;9 M7 H) s4 |0 r9 c3 }# J
}- S. T* L( s7 Z6 R" Z! t% ~5 V
h=pa;
3 b, h4 }/ l" L$ O# o k = h->next;! ~& P: X, U9 F* [1 N0 d
}3 B8 L3 ], ?! u! U
( x& ]4 k- H. T% G
if (q->exp = p->exp). Q. H! x5 u: R5 d3 s
{4 [# ?$ M+ t1 Y' [5 B9 ? m
p->coef = p->coef + q->coef;8 n# L- \0 m9 Y* r- a/ X" p
p->next = q->next;
u2 f1 @; t @* ?0 s6 a q = p->next;' A: T( B( |% F( k% ]0 T' ^
}
6 Z: R! d8 N5 Q! _. z if (q->exp > p->exp)
' i h [8 A" b+ f! [7 J8 [ {
2 K" k' F; m: A. a. w+ G/ C q = q->next;
U% X2 o M1 B' L6 z5 D/ f p = p->next;
4 B) F5 s& E$ s4 q! I/ {4 i8 s4 G }; O% P5 Q8 s& s2 o4 T
}! t) i6 R; |: J: y, |6 _/ d" }
} main()( B% @) a, S1 e) O, o, g6 h
{
7 K) V9 ^. P; }( _. F) E# Z int i, j = 0;3 L9 h9 h4 \) L; M$ c/ r" x
printf("a=");0 t% W" u% e2 x8 N+ f# ?$ N# o
scanf("%s", a);
b5 j2 {3 Y( Q, y* a0 _. F j = strlen(a); input(a, j);
$ r/ c9 d# e3 g& B stinsort(pa);7 y" q/ H9 v6 x1 a/ H# I! N
printf("a=");
% O" Z g/ A5 P; r8 }: n$ ] for (i = 0; i <= j + 10; i ++)/ w; V& [. j# b5 Q' k6 Q
{5 d7 N6 ^5 ^& s( D5 t ?
printf("%dx%d+", pa->coef, pa->exp);: R6 ?( J( R& e: p* k9 I
pa = pa->next;
6 w. p+ D( G7 g8 ?$ _+ m. H. U }
% _# d$ [ x2 S- Z}
5 K/ o2 ^5 p9 z, _7 ]$ h 我把楼猪的代码弄了下,把里面大小写错误排除了,这段代码会有死循环,谁有空谁研究去。 |