| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1004 人关注过本帖
标题:[求助]如何把這題的多項式相加改成相乘!!
只看楼主 加入收藏
y2000j
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2005-10-23
收藏
 问题点数:0 回复次数:3 
[求助]如何把這題的多項式相加改成相乘!!
#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();
}
搜索更多相关主题的帖子: 相乘 相加 
2005-10-23 16:59
jdytz
Rank: 2
等 级:新手上路
威 望:3
帖 子:104
专家分:0
注 册:2005-4-24
收藏
得分:0 
求求你了,别弄这么胖的程序了。
一看就头痛

``` 奔放潇洒活着我的自由
2005-10-24 00:50
y2000j
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2005-10-23
收藏
得分:0 
主要是這段
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;
}
2005-10-24 08:35
y2000j
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2005-10-23
收藏
得分:0 
solved~~~!!
2005-10-24 18:26
快速回复:[求助]如何把這題的多項式相加改成相乘!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021884 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved