[求助]请教各位大虾
这是我的数据结构课程设计实现一个多项式合并,字符串输入多项式,链表插入排序
可是我运行出来结果不一样啊,大家帮我看下哪里错了,小的在此谢过啦~~~
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 400
char a;
typedef struct{char elem;
int top;
}SqStack;
SqStack S;
typedef struct poly
{int coef; /*xishu*/
int exp; /*zhishu*/
struct poly *next;
}Lpoly;
Lpoly *pa;
int cf(int i) /*10^i*/
{ int c=1,j;
for(j=1;j<=i;j++) c=c*10;
return c;
}
void input(char a[],int j)
{ Lpoly *p,*h,*ptr;
int i,k,flag=0,x=0;
S.top=-1;
pa=(Lpoly*)malloc(sizeof(Lpoly));
pa->coef=0;pa->exp=0;
h=pa;h->next=NULL;
p=h; /*jianlipa*/
for(i=0;i<=j-1;i++)
{ptr=(Lpoly*)malloc(sizeof(Lpoly));
if(S.top<MAXSIZE-1) /*kaishifengli*/
{S.top++;
S.elem=a;
}
if(a=='-') flag=1;
if(a=='x') /*fenlixishu*/
{if(S.top==0)
{if(flag==1)
{ptr->coef=-1;ptr->next=NULL;}
else
{ptr->coef=1;ptr->next=NULL;}
}
else
{for(k=S.top-1;k>=0;k--) x=x+(S.elem-'0')*cf(S.top-1-k);
ptr->coef=x;ptr->next=NULL;x=0;
}
S.top=-1;flag=0; /*clear*/
}
if(a=='+'||a=='-') /*fenlizhishu*/
{if(S.top==0)
{ptr->exp=1;ptr->next=NULL;}
else
{for(k=S.top-1;k>=0;k--) x=x+(S.elem-'0')*cf(S.top-1-k);
ptr->exp=x;ptr->next=NULL;x=0;
} /*tiquzhishu*/
S.top=-1;
}
if(ptr->coef!=0&&ptr->exp!=0)
{p->next=ptr;
p=ptr;
}
if(i==j-1)
{if(ptr->coef==0)
{for(k=S.top;k>=0;k--)
x=x+(S.elem-'0')*cf(S.top-k);
ptr->coef=x;ptr->next=NULL;
}
else
{for(k=S.top;k>=0;k--)
x=x+(S.elem-'0')*cf(S.top-k);
ptr->exp=x;ptr->next=NULL;
}
p->next=ptr;ptr->next=NULL;
}
}
}
void stinsort(Lpoly *pa)
{ Lpoly *p,*h,*q,*k;
h=pa;p=K=h->next;q=p->next;
while(p->next!=NULL)
{if(q->exp<p->exp)
{while(q->exp>k->exp)
{k=k->next;
h=h->next;
}
if(q->exp<k->exp)
{p->next=q->next;
h->next=q;
q->next=k;
q=p->next;
}
if(q->exp=k->exp)
{k->coef=k->coef+q->coef;
p->next=q->next;
q=p->next;
}
h=pa;k=h->next;
}
if(q->exp=p->exp)
{p->coef=p->coef+q->coef;
p->next=q->next;
q=p->next;
}
if(q->exp>p->exp)
{q=q->next;
p=p->next;
}
}
}
main()
{ int i,j=0;
printf("a=");
scanf("%s",a);
for(i=0;i<=400;i++)
{if(a!='\0') j++;}
input(a,j);
stinsort(pa);
printf("a=");
for(i=0;i<=j+10;i++)
{printf("%dx%d+",pa->coef,pa->exp);
pa=pa->next;
}
} <P>猛然发现我已经受不了C的那种模式了……尤其是别人写的……</P><P>还有搂主最好打个包,代码发到BBS上全走样了,有些符号没了,没办法帮你看</P> <P>不用对齐的编程风格也就算了,可是把几行挤到一块儿的风格也太小家子气了吧?改改啦,不改成不了程序员的。</P> 还要注释要么用英语,要么就用汉字,不要用拼音,恶心死了,不是每个人都习惯看拼音的。 还有,楼猪这段代码错误满天飞,居然也可以运行? <P>#include "stdlib.h"
#include "string.h"
#include "stdio.h"</P><P>#define MAXSIZE 400
char a;</P><P>typedef struct
{
char elem;
int top;
}SqStack;</P><P>SqStack S;</P><P>typedef struct poly
{
int coef; /*xishu*/
int exp; /*zhishu*/
struct poly *next;
}Lpoly;</P><P>Lpoly *pa;</P><P>int cf(int i) /*10^i*/
{
int c = 1, j;
for(j = 1; j <= i; j ++)
c = c * 10;
return c;
}</P><P>void input(char a[],int j)
{
Lpoly *p, *h, *ptr;
int i, k, flag = 0, x = 0;
S.top = -1;
pa = (Lpoly*)malloc(sizeof(Lpoly));
pa->coef = 0;
pa->exp = 0;
h = pa;
h->next = NULL;
p = h; /*jianlipa*/
for (i = 0; i <= j - 1; i ++)
{
ptr = (Lpoly*)malloc(sizeof(Lpoly));
if(S.top)
{
S.top ++;
S.elem = a;
}
if (a == '-')
flag=1;
if(a == 'x') /*fenlixishu*/
{
if (S.top==0)
{
if (flag == 1)
{
ptr->coef = -1;
ptr->next = NULL;
}
else
{
ptr->coef = 1;
ptr->next = NULL;
}
}
else
{
for (k=S.top - 1; k >= 0; k --)
x = x + (S.elem - '0') * cf(S.top - 1 - k);
ptr->coef = x;
ptr->next = NULL;
x = 0;
}
S.top = -1;
flag = 0; /*clear*/
}
if (a == '+' || a == '-') /*fenlizhishu*/
{
if(S.top == 0)
{
ptr->exp = 1;
ptr->next = NULL;
}
else
{
for (k = S.top - 1; k >= 0; k --)
x = x + (S.elem - '0') * cf(S.top - 1 - k);
ptr->exp = x;
ptr->next = NULL;
x = 0;
}/*tiquzhishu*/
S.top = -1;
}
if (ptr->coef != 0 && ptr->exp != 0)
{
p->next = ptr;
p = ptr;
}
if (i == j - 1)
{
if (ptr->coef == 0)
{
for (k = S.top; k >= 0; k --)
x = x + (S.elem - '0') * cf(S.top - k);
ptr->coef = x;
ptr->next = NULL;
}
else
{
for (k = S.top; k >= 0; k --)
x = x + (S.elem - '0') * cf(S.top - k);
ptr->exp = x;
ptr->next = NULL;
}
p->next = ptr;
ptr->next = NULL;
}
}
}</P><P>void stinsort(Lpoly *pa)
{
Lpoly *p, *h, *q, *k;
h = pa;
p = k = h->next;
q = p->next;
while (p->next != NULL)
{
if (q->exp)
{
while(q->exp > k->exp)
{
k = k->next;
h = h->next;
}
if(q->exp)
{
p->next = q->next;
h->next = q;
q->next = k;
q = p->next;
}
if (q->exp = k->exp)
{
k->coef = k->coef + q->coef;
p->next = q->next;
q = p->next;
}
h=pa;
k = h->next;
}
if (q->exp = p->exp)
{
p->coef = p->coef + q->coef;
p->next = q->next;
q = p->next;
}
if (q->exp > p->exp)
{
q = q->next;
p = p->next;
}
}
}</P><P>main()
{
int i, j = 0;
printf("a=");
scanf("%s", a);
j = strlen(a);</P><P> input(a, j);
stinsort(pa);
printf("a=");
for (i = 0; i <= j + 10; i ++)
{
printf("%dx%d+", pa->coef, pa->exp);
pa = pa->next;
}
}
</P><P>我把楼猪的代码弄了下,把里面大小写错误排除了,这段代码会有死循环,谁有空谁研究去。</P> 这是上学期的数据结构课程设计
结果我已经弄出来了,得了个优秀,没跟大家说不好意思
多谢大家给的意见,我现在水平还很差
至于注释都是给自己看的,发到网上的时候没改,呵呵~~~~~~ 全还给老师了- -想当初为考试忙这忙那的 老帖,一口血就出来了…… <p>it is too hard for me!!~~~~~</p>
页:
[1]