C语言编写的一元式项式相加的程序,当第一次输入即为0<CR>0<CR>0<CR>0<CR>时,为什么程序会出错?
答案:2 悬赏:20 手机版
解决时间 2021-11-27 17:07
- 提问者网友:戎马万世
- 2021-11-27 11:29
C语言编写的一元式项式相加的程序,当第一次输入即为0<CR>0<CR>0<CR>0<CR>时,为什么程序会出错?
最佳答案
- 五星知识达人网友:鱼芗
- 2021-11-27 12:38
我搞不懂为什么做个这东西都喜欢用结构加链表,,,,算了我发个给你,加减乘除都有的
#if !defined _STRUCT_H
#define _STRUCT_H
#include
#include
#include
typedef struct Polynomial //结点类型
{
float coef; //系数
int exp; //指数
Polynomial *next;
}*Polyn; //Polyn为结点指针类型
void PrintPolyn(Polyn P); //输出多项式
void Insert(Polyn p,Polyn head); //插入结点
Polyn CreatePolyn(Polyn head,int m) ; //创建多项式
void DestroyPolyn(Polyn p); //销毁多项式
int compare(Polyn a,Polyn b); //多项式指数比较
Polyn AddPolyn(Polyn pa,Polyn pb); //加法
Polyn SubPolyn(Polyn pa,Polyn pb); //减法
Polyn MultiplyPolyn(Polyn pa,Polyn pb); //乘法
void DevicePolyn(Polyn pa,Polyn pb); //除法
float ValuePolyn(Polyn head,float x); //多项式的值
#endif
#include"struct.h"
void PrintPolyn(Polyn P)
{
Polyn q=P->next;
int flag=1; //项数计数器
if(!q) //若多项式为空,输出0
{
putchar('0');
printf("\n");
return;
}
while(q)
{
if(q->coef>0&&flag!=1) //系数大于0且不是第一项
putchar('+');
if(q->coef!=1&&q->coef!=-1) //系数非1或-1
{
printf("%g",q->coef);
if(q->exp==1)
putchar('X');
else if(q->exp)
printf("X^%d",q->exp);
}
else
{
if(q->coef==1)
{
if(!q->exp)
putchar('1');
else if(q->exp==1)
putchar('X');
else
printf("X^%d",q->exp);
}
if(q->coef==-1)
{
if(!q->exp)
printf("-1");
else if(q->exp==1)
printf("-X");
else
printf("-X^%d",q->exp);
}
}
q=q->next;
flag++;
}
printf("\n");
}
void InsertNode(Polyn p,Polyn head)
{
if(p->coef==0)
delete p; //释放系数为0的结点
else
{
Polyn q1,q2;
q1=head;
q2=head->next;
while(q2!=NULL&&p->expexp) //查找插入位置,
{
q1=q2;
q2=q2->next;
}
if(q2!=NULL&&p->exp==q2->exp) //将指数相同的合并
{
q2->coef+=p->coef; //系数相加
delete p; //释放p
if(!q2->coef) //释放系数为0的结点
{
q1->next=q2->next;
delete q2;
}
}
else //无相同指数时将该结点插入
{
p->next=q2;
q1->next=p;
}
}
}
Polyn CreatePolyn(Polyn head,int m) //头指针为head、项数为m
{
Polyn p;
p=head=new Polynomial; //生成头结点
head->next=NULL;
for(int i=0;i {
p=new Polynomial; //建立新结点
printf("请输入第%d项的系数与指数(空格或回车隔开):",i+1);
scanf("%f%d",&p->coef,&p->exp);
InsertNode(p,head); //调用InsertNode函数插入结点
}
return head;
}
int Compare(Polyn a,Polyn b)//依a的指数值<、=或>b的指数值,分别返回-1、0或1
{
if(a&&b)
{
if(!b||a->exp>b->exp)
return 1;
else if(!a||a->expexp)
return -1;
else
return 0;
}
else if(!a&&b)
return -1; //a多项式为空,但b多项式非空
else
return 1; //b多项式为空,但a多项式非空
}
void DestroyPolyn(Polyn p) //销毁多项式p
{
Polyn q1,q2;
q1=p->next;
if(q1==NULL) //结果系数为0的情况
return ;
q2=q1->next;
while(q1->next)
{
delete q1;
q1=q2; //指针后移
q2=q2->next;
}
}
Polyn AddPolyn(Polyn pa,Polyn pb)
{
Polyn qa=pa->next;
Polyn qb=pb->next;
Polyn heada,hc,qc;
hc=new Polynomial; //建立头结点
hc->next=NULL;
heada=hc;
while(qa||qb)
{
qc=new Polynomial;
switch(Compare(qa,qb))
{
case 1: //a项的指数大于b项的指数
{
qc->coef=qa->coef;
qc->exp=qa->exp;
qa=qa->next;
break;
}
case 0: //a项的指数等于b项的指数
{
qc->coef=qa->coef+qb->coef;
qc->exp=qa->exp;
qa=qa->next;
qb=qb->next;
break;
}
case -1: //a项的指数小于b项的指数
{
qc->coef=qb->coef;
qc->exp=qb->exp;
qb=qb->next;
break;
}
}
if(qc->coef!=0)
{
qc->next=hc->next;
hc->next=qc;
hc=qc;
}
else
delete qc; //当相加系数为0时,释放该结点
}
return heada; //返回其头指针
}
Polyn SubPolyn(Polyn pa,Polyn pb)
{
Polyn p1,p2,heads;
p1=pb;
p2=pb->next;
while(p2)
{
p2->coef*=-1; //将pb的系数取反
p2=p2->next;
}
heads=AddPolyn(pa,p1);
for(p2=p1->next;p2;p2=p2->next) //恢复pb的系数
p2->coef*=-1;
return heads; //返回其头指针
}
Polyn MultiplyPolyn(Polyn pa,Polyn pb)
{
Polyn headf,qc;
Polyn qa=pa->next;
Polyn qb=pb->next;
headf=new Polynomial; //建立头结点
headf->next=NULL;
for(;qa;qa=qa->next)
{
for(qb=pb->next;qb;qb=qb->next)
{
qc=new Polynomial;
qc->coef=qa->coef*qb->coef;
qc->exp=qa->exp+qb->exp;
InsertNode(qc,headf); //调用InsertNode函数合并指数相同的项
}
}
return headf; //返回其头指针
}
void DevicePolyn(Polyn pa,Polyn pb)
{
Polyn quotient,remainder,temp1,temp2;
Polyn qa=pa->next;
Polyn qb=pb->next;
quotient=new Polynomial; //建立头结点,存储商
quotient->next=NULL;
remainder=new Polynomial; //建立头结点,存储余数
remainder->next=NULL;
temp1=new Polynomial;
temp1->next=NULL;
temp2=new Polynomial;
temp2->next=NULL;
temp1=AddPolyn(temp1,pa);
while(qa!=NULL&&qa->exp>=qb->exp)
{
temp2->next=new Polynomial;
temp2->next->coef=(qa->coef)/(qb->coef);
temp2->next->exp=(qa->exp)-(qb->exp);
InsertNode(temp2->next,quotient);
pa=SubPolyn(pa,MultiplyPolyn(pb,temp2));
qa=pa->next;
temp2->next=NULL;
}
remainder=SubPolyn(temp1,MultiplyPolyn(quotient,pb));
pb=temp1;
printf("\t商:");
PrintPolyn(quotient);
printf("\t余数:");
PrintPolyn(remainder);
}
float ValuePolyn(Polyn head,float x)
{
Polyn p;
int i;
float sum=0,t;
for(p=head->next;p;p=p->next)
{
t=1;
for(i=p->exp;i!=0;)
{
if(i<0)
{
t/=x;
i++;
}//指数小于0,进行除法
else
{
t*=x;
i--;
}//指数大于0,进行乘法
}
sum+=p->coef*t;
}
return sum;
}
int main()
{
time_t t;
time(&t);
printf("︵ ︵ §欢迎使用一元稀疏多项式计算器\t\t\t╭─────╮\n");
printf("\t§功能:计算多项式的加、减、乘、除及值\t\t│作者陈显玲│\n");
printf(" ︶ §输入:多项式的项数、系数、指数\t\t\t│09计科二班│\n");
printf("\t§输出:各种运算后的结果\t\t\t\t╰─────╯\n");
printf("━━━━━━━━━━━━━━━━━━━━━━%s",ctime(&t));
int m,n,i;
float x;
Polyn pa=0,pb=0,pc,pd,pf;//定义各式的头指针,pa与pb在使用前赋初值NULL
printf("请输入多项式A项数:");
scanf("%d",&m);
pa=CreatePolyn(pa,m);//建立多项式A
printf("请输入多项式B项数:");
scanf("%d",&n);
pb=CreatePolyn(pb,n);//建立多项式B
//输出菜单
printf("\t\t\t【功能选择项】\n\t≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡\n");
printf("\t║ 1.输出多项式A和B ║\t2.计算多项式A+B ║\n");
printf("\t║ 3.计算多项式A-B ║\t4.计算多项式A*B ║\n");
printf("\t║ 5.计算多项式A/B ║\t6.计算A在x处的值║\n");
printf("\t║ 7.重新创建多项式 ║\t8.退出 ║\n");
printf("\t≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡\n");
do{
printf("\t请输入选择:");
scanf("%d",&i);
switch(i)
{
case 1:
{
printf("\t多项式A=:");
PrintPolyn(pa);
printf("\t多项式B=:");
PrintPolyn(pb);
break;
}
case 2:
{
pc=AddPolyn(pa,pb);
printf("\tA+B=:");
PrintPolyn(pc);
DestroyPolyn(pc);
break;
}
case 3:
{
pd=SubPolyn(pa,pb);
printf("\tA-B=:");
PrintPolyn(pd);
DestroyPolyn(pd);
break;
}
case 4:
{
pf=MultiplyPolyn(pa,pb);
printf("\tA*B=:");
PrintPolyn(pf);
DestroyPolyn(pf);
break;
}
case 5:
{
if(n==0)
{
printf("除数为0时不能进行除法运算!\n");
break;
}
DevicePolyn(pa,pb);
break;
}
case 6:
{
printf("\t输入x的值:x=");
scanf("%f",&x);
printf("\t多项式A的值:%g\n",ValuePolyn(pa,x));
break;
}
case 7:
{
printf("请输入多项式A项数:");
scanf("%d",&m);
pa=CreatePolyn(pa,m);
printf("请输入多项式B项数:");
scanf("%d",&n);
pb=CreatePolyn(pb,n);
break;
}
case 8:
{
printf("欢迎再次使用!O(∩_∩)O谢谢\n");
system("pause");
break;
}
default:
{
printf("\tInput Error!!!\n");
break;
}
}
}while(i!=8);
DestroyPolyn(pa);
DestroyPolyn(pb);
return 0;
}
#if !defined _STRUCT_H
#define _STRUCT_H
#include
#include
#include
typedef struct Polynomial //结点类型
{
float coef; //系数
int exp; //指数
Polynomial *next;
}*Polyn; //Polyn为结点指针类型
void PrintPolyn(Polyn P); //输出多项式
void Insert(Polyn p,Polyn head); //插入结点
Polyn CreatePolyn(Polyn head,int m) ; //创建多项式
void DestroyPolyn(Polyn p); //销毁多项式
int compare(Polyn a,Polyn b); //多项式指数比较
Polyn AddPolyn(Polyn pa,Polyn pb); //加法
Polyn SubPolyn(Polyn pa,Polyn pb); //减法
Polyn MultiplyPolyn(Polyn pa,Polyn pb); //乘法
void DevicePolyn(Polyn pa,Polyn pb); //除法
float ValuePolyn(Polyn head,float x); //多项式的值
#endif
#include"struct.h"
void PrintPolyn(Polyn P)
{
Polyn q=P->next;
int flag=1; //项数计数器
if(!q) //若多项式为空,输出0
{
putchar('0');
printf("\n");
return;
}
while(q)
{
if(q->coef>0&&flag!=1) //系数大于0且不是第一项
putchar('+');
if(q->coef!=1&&q->coef!=-1) //系数非1或-1
{
printf("%g",q->coef);
if(q->exp==1)
putchar('X');
else if(q->exp)
printf("X^%d",q->exp);
}
else
{
if(q->coef==1)
{
if(!q->exp)
putchar('1');
else if(q->exp==1)
putchar('X');
else
printf("X^%d",q->exp);
}
if(q->coef==-1)
{
if(!q->exp)
printf("-1");
else if(q->exp==1)
printf("-X");
else
printf("-X^%d",q->exp);
}
}
q=q->next;
flag++;
}
printf("\n");
}
void InsertNode(Polyn p,Polyn head)
{
if(p->coef==0)
delete p; //释放系数为0的结点
else
{
Polyn q1,q2;
q1=head;
q2=head->next;
while(q2!=NULL&&p->exp
{
q1=q2;
q2=q2->next;
}
if(q2!=NULL&&p->exp==q2->exp) //将指数相同的合并
{
q2->coef+=p->coef; //系数相加
delete p; //释放p
if(!q2->coef) //释放系数为0的结点
{
q1->next=q2->next;
delete q2;
}
}
else //无相同指数时将该结点插入
{
p->next=q2;
q1->next=p;
}
}
}
Polyn CreatePolyn(Polyn head,int m) //头指针为head、项数为m
{
Polyn p;
p=head=new Polynomial; //生成头结点
head->next=NULL;
for(int i=0;i
p=new Polynomial; //建立新结点
printf("请输入第%d项的系数与指数(空格或回车隔开):",i+1);
scanf("%f%d",&p->coef,&p->exp);
InsertNode(p,head); //调用InsertNode函数插入结点
}
return head;
}
int Compare(Polyn a,Polyn b)//依a的指数值<、=或>b的指数值,分别返回-1、0或1
{
if(a&&b)
{
if(!b||a->exp>b->exp)
return 1;
else if(!a||a->exp
return -1;
else
return 0;
}
else if(!a&&b)
return -1; //a多项式为空,但b多项式非空
else
return 1; //b多项式为空,但a多项式非空
}
void DestroyPolyn(Polyn p) //销毁多项式p
{
Polyn q1,q2;
q1=p->next;
if(q1==NULL) //结果系数为0的情况
return ;
q2=q1->next;
while(q1->next)
{
delete q1;
q1=q2; //指针后移
q2=q2->next;
}
}
Polyn AddPolyn(Polyn pa,Polyn pb)
{
Polyn qa=pa->next;
Polyn qb=pb->next;
Polyn heada,hc,qc;
hc=new Polynomial; //建立头结点
hc->next=NULL;
heada=hc;
while(qa||qb)
{
qc=new Polynomial;
switch(Compare(qa,qb))
{
case 1: //a项的指数大于b项的指数
{
qc->coef=qa->coef;
qc->exp=qa->exp;
qa=qa->next;
break;
}
case 0: //a项的指数等于b项的指数
{
qc->coef=qa->coef+qb->coef;
qc->exp=qa->exp;
qa=qa->next;
qb=qb->next;
break;
}
case -1: //a项的指数小于b项的指数
{
qc->coef=qb->coef;
qc->exp=qb->exp;
qb=qb->next;
break;
}
}
if(qc->coef!=0)
{
qc->next=hc->next;
hc->next=qc;
hc=qc;
}
else
delete qc; //当相加系数为0时,释放该结点
}
return heada; //返回其头指针
}
Polyn SubPolyn(Polyn pa,Polyn pb)
{
Polyn p1,p2,heads;
p1=pb;
p2=pb->next;
while(p2)
{
p2->coef*=-1; //将pb的系数取反
p2=p2->next;
}
heads=AddPolyn(pa,p1);
for(p2=p1->next;p2;p2=p2->next) //恢复pb的系数
p2->coef*=-1;
return heads; //返回其头指针
}
Polyn MultiplyPolyn(Polyn pa,Polyn pb)
{
Polyn headf,qc;
Polyn qa=pa->next;
Polyn qb=pb->next;
headf=new Polynomial; //建立头结点
headf->next=NULL;
for(;qa;qa=qa->next)
{
for(qb=pb->next;qb;qb=qb->next)
{
qc=new Polynomial;
qc->coef=qa->coef*qb->coef;
qc->exp=qa->exp+qb->exp;
InsertNode(qc,headf); //调用InsertNode函数合并指数相同的项
}
}
return headf; //返回其头指针
}
void DevicePolyn(Polyn pa,Polyn pb)
{
Polyn quotient,remainder,temp1,temp2;
Polyn qa=pa->next;
Polyn qb=pb->next;
quotient=new Polynomial; //建立头结点,存储商
quotient->next=NULL;
remainder=new Polynomial; //建立头结点,存储余数
remainder->next=NULL;
temp1=new Polynomial;
temp1->next=NULL;
temp2=new Polynomial;
temp2->next=NULL;
temp1=AddPolyn(temp1,pa);
while(qa!=NULL&&qa->exp>=qb->exp)
{
temp2->next=new Polynomial;
temp2->next->coef=(qa->coef)/(qb->coef);
temp2->next->exp=(qa->exp)-(qb->exp);
InsertNode(temp2->next,quotient);
pa=SubPolyn(pa,MultiplyPolyn(pb,temp2));
qa=pa->next;
temp2->next=NULL;
}
remainder=SubPolyn(temp1,MultiplyPolyn(quotient,pb));
pb=temp1;
printf("\t商:");
PrintPolyn(quotient);
printf("\t余数:");
PrintPolyn(remainder);
}
float ValuePolyn(Polyn head,float x)
{
Polyn p;
int i;
float sum=0,t;
for(p=head->next;p;p=p->next)
{
t=1;
for(i=p->exp;i!=0;)
{
if(i<0)
{
t/=x;
i++;
}//指数小于0,进行除法
else
{
t*=x;
i--;
}//指数大于0,进行乘法
}
sum+=p->coef*t;
}
return sum;
}
int main()
{
time_t t;
time(&t);
printf("︵ ︵ §欢迎使用一元稀疏多项式计算器\t\t\t╭─────╮\n");
printf("\t§功能:计算多项式的加、减、乘、除及值\t\t│作者陈显玲│\n");
printf(" ︶ §输入:多项式的项数、系数、指数\t\t\t│09计科二班│\n");
printf("\t§输出:各种运算后的结果\t\t\t\t╰─────╯\n");
printf("━━━━━━━━━━━━━━━━━━━━━━%s",ctime(&t));
int m,n,i;
float x;
Polyn pa=0,pb=0,pc,pd,pf;//定义各式的头指针,pa与pb在使用前赋初值NULL
printf("请输入多项式A项数:");
scanf("%d",&m);
pa=CreatePolyn(pa,m);//建立多项式A
printf("请输入多项式B项数:");
scanf("%d",&n);
pb=CreatePolyn(pb,n);//建立多项式B
//输出菜单
printf("\t\t\t【功能选择项】\n\t≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡\n");
printf("\t║ 1.输出多项式A和B ║\t2.计算多项式A+B ║\n");
printf("\t║ 3.计算多项式A-B ║\t4.计算多项式A*B ║\n");
printf("\t║ 5.计算多项式A/B ║\t6.计算A在x处的值║\n");
printf("\t║ 7.重新创建多项式 ║\t8.退出 ║\n");
printf("\t≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡\n");
do{
printf("\t请输入选择:");
scanf("%d",&i);
switch(i)
{
case 1:
{
printf("\t多项式A=:");
PrintPolyn(pa);
printf("\t多项式B=:");
PrintPolyn(pb);
break;
}
case 2:
{
pc=AddPolyn(pa,pb);
printf("\tA+B=:");
PrintPolyn(pc);
DestroyPolyn(pc);
break;
}
case 3:
{
pd=SubPolyn(pa,pb);
printf("\tA-B=:");
PrintPolyn(pd);
DestroyPolyn(pd);
break;
}
case 4:
{
pf=MultiplyPolyn(pa,pb);
printf("\tA*B=:");
PrintPolyn(pf);
DestroyPolyn(pf);
break;
}
case 5:
{
if(n==0)
{
printf("除数为0时不能进行除法运算!\n");
break;
}
DevicePolyn(pa,pb);
break;
}
case 6:
{
printf("\t输入x的值:x=");
scanf("%f",&x);
printf("\t多项式A的值:%g\n",ValuePolyn(pa,x));
break;
}
case 7:
{
printf("请输入多项式A项数:");
scanf("%d",&m);
pa=CreatePolyn(pa,m);
printf("请输入多项式B项数:");
scanf("%d",&n);
pb=CreatePolyn(pb,n);
break;
}
case 8:
{
printf("欢迎再次使用!O(∩_∩)O谢谢\n");
system("pause");
break;
}
default:
{
printf("\tInput Error!!!\n");
break;
}
}
}while(i!=8);
DestroyPolyn(pa);
DestroyPolyn(pb);
return 0;
}
全部回答
- 1楼网友:夜余生
- 2021-11-27 12:44
有什么出错现象啊?我这边没问题。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