下沙论坛

标题: [求助]请教各位大虾 [打印本页]

作者: lvyan    时间: 2005-7-6 10:23
标题: [求助]请教各位大虾
这是我的数据结构课程设计9 y- M2 y+ i. s/ y }/ b8 N 实现一个多项式合并,字符串输入多项式,链表插入排序 4 z2 ~) a. G! c% |, j7 I6 I. t可是我运行出来结果不一样啊,大家帮我看下哪里错了,小的在此谢过啦~~~5 X7 P* G& H, C0 i# F; ? #include + h, X6 L1 T" d9 q, h8 C8 I#include. W) _4 ~& d7 L) F8 R- O #define MAXSIZE 400 ' u% V- t" w! _: J" ichar a[MAXSIZE];% a0 |& C9 f! N' _/ @+ K% h; M" ^! r typedef struct{char elem[MAXSIZE];; ]8 g- P( d# X* N6 J1 m int top; " @9 U6 z) _* K- }- _+ R' x/ n% S" _ }SqStack;! F) F. }$ U8 a. U. C SqStack S; 3 m: S+ P; e% L& y+ \typedef struct poly 7 \. ~. ?8 N, j5 n {int coef; /*xishu*/ 1 v) {1 O# b# d' R3 H2 t3 D7 ?+ j int exp; /*zhishu*/ 3 O" K$ ^4 H1 K* m s struct poly *next;" ^ e* _/ J* n2 n% R }Lpoly;( M" C* j0 h6 W Lpoly *pa;$ B# E [( M2 ]5 Q P' s) L int cf(int i) /*10^i*/ 4 ^2 L$ a2 r- ?{ int c=1,j; : Q5 o q2 C% h# J( I for(j=1;j<=i;j++) c=c*10;! f1 F6 l; m0 ?* L return c;( q5 W$ f/ ^0 r4 k }% b$ `, t0 t! m o void input(char a[],int j)2 E8 e& H6 a1 [! Q) o" R7 ~' g { Lpoly *p,*h,*ptr; ( o2 [% k! h& W2 P- N" R int i,k,flag=0,x=0;! P0 B. q# @+ Y: m' H S.top=-1;0 l/ K. t6 i& N6 t" n" G% ]8 V9 ] pa=(Lpoly*)malloc(sizeof(Lpoly)); g$ Q5 Q6 D: |1 F; y. k i pa->coef=0;pa->exp=0; - _; _. Y0 p$ W! z# K& I h=pa;h->next=NULL;$ t3 h6 X8 \2 z: `- o& q$ w p=h; /*jianlipa*/1 G9 h, V: i7 H% z) O/ u for(i=0;i<=j-1;i++) 9 ~$ v1 q8 F8 ]4 l$ t+ v {ptr=(Lpoly*)malloc(sizeof(Lpoly)); . z4 i. W* t8 S4 Q% C if(S.top& S/ ~) x0 t6 J b5 H9 o {S.top++; * E' s O$ A7 r. k S.elem[S.top]=a;8 g( k) T4 \ v- i6 d }0 h+ f- q; p. [0 D: X if(a=='-') flag=1; 4 i: J' q- {8 ]* ?. p" f if(a=='x') /*fenlixishu*/; t3 ?' Y T. f1 w9 j7 W {if(S.top==0)6 f0 P7 \" B5 c2 C: I4 L' P3 O {if(flag==1) 9 e) D" r0 [) T5 |; ^( n9 G {ptr->coef=-1;ptr->next=NULL;}8 _( a3 J, i4 m else- c, b! t8 f2 r% z: W+ \& ? {ptr->coef=1;ptr->next=NULL;} U2 {" p1 o. I `; x. A+ t6 G } 0 z3 g- T& ?. B ~! \4 e/ C else * D6 L' ]; V( }7 |8 i e' r {for(k=S.top-1;k>=0;k--) x=x+(S.elem[k]-'0')*cf(S.top-1-k);- r# O6 u; ?: |' I, K# J ptr->coef=x;ptr->next=NULL;x=0;* q& ^- m% X, v/ `/ u } * [5 b9 C% g9 }: A+ @ S.top=-1;flag=0; /*clear*/6 {4 t: D5 Z' x+ E ? } 5 O; V, l* z" n/ U if(a=='+'||a=='-') /*fenlizhishu*/& v1 ~4 [ e& o! |! K {if(S.top==0) # E, \8 S d, N5 u6 J G {ptr->exp=1;ptr->next=NULL;}9 p2 ^8 N" ?8 w8 z D! y2 P* F else 4 q( q |: }* _* C- e% N5 r* ] M {for(k=S.top-1;k>=0;k--) x=x+(S.elem[k]-'0')*cf(S.top-1-k); % D7 q4 W. K* ~- a ptr->exp=x;ptr->next=NULL;x=0; & \" X2 L* r% P% p9 Y. i } /*tiquzhishu*/ " `7 I0 r2 H. V) I S.top=-1; 4 \+ |( _5 `# j: R& z } , _& Y/ m- X& \$ u! c: d if(ptr->coef!=0&&ptr->exp!=0)5 C8 S2 O) j8 r" \0 X {p->next=ptr; 4 \; y- X' e! H p=ptr;) Q' D$ g* |$ B( H% I" f2 _3 ?$ I3 x }7 ^. q( f# v% M0 a# A: y& t" u if(i==j-1) . g# u; W* k( ~+ W7 r6 `% ? {if(ptr->coef==0)6 P E1 b" J0 S {for(k=S.top;k>=0;k--)! ?0 ~7 C2 M' @ x=x+(S.elem[k]-'0')*cf(S.top-k); ) g+ i" i: o2 r) t! W- s ptr->coef=x;ptr->next=NULL;+ p' E! M. G3 v }' o! p4 Y# A. V! ~ else" A1 @* c7 S P$ ~ n- G {for(k=S.top;k>=0;k--) 2 M& o/ e) ?0 N0 l( l2 N( ] x=x+(S.elem[k]-'0')*cf(S.top-k);1 Z3 W) L. p9 A8 W) B- X# y. ?7 f; p ptr->exp=x;ptr->next=NULL;5 ?* ]. _0 z& |8 Z+ n$ d } ! S4 D, b# {! D; \$ }9 _& h p->next=ptr;ptr->next=NULL; & Q1 ?# q6 J% t0 ` } # L9 h/ a' P2 t" L" p5 g& w }3 V1 y+ j6 ^; C, ~( ~& R }, j; Y! o, d9 h- e5 o void stinsort(Lpoly *pa) ( o# p9 T6 l' z6 `{ Lpoly *p,*h,*q,*k;0 t5 P5 t% D2 X) u8 |1 c7 Q5 B% [ h=pa;p=K=h->next;q=p->next;$ _" F1 ~, y4 g6 S7 W* d7 x6 y while(p->next!=NULL) 4 M( p9 Z! V7 w7 o1 i2 I. M {if(q->expexp) 7 i3 a ]9 g2 Q% {0 G2 J {while(q->exp>k->exp)3 c `3 f1 w) I2 B {k=k->next; ( H5 [$ b5 Y$ h! L6 g h=h->next;3 j! P3 ^: j3 d s9 m4 ?/ M }, w* `5 y/ E( ^$ X8 p2 i/ W if(q->expexp) 6 [. [+ T# h5 B {p->next=q->next;& Q% I( V1 d) i5 B, [. h* x h->next=q;2 L+ f/ B: F+ D q->next=k;2 k9 K# w" _, ?* b/ q* L q=p->next;3 m8 x% n# o+ n' V8 X" H } 9 f$ _# ~0 \5 _& Z5 {# D if(q->exp=k->exp) / ?) _& {0 D1 ?( p {k->coef=k->coef+q->coef;5 q L8 X2 N' x+ e9 `+ I1 i' N& q p->next=q->next;4 q$ N& L, B/ V2 C q=p->next; . E. |2 {( e- o0 w7 G+ W+ K } . w, T! Q/ e1 T& r8 o s h=pa;k=h->next; & { j! |0 J4 b: E! Y } $ P" a6 U3 l& [ J5 g' u: \" r0 D if(q->exp=p->exp); f7 p* a& m8 ?/ ] {p->coef=p->coef+q->coef; 6 H2 |' K9 {3 }5 O p->next=q->next;- b" D+ u2 y% ?& {& A q=p->next; ( b& d: I6 N1 Z# ~ } 5 _% E. G4 l/ s! z+ f7 }7 w if(q->exp>p->exp) % N1 s" R/ W. `0 L* @ {q=q->next;" W' S: F; k, s4 Z! Y3 i1 T p=p->next; - k; @/ n F- c. k: A }$ F# y* T( l, f } - a& t* U9 s* U' k0 F} 1 l' i8 B" V( Z* a# L3 O0 Pmain()% S9 J0 x/ U' p/ M5 }9 D { int i,j=0;9 ^ [/ j& ~2 N( W, A printf("a=");5 b: p3 J, h5 u" ` scanf("%s",a);* I2 N c' x5 g( c for(i=0;i<=400;i++) . u; d, z/ e3 j) T+ F {if(a!='\0') j++;}6 G& s7 m+ o9 m( j2 O% L0 O input(a,j); 4 d) G& a) P" Q+ c8 Z/ w stinsort(pa);5 D7 }/ m% {; A1 ?, W. { printf("a=");+ d$ ?! Q. d! r7 o( ] \0 l0 X for(i=0;i<=j+10;i++) # I$ o. G. g: p2 \4 Z {printf("%dx%d+",pa->coef,pa->exp); * [/ [- C8 H$ x0 w" z, w pa=pa->next; $ C* |5 l7 z; C, ? }3 O9 l$ v: `4 O5 `. r6 z }
作者: 游侠无极限    时间: 2005-7-27 21:37

