template class Type void Intersection( Seqlist <Type> la, Seqlist<Type>& lB) ∥求顺序表LA与LB的共同元素,结果在LA中 int n=LA Length() int m=LB Length( int i=0 while (i<n) pex= LA. Get(i);∥从LA中取一元素 intk= LB Find(x)∥在LB中查找该元素 if(k==-1) ∥未找到 LA. Remove(i);n-;}∥则从LA中删除该元素 elsei++ 2021220
2021/2/20 21 template < class Type > void Intersection( Seqlist <Type> & LA,Seqlist <Type> & LB) // 求顺序表LA 与 LB 的共同元素,结果在LA 中 { int n = LA.Length( ); int m = LB.Length( ); int i = 0; while ( i < n ) { Type x = LA.Get( i ); // 从 LA 中取一元素 int k = LB.Find( x ); // 在 LB 中查找该元素 if ( k== - 1) // 未找到 { LA.Remove( i ); n--;} // 则从 LA 中删除该元素 else i ++; } }
多项式及其运算 A(x=aoxax'ta2x+ an-ixanx=2ax B(x)-box+bx+bix++bn- X+bnx=2 bix n A(x)+B(x)=2(ai+bix 实际应用中,上述一元多项式中的很多系数有可能为零, 即为稀疏多项式,如下式所示: P(x)=1.2+51.3x+3.7x0 P(x)∑cx C1=51.3e1=50 0 c2=3.7e2=101 2021220
2021/2/20 22 多项式及其运算 A(x)=a0x + a1x +a2x +…+an-1x + anx = B(x)=b0x + b1x +b2x +…+bn-1x +bnx = A(x)+B(x)= 实际应用中,上述一元多项式中的很多系数有可能为零, 即为稀疏多项式,如下式所示: Σ aix i=0 n 0 1 2 n-1 n i 0 1 2 n-1 n Σ bix i i=0 Σ (ai+bi)x n i=0 i P (x)=1.2+51.3x +3.7x 5 0 50 101 P(x)=Σci x i=0 n ei c0=1.2 e0=0 c1=51.3 e1=50 c2=3.7 e2=101
多项式的表示 对于稀疏多项式,采用二元组表示法可以大大节省存储 (1)将稀疏多项式的每一项用二元组<Ci,ei>表示 客体的表示方法 (2)用一顺序表(一维数组)来存放上述的二元组 数据的组织方法 系数C0C1 C Cn 指数eoei e e 2021220 23
2021/2/20 23 多项式的表示 对于稀疏多项式,采用二元组表示法可以大大节省存储 空间: (1)将稀疏多项式的每一项用二元组< ci , ei >表示 ——客体的表示方法 (2)用一顺序表(一维数组)来存放上述的二元组 ——数据的组织方法 c0 c1 ci cn e0 e1 ei en 系数 指数 . . . . . . . . . . . . .
多项式二元组的类定义—客体的表示方法 class polynomial;∥多项式类的前视声明 class tern∥多项式中项(二元组)的类定义 friend polynomial;∥定义 Polynomial类为term类的友元类 private float coef;∥/系数 Int exp;∥指数 2021220 24
2021/2/20 24 多项式二元组的类定义——客体的表示方法 class Polynomial; // 多项式类的前视声明 class term // 多项式中项(二元组)的类定义 { friend Polynomial; // 定义 Polynomial 类为 term 类的友元类 private : float coef; // 系数 int exp; // 指数 }
多项式的类定义—数据的表示方法 class polynomial public ∥成员函数声明(构造、释构、相加等函数) private int maxTerms 共享空间(顺序表)的最大项数 static term termArray MaxTerms] /1放二元组的数组,存放多个多项式的共享空间 static int free ∥共享空间中自由空间之起始下标 int start, finish;∥多项式在共享空间中的首、尾下标 2021220
2021/2/20 25 多项式的类定义——数据的表示方法 class Polynomial { public : . . . // 成员函数声明(构造、释构、相加等函数) private : int MaxTerms ; //共享空间(顺序表)的最大项数 static term termArray[MaxTerms]; //存放二元组的数组,存放多个多项式的共享空间 static int free ; // 共享空间中自由空间之起始下标 int start , finish ; // 多项式在共享空间中的首、尾下标 }