多项式的加减乘
【摘要】 这是我学数据结构写的第一个比较"大型"的程序,唉,还是有不足之处啊。。。
# include <stdio.h># include <iostream.h># include <stdlib.h># include <malloc.h> # define OK 1# define ERROR 0# define OVERFLO...
这是我学数据结构写的第一个比较"大型"的程序,唉,还是有不足之处啊。。。
-
# include <stdio.h>
-
# include <iostream.h>
-
# include <stdlib.h>
-
# include <malloc.h>
-
-
# define OK 1
-
# define ERROR 0
-
# define OVERFLOW -1
-
-
-
typedef struct Polynomial
-
{
-
float coef;
-
int expn;
-
struct Polynomial *next;
-
}Polynomial,*LinkPolyn;
-
-
void Create_Polyn(LinkPolyn &L)
-
{
-
int i,n;
-
LinkPolyn p,s;
-
L = (LinkPolyn) malloc (sizeof (Polynomial));
-
if (!L)
-
exit(OVERFLOW);
-
L->next = NULL;
-
p = L;
-
cout<<" 请输入您要创建多项式的项数:";
-
cin>>n;
-
for (i=0; i<n; i++)
-
{
-
-
s = (LinkPolyn) malloc (sizeof (Polynomial));
-
p->next = s;
-
cout<<" 请输入第"<<i+1<<"项的系数:";
-
cin>>s->coef;
-
cout<<" 请输入第"<<i+1<<"项的指数:";
-
cin>>s->expn;
-
-
p = s;
-
}
-
-
p->next = NULL;
-
}
-
-
void Display(LinkPolyn L)
-
{
-
LinkPolyn p;
-
if (!L->next )
-
cout<<" 多项式的项数为0!无法显示"<<endl;
-
p = L->next ;
-
while (p)
-
{
-
-
-
if (p->coef == 0.0)
-
cout<<"";
-
else if (p->expn == 0 )
-
cout<<p->coef ;
-
else if (p->coef == 1 && p->expn == 1)
-
cout<<"X";
-
else if (p->coef == 1 && p->expn != 1)
-
cout<<"X^"<<p->expn;
-
else if (p->coef == -1 && p->expn == 1)
-
cout<<"-X";
-
else if (p->coef != 1 && p->expn == 1)
-
cout<<p->coef<<"X";
-
-
else if (p->coef == -1 && p->expn != 1)
-
cout<<"-X^"<<p->expn ;
-
else
-
cout<<p->coef <<"X^"<<p->expn ;
-
-
-
-
-
if (p->next!=NULL && p->coef !=0 && p->next->coef >=0)
-
cout<<"+";
-
p = p->next ;
-
}
-
cout<<endl;
-
}
-
-
void SortPolyn(LinkPolyn &L)
-
{
-
LinkPolyn p,q;
-
int temp;
-
float tmp;
-
if (!L->next )
-
cout<<" 多项式的项数为0,无法排序!"<<endl;
-
-
for(p=L->next;p; p=p->next )
-
for(q=L->next ;q; q=q->next)
-
{
-
if(p->expn < q->expn )
-
{
-
temp = p->expn ;
-
p->expn = q->expn ;
-
q->expn = temp;
-
-
tmp = p->coef ;
-
p->coef = q->coef ;
-
q->coef = tmp;
-
}
-
else if (p != q && p->expn == q->expn)
-
{
-
p->coef +=q->coef ;
-
q->coef = 0;
-
q->expn = 0;
-
-
-
}
-
-
}
-
-
-
}
-
-
void Diff_Polyn(LinkPolyn &L)
-
{
-
LinkPolyn p;
-
if (!L->next )
-
cout<<" 多项式的项数为0,无法求导!"<<endl;
-
p = L->next ;
-
while (p)
-
{
-
-
p->coef *= p->expn ;
-
p->expn -=1;
-
p = p->next ;
-
}
-
}
-
-
void Int_Polyn(LinkPolyn &L)
-
{
-
LinkPolyn p;
-
if (!L->next )
-
cout<<" 多项式的项数为0,无法求积分!"<<endl;
-
p = L->next ;
-
while (p)
-
{
-
-
p->coef = p->coef/(p->expn+1);
-
p->expn +=1;
-
p = p->next ;
-
}
-
}
-
-
int cmp (int a, int b)
-
{
-
if (a>b)
-
return 1;
-
else if (a == b)
-
return 0;
-
return -1;
-
}
-
-
int Add_Polyn(LinkPolyn &PA, LinkPolyn &PB)
-
{
-
LinkPolyn qc,qa,qb,temp;
-
float sum;
-
qc = PA;
-
qa = PA->next ;
-
qb = PB->next ;
-
-
while (qa && qb)
-
{
-
switch(cmp(qa->expn ,qb->expn ))
-
{
-
case -1:
-
qc->next = qa;
-
qc = qa;
-
qa = qa->next;
-
break;
-
-
case 0:
-
sum = qa->coef + qb->coef ;
-
if(sum !=0.0)
-
{
-
qa->coef = sum;
-
qc->next = qa;
-
qc = qa;
-
qa = qa->next;
-
}
-
-
else
-
{
-
temp = qa;
-
qa = qa->next ;
-
free(temp);
-
}
-
-
temp = qb;
-
qb = qb->next ;
-
free(temp);
-
break;
-
-
case 1:
-
qc->next = qb;
-
qc = qb;
-
qb = qb->next;
-
break;
-
}
-
}
-
-
while(qa)
-
{
-
qc->next = qa;
-
qc = qa;
-
qa = qa->next;
-
}
-
while(qb)
-
{
-
qc->next = qb;
-
qc = qb;
-
qb = qb->next;
-
}
-
-
free(qb);
-
return OK;
-
}
-
-
-
int Sub_Polyn(LinkPolyn &PA, LinkPolyn &PB)
-
{
-
LinkPolyn qc,qa,qb,temp;
-
float sum;
-
qc = PA;
-
qa = PA->next ;
-
qb = PB->next ;
-
-
while (qa && qb)
-
{
-
-
switch(cmp(qa->expn ,qb->expn ))
-
{
-
-
case -1:
-
qc->next = qa;
-
qc = qa;
-
qa = qa->next;
-
break;
-
-
case 0:
-
sum = qa->coef - qb->coef ;
-
if(sum !=0.0)
-
{
-
qa->coef = sum;
-
qc->next = qa;
-
qc = qa;
-
qa = qa->next;
-
}
-
-
else
-
{
-
temp = qa;
-
qa = qa->next ;
-
free(temp);
-
}
-
-
temp = qb;
-
qb = qb->next ;
-
free(temp);
-
break;
-
-
case 1:
-
qb->coef =-qb->coef ;
-
qc->next = qb;
-
qc = qb;
-
qb = qb->next;
-
break;
-
}
-
}
-
-
while(qa)
-
{
-
qc->next = qa;
-
qc = qa;
-
qa = qa->next;
-
}
-
while(qb)
-
{
-
qb->coef =-qb->coef ;
-
qc->next = qb;
-
qc = qb;
-
qb = qb->next;
-
}
-
-
free(qb);
-
return OK;
-
}
-
-
int Mul_Polyn(LinkPolyn pa,LinkPolyn pb,LinkPolyn &pc)
-
{
-
LinkPolyn qa,qb,qc,s;
-
pc = (LinkPolyn) malloc (sizeof (Polynomial));
-
pc->next = NULL;
-
qc = pc;
-
for(qa=pa->next;qa;qa=qa->next)
-
{
-
for(qb=pb->next;qb;qb=qb->next)
-
{
-
s = (LinkPolyn) malloc (sizeof (Polynomial));
-
qc->next = s;
-
s->coef=qa->coef*qb->coef;
-
s->expn=qa->expn+qb->expn;
-
qc = s;
-
-
}
-
}
-
qc->next = NULL;
-
-
SortPolyn(pc);
-
return OK;
-
}
-
-
-
int main (void)
-
{
-
char i;
-
LinkPolyn L;
-
LinkPolyn PA,PB,PC;
-
-
cout<<endl;
-
cout<<" ********************************************************"<<endl;
-
cout<<" | 欢迎使用本系统! |"<<endl;
-
cout<<" | 制作人:风尘三侠 |"<<endl;
-
cout<<" | 1:创建一个多项式 2:对一个多项式求导 |"<<endl;
-
cout<<" | 3:对一个多项式求积分 4:两个多项式相加 |"<<endl;
-
cout<<" | 5:两个多项式相减 6:两个多项式相乘 |"<<endl;
-
cout<<" | 7:帮助 8:退出 |"<<endl;
-
cout<<" ********************************************************"<<endl;
-
cout<<endl;
-
do
-
{
-
cout<<" 请选择您要执行的操作:";
-
cin>>i;
-
switch (i)
-
{
-
case '1':
-
Create_Polyn(L);
-
SortPolyn(L);
-
cout<<" 您创建的多项式是: F(X) = ";
-
Display(L);
-
break;
-
case '2':
-
cout<<" 对一个多项式求导前请先创建一个多项式!"<<endl;
-
Create_Polyn(L);
-
SortPolyn(L);
-
cout<<" 您创建的多项式是: F(X) = ";
-
Display(L);
-
Diff_Polyn(L);
-
cout<<" 求导后的多项式是: F'(X) = ";
-
Display(L);
-
break;
-
case '3':
-
cout<<" 对一个多项式求积分前请先创建一个多项式!"<<endl;
-
Create_Polyn(L);
-
SortPolyn(L);
-
cout<<" 您创建的多项式是: F(X) = ";
-
Display(L);
-
Int_Polyn(L);
-
cout<<" 积分后的多项式是: F'(X) = ";
-
Display(L);
-
break;
-
case '4':
-
cout<<" 请先创建两个多项式!"<<endl;
-
cout<<" 创建第一个多项式!"<<endl;
-
Create_Polyn(PA);
-
SortPolyn(PA);
-
cout<<" 您创建的第一个多项式是: F(X) = ";
-
Display(PA);
-
-
cout<<" 创建第二个多项式!"<<endl;
-
Create_Polyn(PB);
-
SortPolyn(PB);
-
cout<<" 您创建的第二个多项式是: G(X) = ";
-
Display(PB);
-
-
cout<<" 两个多项式相加后:F(X) + G(X) = ";
-
Add_Polyn(PA, PB);
-
Display(PA);
-
break;
-
case '5':
-
cout<<" 请先创建两个多项式!"<<endl;
-
cout<<" 创建第一个多项式!"<<endl;
-
Create_Polyn(PA);
-
SortPolyn(PA);
-
cout<<" 您创建的第一个多项式是: F(X) = ";
-
Display(PA);
-
-
cout<<" 创建第二个多项式!"<<endl;
-
Create_Polyn(PB);
-
SortPolyn(PB);
-
cout<<" 您创建的第二个多项式是: G(X) = ";
-
Display(PB);
-
-
cout<<" 两个多项式相减后:F(X) - G(X) = ";
-
Sub_Polyn(PA, PB);
-
Display(PA);
-
break;
-
-
case '6':
-
cout<<" 请先创建两个多项式!"<<endl;
-
cout<<" 创建第一个多项式!"<<endl;
-
Create_Polyn(PA);
-
SortPolyn(PA);
-
cout<<" 您创建的第一个多项式是: F(X) = ";
-
Display(PA);
-
-
cout<<" 创建第二个多项式!"<<endl;
-
Create_Polyn(PB);
-
SortPolyn(PB);
-
cout<<" 您创建的第二个多项式是: G(X) = ";
-
Display(PB);
-
-
cout<<" 两个多项式相乘后:F(X) * G(X) = ";
-
Mul_Polyn(PA, PB,PC);
-
Display(PC);
-
break;
-
case '7':
-
cout<<" 创建多项式! "<<endl;
-
cout<<" 先输入多项式的项数,要输入自然数!"<<endl;
-
cout<<" 可以输入重复的项,先输入系数,再输入指数!"<<endl;
-
cout<<" 系数可以是任意的数字,指数要输入自然数!"<<endl;
-
break;
-
case '8':
-
cout<<" 谢谢使用,再见!"<<endl;
-
exit(OVERFLOW);
-
break;
-
default:
-
{
-
cout<<" 您的输入有误,请重新输入!"<<endl;
-
break;
-
}
-
}
-
}while (i != 8);
-
-
return 0;
-
}
文章来源: blog.csdn.net,作者:悦来客栈的老板,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq523176585/article/details/6818975
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)