|
常用链接
留言簿(30)
随笔分类(128)
随笔档案(169)
文章分类
文章档案(3)
others
something special
经典的c/c++
搜索
积分与排名
最新评论

阅读排行榜
评论排行榜
Powered by: 博客园
模板提供:沪江博客
|
|
|
|
|
发新文章 |
|
|
1 /**//******************************************************************** 2 created: 2005/12/21 3 created: 21:12:2005 10:49 4 filename: poly.h 5 author: Liu Qi 6 7 purpose: 多项式相关操作的接口与实现 8 *********************************************************************/ 9 #ifndef POLY_H 10 #define POLY_H 11 12 13 #include <stdio.h> 14 #include <assert.h> 15 #include "../sllist.h" 16 17 18 typedef List PolynomialList; 19 20 /**//*=========================================================================== 21 * Function name: PolynomialAdd 22 * Parameter: lhs:多项式 23 * Precondition: NULL != lhs && NULL != rhs, NULL != lhs->Next && NULL != rhs->Next 24 * Description: 返回一个新的多项式头指针 25 * Return value: 26 * Author: Liu Qi, [12/21/2005] 27 ===========================================================================*/ 28 PolynomialList PolynomialAdd(PolynomialList lhs, PolynomialList rhs) 29  { 30 Position lhsPos = lhs->Next; //指向第一个多项式的第一个元素 31 Position rhsPos = rhs->Next; //指向第二个多项式的第一个元素 32 PolynomialList retPolynomialList= SLL_Create(); 33 ElemType Elem; 34 35 assert( NULL != lhs && NULL != rhs ); 36 assert( NULL != lhs->Next && NULL != rhs->Next ); 37 38 while (NULL != lhsPos && NULL != rhsPos) 39 { 40 //第一个多项式的指数大,复制第一个多项式的一个节点插入到retPolynomialList头部 41 if ((lhsPos->Element.HighPower) > (rhsPos->Element.HighPower)) 42 { 43 Elem.coef = lhsPos->Element.coef; 44 Elem.HighPower = lhsPos->Element.HighPower; 45 SLL_PushFront(Elem, retPolynomialList); 46 lhsPos = SLL_NextPos(lhsPos); 47 } 48 else if ((lhsPos->Element.HighPower) < (rhsPos->Element.HighPower)) 49 { 50 Elem.coef = rhsPos->Element.coef; 51 Elem.HighPower = rhsPos->Element.HighPower; 52 SLL_PushFront(Elem, retPolynomialList); 53 rhsPos = SLL_NextPos(rhsPos); 54 } 55 else //指数相等 56 { 57 Elem.coef = lhsPos->Element.coef + rhsPos->Element.coef; 58 Elem.HighPower = lhsPos->Element.HighPower; 59 SLL_PushFront(Elem, retPolynomialList); 60 lhsPos = SLL_NextPos(lhsPos); 61 rhsPos = SLL_NextPos(rhsPos); 62 } 63 } 64 65 while (NULL != lhsPos) 66 { 67 Elem.coef = lhsPos->Element.coef; 68 Elem.HighPower = lhsPos->Element.HighPower; 69 SLL_PushFront(Elem, retPolynomialList); 70 lhsPos = SLL_NextPos(lhsPos); 71 } 72 73 while (NULL != rhsPos) 74 { 75 Elem.coef = rhsPos->Element.coef; 76 Elem.HighPower = rhsPos->Element.HighPower; 77 SLL_PushFront(Elem, retPolynomialList); 78 rhsPos = SLL_NextPos(rhsPos); 79 } 80 81 return retPolynomialList; 82 } 83 84 #endif 85 测试代码:
 
#include <stdio.h>
#include "poly.h"


void PrintPolynomial(PolynomialList L)
  {
Position pos = L->Next;
assert (NULL != L);
for ( ; NULL != pos; pos = pos->Next)
 {
printf("%dX^%d + ", pos->Element.coef, pos->Element.HighPower);
}

printf("\n");
}


int main()
  {
Polynomial p;

PolynomialList p1 = SLL_Create();
PolynomialList p2 = SLL_Create();
PolynomialList result = NULL;

// P1(X) = X^2 + 2X + 3
p.coef = 3;
p.HighPower = 0;
SLL_PushFront(p, p1);
p.coef = 2;
p.HighPower = 1;
SLL_PushFront(p, p1);
p.coef = 1;
p.HighPower = 2;
SLL_PushFront(p, p1);
PrintPolynomial(p1);

// P2(X) = 3X^3 + 10X + 6
p.coef = 6;
p.HighPower = 0;
SLL_PushFront(p, p2);
p.coef = 10;
p.HighPower = 1;
SLL_PushFront(p, p2);
p.coef = 3;
p.HighPower = 3;
SLL_PushFront(p, p2);
PrintPolynomial(p2);

result = PolynomialAdd(p1, p2);

PrintPolynomial(result);

SLL_DeleteList(p1);
SLL_DeleteList(p2);
SLL_DeleteList(result);
return 0;
}
|
|