|
试题六
阅读下列程序说明和 C++ 代码,将应填入__(n)__处的字句写在答卷的对应栏内。
[程序 6 说明]
本程序实现两个多项式的乘积运算。多项式的每一项由类 Item 描述,而多项式由类 List 描述。类 List 的成员函数有:
createList():创建按指数降序链接的多项式链表,以表示多项式。
reverseList():将多项式链表的表元链接顺序颠倒。
multiplyList( List L1,List 12 )计算多项式 L1 和多项式 L2 的乘积多项式。
[程序6]
#include <iostream.h>
class List;
class ltem {
friend class List;
private:
double quot;
int exp;
Item *next;
public:
Item( double_quot,int_exp)
{__(1)__;}
};
class List{
private:
Item *list;
public:
List(){list:NULL;}
void reverseList();
void multiplyList(List L1,List L2);
void createList();
};
void List::createList()
{ Item *p, *U, *pre;
int exp;
double quot;
1ist = NULL;
while __(1)__ {
cout << "输入多项式中的一项(系数、指数):" << endl;
cin >> quot >> exp:
if ( exp<0 ) break; //指数小于零,结束输入
if ( quot=0 ) continue;
p = list;
while ( __(2)__ ) { //查找插入点
pre = p;p = p->next;}
if ( p != NULL && exp = p->exp ) { p->quot += quot;continue;}
u = __(3)__ ;
if (p == list) list = u;
else pre->next = u;
u ->next = p;}
}
void List :: reverseList()
{ Item *p,*u;
if ( 1ist=NULL ) return;
p = list ->next;list -> next = NULL;
while ( p != NULL) {
u = p -> next;p ->next = list;
list = p; p = u;}
}
void List :: multiplyList ( List L1,List L2 )
{ Item *pLl,*pL2,*u;
int k,maxExp;
double quot;
maxExp = __(4)__;
L2.reverseList(); list=NULL
for ( k = maxExp;k >= 0;k-- ){
pLl = L1.1ist;
while ( pLl != NULL && pLl -> exp > k ) pLl = pLl ->next;
pL2 = L2.1ist;
while (pL2 NULL && __(5)__ pL2 = pL2 -> next;
quot = 0.0;
while (pLl != NULL && pL2 != NULL){
if(pLl—>exp+pL2->exp’:k) {
__(6)__ ;
pLl = pLl -> next;pL2 = pL2 -> next;
} else if ( pLl -> exp + pL2 -> exp > k ) pLl = pLl -> next;
else pL2 = pL2 -> next;
}
if ( quot !=0.0 ){
u = new ltem( quot,k );
u -> next = list;list = u;}
}
reverseList(); L2.reverseList():
}
void main()
{ List L1,L2,L;
cout << "创建第一个多项式链表\n"; L1.createList();
cout << "创建第二个多项式链表\n"; L2.createList();
L.multiplyList ( L1,L2);
}
|
|