[求助]如何把這題的多項式相加改成相乘!!
#include <iostream>using std::cin;
using std::cout;
using std::endl;
#include <cstdlib>
//============================================================================
// Term 類別的定義與實作
//============================================================================
class Term{
private:
double coef;
int exp;
public:
Term();
Term(double c, int e);
double getCoef();
void setCoef(double c);
int getExp();
void setExp(int e);
};
Term::Term(){
coef=0;
exp=0;
}
Term::Term(double c, int e){
coef=c;
exp=e;
}
double Term::getCoef(){
return coef;
}
void Term::setCoef(double c){
coef=c;
}
int Term::getExp(){
return exp;
}
void Term::setExp(int e){
exp=e;
}
//============================================================================
// TermArray 類別的定義與實作
//============================================================================
class TermArray{
private:
Term *tArray;
int size;
int freePos;
public:
TermArray();
TermArray(int size);
void addTerm(Term aTerm);
int getFreePos();
int getSize();
double getTermCoef(int index);
int getTermExp(int index);
void printInfo(int start, int finish);
};
TermArray::TermArray(){
size=20;
tArray=new Term[size];
freePos=0;
}
TermArray::TermArray(int s){
size=s;
tArray=new Term[size];
freePos=0;
}
void TermArray::addTerm(Term aTerm){
if(freePos>=size){
cout<<"Too many terms !!"<<endl;
exit(1);
}else{
tArray[freePos++]=aTerm;
}
}
int TermArray::getFreePos(){
return freePos;
}
int TermArray::getSize(){
return size;
}
double TermArray::getTermCoef(int index){
return tArray[index].getCoef();
}
int TermArray::getTermExp(int index){
return tArray[index].getExp();
}
void TermArray::printInfo(int start, int finish){
for(int i=start;i<=finish;i++){
cout<<"("<<tArray[i].getCoef()<<", "<<tArray[i].getExp()<<")"<<endl;
}
}
//============================================================================
// Polynomial 類別的定義與實作
//============================================================================
class Polynomial{
private:
char theName;
int startPos, finishPos;
TermArray *tStorage;
public:
Polynomial(char ch, TermArray* array);
void getInput();
void newTerm(double c, int e);
int getStartPos();
int getFinishPos();
char termComp(int e1, int e2);
void printInfo();
Polynomial* addPoly(Polynomial* ptr);
};
Polynomial::Polynomial(char ch, TermArray* array){
theName=ch;
tStorage=array;
startPos=tStorage->getFreePos();
finishPos=startPos-1;
}
void Polynomial::getInput(){
cout<<"請依降冪順序輸入多項式 "<<theName<<" 的資料項內容:";
cout<<"freePos = "<<tStorage->getFreePos()<<endl;
cout<<"------------------------------------------------------"<<endl;
double c; int e; cin>>c>>e;
while((c!=0)||(e!=0)){
newTerm(c, e);
cin>>c>>e;
}
}
void Polynomial::newTerm(double c, int e){
Term term(c, e);
tStorage->addTerm(term);
finishPos++;
}
int Polynomial::getStartPos(){
return startPos;
}
int Polynomial::getFinishPos(){
return finishPos;
}
char Polynomial::termComp(int e1, int e2){
if(e1==e2){
return '=';
}else if(e1>e2){
return '>';
}else{
return '<';
}
}
void Polynomial::printInfo(){
cout<<endl<<"多項式 "<< theName <<" 的內容如下:";
cout<<"startPos = "<<startPos<<", finishPos = "<<finishPos<<endl;
cout<<"------------------------------------------------------"<<endl;
tStorage->printInfo(startPos, finishPos);
cout<<endl;
}
//
// 多項式加法運算, 時間複雜度:O(n+m), n 和 m 分別為 A、B 的非零項個數
//----------------------------------------------------------------------------
Polynomial* Polynomial::addPoly(Polynomial* ptr){
Polynomial *C=new Polynomial('C', tStorage);
int p=this->getStartPos(), q=ptr->getStartPos();
// 如果多項式 A 與 B 皆有資料項尚未處理
while((p<=this->getFinishPos())&&(q<=ptr->getFinishPos())){
switch(this->termComp(tStorage->getTermExp(p), tStorage->getTermExp(q))){
case '=':
C->newTerm(tStorage->getTermCoef(p)+tStorage->getTermCoef(q), tStorage->getTermExp(p));
p++; q++;
break;
case '>':
C->newTerm(tStorage->getTermCoef(p), tStorage->getTermExp(p));
p++;
break;
case '<':
C->newTerm(tStorage->getTermCoef(q), tStorage->getTermExp(q));
q++;
break;
}
}
// 加入多項式 A 中剩餘的未處理資料項
for(;p<=this->getFinishPos();p++){
C->newTerm(tStorage->getTermCoef(p), tStorage->getTermExp(p));
}
// 加入多項式 B 中剩餘的未處理資料項
for(;q<=this->getFinishPos();q++){
C->newTerm(tStorage->getTermCoef(q), tStorage->getTermExp(q));
}
return C;
}
void main(){
TermArray *tStorage=new TermArray(20);
Polynomial *A=new Polynomial('A', tStorage);
A->getInput();
A->printInfo();
Polynomial *B=new Polynomial('B', tStorage);
B->getInput();
B->printInfo();
Polynomial *C=A->addPoly(B);
C->printInfo();
}