多项式计算要求:1主要功能:(1)输入并建立多项式;(2)输出多项式;(3)两个多项式相加,建立并输出和多项式;(4)两个多项式相减,建立并输出差多项式.2.要求:一元多项式简单计算器的基本
来源:学生作业帮助网 编辑:六六作业网 时间:2025/02/03 12:25:25
多项式计算要求:1主要功能:(1)输入并建立多项式;(2)输出多项式;(3)两个多项式相加,建立并输出和多项式;(4)两个多项式相减,建立并输出差多项式.2.要求:一元多项式简单计算器的基本
多项式计算
要求:
1主要功能:
(1)输入并建立多项式;
(2)输出多项式;
(3)两个多项式相加,建立并输出和多项式;
(4)两个多项式相减,建立并输出差多项式.
2.要求:一元多项式简单计算器的基本功能
多项式计算要求:1主要功能:(1)输入并建立多项式;(2)输出多项式;(3)两个多项式相加,建立并输出和多项式;(4)两个多项式相减,建立并输出差多项式.2.要求:一元多项式简单计算器的基本
#define LEN sizeof(struct STD)
#define NULL 0
#include
#include
#include
struct STD
{
int a;
int b;
struct STD *next;
};
int V;
struct STD *creat1(void)
{
FILE *fp;
struct STD *pN,*pT,*p;
struct STD *head=NULL;
pT=pN=(struct STD*)malloc(LEN);
fp=fopen("D:\\IN1.TXT","r");
if (fp==NULL)
{
printf("NULL\n");
return NULL;
}
while(!feof(fp))
{
fscanf(fp,"(%d,%d)",&pN->a,&pN->b);
/*if(pN->a!=0)
{ */
if (head==NULL) head=pN;
else pT->next=pN;
p=pT;
pT=pN;
pN=(struct STD*)malloc(LEN);
/*fscanf(fp,"(%d,%d)",&pN->a,&pN->b); */
}
if(head!=NULL)
p->next=NULL;
V=pT->b;
fclose(fp);
return head;
}
struct STD *creat2(void)
{
struct STD *head=NULL;
struct STD *pN,*pT;
pN=pT=(struct STD*)malloc(LEN);
scanf("%d,%d",&pN->a,&pN->b);
while(pN->a!=0)
{
if(head==NULL) head=pN;
else
pT->next=pN;
pT=pN;
pN=(struct STD*)malloc(LEN);
scanf("%d,%d",&pN->a,&pN->b);
}
pT->next=NULL;
return head;
}
void output(struct STD *head)
{
struct STD *p=head;
while(p!=NULL)
{
printf("%d,%d\n",p->a,p->b);
p=p->next;
}
if (head==NULL) printf("the list is null\n");
}
struct STD *count(struct STD *head1,struct STD *head2)
{
int i=0;
int a,b;
struct STD *pt=NULL;
struct STD *p1=head1,*p2=head2;
struct STD *p,*s;
struct STD *head=pt;
while(p1!=NULL||p2!=NULL)
{
s=(struct STD *)malloc(sizeof(struct STD));
if(p1==NULL)
{
s->a=p2->a;
s->b=p2->b;
p2=p2->next;
}
else
if(p2==NULL)
{
s->a=p1->a;
s->b=p1->b;
p1=p1->next;
}
else
if(p1->b==p2->b)
{
s->a=p1->a+p2->a;
s->b=p1->b;
p1=p1->next;
p2=p2->next;
}
else
{
if(p1->b>p2->b)
{
p=p1;
p1=p1->next;
}
else
{
p=p2;
p2=p2->next;
}
s->a=p->a;
s->b=p->b;
}
if(s->a!=0)
if(pt==NULL)
head=pt=s;
else
{
pt->next=s;
pt=pt->next;
}
}
if(pt!=NULL)
pt->next=NULL;
return head;
}
void main()
{
struct STD *head1=NULL,*head2=NULL;
clrscr();
head1=creat1();
printf("input the new:#,#\n");
head2=creat2();
head1=count(head1,head2);
printf("the result is :\n");
output(head1);
}