#include "stdlib.h"$ r! ]9 O9 ]0 r; O
#include "string.h"
$ w( @2 x" B$ P2 q0 d- y#include "stdio.h" #define MAXSIZE 400! t& T. { M( k5 C( ?8 Y
char a[MAXSIZE]; typedef struct
4 e& o/ |/ @( a3 d{8 V% H: b# \7 w4 X6 W8 b
char elem[MAXSIZE];
9 ^$ Q5 P V8 m, _# s# H int top;- l- I3 ^, O6 i- o
}SqStack; SqStack S; typedef struct poly
3 N, h5 w( A _: H0 S% G) d0 z4 m{
: x; d+ a5 X; x: j4 F2 X* h int coef; /*xishu*/! u& X0 M, F0 g: E; B
int exp; /*zhishu*/+ \+ L+ c I, d8 o& n- [/ E
struct poly *next;
2 \4 F! z" E! b/ M* h2 I( U}Lpoly; Lpoly *pa; int cf(int i) /*10^i*/
4 J2 Z5 w7 ]/ ~+ v$ z2 z{/ f/ Y- \' H8 L# D- c" F, H C
int c = 1, j;; m' R- `8 b. F1 m/ _
for(j = 1; j <= i; j ++)7 p) ?+ d) ^7 \
c = c * 10;
" b j4 i5 w" W2 {2 K r+ r return c;
1 s& @7 A; r, f6 a- B3 R3 m4 H* C} void input(char a[],int j), V# |9 m# |, H+ }& t
{
* i4 H$ a# V1 e/ C6 p2 w/ b Lpoly *p, *h, *ptr;
* ~# p& q( ^3 {; i' Y5 @ int i, k, flag = 0, x = 0;$ r4 `4 D3 ^! V' e1 U. n
S.top = -1;. t* V8 B; T% P+ F
pa = (Lpoly*)malloc(sizeof(Lpoly));
( h4 y) J4 z8 x- c0 H pa->coef = 0;
8 {3 o( W+ n( d- C0 z& R$ G pa->exp = 0;" T7 j# l: P |2 f
h = pa;- r( v2 b* {) B) W& s
h->next = NULL;
/ Q8 ~ V0 A5 u1 t) e p = h; /*jianlipa*/
6 X& S6 @& w; G( w% M- C6 L . G; ?7 O) o' x& b" {) M
for (i = 0; i <= j - 1; i ++)+ o: _' t4 {8 |
{9 S$ w0 m! V: U' S- @/ n
ptr = (Lpoly*)malloc(sizeof(Lpoly));
9 Z5 ]0 z! A% y, k if(S.top)1 j' f# k5 C4 X
{4 R" Y7 z) @% _8 ?; t3 Y4 S
S.top ++;
: Q/ s" a# K. [6 O9 k; P4 w/ y S.elem[S.top] = a;6 H$ ?- n0 N% N
}- S3 i' ]4 \9 ~5 a6 {) ^# o' G
if (a == '-')
. _' q3 N' @6 K0 V, V) f2 f flag=1;6 Z9 v; ~- ]/ ^* n
* {- c7 E7 Z4 L! I7 b! T( T, @ if(a == 'x') /*fenlixishu*/
! m* S% _( u; j6 S% J {- P/ y5 x0 @- D" _0 ~
if (S.top==0)
+ l1 W- Z% z( `! N3 \ {0 y( d3 `3 U) `6 e1 v
if (flag == 1)
: b- v7 o' C: M% ?) i+ C P {8 o9 V/ [& @1 j# n+ g$ @* d
ptr->coef = -1;
8 z! Z0 p) O" x+ i' ` ptr->next = NULL;
& z5 i8 `; c/ n. S- P# z0 X& t }
. H: X# B3 B+ }! f else
5 w6 n0 M. P6 I2 `7 _5 T {
$ P4 J Q0 b6 [$ ]( m ptr->coef = 1;
1 O, R, X& b, d" q% B* e ptr->next = NULL;
% A( V/ ~: t: b5 _ h# N7 i }
8 ?4 @6 Q; l# P3 I }
0 L( A- J( a& z, a" E9 k5 W3 x else# r9 L" L1 G, u1 @
{" N' J6 t9 w* `/ q- W' H
for (k=S.top - 1; k >= 0; k --)
. E( J3 \8 _" H w# N" y' Y4 Q. d x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);/ V8 p, s& ?+ ?& j. Y+ [4 V4 u5 P
ptr->coef = x;
/ B" a; V! N) u2 P y* J( T7 U ptr->next = NULL;
: Q$ v$ ~5 {8 P5 b" v* g; T% j x = 0;- W9 w0 ^$ H4 t! N9 U
}' A; W$ Q k6 K
S.top = -1;! B& Y# m) F/ L- \* P8 u5 ]' U! T
flag = 0; /*clear*/
T, @- B: c6 ?3 y9 S" K/ r }
. t3 `/ g- M7 t. I) m if (a == '+' || a == '-') /*fenlizhishu*/
' h- N6 R M" m7 B0 J7 a {
, X7 p2 g& D1 L" `% z1 ]7 w if(S.top == 0)6 q. ]$ B7 M$ V9 T
{
. K. E) d- @, [ ptr->exp = 1;
( g Q4 o9 t1 U% `" F& y2 l ptr->next = NULL;
, h+ K l0 o- }; `, F/ U" Q$ { }
* n/ Z9 \" {* @ else3 I: Q# t/ H: x+ b. K9 M
{( E* r, c* T: E
for (k = S.top - 1; k >= 0; k --)
# Z: m0 |: T2 [5 u' V! e6 H x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);
1 ]) F9 l" C( ^5 g ptr->exp = x;+ d8 A. {. r, Y% N
ptr->next = NULL;
4 Z+ E o/ ]: B1 j: \8 E& [ x = 0;
2 R: U9 X. L, Z) C }/*tiquzhishu*/
- a& B6 u/ w3 Q) E S.top = -1; n0 A2 ]/ ]7 g; Z$ R
}
* w& u% K5 l) y, r. G if (ptr->coef != 0 && ptr->exp != 0)
6 c% D6 C' P7 U( w+ K {
" C: F3 [* D( O+ @# O/ Y. \ p->next = ptr;- N1 |3 {. U7 R9 k; |
p = ptr;
" v* T9 A# R% Z }) |( k+ S B8 {1 I# _/ T( V
if (i == j - 1)
0 f* [# n$ W" i8 a" ?) ^ {
* I9 u; Y2 d$ Y* V8 N! O7 _ if (ptr->coef == 0)& d0 Z* C w3 h! T/ ~' U
{
- f. i$ H$ |. v1 ?, m for (k = S.top; k >= 0; k --)
Q4 [* H# L+ s( u1 u x = x + (S.elem[k] - '0') * cf(S.top - k);# x$ r* N! a- s6 }+ ^5 V
ptr->coef = x;
$ V; k$ N2 C! v H ptr->next = NULL;: f, \5 Q- @7 u) y. V. y' Y4 l
}% J0 c" M1 K5 q# { i. @: h
else* l5 O1 W/ F4 X. e9 {
{2 s% ]7 r3 ]: b1 [# B; n' f! z
for (k = S.top; k >= 0; k --)
; r. ?! t E& E u% H x = x + (S.elem[k] - '0') * cf(S.top - k);
+ y) p7 d- \. K- }3 v7 x( G( u0 T ptr->exp = x;7 G% N& ]; o! G* h# g
ptr->next = NULL;
+ f* l# h$ N$ e, y5 i. R$ E } L8 \5 v& Z/ }+ \. S y: A
p->next = ptr;6 }1 d4 g% a5 u* J, C0 d* z
ptr->next = NULL;
5 G: c/ d* Z' m5 Z/ _3 v1 x }
0 ]( t9 C1 Y7 ?1 p9 F }: ]' b' \' _# [* z4 f; z
} void stinsort(Lpoly *pa)
+ b' I1 S8 w& ]" G1 t1 N{3 C0 N6 g5 p/ c& c) @
Lpoly *p, *h, *q, *k;
x, h, C. R/ Q1 S' T+ Z h = pa;1 Q& x" B, Z# d9 e
p = k = h->next;
2 \9 Q0 W# D7 x( D2 c q = p->next;1 x+ J8 H7 g& G. W- F$ u
while (p->next != NULL)
/ K5 Y% [& ? m- q# L0 i {$ ]2 I) ], o. f3 C V4 L Y9 Y0 [
if (q->exp)2 R P& o B; K; s/ q
{, V+ V' e$ c" c. q1 {: b
while(q->exp > k->exp)
" D" p: f/ ~* B {
S* Z& {- f1 a7 Y k = k->next;6 [, K0 {0 d. z
h = h->next;6 u( v8 {8 Q' X* J
}! P1 K1 W9 A' z) R- K3 d; p
if(q->exp)
- l, I! b( c0 ~' w6 z( c8 l/ r4 } {2 k5 z# K. y' R5 h% p( a
p->next = q->next;
( N. s) G) d! q2 @4 b, c& `- I h->next = q;
0 b6 o1 b: i( |# x6 ~: r3 u8 x q->next = k;
3 w# D6 n/ _" j9 Y& Q7 Y( j q = p->next;% t3 o/ F# V& a8 ^; N% s! ]
}9 y7 L7 F* z: K$ R# x9 U
if (q->exp = k->exp)
3 Y2 d7 F7 r |6 |. ~# W4 Y {
9 z: Q7 J* c% p# S3 ?8 m: h& a& t- B3 x k->coef = k->coef + q->coef;
2 v. E7 ]" o% g+ p# @' l1 V3 k p->next = q->next;
% \/ b3 F- O( N2 I q = p->next;! W6 N4 z, V) R0 ]1 O% S
}
5 q6 N( D4 q# d+ A$ Q0 o- N h=pa;
' ~* l7 Q1 i$ S5 c( W8 B ` k = h->next;' q# U( D' Q# H4 S6 k+ S! p% d
}
2 D2 j n* J+ T \' \ W * p# M" V1 w# _6 Q) `/ x
if (q->exp = p->exp)$ @ Z5 F H, ~/ b: _
{% P, c, _2 Y+ a7 d' R
p->coef = p->coef + q->coef;- t0 Z n2 T; g' J3 c! d
p->next = q->next;' t4 ^0 ` \2 L3 T
q = p->next;
- @6 e- P. Z5 \ }: q( q$ a$ ^, b) ^4 Z5 D9 L
if (q->exp > p->exp)- w. b. E' y. _7 e) w- R
{1 _0 R( f$ [/ E) k
q = q->next;
8 p s4 c0 a! y p = p->next;
- f5 f1 z, t+ h' J, B }9 P$ h) P2 ]; a+ Q& C, a9 i
}
4 R/ s/ O# g, o7 s} main()
* ]5 b# }& B5 H2 [{
$ u+ M1 q7 k, f5 o int i, j = 0;
( X" K# Z: f I$ W& Z printf("a=");
/ p* H( k9 A# o( ^4 U: ? scanf("%s", a);
0 n4 D' j* Y: l) |7 Z3 ~ K) f* W) { j = strlen(a); input(a, j);
- ~& _/ C3 L; o, T stinsort(pa);. B3 m" `' Y( P; |* Z
printf("a=");
+ J% B" _, C+ |6 R/ ~' { for (i = 0; i <= j + 10; i ++)
& w, s& \' V+ | X( [ {! p! w3 \: E' D/ F' G E
printf("%dx%d+", pa->coef, pa->exp);5 U$ ^: A5 g4 F+ ^
pa = pa->next;4 S; I5 [- P8 ^6 v
}+ f" r3 v9 _( l1 C3 l0 E" `
}
( @5 ?+ k- o; K8 V' I 我把楼猪的代码弄了下,把里面大小写错误排除了,这段代码会有死循环,谁有空谁研究去。 |