c语⾔计数器程序count,简单计数器(C语⾔).doc #includetypedef struct term /*项的表⽰,多项式的项作为LinkList的数据元素 */
{ float coef; /*系数*/
int expn; /*指数*/
struct term *next;
}term;
term *CreatPolyn(term *P,int m) /* 输⼊m项的系数和指数,建⽴⼀个表⽰⼀
元多项式的有序链表P,并返回头结点 */
{ term *h,*q; float x; int i,y;
P=(term*)malloc(sizeof(term));
h=P;
if (m<=0) return NULL;
printf("\nPut %d not zero items : ",m);
printf("\n");
for (i=1; i<=m; ++i) /* 依次输⼊m个⾮零项*/
{ scanf("%f,%d",&x,&y);
P->coef=x;P->expn=y;
if(P->coef)
q = P;
P = (term*)malloc(sizeof(term)); /* P后移并分配新空间 ,q记录P的前结点 */vbs整人代码怎么运行
eclipse查代码快捷键十进制转二进制对照表免费下载q->next=P;
}
q->next = NULL;
free(P);
return h;
}
term *selsort(term *h) /*保证链表h指数有序,且⽆同指数结点,返回头结点*/
{ term *g, *p, *q;
float f;
int i, fini = 1;网页制作登录注册页面代码
if(!h) return NULL;
for(g = h;g->next&&fini;g = g->next) /*将表中指数排序,思路同冒泡排序*/
{
fini = 0;
for(p = h,q = h->next;q;p = p->next,q = q->next) /* p始终指在q的前⾯ */ if (p->expn > q->expn) /* 如果p⼤于q的指数就交换*/
{
f=p->coef;i=p->expn;
p->coef=q->coef;p->expn = q->expn;
q->coef = f;q->expn = i;
fini = 1;
}
}
for(g = h,p = g->next;p;) /*将指数相等的合并系数*/
if(g->expn==p->expn)
{
g->coef += p->coef;
g->next = p->next;
q=p;
p=p->next; /*只要p下移*/
free(q);
}
else if(g->next)
{
g=g->next; /*p,g都下移*/
p=p->next;
}
return h;
}
void PrintfPoly(term *P) /*输出代表多项式的链表P*/
{ term *q;
q=P;
if(!q) /* 0多项式 */
{
putchar(0);
return;
}
if(q->coef!=1) /* 第⼀项的系数不为1 */
{
printf("%f",q->coef); /*输出第⼀项的系数*/
if(q->expn==1) putchar(X);
else if(q->expn) printf("X%d",q->expn);
}
else if(!q->expn) putchar(1); /* 系数为1,指数为0*/
else if(q->expn==1) putchar(X);
else printf("X%d",q->expn); /*以上是针对第⼀项,因为第⼀项系数⼤于0不需要加号*/
q=q->next;
while (q) /*从第⼆项开始输出,正项输出+ */
{
if(q->coef > 0) putchar(+);
if(q->coef!=1)
{
printf("%g",q->coef);
if(q->expn==1) putchar(X);
else if(q->expn) printf("X%d",q->expn);冒泡排序代码c语言
}
else if(!q->expn) putchar(1);
else if(q->expn==1) putchar(X);
else printf("X%d",q->expn);
q=q->next;
}
}
int Compare(term *a, term *b) /* ⽐较a,b的⼤⼩关系,a与b⼤⼩关系是⼩于,
等于,⼤于分别返回-1,0,1;*/
{
if (a->expn < b->expn) return -1;
if (a->expn > b->expn) return 1;
return 0;
}
term *AddPolyn(term *Pa, term *Pb) /* 实现多项式加法运算:Pa = Pa+Pb,利⽤两个多项式的结点构成"和多项式",并返回和Pa的头结点。*/
{ term *h, *qa = Pa, *qb = Pb, *p, *q;
float sum;
h = p = (term*)malloc(sizeof(term));
p->next = NULL;
while (qa && qb) /* Pa和Pb均⾮空*/
{switch (Compare(qa,qb)) {
case -1: /* 多项式PA中当前结点的指数值⼩*/
p->next = qa;
p = qa;
qa = qa->next;
break;
case 0: /* 两者的指数值相等*/
sum = qa->coef + qb->coef;
if (sum != 0.0) /* 修改多项式PA中当前结点的系数值*/ { p->next=qa;
qa->coef=sum;
p=qa;
qa=qa->next;
}
else /* 删除多项式PA中当前结点 */
{ q = qa;
qa = qa->next;
free(q);
}
q = qb;
qb = qb->next;
free(q);
break;
case 1: /* 多项式PB中当前结点的指数值⼩*/
p->next = qb;
p = qb;
qb = qb->next;
break;
}
}
if (qa)
p->next = qa; /* 链接Pa中剩余结点*/
else if(qb)
p->next = qb; /* 链接Pb中剩余结点*/
q = h;
h = h->next;
free(q);
return h;
}
term *AddResult(term *Pa, term *Pb) /* 输出Pa各项加Pb各项,并输出加后Pa的结果*/
{
PrintfPoly(Pa);
if(Pb && Pb->coef>0) printf(" + ");
PrintfPoly(Pb);
Pa = AddPolyn(Pa,Pb);
printf(" = ");
Pa = selsort(Pa);
PrintfPoly(Pa);
return Pa;
}
term *ReducePolyn(term *Pa, term *Pb) /* 算法2.23 多项式减法:Pa = Pa-Pb,
利⽤两个多项式的结点构成"差多项式",并返回差Pa的头结点。*/
{ term *p = Pb;
while(p) /*将Pb中的各项系数变成相反数*/
{
p->coef *= -1;
p = p->next;
}
return AddPolyn(Pa,Pb);
}
term *ReduceResult(term *Pa, term *Pb) /* 输出Pa各项加Pb各项,并输出减后Pa的结果*/ {
PrintfPoly(Pa);
printf(" - ");
html两行文字之间加空格

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。