猛然发现我已经受不了C的那种模式了……尤其是别人写的……

还有搂主最好打个包,代码发到BBS上全走样了,有些符号没了,没办法帮你看


作者: 夜编程    时间: 2005-7-28 09:27

不用对齐的编程风格也就算了,可是把几行挤到一块儿的风格也太小家子气了吧?改改啦,不改成不了程序员的。


作者: 夜编程    时间: 2005-7-28 09:28
还要注释要么用英语,要么就用汉字,不要用拼音,恶心死了,不是每个人都习惯看拼音的。
作者: 夜编程    时间: 2005-7-28 09:37
还有,楼猪这段代码错误满天飞,居然也可以运行?
作者: 夜编程    时间: 2005-7-28 09:43

#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 % O+ l. d' b7 I+ x/ Z- m8 K* `char a[MAXSIZE];

typedef struct ! n+ C& `6 o6 Z6 N{ . F1 Y% g+ N$ V. ` 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; 5 u9 \6 O8 E8 n, c# m1 s}Lpoly;

Lpoly *pa;

int cf(int i) /*10^i*/ & f: h0 a9 z6 L- l{* j8 p2 U3 C1 u; S6 ]7 } int c = 1, j; $ ^6 S; }8 a/ G5 ] b- @ 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; 7 k' h6 E w2 }% u}

