用List 完成多项式的四则运算
来源:互联网 发布:诲女知之乎通假字读音 编辑:程序博客网 时间:2024/05/06 17:45
#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;
}
- 用List 完成多项式的四则运算
- 用List 完成矩阵四则运算
- 一元多次多项式的四则运算
- C++完成多项式的加减法
- 用C++实现一元多项式的四则运算包括数据的文件导入与导出
- java 编写用于完成四则运算的类
- 使用栈结构完成四则运算
- 不用四则运算完成加法运算
- 用STL中的list模板类实现的简单多项式类
- 多项式加减 List
- 借助stl中的list实现多项式的相加
- 第15周任务2(窗口程序,完成分数的四则运算)
- 用java实现简单四则运算的算法
- 用栈实现整型数据的四则运算
- 用栈实现简单的四则运算
- list容器实例-多项式相加
- 接受用户输入的表达式,利用数据结构中栈完成多项式(+ - * / ( ))求值
- 【Visual Basic】纯代码不拖控件,利用动态生成控件的方式完成一个简单的四则运算计算器
- Google的App Engine支持Java啦
- Cannot find bean in any scope
- webservice 开发实践小结
- error listener start;startup failed due to previous errors
- 在销售软件时,如何把软件价格拉高
- 用List 完成多项式的四则运算
- org.xml.sax.SAXParseException;Invalid encoding name ‘XXX'
- java.io.NotSerializableException 解决办法
- nhibernate初步掌握连载: one-to-one映射
- 中文排版CSS心得
- 16位汇编_汉化DOS的常驻程序
- 软件的重构
- 用stack完成postfix to infix
- 用List 完成矩阵四则运算