📄 polymial.cpp
字号:
//Release Date:Apr. 1st 2007
//Author:MeteorJ
//Subject:Add two Polynmials
#include<stdio.h>
#include<stdlib.h>
/*
* Data Area in the PolynList
*
*
*/
typedef struct data
{
float objhead_num; //coefficient of the data
int objshoulder_num; //exponent of the data
}Polyn_data;
/*
* Node define in the PolynList
*
*
*/
typedef struct inPoly
{
Polyn_data MyPolyn_data;
struct inPoly *next;
}Polyn,*PolyList;
/*
* Initial the PolynList
*
*
*/
void init_lklist(PolyList &L)
{
PolyList s;
s=new Polyn;
s->next=NULL;
L=s;
}
/*
* locate the element
*
*
*/
int get(PolyList L,int x)
{
PolyList q;
q=L;
while(q)
{
if(q->MyPolyn_data.objshoulder_num==x)
return 1;
else
q=q->next;
}
return 0;
}
/*
* Insert an element in the PolynList
*
*
*/
void insert(PolyList L,Polyn_data x)
{
PolyList p,q;
p=L;
PolyList addNode;
addNode=new Polyn;
while(p)
{
if(p->MyPolyn_data.objshoulder_num<x.objshoulder_num)
{
q=p;
p=p->next;
}
else break;
}
addNode->MyPolyn_data=x;
addNode->next=q->next;
q->next=addNode;
}
/*
* Create a PolynList
*
*
*/
void create(PolyList &L)
{
PolyList p;
p=new Polyn;
p->MyPolyn_data.objhead_num=1.0;
p->MyPolyn_data.objshoulder_num=-1;
p->next=NULL;
L=p;
Polyn_data d;
printf("Input the coefficient of the Polynmial:\n");
scanf("%f",&d.objhead_num);
printf("Input the exponent of the Polynmial:\n");
scanf("%d",&d.objshoulder_num);
while(d.objshoulder_num!=0)
{
if(!get(L,d.objshoulder_num))
insert(L,d);
printf("Input the coefficient of the Polynmial:\n");
scanf("%f",&d.objhead_num);
printf("Input the exponent of the Polynmial:\n");
scanf("%d",&d.objshoulder_num);
}
}
/*
* Display the PolynList
*
*
*/
void display(PolyList L)
{
PolyList p;
p=L->next;
printf("Display the Polynmial:\n");
printf("F(X) = ");
if(p)
{
if(p->MyPolyn_data.objshoulder_num!=0)
printf("%f * X^%d",p->MyPolyn_data.objhead_num,p->MyPolyn_data.objshoulder_num);
p=p->next;
}
while(p)
{
float m;
m=p->MyPolyn_data.objhead_num;
if(m<0)
printf("%f * X^%d",p->MyPolyn_data.objhead_num,p->MyPolyn_data.objshoulder_num);
else if(m>0)
printf("+%f * X^%d",p->MyPolyn_data.objhead_num,p->MyPolyn_data.objshoulder_num);
p=p->next;
}
printf("\n");
}
/*
* add two polyn list function
*
*
*/
void addPolyn(PolyList L1,PolyList L2)
{
PolyList p1=L1->next,p2=L2->next;
int i;
if(!p1)
{
if(!p2) printf("Empty Polynmial!\n");
else display(L2);
}
else if(!p2)
{
if(!p1) printf("Empty Polynmial!\n");
else display(L1);
}
else
{
while(p2)
{
while(p1)
{
if(p2->MyPolyn_data.objshoulder_num==p1->MyPolyn_data.objshoulder_num)
{
i=1;
break;
}
else
{
p1=p1->next;
i=0;
}
}
if(i==0)
insert(L1,p2->MyPolyn_data);
else if(i==1)
p1->MyPolyn_data.objhead_num+=p2->MyPolyn_data.objhead_num;
p1=L1;
p2=p2->next;
}
}
}
/*
* main function
*
*
*/
void main()
{
PolyList q1,q2;
printf("/***************************************\n");
printf("\n");
printf("Polynmial List Program Test By MeteorJ\n");
printf("\n");
printf("***************************************/\n");
printf("\n");
printf("Input Polynmial A:(End with 0 as the exponent)\n");
printf("\n");
create(q1);
display(q1);
printf("\n");
printf("Input Polynmial B:(End with 0 as the exponent)\n");
printf("\n");
create(q2);
display(q2);
addPolyn(q1,q2);
printf("Polynmial after Added:\n");
display(q1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -