一元任意多项式四则运算求解

发布于 2021-10-09  2050 次阅读


#include<bits/stdc++.h>
using namespace std;

class poly{
public:
    map<double,double> coe;//指数&系数对
    poly()= default;
    poly(poly& o){this->coe=o.coe;}
    void add(double k, double e){//k系数,e指数
       if(coe.count(e))coe[e]+=k;
       else coe[e]=k;
       if(coe.find(e)!=coe.end()&&coe[e]==0)coe.erase(e);
    }//添加一项
    poly(vector<double> k,vector<double> e){//k系数,e指数
        for(int i=0;i<k.size();++i)add(k[i],e[i]);
    }//批量添加
    void batch_add(vector<double> k,vector<double> e){//k系数,e指数
        for(int i=0;i<k.size();++i)add(k[i],e[i]);
    }//批量添加
    void clear(){coe.clear();}//清空多项式
    int item_num() const{return coe.size();}//获得项数
    double calc(double x){
        double res=0;
        for(const auto&each:coe)
            res+=each.second*pow(x,each.first);
        return res;
    }//获得对应x的多项式取值,返回值类型根据传入x类型确定
    string to_str(double x){
        string res=std::to_string(round(x*100)/100);
        return string(res.begin(),res.begin()+res.find('.')+3);
    }
    string to_string(){
        string res;
        for(auto it=coe.rbegin();it!=coe.rend();++it) {
            auto each=*it;
            if (res.empty() && each.second > 0 && each.first != 0)
                res += to_str(each.second) + "x^" + to_str(each.first);
            else if (each.second > 0 && each.first != 0)
                res += "+" + to_str(each.second) + "x^" + to_str(each.first);
            else if (each.second < 0 && each.first != 0)
                res += to_str(each.second) + "x^" + to_str(each.first);
            else if (res.empty() && each.second > 0 && each.first == 0)
                res += to_str(each.second);
            else if (each.second > 0 && each.first == 0)
                res += "+" + to_str(each.second);
            else res += to_str(each.second);
        }
        return res==""?"0":res;
    }
    poly operator+(poly o){
        poly new_poly(*this);
        for(const auto&each:o.coe)
            new_poly.add(each.second,each.first);
        return new_poly;
    }
    void operator+=(poly o){*this=*this+o;}//以上重载加法
    poly operator-(poly o){
        poly new_poly(*this);
        for(const auto&each:o.coe)
            new_poly.add(-each.second,each.first);
        return new_poly;
    }
    void operator-=(poly o){*this=*this-o;}//以上重载减法
    poly operator*(poly o){
        poly new_poly;
        for(const auto&each1:this->coe)
            for(const auto&each2:o.coe)
                new_poly.add(each1.second*each2.second,each1.first+each2.first);
        return new_poly;
    }
    void operator*=(poly o){*this=*this*o;}//以上重载乘法
    poly operator/(poly o){
        poly new_poly(*this),res_poly;
        while(new_poly.coe.size()&&new_poly.coe.rbegin()->first>=o.coe.rbegin()->first){
            while(new_poly.coe.rbegin()->second==0)
                new_poly.coe.erase(new_poly.coe.rbegin()->first);
            poly tool_poly({new_poly.coe.rbegin()->second/o.coe.rbegin()->second},
                           {new_poly.coe.rbegin()->first-o.coe.rbegin()->first});
            res_poly+=tool_poly,tool_poly*=o,new_poly-=tool_poly;
        }
        return res_poly;
    }
    void operator/=(poly o){*this=*this/o;}//以上重载除法
    poly operator%(poly o){
        poly new_poly,tool_poly;
        new_poly=*this/o,tool_poly=new_poly*o;
        new_poly=*this-tool_poly;
        return new_poly;
    }
    void operator%=(poly o){*this=*this%o;}//以上重载取模
};

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    poly a, b, res;
    cout<<"请输入多项式a(按格式项数,系数数组,指数数组),如多项式1+2x+3x^2请输入3 1 2 3 0 1 2"<<endl;
    int n;cin>>n;
    vector<double> a_k(n),a_e(n);
    for(int i=0;i<n;++i)cin>>a_k[i];
    for(int i=0;i<n;++i)cin>>a_e[i];
    a.batch_add(a_k,a_e);
    cout<<"请输入多项式b"<<endl;
    cin>>n;
    vector<double> b_k(n),b_e(n);
    for(int i=0;i<n;++i)cin>>b_k[i];
    for(int i=0;i<n;++i)cin>>b_e[i];
    b.batch_add(b_k,b_e);
    res=a+b;
    cout<<"ab和为"<<res.to_string()<<endl;
    res=a-b;
    cout<<"ab差为"<<res.to_string()<<endl;
    res=a*b;
    cout<<"ab积为"<<res.to_string()<<endl;
    res=a/b;
    cout<<"ab商为"<<res.to_string()<<endl;
    res=a%b;
    cout<<"a模b余数为"<<res.to_string()<<endl;
    return 0;
}

华科菜鸡计科学生