IDE:Visual Studio 2019 声明:为了方便书写代码,用到了C++的引用调用特性和iostream作为输入输出,读者可以使用指针调用和scanf_s和print语句实现相同效果 tips:如果只是为了实现函数,很多方程式不需要写得,我感觉debug中最经常出现的错误就是尾指针->next没有指向NULL导致的。有疑问可以在下面交流,我会尽量回复的
头文件heads.h
#pragma once
#include "stdio.h"
#include "iostream"
#define OK 1
#define TRUE 1
#define FALSE 0
#define ERROR 0
typedef int Status
;
using namespace std
;
typedef struct{
float coef
;
int expn
;
}term
,ElemType
;
typedef struct LNode
{
ElemType data
;
struct LNode
* next
;
}*Link
,*Position
;
typedef struct {
Link head
, tail
;
int length
;
}LinkList
;
头文件functions.h
#include "head.h"
Status
MakeNode(Link
& p
, ElemType e
) {
p
= (Link
)malloc(sizeof(LNode
));
if (!p
)return ERROR
;
p
->data
= e
;
p
->next
= NULL;
return OK
;
}
void FreeNode(Link
* p
) {
free(*p
);
*p
= NULL;
}
bool Empty(LinkList L
) {
if (L
.length
== 0)return TRUE
;
return FALSE
;
}
Status
visit(ElemType e
) {
if (e
.coef
> 0 && e
.coef
!= 1 && e
.expn
!= 0)
{
if (e
.expn
> 0)
cout
<< e
.coef
<< "X^" << e
.expn
;
else
cout
<< e
.coef
<< "X^(" << e
.expn
<< ")";
}
else if (e
.coef
< 0 && e
.expn
!= 0)
{
if (e
.expn
> 0)
cout
<< "(" << e
.coef
<< ")X^" << e
.expn
;
else
cout
<< "(" << e
.coef
<< ")X^(" << e
.expn
<< ")";
}
else if (e
.coef
== 1 && e
.expn
!= 0)
{
if (e
.expn
> 0)
cout
<< "X^" << e
.expn
;
else
cout
<< "X^(" << e
.expn
<< ")";
}
else if (e
.expn
== 0 && e
.coef
!= 0)
cout
<< e
.coef
<< "+";
else
cout
<< "";
return OK
;
}
Status
Inislist(LinkList
& L
) {
Link p
= (Link
)malloc(sizeof(LNode
));
if (!p
)return ERROR
;
L
.head
= p
;
L
.tail
= p
;
p
->next
= NULL;
L
.length
= 0;
return OK
;
}
Status
Clearlist(LinkList
& L
) {
Link p
= L
.head
->next
,q
;
if (!p
) return ERROR
;
while (p
) {
q
= p
;
p
= p
->next
;
FreeNode(&q
);
}
L
.tail
= L
.head
;
L
.length
= 0;
return OK
;
}
Status
Destory(LinkList
& L
) {
Clearlist(L
);
FreeNode(&L
.head
);
return OK
;
}
Status
DelFirst(LinkList
& L
, Link
& q
) {
Link p
= L
.head
;
if (!p
->next
) return ERROR
;
q
= p
->next
;
if (!q
->next
) {
L
.tail
= p
;
}
else {
p
->next
= q
->next
;
}
L
.length
--;
return OK
;
}
Status
InsFirst(LinkList
& L
, Link S
) {
Link p
= L
.head
;
if (!p
->next
) {
p
->next
= S
;
L
.tail
= S
;
}
else {
S
->next
= p
->next
;
p
->next
= S
;
}
L
.length
++;
return OK
;
}
Status
Append(LinkList
& L
, Link s
) {
L
.tail
->next
= s
;
L
.length
++;
while (s
->next
) {
s
= s
->next
;
L
.length
++;
}
L
.tail
= s
;
L
.tail
->next
= NULL;
return OK
;
}
Status
InsLast(LinkList
& L
,Link s
) {
L
.tail
->next
= s
;
L
.length
++;
L
.tail
= s
;
s
->next
= NULL;
return OK
;
}
Status
Insfirst(LinkList
& L
, Link
& h
, Link
& s
) {
s
->next
= h
->next
;
h
->next
= s
;
if (!s
->next
)L
.tail
= s
;
L
.length
++;
return OK
;
}
Status
Remove(LinkList
& L
, Link
& q
) {
if (L
.head
= L
.tail
)return ERROR
;
Link p
= L
.head
;
q
= L
.tail
;
while (p
->next
!=q
) {
p
= p
->next
;
}
L
.tail
= p
;
p
->next
= NULL;
return OK
;
}
Status
Create(LinkList
& L
) {
Link q
;
float a
;
int b
;
Inislist(L
);
cin
>> a
;
while (a
) {
cin
>> b
;
ElemType e
;
e
.coef
= a
;
e
.expn
= b
;
MakeNode(q
, e
);
cin
>> a
;
InsLast(L
, q
);
}
return OK
;
}
Status
Traverse(LinkList L
, Status(*visit
)(ElemType
)) {
Link p
= L
.head
->next
;
if (L
.length
== 0)cout
<< "0";
while (p
){
visit(p
->data
);
p
= p
->next
;
if (p
)cout
<< "+";
}
cout
<< endl
;
return OK
;
}
Status
Compare(Link p
, Link q
) {
if (p
->data
.expn
> q
->data
.expn
)return 1;
if (p
->data
.expn
== q
->data
.expn
)return 0;
if (p
->data
.expn
< q
->data
.expn
)return -1;
return OK
;
}
Status
AddPoly(LinkList
& P
, LinkList
& Q
) {
Link p
= P
.head
, q
= Q
.head
, pa
= p
->next
, qa
= q
->next
;
if (P
.length
== 0 && Q
.length
== 0)return ERROR
;
while (pa
&& qa
) {
switch (Compare(pa
, qa
))
{
float sum
;
case -1:
pa
= pa
->next
; p
= p
->next
;
break;
case 0:
sum
= pa
->data
.coef
+ qa
->data
.coef
;
q
->next
= qa
->next
;
if (sum
!= 0) {
pa
->data
.coef
= sum
;
}
else {
p
->next
= pa
->next
;
FreeNode(&pa
);
pa
= pa
->next
;
P
.length
--;
}
FreeNode(&qa
);
qa
= q
->next
;
Q
.length
--;
break;
case 1:
q
->next
= qa
->next
;
p
->next
= qa
;
qa
->next
= pa
;
qa
= q
->next
;
p
= p
->next
;
P
.length
++;
Q
.length
--;
break;
}
}
if (qa
) {
q
->next
= NULL;
Append(P
, qa
);
}
Q
.tail
= q
;
Destory(Q
);
return OK
;
}
调用文件main.cpp
#include "head.h"
#include "functions.h"
int main(void) {
LinkList L
,M
;
Create(L
);
Traverse(L
, visit
);
Create(M
);
Traverse(M
, visit
);
AddPoly(L
, M
);
Traverse(L
,visit
);
}
转载请注明原文地址: https://lol.8miu.com/read-6800.html