猛然发现我已经受不了C的那种模式了……尤其是别人写的……
还有搂主最好打个包,代码发到BBS上全走样了,有些符号没了,没办法帮你看
不用对齐的编程风格也就算了,可是把几行挤到一块儿的风格也太小家子气了吧?改改啦,不改成不了程序员的。
#include "stdlib.h"2 X# O, L& s0 Y4 h4 q( y- X #include "string.h"8 L! Z: s; `, z #include "stdio.h"
#define MAXSIZE 400
char a[MAXSIZE];typedef struct
{ char elem[MAXSIZE];5 _3 h+ O8 g% e int top;, {+ h7 \2 P7 O) w- @ }SqStack;SqStack S;
typedef struct poly4 u+ _) M- x3 F4 u {- o4 |& v+ \7 t" E) P int coef; /*xishu*/: I: W0 ]! |3 j" ?2 l int exp; /*zhishu*/: }9 h/ h! k+ v% t9 e) K struct poly *next;
}Lpoly;Lpoly *pa;
int cf(int i) /*10^i*/
{* j8 p2 U3 C1 u; S6 ]7 } int c = 1, j; for(j = 1; j <= i; j ++)! c5 G4 o% G6 v6 a1 V* V) F! m: D c = c * 10;/ l* E- N! o% l4 \' r% b. b% [9 V return c; }void input(char a[],int j); b( d% n) `0 d9 E4 `% | {
Lpoly *p, *h, *ptr;/ d6 d$ ^ x4 m) [+ h int i, k, flag = 0, x = 0; S.top = -1; r. F: H2 t7 S. N; v pa = (Lpoly*)malloc(sizeof(Lpoly)); pa->coef = 0;* [1 D) p- `6 _8 ^' [ pa->exp = 0; h = pa; h->next = NULL;2 \, B0 ?* O; C p = h; /*jianlipa*/4 E, I8 b, Y2 b6 S for (i = 0; i <= j - 1; i ++)6 v9 [0 [. Y: `" z& o { ptr = (Lpoly*)malloc(sizeof(Lpoly));! E) P) _+ [0 G4 m+ H: T+ d G if(S.top); _+ s" |6 ?1 e5 O# }; i {$ J w4 B4 w/ ?0 l9 ~0 i( S8 W S.top ++; S.elem[S.top] = a; }& \( |/ G U4 s6 ^- {1 @- t if (a == '-') flag=1;$ Z9 C+ M) e4 N: I3 f if(a == 'x') /*fenlixishu*/. }. e0 u9 @/ j% B0 m) y$ o6 ~, ^ { if (S.top==0), {, u7 M% w" n; N2 ^3 j {! c% o' p- T- ?* F& a- o/ ? v- R if (flag == 1) {4 V! k7 W9 W; \) ]( J# L% O ptr->coef = -1; ptr->next = NULL;% u/ {" W5 n3 z7 Y8 h }( M+ {( N2 ?" S1 @7 T% e! p3 S else {, w9 k$ Z6 ?7 z, A ptr->coef = 1; ptr->next = NULL; }( [# S+ |; e# k } p8 S' q! h! D: P% B9 ^ else6 G1 {0 K- \& H {2 L" V5 h8 N; ]) R' t- }7 u for (k=S.top - 1; k >= 0; k --)1 u% f6 U0 o3 ~ x- F/ p x = x + (S.elem[k] - '0') * cf(S.top - 1 - k);: C. ?3 A% ~, }: }' s: W6 V! t ptr->coef = x; ptr->next = NULL; x = 0;8 _ r& [. i( R0 R/ s$ w, C( u } S.top = -1; flag = 0; /*clear*/ }) m& N' f$ W: m1 r/ w; R if (a == '+' || a == '-') /*fenlizhishu*/9 u* h+ _" D* N, P# q2 W) J {1 m& x6 d' U; z' V& I8 x if(S.top == 0)7 m1 Q6 V# v3 }; H5 b {8 ^3 w0 F: l: A ptr->exp = 1;2 v, S% S2 S( p% Z. p ptr->next = NULL;/ y4 B% P& K6 ]2 Y% L1 G }# v6 K$ ^& N% w, K8 I- ^ else {4 [- }/ M3 K& y8 l/ @7 M for (k = S.top - 1; k >= 0; k --): O5 U3 _7 K7 x1 ?+ F$ W x = x + (S.elem[k] - '0') * cf(S.top - 1 - k); ptr->exp = x; ptr->next = NULL; x = 0; }/*tiquzhishu*/ S.top = -1;" H; A }" {6 O' n7 H- A m; }, M }% r' Y) }: y t6 X8 ^: D if (ptr->coef != 0 && ptr->exp != 0)% b8 ?( w0 R" c8 [+ l { p->next = ptr; p = ptr; }& J+ n+ m: K% s1 |( ?1 }+ e" c$ V if (i == j - 1) { if (ptr->coef == 0), }3 f5 {5 n) F, v+ Z { for (k = S.top; k >= 0; k --)# m/ R+ ~4 |! B. M( ^3 y6 F( s& x x = x + (S.elem[k] - '0') * cf(S.top - k); ptr->coef = x; ptr->next = NULL; } else* |' e5 J- |9 ], I% ^* X { for (k = S.top; k >= 0; k --) x = x + (S.elem[k] - '0') * cf(S.top - k);; G6 l- f8 `' p5 X O" z" M ptr->exp = x; ptr->next = NULL;0 u- D8 H' e5 }% F) A } p->next = ptr;- V5 h; Z5 w, [- n" f2 B ptr->next = NULL; } } }void stinsort(Lpoly *pa) X# D( q6 b/ X& C2 a {
Lpoly *p, *h, *q, *k;- n4 ]' ?4 ]( c$ S7 K4 ` h = pa;8 t: p" N0 H, ^" p p = k = h->next; q = p->next; while (p->next != NULL) { if (q->exp) { while(q->exp > k->exp), B, y2 }* l+ |9 S, C% F {7 i6 |. G! o! X% g7 `( X) n. Z5 p k = k->next;7 u' C/ K3 f8 i) N1 T, e h = h->next; }% O' K6 m9 ~! S' S. _; A if(q->exp)) \* d# \& I) I* u2 A, s { p->next = q->next;3 k0 \% H ]; |7 |5 ^5 k l h->next = q; q->next = k;* I; Z" x' X/ T2 U q = p->next; }9 z3 S5 h$ S& Q1 n if (q->exp = k->exp)% L1 G/ k0 O( j% h7 j) ^ {- p, o; }! M2 X4 T3 C+ e8 F$ W W k->coef = k->coef + q->coef;+ R& f+ u# L$ g7 o9 j* ` p->next = q->next; q = p->next;+ q% d* V. X3 n8 s/ @1 z }, L8 o: d7 f2 K h=pa; k = h->next;" X% N; w; `+ |( w' h& w% `( E& ?! C }5 o* d; i2 E5 L 6 P0 t h. Q6 j& T if (q->exp = p->exp)" v; Q5 ^ Y) h) V8 ]4 t {6 ?0 @" |+ _) _- M% q: O1 h p->coef = p->coef + q->coef;6 R# i/ S$ @$ h* S* P4 W" L6 S p->next = q->next; q = p->next;1 F+ ~/ m& P- r0 q$ p0 {$ { } if (q->exp > p->exp) { q = q->next;( `9 t; Q2 A9 ^! L p = p->next; } }+ y; Q' E; Z2 X }main()
{" r- c" _+ y: h( q! r5 r int i, j = 0; printf("a="); scanf("%s", a);3 u; C5 b) m8 S# _ j = strlen(a);input(a, j);: S" f- z) \0 b- \# J stinsort(pa);. v" J2 m3 T C" t0 G |( D, N9 U printf("a=");3 L/ `2 R" v8 \0 Q9 Q for (i = 0; i <= j + 10; i ++)
{ printf("%dx%d+", pa->coef, pa->exp); pa = pa->next; }( h' k1 w$ c7 G( A3 r }我把楼猪的代码弄了下,把里面大小写错误排除了,这段代码会有死循环,谁有空谁研究去。
it is too hard for me!!~~~~~
欢迎光临 下沙论坛 (http://bbs.xiasha.cn/) | Powered by Discuz! X3.3 |