一元多项式简单的计算器
发布网友
发布时间:2022-04-21 21:35
我来回答
共5个回答
热心网友
时间:2023-05-15 04:15
//一元稀疏多项式计算器
#include <stdio.h>
#include <stdlib.h>
#define OK 1;
#define READ(I) while (((I)=getchar())=='\n'); //读入合法字符
typedef int status;
typedef struct PolyNode
{
float coef;
int expn;
struct PolyNode *next;
}Poly,*P_Poly;
status CreatPoly(P_Poly*,int); //创建多项式链表
void OutP_Poly(P_Poly); //输出多项式
void AddPoly(P_Poly*,P_Poly,P_Poly); //求和
void DecPoly(P_Poly*,P_Poly,P_Poly); //求差
void DelPoly(P_Poly*); //删除并释放多项式
status main()
{
P_Poly PolyHead[26]={0};
char menu,name;
for (;;)
{
printf ("1.建立多项式\n");
printf ("2.输出多项式\n");
printf ("3.求两个多项式之和(必须先创建),建立和式并输出\n");
printf ("4.求两个多项式之差(必须先创建),建立差式并输出\n");
printf ("5.删除多项式\n");
printf ("6.退出\n");
printf ("请选择指令:");
READ(menu)
switch (menu)
{
case '1':
{
int n;
printf ("请输入要创建的多项式的名称(单个小写字母表示):");
READ(name)
if (NULL!=PolyHead[name-'a'])
{
printf ("此多项式已经存在!\n");
break;
}
printf ("请输入项数:");
scanf ("%d",&n);
if(CreatPoly(&PolyHead[name-'a'],n)) printf ("多项式%c创建完毕!\n",name);
break;
}
case '2':
printf ("请输入要输出的多项式的名称:");
READ(name)
if (NULL==PolyHead[name-'a'])
{
printf ("此多项式不存在!\n");
break;
}
OutP_Poly(PolyHead[name-'a']);
break;
case '3':
{
char name1,name2;
printf ("请输入加式一的名称:");
READ(name1)
if (NULL==PolyHead[name1-'a'])
{
printf ("此多项式不存在!请先创建\n");
break;
}
printf ("请输入加式二的名称:");
READ(name2)
if (NULL==PolyHead[name2-'a'])
{
printf ("此多项式不存在!请先创建\n");
break;
}
printf ("请输入要创建的和式的名称:");
READ(name)
if (NULL!=PolyHead[name-'a'])
{
printf ("此多项式已经存在!\n");
break;
}
AddPoly(&PolyHead[name-'a'],PolyHead[name1-'a'],PolyHead[name2-'a']);
OutP_Poly(PolyHead[name-'a']);
break;
}
case '4':
{
char name1,name2;
printf ("请输入被减式的名称:");
READ(name1)
if (NULL==PolyHead[name1-'a'])
{
printf ("此多项式不存在!请先创建\n");
break;
}
printf ("请输入减式的名称:");
READ(name2)
if (NULL==PolyHead[name2-'a'])
{
printf ("此多项式不存在!请先创建\n");
break;
}
printf ("请输入要创建的差式的名称:");
READ(name)
if (NULL!=PolyHead[name-'a'])
{
printf ("此多项式已经存在!\n");
break;
}
DecPoly(&PolyHead[name-'a'],PolyHead[name1-'a'],PolyHead[name2-'a']);
OutP_Poly(PolyHead[name-'a']);
break;
}
case '5':
printf ("请输入要删除的多项式名称:");
READ(name)
if (NULL==PolyHead[name-'a'])
{
printf ("此多项式不存在!\n");
break;
}
DelPoly(&PolyHead[name-'a']);
printf ("多项式%c已删除!\n",name);
break;
case '6':
return OK;
}
}
}
status CreatPoly(P_Poly* P_PolyHead,int n)
{ //为确保其他子函数正常运行,入口参数要求不含同类项、指数递减
int i=0;
P_Poly p,tail;
*P_PolyHead=(Poly*)malloc(sizeof(Poly));
(*P_PolyHead)->expn=n;
tail=*P_PolyHead;
printf ("请按照指数递减序输入最简形式的多项式\n");
for (;i<n;i++)
{
p=(Poly*)malloc(sizeof(Poly));
printf ("请输入第%d项的系数和指数(系数,指数):",i+1);
scanf ("%f,%d",&(p->coef),&(p->expn));
tail->next=p;
tail=p;
}
tail->next=NULL;
return OK;
}
void OutP_Poly(P_Poly PolyHead)
{
int i=0;
P_Poly p;
printf ("%d ",PolyHead->expn);
p=PolyHead->next;
for (;i<PolyHead->expn;i++)
{
printf ("%f,%d ",p->coef,p->expn);
p=p->next;
}
printf ("\n");
}
void AddPoly(P_Poly* P_PolyHead,P_Poly PolyHead1,P_Poly PolyHead2)
{
int i=0;
P_Poly p1,p2,p,tail;
p1=PolyHead1->next;
p2=PolyHead2->next;
*P_PolyHead=(P_Poly)malloc(sizeof(Poly));
p=tail=*P_PolyHead;
while(p1&&p2)
{
int e1,e2;
e1=p1->expn;
e2=p2->expn;
if (e1>e2)
{
p=(P_Poly)malloc(sizeof(Poly));
tail->next=p;
tail=p;
*p=*p1;
p1=p1->next;
i++;
}
else if(e1==e2)
{
float sum_c=p1->coef+p2->coef;
if (sum_c!=0.0)
{
p=(P_Poly)malloc(sizeof(Poly));
tail->next=p;
tail=p;
p->coef=sum_c;
p->expn=e1;
i++;
}
p1=p1->next;
p2=p2->next;
}
else if (e1<e2)
{
p=(P_Poly)malloc(sizeof(Poly));
tail->next=p;
tail=p;
*p=*p2;
p2=p2->next;
i++;
}
}
while (p1)
{
p=(Poly*)malloc(sizeof(Poly));
*p=*p1;
tail->next=p;
tail=p;
p1=p1->next;
i++;
}
while (p2)
{
p=(Poly*)malloc(sizeof(Poly));
*p=*p2;
tail->next=p;
tail=p;
p2=p2->next;
i++;
}
(*P_PolyHead)->expn=i;
tail->next=NULL;
}
void DecPoly(P_Poly* P_PolyHead,P_Poly PolyHead1,P_Poly PolyHead2)
{
int i=0;
P_Poly p1,p2,p,tail;
p1=PolyHead1->next;
p2=PolyHead2->next;
*P_PolyHead=(P_Poly)malloc(sizeof(Poly));
p=tail=*P_PolyHead;
while(p1&&p2)
{
int e1,e2;
e1=p1->expn;
e2=p2->expn;
if (e1>e2)
{
p=(P_Poly)malloc(sizeof(Poly));
tail->next=p;
tail=p;
*p=*p1;
p1=p1->next;
i++;
}
else if(e1==e2)
{
float dif_c=p1->coef-p2->coef;
if (dif_c!=0.0)
{
p=(P_Poly)malloc(sizeof(Poly));
tail->next=p;
tail=p;
p->coef=dif_c;
p->expn=e1;
i++;
}
p1=p1->next;
p2=p2->next;
}
else if (e1<e2)
{
p=(P_Poly)malloc(sizeof(Poly));
tail->next=p;
tail=p;
p->coef=-p2->coef;
p->expn=p2->expn;
p2=p2->next;
i++;
}
}
while (p1)
{
p=(Poly*)malloc(sizeof(Poly));
*p=*p1;
tail->next=p;
tail=p;
p1=p1->next;
i++;
}
while (p2)
{
p=(Poly*)malloc(sizeof(Poly));
*p=*p2;
tail->next=p;
tail=p;
p2=p2->next;
i++;
}
(*P_PolyHead)->expn=i;
tail->next=NULL;
}
void DelPoly(P_Poly* P_PolyHead)
{
P_Poly p,prior;
p=prior=*P_PolyHead;
while (p->next)
{
p=p->next;
free(prior);
prior=p;
}
*P_PolyHead=NULL;
}
自己修改修改风格就可以用了
热心网友
时间:2023-05-15 04:16
我靠,如楼上说的。。真懒。。第一道多项式的数据结构那本书上都有详细的解说了。。实现一下就可以了。。第二道是有点复杂
热心网友
时间:2023-05-15 04:16
工作量比较大。
百度的分没用,还不如何丹的论坛的金币,至少可以换QB
如果100何丹论坛的金币,我今天晚上就答。
这样吧,如果你的问题明天还没人答,我就帮你答吧
热心网友
时间:2023-05-15 04:17
有偿服务。绝对满足所有要求,良好的代码风格,明了的代码注释。
Q Q 703252627
热心网友
时间:2023-05-15 04:17
这个是有点难。。。