用List 完成多项式的四则运算
2009-04-12 11:59
211 查看
#include <stdio.h>
#include <conio.h>
#define SIZE1 4
#define SIZE2 4
typedef struct node
{
int Coefficient;
int Exponent;
struct node *link;
}Node, *List;
void InitList(List *p)
{
List q;
q =(List)malloc(sizeof(Node));
q->link=NULL;
*p = q;
return;
}
void InsertList(List H,int i,int coe, int exp)
{ int j=1;
List p,q;
p = H;
while(p->link!=NULL && j<i)
{
p = p->link;
j++;
}
q =(List)malloc(sizeof(Node));
q->Coefficient=coe;
q->Exponent=exp;
q->link=p->link;
p->link=q;
return;
}
void TravList(List H)
{
List p;
p = H->link->link;
printf("%dX^%d",H->link->Coefficient, H->link->Exponent);
while(p!=NULL)
{
if(p->Coefficient>0)
printf("+");
printf("%d",p->Coefficient );
if(p->Exponent!=0)
printf("X^%d",p->Exponent);
p=p->link;
}
printf("/n");
return;
}
void DeleteList(List H,int i)
{ int j=1;
List p,q;
p = H;
while(p->link!=NULL && j<i)
{ p = p->link;j++;}
q = p->link;
p->link=p->link->link;
free(q);
return;
}
void muliPolynomial(List L1, List L2, List L3)
{
List p1,p2,p3;
int i=1,power,coe,add=0;
p1=L1->link;
p2=L2->link;
InsertList(L3,i,p1->Coefficient * p2->Coefficient,p1->Exponent+p2->Exponent); /*get the first element of the new list*/
p2=p2->link;
p3=L3->link;
while(p1 != NULL)
{
while(p2 != NULL)
{
power=p1->Exponent+p2->Exponent;
coe= p1->Coefficient * p2->Coefficient;
while(p3!=NULL)
{
if(p3->Exponent==power)
{
p3->Coefficient+=coe;
if(p3->Coefficient==0)
{
DeleteList(L3,i);
i--;
}
add=1;
break;
}
i++;
p3=p3->link;
}
if(add!=1)
{
InsertList(L3,i,coe,power);
}
add=0;
p2=p2->link;
i=1;
p3=L3->link;
}
p2=L2->link;
p1=p1->link;
add=0;
}
return;
}
void addPolynomial(List L1, List L2, List L3)
{
List p1,p2,p3,p;
int i=1;
p1=L1->link;
p2=L2->link;
p3=L3;
while(p1!=NULL&&p2!=NULL)
{
/*if the exponent is the same, add and put the result to the new list*/
if(p1->Exponent==p2->Exponent)
{
if(p1->Coefficient+p2->Coefficient!=0)
InsertList(L3,i,p1->Coefficient+p2->Coefficient,p1->Exponent) ;
p1=p1->link;
p2=p2->link;
}
/*if not the same, put the higher one into the new list
,and and move the pointer to the next and compare again*/
else if(p1->Exponent>p2->Exponent)
{
InsertList(L3,i,p1->Coefficient,p1->Exponent);
p1=p1->link;
}
else if(p1->Exponent<p2->Exponent)
{
InsertList(L3,i,p2->Coefficient,p2->Exponent);
p2=p2->link;
}
i++;
p3=p3->link;
}
/*if one list is empty and the other is not, put the rest into the new list*/
if(p1!=NULL)
p3->link=p1;
else if(p2!=NULL)
p3->link=p2;
return;
}
/*在这个程序中,我们假设拥有高阶的项在链表中始终在排在低阶项之前*/
void main()
{
List list1,list2,list3,list4;
int i;
/*int content1[4] ={1,2,-1,0}; */
int content1[SIZE1] ={1,2,-1,0}; /*位数从1算起,奇数位为系数,偶数位为指数*/
int content2[SIZE2] ={1,2,1,0};
InitList(&list1);
InitList(&list2);
InitList(&list3);
InitList(&list4);
for(i=0;i<SIZE1;i=i+2)
{
InsertList(list1,i/2+1,content1[i],content1[i+1]);
}
for(i=0;i<SIZE2;i=i+2)
{
InsertList(list2,i/2+1,content2[i],content2[i+1]);
} /**/
TravList(list1);
TravList(list2);
printf("The result of multiplying two polynomials:/n");
muliPolynomial(list1, list2, list4) ;
addPolynomial(list1,list2,list3);
TravList(list3);
printf("/nThe result of adding two polynomials:/n");
TravList(list4);
getch();
return;
}
#include <conio.h>
#define SIZE1 4
#define SIZE2 4
typedef struct node
{
int Coefficient;
int Exponent;
struct node *link;
}Node, *List;
void InitList(List *p)
{
List q;
q =(List)malloc(sizeof(Node));
q->link=NULL;
*p = q;
return;
}
void InsertList(List H,int i,int coe, int exp)
{ int j=1;
List p,q;
p = H;
while(p->link!=NULL && j<i)
{
p = p->link;
j++;
}
q =(List)malloc(sizeof(Node));
q->Coefficient=coe;
q->Exponent=exp;
q->link=p->link;
p->link=q;
return;
}
void TravList(List H)
{
List p;
p = H->link->link;
printf("%dX^%d",H->link->Coefficient, H->link->Exponent);
while(p!=NULL)
{
if(p->Coefficient>0)
printf("+");
printf("%d",p->Coefficient );
if(p->Exponent!=0)
printf("X^%d",p->Exponent);
p=p->link;
}
printf("/n");
return;
}
void DeleteList(List H,int i)
{ int j=1;
List p,q;
p = H;
while(p->link!=NULL && j<i)
{ p = p->link;j++;}
q = p->link;
p->link=p->link->link;
free(q);
return;
}
void muliPolynomial(List L1, List L2, List L3)
{
List p1,p2,p3;
int i=1,power,coe,add=0;
p1=L1->link;
p2=L2->link;
InsertList(L3,i,p1->Coefficient * p2->Coefficient,p1->Exponent+p2->Exponent); /*get the first element of the new list*/
p2=p2->link;
p3=L3->link;
while(p1 != NULL)
{
while(p2 != NULL)
{
power=p1->Exponent+p2->Exponent;
coe= p1->Coefficient * p2->Coefficient;
while(p3!=NULL)
{
if(p3->Exponent==power)
{
p3->Coefficient+=coe;
if(p3->Coefficient==0)
{
DeleteList(L3,i);
i--;
}
add=1;
break;
}
i++;
p3=p3->link;
}
if(add!=1)
{
InsertList(L3,i,coe,power);
}
add=0;
p2=p2->link;
i=1;
p3=L3->link;
}
p2=L2->link;
p1=p1->link;
add=0;
}
return;
}
void addPolynomial(List L1, List L2, List L3)
{
List p1,p2,p3,p;
int i=1;
p1=L1->link;
p2=L2->link;
p3=L3;
while(p1!=NULL&&p2!=NULL)
{
/*if the exponent is the same, add and put the result to the new list*/
if(p1->Exponent==p2->Exponent)
{
if(p1->Coefficient+p2->Coefficient!=0)
InsertList(L3,i,p1->Coefficient+p2->Coefficient,p1->Exponent) ;
p1=p1->link;
p2=p2->link;
}
/*if not the same, put the higher one into the new list
,and and move the pointer to the next and compare again*/
else if(p1->Exponent>p2->Exponent)
{
InsertList(L3,i,p1->Coefficient,p1->Exponent);
p1=p1->link;
}
else if(p1->Exponent<p2->Exponent)
{
InsertList(L3,i,p2->Coefficient,p2->Exponent);
p2=p2->link;
}
i++;
p3=p3->link;
}
/*if one list is empty and the other is not, put the rest into the new list*/
if(p1!=NULL)
p3->link=p1;
else if(p2!=NULL)
p3->link=p2;
return;
}
/*在这个程序中,我们假设拥有高阶的项在链表中始终在排在低阶项之前*/
void main()
{
List list1,list2,list3,list4;
int i;
/*int content1[4] ={1,2,-1,0}; */
int content1[SIZE1] ={1,2,-1,0}; /*位数从1算起,奇数位为系数,偶数位为指数*/
int content2[SIZE2] ={1,2,1,0};
InitList(&list1);
InitList(&list2);
InitList(&list3);
InitList(&list4);
for(i=0;i<SIZE1;i=i+2)
{
InsertList(list1,i/2+1,content1[i],content1[i+1]);
}
for(i=0;i<SIZE2;i=i+2)
{
InsertList(list2,i/2+1,content2[i],content2[i+1]);
} /**/
TravList(list1);
TravList(list2);
printf("The result of multiplying two polynomials:/n");
muliPolynomial(list1, list2, list4) ;
addPolynomial(list1,list2,list3);
TravList(list3);
printf("/nThe result of adding two polynomials:/n");
TravList(list4);
getch();
return;
}
相关文章推荐
- 一元多次多项式的四则运算
- 【数据结构_链表_List_1040】一元多项式加法运算的实现
- java 四则运算,并可完成 自定义函数 表达式 功能
- 实现分数类中的对象和整型数的四则运算。分数类中的对象可以和整型数进行四则运算,且运算符合交换律。例如:CFraction a(1,3),b; int i=2; 可以完成b=a+i;。同样,可以完成i+
- 第15周任务2(窗口程序,完成分数的四则运算)
- 四则运算App--大总结(已完成)
- 在(1)的基础上,实现分数类中的对象和整型数的四则运算。分数类中的对象可以和整型数进行四则运算,且运算符合交换律。例如:CFraction a(1,3),b; int i=2; 可以完成b=a+i;。
- 【Visual Basic】纯代码不拖控件,利用动态生成控件的方式完成一个简单的四则运算计算器
- 不用四则运算完成加法运算
- 第十五周实验--任务2--设计分数类,开发一个窗口式程序,可以完成分数的四则运算。
- 编制一个完成两个数的四则运算程序。如:用户输入34+56则输出结果90.00。要求运算结果保留两位小数,用户输入时一次将两个数和操作符输入。
- 实验二 四则运算 完成版 ver.1
- 写输出30道小学生四则运算程序的解题思路及未在规定时间内完成程序的原因
- 四则运算2任务完成表
- 四则运算2的任务完成时间表
- 四则运算终极版
- 四则运算题目生成器项目博客
- js实现随机的四则运算题目效果
- 个人项目——四则运算生成器和解析器(补充版本)
- 四则运算2代码