void input(char a[],int j); b( d% n) `0 d9 E4 `% | { . K: r2 _/ V; `5 O$ I Lpoly *p, *h, *ptr;/ d6 d$ ^ x4 m) [+ h int i, k, flag = 0, x = 0; : z$ o9 Y, n# T8 c S.top = -1; r. F: H2 t7 S. N; v pa = (Lpoly*)malloc(sizeof(Lpoly)); . G, [$ |4 D9 [8 z pa->coef = 0;* [1 D) p- `6 _8 ^' [ pa->exp = 0; 0 O1 [, _& P1 y n* e h = pa; / v# J; j" ^3 T4 s. @& R$ S h->next = NULL;2 \, B0 ?* O; C p = h; /*jianlipa*/4 E, I8 b, Y2 b6 S + Y1 g% `. h& [3 u. X6 H for (i = 0; i <= j - 1; i ++)6 v9 [0 [. Y: `" z& o { 5 @! |/ D1 M2 h& W6 {& R: I 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 ++; * Y5 A( W+ q7 |" z S.elem[S.top] = a; & L, L- {0 \3 n8 ] }& \( |/ G U4 s6 ^- {1 @- t if (a == '-') , n0 Q. x, y c' j p flag=1;$ Z9 C+ M) e4 N: I3 f , n) P7 W0 C- | c if(a == 'x') /*fenlixishu*/. }. e0 u9 @/ j% B0 m) y$ o6 ~, ^ { 9 K8 k: r. p8 j" Y, G+ y if (S.top==0), {, u7 M% w" n; N2 ^3 j {! c% o' p- T- ?* F& a- o/ ? v- R if (flag == 1) 4 \% H2 A/ M1 v3 n, }! Y F1 |- J$ h {4 V! k7 W9 W; \) ]( J# L% O ptr->coef = -1; 8 Z# d/ f" \; [ ptr->next = NULL;% u/ {" W5 n3 z7 Y8 h }( M+ {( N2 ?" S1 @7 T% e! p3 S else / B; Q: Q% X% s! X3 M# _ {, w9 k$ Z6 ?7 z, A ptr->coef = 1; ' J1 a5 j) Z4 w% x ptr->next = NULL; + [" V0 k! h4 H! K" V6 D }( [# 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; : T1 h% W$ M R0 w! _ ptr->next = NULL; : z ?# ]% S, u. M# @ x = 0;8 _ r& [. i( R0 R/ s$ w, C( u } : N, r* ^7 Q1 V9 h( Q. s S.top = -1; + f4 a$ z* U1 y' E1 e5 _3 _& H flag = 0; /*clear*/ * v9 V+ I- X: w }) 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 ! L3 H) s9 {8 H; l0 E# l6 M {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); % Y' [; R6 M: ]+ l ptr->exp = x; ( j. B0 j4 F' z7 P" B ptr->next = NULL; ' s3 v$ q; y' E4 |* f* m% R x = 0; 3 Z# C0 I$ a* Y! n0 l }/*tiquzhishu*/ ! }% k; n \' p 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 { - B/ v5 H1 i- w5 L7 v) M p->next = ptr; % y9 Y/ {; K, g( p! Z# F) L$ ` p = ptr; 9 g, T$ w/ @( W- X& X$ V9 I( q }& J+ n+ m: K% s1 |( ?1 }+ e" c$ V if (i == j - 1) ) [+ p9 L5 F Q: a Q( W2 [( B { 7 T: q: W4 z5 y# V; H if (ptr->coef == 0), }3 f5 {5 n) F, v+ Z { ( g! z! |8 _/ _$ c" a, S0 t e 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); 9 o! D Q0 h' K ptr->coef = x; ) ~3 F' w) i, M: R) N$ n ptr->next = NULL; + N3 C1 Y' @: d6 p4 i" z/ F# f } - Z0 x7 {) d m4 Y else* |' e5 J- |9 ], I% ^* X { , r' M. @- s! ^ for (k = S.top; k >= 0; k --) " {% F+ z/ c+ s* I0 ` x = x + (S.elem[k] - '0') * cf(S.top - k);; G6 l- f8 `' p5 X O" z" M ptr->exp = x; - P. B- D1 |0 B5 z5 I) j ptr->next = NULL;0 u- D8 H' e5 }% F) A } - g& E+ n$ k3 K p->next = ptr;- V5 h; Z5 w, [- n" f2 B ptr->next = NULL; * L% \. Q0 L- M K } % W+ A* P& r7 B5 @8 b: x } $ g. j7 h: ?, |; ]9 c7 E}

void stinsort(Lpoly *pa) X# D( q6 b/ X& C2 a { # p# `- u! ~: d4 ~# T Lpoly *p, *h, *q, *k;- n4 ]' ?4 ]( c$ S7 K4 ` h = pa;8 t: p" N0 H, ^" p p = k = h->next; : Q% l# G9 F+ I5 o W q = p->next; + H9 w G! b) V+ {: F2 Q while (p->next != NULL) ! a: c" y& p2 F { , G+ M$ B/ m* { if (q->exp) 3 |( z0 x9 a& p' j { . ? w$ c2 p; v 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; . j, ]- Q8 j8 Y: b+ O ~! [# [ }% O' K6 m9 ~! S' S. _; A if(q->exp)) \* d# \& I) I* u2 A, s { ' a+ }9 n& F3 T+ J' T; F9 U p->next = q->next;3 k0 \% H ]; |7 |5 ^5 k l h->next = q; 4 V/ h9 g6 r* {! X0 U, e4 Y q->next = k;* I; Z" x' X/ T2 U q = p->next; ; v" V: L+ D" }1 C0 k8 x- Z3 t }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; % u8 D1 A" `$ U, N% W( z! ~# d2 D q = p->next;+ q% d* V. X3 n8 s/ @1 z }, L8 o: d7 f2 K h=pa; 1 D! E6 m3 U2 n- t9 W9 @ L 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; ! d# V8 ?& x- Q; z4 c q = p->next;1 F+ ~/ m& P- r0 q$ p0 {$ { } # y2 h- t3 _! \ if (q->exp > p->exp) ) ~, [ P# P( S1 a- m7 G { 9 H' L F% b* O4 | q = q->next;( `9 t; Q2 A9 ^! L p = p->next; ! y+ S$ a6 U5 r: v8 { } * t2 U+ u: Y! z% H. Y3 @- v; Q ` }+ y; Q' E; Z2 X }

main() + C* [& K8 y% G$ o4 X{" r- c" _+ y: h( q! r5 r int i, j = 0; . ]+ e6 h; a0 y: I printf("a="); . o6 P: u4 U. B, l& t Z 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 ++) + x8 W% w+ b( A { 2 I# U7 I/ I9 L( V' { printf("%dx%d+", pa->coef, pa->exp); 5 K# H6 e4 b2 A' O6 x0 Z pa = pa->next; & G' x) H& G/ g }( h' k1 w$ c7 G( A3 r } , B" q) j0 t4 r. ]6 P0 c: ^' Y. K

我把楼猪的代码弄了下,把里面大小写错误排除了,这段代码会有死循环,谁有空谁研究去。


作者: lvyan    时间: 2005-9-17 17:28
这是上学期的数据结构课程设计
/ o- g- ]5 C* c  e" O4 ~" o结果我已经弄出来了,得了个优秀,没跟大家说不好意思& d  w+ F" h6 E2 y/ C
多谢大家给的意见,我现在水平还很差
) s" Q3 V! `$ o* H至于注释都是给自己看的,发到网上的时候没改,呵呵~~~~~~
作者: Q7    时间: 2007-2-25 14:06
全还给老师了- -想当初为考试忙这忙那的
作者: Eagle    时间: 2007-2-26 10:52
老帖,一口血就出来了……
作者: 406082496    时间: 2007-3-17 00:45

it is too hard for me!!~~~~~






欢迎光临 下沙论坛 (http://bbs.xiasha.cn/) Powered by Discuz! X3.3