#include "stdlib.h"4 B& R$ T; {0 c8 k4 k# H- O' _
#include "string.h"
$ g! p- D( ~) U- L3 r#include "stdio.h" #define MAXSIZE 400. U/ r0 a/ F# J- O) R
char a[MAXSIZE]; typedef struct; q3 D/ [' I ^0 ^; E6 l
{
4 J3 s6 z& B0 B1 E8 A; G char elem[MAXSIZE];
: h5 \& u" l& ]7 x0 ?9 v; h5 w int top;4 v8 ~1 r( a; Q- ~7 S
}SqStack; SqStack S; typedef struct poly6 C. E6 F* D& [+ q
{& v% C1 \& W* ?% b5 Y: Y$ y3 I
int coef; /*xishu*// o/ _- [) m+ o
int exp; /*zhishu*/" ~2 Y# z; a( R( m) `6 e
struct poly *next;* r; b. R; O# ] u, P
}Lpoly; Lpoly *pa; int cf(int i) /*10^i*/
$ J% U- q- x% P3 Z$ p( l{
) ], o8 l# C- { int c = 1, j;
; Z3 w) X; C3 Y; r6 L for(j = 1; j <= i; j ++)
! V! I% @# L& k o3 ^2 C1 w. q c = c * 10;
! \ R/ l: n; m4 b" j' u return c;" l; e" H$ n7 K
} void input(char a[],int j)
, @) I! ~5 L N* T7 U/ ?6 J{
/ b; D) R1 {0 G) d* k+ y Lpoly *p, *h, *ptr;
. x& P3 z* @) C8 A* T" ~ int i, k, flag = 0, x = 0;% _2 J1 _, G+ {* X1 o
S.top = -1;
/ ~& |2 M' W- w0 S7 H pa = (Lpoly*)malloc(sizeof(Lpoly));/ ?" n" N9 H N# U: I- c& Q
pa->coef = 0;
7 J. P& F- b4 Z: t7 l pa->exp = 0;. C1 e& i( j3 r" }: V% H* k1 I
h = pa;
! c T+ d: i9 K( R5 Q3 a h->next = NULL;
+ J! v/ ]% g+ A% [) ^( A4 o p = h; /*jianlipa*/
% y6 ?0 F: E4 u6 r # k- i- y; M2 p, p5 e
for (i = 0; i <= j - 1; i ++)
: ] \2 t4 R: V5 V* @ {4 o; Y: k- t: O4 h
ptr = (Lpoly*)malloc(sizeof(Lpoly));
% W$ x8 j, M- g* Q if(S.top)
1 L" n' i0 ]/ ^5 K0 ]) B! G- L' C! b {& n( w; S, B9 d6 S. d5 `
S.top ++;" I0 y% z7 e& O2 S9 ^
S.elem[S.top] = a;
3 j) E% m2 ?6 O }0 U; F, O: w3 J& b
if (a == '-')- M4 N- F7 p& ?4 R6 b
flag=1;
! @/ T5 \* K. e- H! I# r& q # f+ l( @8 ?/ n! k9 X W- L9 f; M1 g
if(a == 'x') /*fenlixishu*/
! B6 ^' P# G2 c- C {
7 \& k! w) n5 @7 }$ b }+ S' e& Q! I if (S.top==0)
7 r8 S( W$ {1 K7 z1 s {
6 O; M. P8 Y; ^5 l6 n- o if (flag == 1)9 u( K) H, N2 q& t6 ~* |% |
{ E! H* s9 W0 `1 R8 c3 U# g& N
ptr->coef = -1;
, Y6 D( l# L& k" ^ ptr->next = NULL;
# j5 v \) {& u/ p }
8 z+ i( w& p6 a( H- q+ Z else
F) ^- F T7 Q! Z0 A {
( H; {" Y, T6 t* `9 s ptr->coef = 1;$ Z1 n: o2 T2 V2 f/ L7 l7 \
ptr->next = NULL;3 r! E) a& I6 {7 {
}- n/ S& O2 R, q/ G1 \; t9 P0 J
}% d* O* j) W- F# ?5 A& `
else( j" _9 Q8 E4 k1 q( d% A" I" d
{
) @* O! o% c% Q for (k=S.top - 1; k >= 0; k --)
/ c0 S, l/ A# D" R; U x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);1 w- G8 A2 a) o" X7 x
ptr->coef = x;6 [! z3 \9 _3 ~6 h) g! e! W
ptr->next = NULL;! [0 w ~( P# a$ w: V
x = 0;2 p: m# v4 x% Q, B, U' D7 h2 D0 g
}
' c& U% G* P8 |' p; ^4 b/ \ S.top = -1; a& L4 R3 G! M0 k5 l7 k+ G$ F. _" S
flag = 0; /*clear*/6 N0 l6 C4 B. F4 F$ h7 R
}
2 ~6 ?6 x, E2 y+ y# f } if (a == '+' || a == '-') /*fenlizhishu*/9 C* F* H: |+ |. z
{9 j& V& ]4 I- i% W% G
if(S.top == 0)
) l* Y8 i/ D' v/ H) I8 }- e: f {7 n+ @5 U4 Y+ O$ K/ Y- f) c; P
ptr->exp = 1;
/ b& c6 o; q) O4 ], ^. q ptr->next = NULL;' t. e' f% G# x6 a$ N- v! f' F
}
/ c6 m* r$ m) t else1 v8 H; O7 Z5 j. S; U
{. j9 K# D; ^* `" \9 b! l2 f
for (k = S.top - 1; k >= 0; k --)
+ k8 A; c4 @/ `4 Z5 U8 M. T x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);
( z3 A# `( C$ U0 }+ x ptr->exp = x;+ \/ J6 P5 v! r+ W2 v' t
ptr->next = NULL;9 y e: x1 {4 V6 S7 V1 Q
x = 0;6 f2 M5 M$ R. A# b6 f# u" \& g( b
}/*tiquzhishu*/
. _( \1 m" n3 ~9 { S.top = -1;8 [. k6 c3 G1 _: h+ }& _
}' V; w- F9 [. H+ g0 _0 [
if (ptr->coef != 0 && ptr->exp != 0)! R/ r6 [# s( j7 x5 S# h
{
) G4 o' g' A+ A1 h, T& ~. s p->next = ptr;
I! m& J8 V; u" s4 C" G, ]. U p = ptr;7 E6 C! ]; b4 \5 H
}
: V. H9 r$ U3 j2 u if (i == j - 1)
2 V) s, x J, X {3 Q# i- G, k# S. y
if (ptr->coef == 0)2 t/ X# D+ ?+ e1 c" Y! z
{
' M7 b2 n G' |9 ^ for (k = S.top; k >= 0; k --)+ L0 n: L* B# w9 W: n6 a2 V
x = x + (S.elem[k] - '0') * cf(S.top - k);( s5 W: J3 `& J3 `
ptr->coef = x;7 Z1 h, G" v' @% I
ptr->next = NULL;. N% s; h+ T E/ ?
}3 S: {' f8 P( K' X$ C, E
else
) F" M$ H0 |) Z6 k; J2 | {
1 W2 Z$ a( A( I4 ^ for (k = S.top; k >= 0; k --)
( k; D. N3 x# P5 G8 Y+ I x = x + (S.elem[k] - '0') * cf(S.top - k);" S7 y+ z8 q. R" i
ptr->exp = x;! S) h5 A! g: C. J" a0 s
ptr->next = NULL;% l9 r5 N ^& A& v" ~
}
- l1 T G, n! c2 n$ n p->next = ptr;
1 a) o) q0 J/ L5 \ ptr->next = NULL;
2 d, J5 a& g9 }3 N& e }8 o: t/ t% W) h7 ~3 c, e" i- b4 d5 t
}& s) S% ?# P0 i- H: q' f1 n
} void stinsort(Lpoly *pa)
6 l+ d$ b5 X4 S$ h m$ r8 T6 G: l{7 l/ z6 O) S: x6 R) Q- |
Lpoly *p, *h, *q, *k;! [0 T6 p. d) M0 H* B7 {) E, r
h = pa;
0 y% w0 G8 h2 I3 P- ?! O9 S p = k = h->next;& W' x; V B4 _& {: R0 `; \" Y0 b' `
q = p->next;
2 X/ g H" @ {& t- `6 B while (p->next != NULL)
8 ^" I. h" f7 o- p1 \# A {5 E& @0 _$ P( U# j- Q
if (q->exp)
& q D0 \5 `! u, U( \ {
* d, C- e! Z( J7 S+ ^ while(q->exp > k->exp)
9 `7 ?3 d7 t( g3 _* I8 } {( a4 ~1 C) k. u/ e/ S
k = k->next;# J! x- _8 |8 i2 R0 X0 P
h = h->next;
" v9 b) \% k8 _; ^ }9 Z1 N- v, y6 K3 z7 L- w# h
if(q->exp)9 z. i* h) A: Y3 m
{
6 Q* ^6 e& H" G* Y' B' K1 K; { p->next = q->next;% n- u+ \# Z, R: h+ ]
h->next = q;/ ~! J( ]4 X3 L+ k
q->next = k;
- r; `8 x2 L( c q = p->next;, a' o) l5 v% l: @4 y3 Q3 w
}
* ?/ \* c2 R% B; g if (q->exp = k->exp)
: n* T: x0 O# K, c2 u3 w/ R {
+ @( u5 c4 _6 L+ @ k->coef = k->coef + q->coef;+ s: {2 z% u+ d# `8 ?
p->next = q->next;
3 Y1 ~0 x0 M$ J& ?3 P, J1 | q = p->next;
* ?) q6 F0 g0 y d }
. F( R" x6 D7 v- K W" |- S1 C h=pa;
- y& U1 W0 S( R: G/ g k = h->next;
, e( q1 r2 e( N! G5 o# P) |) c3 y ] }2 A$ L7 L w6 p: T5 z( r# e
- }- U: Z! `: {# Q' [8 v; E if (q->exp = p->exp)
5 \! h" a B$ W0 ? {; k `6 L+ Y1 c
p->coef = p->coef + q->coef;" A) l# d. p7 r0 O7 w4 A7 X) {
p->next = q->next;
# N9 a0 O' e2 {% F/ k q = p->next;0 |+ T, k7 T, p
}
( A) P$ d3 z8 ?; \9 \ if (q->exp > p->exp)) C: U" x' @9 _$ R% T9 \" i
{
- B$ e% C$ V5 s1 K) m q = q->next;
0 Q4 }0 a+ ^- ~4 {. u: a/ q p = p->next;
0 T/ ]& P- Q/ } }
3 `! B e4 n2 C' _ }
% l# E$ i# n# t5 B: p3 F} main()
0 I- v7 H( ?$ h1 \' G t{
. n/ x; L3 ^6 K; Q$ y# D: ^& ^ int i, j = 0;
k7 ^7 ^' o0 r# p% k8 M printf("a=");
2 H! x7 v" P2 ~ b& i5 y0 w scanf("%s", a);
# d+ g- ~" [/ K- W1 Y j = strlen(a); input(a, j);7 ^* g {: V$ b. @5 P! `
stinsort(pa);3 m% b& Z; u% \- ~) K% |4 S
printf("a=");
; }7 q/ H" T9 R. c" E% l$ { for (i = 0; i <= j + 10; i ++)
) ^6 H7 [) N3 K# k) C% Q {
2 ^: C' r- L- m' d6 v/ l* ] printf("%dx%d+", pa->coef, pa->exp);' h6 ?- n. H1 i7 k# d' L
pa = pa->next;
* W' C# Q* ?! R3 U. ?( Z }
3 N% D; i; V$ F- N" r5 [}
; q/ i' b% f' a 我把楼猪的代码弄了下,把里面大小写错误排除了,这段代码会有死循环,谁有空谁研究去。 |