#include "stdlib.h"
$ p) o+ c, b' P& f$ v* H#include "string.h"* S) B( h. g9 d d) V4 H. O6 t
#include "stdio.h" #define MAXSIZE 400
) m) t( N3 ]' l6 [% I" F) jchar a[MAXSIZE]; typedef struct
8 ^- l. B% A; G; { P: q4 s{. B' |: l; l7 Y: ` J8 `
char elem[MAXSIZE];
0 q. x Q- H+ ^# Q% \ Y) n int top;5 F( H7 c/ z# h1 m, r4 p7 E
}SqStack; SqStack S; typedef struct poly
3 [- T. {. @: z. a5 F6 G" {1 Z1 a5 K{
* k7 T3 |) ?4 w4 ~# I int coef; /*xishu*/1 z+ u8 Z2 I. }% V4 [# x# z1 N) t
int exp; /*zhishu*/
6 V+ ~( U7 j' k. l, V3 M struct poly *next;
5 z7 g, ~# b: b}Lpoly; Lpoly *pa; int cf(int i) /*10^i*/3 s( H- _. z. e7 |/ c8 y6 m
{$ X2 b% r8 k3 D6 ?' y4 [
int c = 1, j;( {, M/ m. e: o- N( X* w- k
for(j = 1; j <= i; j ++)
$ e( `" T: k8 _ c = c * 10;
! I3 e" z$ ^, b$ v; j return c;% A& n, H6 I: U) X- ]) ^2 A
} void input(char a[],int j)4 o4 ?* h0 q& f4 [
{
+ [! Q `7 v; Q9 b5 [6 I Lpoly *p, *h, *ptr;
) i- a$ N. Z* @: S1 b2 b ]4 X int i, k, flag = 0, x = 0;* z/ K! B3 o# T. k; g9 l
S.top = -1;" a+ h/ [* d* D+ n+ }# x# L
pa = (Lpoly*)malloc(sizeof(Lpoly));; ~$ `# ?+ Y7 i7 ]5 e
pa->coef = 0;
% \- z2 A% y2 ]( X, D8 m+ ~ pa->exp = 0;
2 l# O' I2 ^: I0 K( J3 h$ I h = pa;
. L7 d* p: j' q% W6 A. X5 p q, | h->next = NULL;0 K0 R7 R3 E0 Z3 u7 m# E7 k+ I7 R
p = h; /*jianlipa*/# Q/ u9 v& h4 ?+ c/ D( I. t
; F- E7 F/ f) a# y
for (i = 0; i <= j - 1; i ++)
) }) m6 _+ c& r, |; W {
% Z$ P- L% g6 Y4 r, m; s0 T% n ptr = (Lpoly*)malloc(sizeof(Lpoly));
- H0 h( ^) e6 { [ if(S.top)
" o5 ?" z; [' e& m: o" {9 o {+ d( T3 H/ G3 S2 s9 x! Y$ B" w/ [
S.top ++;
+ N B2 a: m3 m3 g" ^! g S.elem[S.top] = a;% V! N9 S" V( @5 {4 U6 G: P6 h
}2 f( m Q/ ?% T+ i7 Z; N
if (a == '-')0 [* P( V9 D O/ J# |
flag=1;
4 Z) s& _/ r) j4 H9 ~" e; [ $ F7 Z7 p7 G4 A" b& x
if(a == 'x') /*fenlixishu*/+ d& R- z* ?# j; q% K
{
r! D5 @( I' V, q if (S.top==0)
' D! O9 f& k/ W" C: q7 ?4 M {
! u# u' l6 w% F- f; w/ t if (flag == 1)5 C& j4 p. R; ~4 E3 `
{! S* |* [$ D7 s+ F! C5 K
ptr->coef = -1;
0 M {$ G" Y, w- y- T1 C6 O# e ptr->next = NULL;
& h8 ?5 v4 ~7 R0 ?6 g }
3 Y! k5 l& j p7 B: Y% ^; u1 c else: G" F. V2 N7 H: c% R) S
{
@+ Z" f/ Z' c2 G7 x- Q) R& A ptr->coef = 1;
( r& J' x* O. @/ f) s ptr->next = NULL;
, b6 L2 D8 p6 a% d }
) x- d2 b5 h: I5 u }
7 q2 ^2 N0 }4 [) ?. |# M else' k+ _* d" I: [& O2 ^8 B6 a! S
{
0 ^1 K- U* R/ G# S: z2 w for (k=S.top - 1; k >= 0; k --)3 e( C0 V" r( C8 J: d- D6 C
x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);3 ~- P" f' \7 J8 l+ h
ptr->coef = x; Q$ I3 d4 J4 B
ptr->next = NULL;
6 A% q( y" B8 J7 l& |! s x = 0;
$ k& U3 x y3 S }8 \! }% N4 F' }# H0 i9 R. d( T
S.top = -1;* b7 E$ W; Q6 j
flag = 0; /*clear*/; u2 ~! u- V4 D0 ^) b
}' \1 N7 b2 M' ]' n' M2 t# M
if (a == '+' || a == '-') /*fenlizhishu*/) p) g( N$ d# ]& O
{" o3 f1 ~5 ^! c7 X
if(S.top == 0)7 x, @) v" T. h" G1 g0 _% x" c
{4 Z* B" \: d% i; V# M
ptr->exp = 1;
* T% p) i2 b* C) k ptr->next = NULL;+ E, i& N( m; |% h3 x
}6 C0 l M3 f4 ]
else
( W6 h; |8 I8 W: S/ _5 o7 v {9 ^1 }4 ~* q! U$ L$ G- i
for (k = S.top - 1; k >= 0; k --); j3 j5 s" Z- l( H/ d
x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);
7 d6 E1 |: J8 o6 }& a ptr->exp = x;
) ~/ o) g2 u4 \' e- A ptr->next = NULL;6 j8 W+ M6 w1 b9 I4 t
x = 0;; B N3 g) N# J T( L
}/*tiquzhishu*/8 K+ P% v6 F. T0 H0 [
S.top = -1;( }8 o, i5 \7 V- [& u0 ?
}
' r; L% x! z* k3 a6 y6 r* x* ^ if (ptr->coef != 0 && ptr->exp != 0)
3 q) ?- O1 z( m h, b" c1 d, ^$ G {
1 }, {3 {7 e% v5 F p->next = ptr;7 q4 o2 ?( z* y: b
p = ptr;0 ^" G* J! r; p4 e0 i q! v; _( l
}7 E! m5 o, o8 w* X- h2 z J1 V
if (i == j - 1); t! B, ~# |9 x# x$ V
{
9 d6 O, I' P4 n" B, m A4 `# G" a, S if (ptr->coef == 0)/ q2 T( H+ }3 m3 r* k
{6 U U' l4 L" x
for (k = S.top; k >= 0; k --)
$ ?0 P/ N$ X4 r) u9 ` s0 p x = x + (S.elem[k] - '0') * cf(S.top - k);7 }: j- R/ x" V# w! K* M# T
ptr->coef = x;; G# I( l3 M- A" d2 o# F
ptr->next = NULL;6 f+ v4 R% H/ N5 f3 p* C
}
, E3 ^* q& }! b0 s _ else( y0 j% k( M5 R) }' o- [7 G
{
1 M3 Q! R! M. z W. C7 a for (k = S.top; k >= 0; k --): T' v$ m/ o0 k. M# W/ E) @
x = x + (S.elem[k] - '0') * cf(S.top - k);3 i% Z" N8 Q2 G! M5 W* k: X
ptr->exp = x;
# f# t( |5 o+ L3 G ptr->next = NULL;
$ n+ b r& q% E" L }& Q! s& L8 `$ Y0 \' f" ?
p->next = ptr;; x0 Y' H0 s, a3 a2 ~. Y3 e- T' m
ptr->next = NULL;
3 p4 Z* u0 l' p ]( m% Z: s3 x" C }
; x& q5 q, L1 l! r, P- O! n% O( e( j }
# v6 m; Z3 Y s. A0 c} void stinsort(Lpoly *pa) `& @# n0 D+ x5 L# b2 }% _+ @
{% X+ l- k" E+ m( U0 ]' }( I
Lpoly *p, *h, *q, *k;
% M- D# @: |0 C$ }8 I& z h = pa; H' m! w# f' C/ k3 w
p = k = h->next; m6 [( Q0 K: }
q = p->next;! n0 Y3 {; F- e% c4 L; h3 n' p% ]
while (p->next != NULL)
( d: x0 C: O# o4 m" \* D; G {: a/ t# t# w7 v) z9 X
if (q->exp)
7 a6 B: j$ F/ k! }5 Y- ]& `# H* [ {
" j$ D* {6 a, ^+ q+ q" H! r while(q->exp > k->exp)
5 _5 |1 p% q8 z/ F {
+ l9 y% O- f# X4 y8 P% a k = k->next;
* l; R7 g& O0 f( B1 ^ h = h->next;3 a1 p8 k. e& T, N% z
}
! F5 b" i( N0 P if(q->exp)' D/ A, Q7 N5 U2 `6 @
{
) S% z; s+ `9 G& B. g p->next = q->next;% o8 w0 m/ F, \. N$ {6 ^7 e
h->next = q;
9 a2 D F) ?5 \ q->next = k;
7 {1 ?% L3 m7 X( d# Y" C0 I q = p->next;
8 V1 p* F4 p5 J0 x! @% k }
/ w: n; X* H1 d8 m2 _# r if (q->exp = k->exp)
, P2 J7 P) k. L( |' z {9 A0 S1 F& \- r8 \( ]; B; P4 e
k->coef = k->coef + q->coef;% b) S3 T3 F4 G; h5 P7 O
p->next = q->next;
1 U8 J2 V- C& G$ k: a, E" A+ O q = p->next;/ D" e6 j A0 k5 W4 p
}
5 W7 N" O* j( [. K! J h=pa;, ~ K$ J# a- w$ y- Q! h
k = h->next;
( x1 U- R' Q9 Y) j; e }
7 y& g- t1 C. j8 C+ t- p3 P
9 `$ |. U2 ]0 \4 G if (q->exp = p->exp)
; p# b3 M _8 H* r1 b {- y6 o/ N Q% n; e* _
p->coef = p->coef + q->coef;
$ ?/ u) M+ B$ E1 X- { p->next = q->next;5 o- i* B! R6 l4 D1 a
q = p->next;, i5 Q( \" e% e5 B+ U1 s8 q
}- z7 U/ {6 I" a0 W0 O
if (q->exp > p->exp)! j6 c8 h( |8 X5 k1 O8 K$ e$ x
{
" ]. h6 s) T F' F' Q9 f q = q->next;
; c9 B5 I+ p, T y5 A; Q7 h p = p->next;
! d' R, X' V1 w% o }
& t2 r, E1 @" s$ @, t/ W& h }
& Q/ K! A' _" ^! R/ J} main(). s5 m$ m1 Q- V! B- q
{
$ c7 @$ g" w* S int i, j = 0;# I. n& h8 d! x* B0 R
printf("a=");
! {6 U& N3 G" G) ]2 J) n* G scanf("%s", a);
& _9 o/ r3 u' q( I' N4 i j = strlen(a); input(a, j);
" m9 Y% j- N6 d5 g1 a% D a3 ^ stinsort(pa);% s6 N2 e' ]1 i5 W: z
printf("a=");
; c- L" a; h; S8 G9 [ i. r for (i = 0; i <= j + 10; i ++)9 Q1 f+ i5 ]4 p" P& h' S" e( N
{/ `/ h) c) L4 \7 F; k
printf("%dx%d+", pa->coef, pa->exp);
) h9 y6 Z0 \3 ^( u# b/ m% S& } pa = pa->next;- Q2 R' F9 O* H# O4 D2 x, k
}
: b7 b" B5 m. A/ g) p}
! U3 o# n3 f1 C 我把楼猪的代码弄了下,把里面大小写错误排除了,这段代码会有死循环,谁有空谁研究去。 |