设为首页收藏本站

新微赢技术网

 找回密码
 注册
搜索
热搜: 回贴
查看: 282|回复: 0
打印 上一主题 下一主题

[求助]新手求教.....

[复制链接]
跳转到指定楼层
1#
发表于 2009-11-3 04:09:02 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
下面是一元稀疏多项式计算器的程序.........可以实现加法可以实现减法(其实就是负数相加)...........可是不能实现正数加负数.........比如2x^4+5x^5可以实现,-2x^4-5x^5也可以实现,2x^4-5x^5就出错.......这是为什么?在哪修改啊?







#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <conio.h>
typedef struct node
{
float coef; //系数
int expn; //项数
struct node * next;
} PolyNode;
PolyNode * Create_Poly(char ch) //输入多项式
{
PolyNode * p, *s,*r;
float x; int y;
p=(PolyNode *)malloc(sizeof(PolyNode));
p->next=NULL;
printf("请输入一元稀疏多项式%c:(格式:系数 指数,指数递增,以0 0结束.)\n",ch);
scanf("%f %d",&x,&y);
while(x!=0)
{
s=(PolyNode *)malloc(sizeof(PolyNode));
s->coef=x;
s->expn=y;
s->next=NULL;
if(p->next==NULL)
{
p->next=s;
r=s;
}
else
{
r->next=s;
r=s;
}
scanf("%f %d",&x,&y);
}
return p;
}

PolyNode * Create_Poly2(char ch) //输入多项式
{
PolyNode * p, *s,*r;
float x; int y;
p=(PolyNode *)malloc(sizeof(PolyNode));
p->next=NULL;
printf("请输入一元稀疏多项式%c:(格式:系数 指数,指数递增,以0 0结束.)\n",ch);
scanf("%f %d",&x,&y);
while(x!=0)
{
s=(PolyNode *)malloc(sizeof(PolyNode));
s->coef=-x;
s->expn=y;
s->next=NULL;
if(p->next==NULL)
{
p->next=s;
r=s;
}
else
{
r->next=s;
r=s;
}
scanf("%f %d",&x,&y);
}
return p;
}

PolyNode * Add_Poly(PolyNode * f,PolyNode * g) //多项式相加
{
PolyNode * fg;
PolyNode *t,*q,*s,*r;
float m;
t=f->next;
q=g->next;
fg=r=(PolyNode*)malloc(sizeof(PolyNode));
fg->next=NULL;
while(t&&q)
{
if(t->expn==q->expn) //指数相等时系数相加
{
m=t->coef+q->coef;
if(m!=0) //系数不为0时加到结果中去
{
s=(PolyNode *)malloc(sizeof(PolyNode));
s->coef=m;
s->expn=t->expn;
s->next=NULL;
}
t=t->next;
q=q->next;
}
else //指数小的加到结果中去再后移
if(t->expn<q->expn)
{
s=(PolyNode *)malloc(sizeof(PolyNode));
s->coef=t->coef;
s->expn=t->expn;
s->next=NULL;
t=t->next;
}
else
{
s=(PolyNode *)malloc(sizeof(PolyNode));
s->coef=q->coef;
s->expn=q->expn;
s->next=NULL;
q=q->next;
}
if(fg->next==NULL)
{
fg->next=s;
r=s;
}
else
{
r->next=s;
r=s;
}
}
r->next=t?t:q; //把没加完的接上
return fg;
}//Addpoly
PolyNode * Sub_Poly(PolyNode * f,PolyNode * g) //多项式相减
{
PolyNode * fg;
PolyNode *t,*q,*s,*r;
float m;
t=f->next;
q=g->next;
fg=r=(PolyNode*)malloc(sizeof(PolyNode));
fg->next=NULL;
while(t&&q)
{
if(t->expn==q->expn) //指数相等时系数相加
{
m=t->coef+q->coef;
if(m!=0) //系数不为0时加到结果中去
{
s=(PolyNode *)malloc(sizeof(PolyNode));
s->coef=m;
s->expn=t->expn;
s->next=NULL;
}
t=t->next;
q=q->next;
}
else //指数小的加到结果中去再后移
if(t->expn<q->expn)
{
s=(PolyNode *)malloc(sizeof(PolyNode));
s->coef=t->coef;
s->expn=t->expn;
s->next=NULL;
t=t->next;
}
else
{
s=(PolyNode *)malloc(sizeof(PolyNode));
s->coef=q->coef;
s->expn=q->expn;
s->next=NULL;
q=q->next;
}
if(fg->next==NULL)
{
fg->next=s;
r=s;
}
else
{
r->next=s;
r=s;
}
}
r->next=t?t:q; //把没加完的接上
return fg;
}//Subpoly
void Out_Poly(PolyNode * f) //输出多项式
{
PolyNode *t;
t=f->next;
if(!f->next){
printf("0\n"); return;
}
while(t)
{ if(t->coef>0&&f->next!=t) printf("+");
if(t->expn==0)
printf("%f",t->coef);
else
printf("%f*X^%d",t->coef,t->expn);
t=t->next;
}
printf("\n");
}
void Open() //用户选择界面
{
printf("******************************\n");
printf(" 一元稀疏多项式计算器\n");
printf("******************************\n");
printf("请选择操作:\n");
printf("0.退出\n");
printf("1.两个一元多项式相加\n");
printf("2.两个一元多项式相减\n");
printf("3.帮助\n");
}

void Help()
{
printf("************帮助***********\n");
printf("1.输入时只输入多项式的系数与指数,以0 0结束.\n");
printf("2.请按指数升幂形式输入.\n");
printf("3.例如输入 \"1 1 2 2 0 0\" 表示 \"1*X^1+2*X^2\"\n");
}

void main()
{
Open();
PolyNode * f,* g,* fg;
int i=-1; //设置标志
while(i!=0)
{
scanf("%d",&i);
getchar();
switch(i)
{
case 0:
return; //退出
case 1: //多项式相加
//clrscr(); //清屏
printf("你选择的操作是多项式相加:\n");
f=Create_Poly('A'); //输入多项式A
printf("A=");
Out_Poly(f);
g=Create_Poly('B'); //输入多项式B
printf("B=");
Out_Poly(g);
printf("A+B=");
fg=Add_Poly(f,g);
Out_Poly(fg);
i=-1; //复位标志
//clrscr(); //清屏
Open(); //回复用户选择界面
break;
case 2: //多项式相减
//clrscr(); //清屏
printf("你选择的操作是多项式相减:\n");
f=Create_Poly('A'); //输入多项式A
printf("A=");
Out_Poly(f);
g=Create_Poly2('B'); //输入多项式B
printf("-B=");
Out_Poly(g);
printf("A-B=");
fg=Sub_Poly(f,g);
Out_Poly(fg);
i=-1; //复位标志
//clrscr(); //清屏
Open(); //回复用户选择界面
break;
case 3: //用户帮助
//clrscr(); //清屏
Help(); //帮助界面
i=-1; //复位标志
//clrscr(); //清屏
Open(); //回到用户选择界面
break;
default: //输入错误
//clrscr(); //清屏
printf("输入有误!请重新选择操作!\n"); //选择错误,返回选择界面
Open();
}
}

}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

申请友链|小黑屋|最新主题|手机版|新微赢技术网 ( 苏ICP备08020429号 )  

GMT+8, 2024-11-18 19:37 , Processed in 0.089520 second(s), 8 queries , Gzip On, Memcache On.

Powered by xuexi

© 2001-2013 HaiAn.Com.Cn Inc. 寰耽

快速回复 返回顶部 返回列表