데이터 구조 일원 다항식 의 표시 및 더하기
typedef struct
{
float coef; //계수
int expn; //지수
}term,ElemType;
/ / 정의 구조 체
typedef LinkList polynomial;
/ / 선두 결점 의 질서 있 는 링크 로 다항식 표시
int cmp(term a,term b);
/ / a 의 지수 값 에 따라 < = >b 의 지수 값.각각 돌아 가기 - 1 0 1
Status InitList(polynomial &P);
/ / 구조 공 선형 링크
Position GetHead(polynomial P);
/ / P 의 끝 점 을 되 돌려 줍 니 다.
Status SetCurElem(Position h,term e);
/ / h 요 소 를 e 로 변경 합 니 다.
Status LocateElem(LinkList P,ElemType e,Position &q,int(*cmp)(ElemType,ElemType));
/ / 이 지수 항목 이 존재 하 는 지 되 돌려 줍 니 다.
Status MakeNode(Link &p,ElemType e);
/ / 결산 점 신청
Status InsFirst(LinkList &P,Link h,Link s);
/ / 삽입
void CreatPolyn(polynomial &P,int m);
/ / m 항목 의 지수 와 계 수 를 입력 하고 1 원 다항식 을 나타 내 는 질서 있 는 링크 P 를 구축한다.
Position NextPos(Link p);
/ / 다음 결점 을 가리키다
ElemType GetCurElem(Link p);
/ / 반환 값
Status DelFirst(LinkList &L,Link h,Link &q);
/ / 현재 노드 삭제
void FreeNode(Link &p);
/ / 석방 P
Status ListEmpty(LinkList L);
/ / L 이 빈 링크 인지 판단
Status Append(LinkList &L,Link s);
/ / s 남 은 노드 를 L 로 연결
void PrintPolyn(polynomial P);
/ / 출력 1 원 다항식 P
Status ClearList(LinkList &L);
// 체인 리스트 비우 기
Status DestroyPolyn(LinkList &L);
// 선형 링크 제거 L, L 은 존재 하지 않 습 니 다
void AddPolyn(polynomial &Pa,polynomial &Pb);
// 다항식 덧셈: Pa = Pa + Pb, 그리고 일원 다항식 Pb 를 소각 합 니 다.
*/
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR -1
#define FALSE 0
#define TRUE 2
typedef int Status;
typedef struct{
float coef; //
int expn; //
}term,ElemType; //
typedef struct LNode{
ElemType data;
struct LNode *next;
}*Link,*Position;
typedef struct{
Link head,tail;
int len;
}LinkList;
typedef LinkList polynomial; //
int cmp(term a,term b)
{// a < = >b 。 -1 0 1
if(a.expn<b.expn) return -1;
else if(a.expn==b.expn) return 0;
else return 1;
}
Status InitList(polynomial &P)
{//
Link p;
p=(Link)malloc(sizeof(LNode));//
if(p)
{
p->next=NULL;
P.head=P.tail=p;
P.len=0;
return OK;
}
else return ERROR;
}
Position GetHead(polynomial P)
{// P
return P.head;
}
Status SetCurElem(Position h,term e)
{// h e
h->data=e;
return OK;
}
Status LocateElem(LinkList P,ElemType e,Position &q,int(*cmp)(ElemType,ElemType))
{//
Link p=P.head,pp;
while(p&&(cmp(p->data,e)==-1))
{
pp=p;
p=p->next;
}
if(!p||(cmp(p->data,e)==1))
{
q=pp;
return FALSE;
}
else
{
q=p;
return TRUE;
}
}
Status MakeNode(Link &p,ElemType e)
{//
p=(Link)malloc(sizeof(LNode));
if(!p) return ERROR;
p->data=e;
return OK;
}
Status InsFirst(LinkList &P,Link h,Link s)
{//
s->next=h->next;
h->next=s;
if(h==P.tail)
P.tail=h->next;
++P.len;
return OK;
}
void CreatPolyn(polynomial &P,int m)
{// m , P
InitList(P);
Position h,q,s;
h=GetHead(P); //h P
term e;
e.coef=0.0;
e.expn=-1;
SetCurElem(h,e);//
printf(" :
");
scanf("%d",&m);
printf(" %d ( ^ )
",m);
for(int i=1;i<=m;++i)
{
scanf("%f^%d",&e.coef,&e.expn);
if(!LocateElem(P,e,q,cmp))
{
if(MakeNode(s,e)) InsFirst(P,q,s);
}//if ,
}
}
Position NextPos(Link p)
{//
return p->next;
}
ElemType GetCurElem(Link p)
{//
return p->data;
}
Status DelFirst(LinkList &L,Link h,Link &q)
{//
q=h->next;
if(q)//
{
h->next=q->next;
if(!h->next) //
L.tail=h;
L.len--;
return OK;
}//if
else return FALSE; //
}
void FreeNode(Link &p)
{// P
free(p);
p=NULL;
}
Status ListEmpty(LinkList L)
{// L
if(L.len)
return FALSE;
else return TRUE;
}
Status Append(LinkList &L,Link s)
{// L
int i=1;
L.tail->next=s;
while(s->next)
{
s=s->next;
i++;
}//while
L.tail=s;
L.len+=i;
return OK;
}
void PrintPolyn(polynomial P)
{// P
Link q;
q=P.head->next;
printf(" :");
printf("0.0^0");
while(q)
{
printf("+%.6f^%d",q->data.coef,q->data.expn);
q=q->next;
}//while
printf("
");
}
Status ClearList(LinkList &L)
{ //
Link q,p;
if(L.head!=L.tail)
{
p=q=L.head->next;
L.head->next=NULL;
while(p!=L.tail)
{
p=q->next;
free(q);
q=p;
}//while
free(q);
L.tail=L.head;
L.len=0;
}//if
return OK;
}
Status DestroyPolyn(LinkList &L)
{ // L,L
ClearList(L);
FreeNode(L.head);
L.tail=NULL;
L.len=0;
return OK;
}
void AddPolyn(polynomial &Pa,polynomial &Pb)
{// :Pa=Pa+Pb, Pb
Position ha,hb,qa,qb;
term a,b;
ha=GetHead(Pa); hb=GetHead(Pb); // ha hb Pa Pb
qa=NextPos(ha); qb=NextPos(hb); // qa qb Pa Pb ( )
while(qa&&qb)
{ // Pa Pb ha (qa!=0)
a=GetCurElem(qa); b=GetCurElem(qb); // a b
switch(cmp(a,b))
{
case -1:ha=qa; // Pa
qa=NextPos(ha); // ha qa
break;
case 0: qa->data.coef+=qb->data.coef;
// , Pa
if(qa->data.coef!=0.0)
ha=qa;
else
{
DelFirst(Pa,ha,qa);
FreeNode(qa);
}// Pa
DelFirst(Pb,hb,qb);
FreeNode(qb);
qb=NextPos(hb);
qa=NextPos(ha);
break;
case 1: DelFirst(Pb,hb,qb); // Pb
InsFirst(Pa,ha,qb);
ha=ha->next;
qb=NextPos(hb);
break;
}
}
if(!ListEmpty(Pb))
{
Pb.tail=hb;
Append(Pa,qb); // Pb
}
DestroyPolyn(Pb); // Pb
}
int main()
{
polynomial Pa,Pb;
int m;
CreatPolyn(Pa,m);
PrintPolyn(Pa);
printf(" %d
",Pa.len);
CreatPolyn(Pb,m);
PrintPolyn(Pb);
printf(" %d
",Pb.len);
AddPolyn(Pa,Pb);
PrintPolyn(Pa);
printf(" %d
",Pa.len);
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.