问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
你好,欢迎来到懂视!登录注册
当前位置: 首页 - 正文

C++数据结构设计多项式运算有没有大神会做

发布网友 发布时间:2023-07-13 05:40

我来回答

2个回答

热心网友 时间:2024-11-04 17:23

#include <iostream>
#include <sstream>
#include <string>
#include <list>
#include <cmath>
#include <cctype>
using namespace std;
bool IsZero(double num){return fabs(num) < 0.0000001;}
class Polynomial
{
public:
    Polynomial();
    Polynomial(double coefs[], int exps[], int size);
    Polynomial(const Polynomial&);
    ~Polynomial();
    Polynomial& operator=(const Polynomial&);
    int degree() const;
    double evaluate(double x) const;
    Polynomial operator+(const Polynomial&) const;
    Polynomial operator-(const Polynomial&) const;
    Polynomial operator*(const Polynomial&) const;
    Polynomial& operator+=(const Polynomial&); 
    Polynomial& operator-=(const Polynomial&); 
    Polynomial& operator*=(const Polynomial&);
    ostream& doWrite(ostream&) const;
    int fromString(string&);
private:
    class Term{
    public:
        Term(double c, int e):coef(c), exp(e){}
        double coef;
        int exp;
    };
    list<Term> terms;
    void RemoveZeros();
};
ostream& operator<<(ostream& out, const Polynomial& poly)
{
    return poly.doWrite(out);
}
Polynomial::Polynomial()
{
    terms.push_back(Term(0, 0));
}
Polynomial::Polynomial(double coefs[], int exps[], int size)
{
    for(int i = 0; i < size; i++)
        terms.push_back(Term(coefs[i], exps[i]));
    if(size == 0)
        terms.push_back(Term(0, 0));
}
Polynomial::Polynomial(const Polynomial& poly)
{
    terms = poly.terms;
}
Polynomial::~Polynomial()
{
}
Polynomial& Polynomial::operator=(const Polynomial& poly)
{
    terms = poly.terms;
    return *this;
}
void Polynomial::RemoveZeros()
{
    list<Term>::iterator it;
    for(it = terms.begin(); it != terms.end();)
    {
        if(IsZero(it->coef))
            it = terms.erase(it);
        else
            ++it;
    }
    if(terms.empty())
    {
        terms.push_back(Term(0, 0));
    }
}
int Polynomial::degree() const
{
    return terms.front().exp;
}
double Polynomial::evaluate(double x) const
{
    double y = 0.0;
    list<Term>::const_iterator it;
    for(it = terms.begin(); it != terms.end(); it++)
    {
        y += it->coef * pow(x, it->exp);
    }
    return y;
}
Polynomial Polynomial::operator+(const Polynomial& b) const
{
    Polynomial c;
    list<Term>::const_iterator ai, bi;
    ai = this->terms.begin(), bi = b.terms.begin();
    while(ai != this->terms.end() && bi != b.terms.end())
    {
        if(ai->exp > bi->exp){
            c.terms.push_back(*ai);
            ai++;
        }
        else if(ai->exp < bi->exp){
            c.terms.push_back(*bi);
            bi++;
        }
        else{
            c.terms.push_back(Term(ai->coef + bi->coef, ai->exp));
            ai++, bi++;
        }
    }
    if(ai != this->terms.end())
    {
        for(; ai != this->terms.end(); ai++)
            c.terms.push_back(*ai);
    }
    else if(bi != b.terms.end())
    {
        for(; bi != this->terms.end(); bi++)
            c.terms.push_back(*bi);
    }
    c.RemoveZeros();
    return c;
}
Polynomial Polynomial::operator-(const Polynomial& b) const
{
    Polynomial c;
    list<Term>::const_iterator ai, bi;
    ai = this->terms.begin(), bi = b.terms.begin();
    while(ai != this->terms.end() && bi != b.terms.end())
    {
        if(ai->exp > bi->exp){
            c.terms.push_back(*ai);
            ai++;
        }
        else if(ai->exp < bi->exp){
            c.terms.push_back(Term(-bi->coef, bi->exp));
            bi++;
        }
        else{
            c.terms.push_back(Term(ai->coef - bi->coef, ai->exp));
            ai++, bi++;
        }
    }
    if(ai != this->terms.end())
    {
        for(; ai != this->terms.end(); ai++)
            c.terms.push_back(*ai);
    }
    else if(bi != b.terms.end())
    {
        for(; bi != this->terms.end(); bi++)
            c.terms.push_back(Term(-bi->coef, bi->exp));
    }
    c.RemoveZeros();
    return c;
}
Polynomial Polynomial::operator*(const Polynomial& b) const
{
    Polynomial c;
    list<Term>::const_iterator ai, bi;
    list<Term>::iterator ci;
    for(ai = this->terms.begin(); ai != this->terms.end(); ai++){
        for(bi = b.terms.begin(); bi != b.terms.end(); bi++){
            Term t(ai->coef * bi->coef, ai->exp + bi->exp);
            for(ci = c.terms.begin(); ci != c.terms.end();){
                if(t.exp > ci->exp)
                    ci++;
                else
                    break;
            }
            if(ci == c.terms.end())
                c.terms.push_back(t);
            else if(t.exp == ci->exp){
                ci->coef += t.coef;
            }
            else{
                c.terms.insert(ci, t);
            }
        }
    }
    c.terms.reverse();
    c.RemoveZeros();
    return c;
}
Polynomial& Polynomial::operator+=(const Polynomial& b)
{
    list<Term>::iterator ai;
    list<Term>::const_iterator bi;
    ai = this->terms.begin(), bi = b.terms.begin();
    while(ai != this->terms.end() && bi != b.terms.end())
    {
        if(ai->exp > bi->exp){
            ai++;
        }
        else if(ai->exp < bi->exp){
            this->terms.insert(ai, *bi);
            bi++;
        }
        else{
            ai->coef += bi->coef;
            ai++, bi++;
        }
    }
    if(bi != b.terms.end())
    {
        for(; bi != this->terms.end(); bi++)
            this->terms.push_back(*bi);
    }
    RemoveZeros();
    return *this;
}
Polynomial& Polynomial::operator-=(const Polynomial& b)
{
    list<Term>::iterator ai;
    list<Term>::const_iterator bi;
    ai = this->terms.begin(), bi = b.terms.begin();
    while(ai != this->terms.end() && bi != b.terms.end())
    {
        if(ai->exp > bi->exp){
            ai++;
        }
        else if(ai->exp < bi->exp){
            this->terms.insert(ai, *bi);
            bi++;
        }
        else{
            ai->coef -= bi->coef;
            ai++, bi++;
        }
    }
    if(bi != b.terms.end())
    {
        for(; bi != this->terms.end(); bi++)
            this->terms.push_back(*bi);
    }
    RemoveZeros();
    return *this;
}
Polynomial& Polynomial::operator*=(const Polynomial& b)
{
    *this = *this * b;
    return *this;
}
ostream& Polynomial::doWrite(ostream& out) const
{
    list<Term>::const_iterator it;
    if(terms.empty())
    {
        out << "0";
        return out;
    }
    for(it = terms.begin(); it != terms.end(); it++)
    {
        if(it != terms.begin() && it->coef > 0)
            out << "+";
        if(!IsZero(fabs(it->coef) - 1.0) || it->exp == 0)
            out << it->coef;
        if(IsZero(it->coef + 1.0) && it->exp != 0)
            out << "-";
        if(it->exp >= 1)
            out << "x";
        if(it->exp > 1)
            out << "^" << it->exp;
    }
    return out;
}
int Polynomial::fromString(string& str)
{
    const int ERR = -1, END = 0, NEWTERM = 1, COEFF = 2, XVAR = 3, POWOP = 4, EXP = 5;
    char ch;
    double a = 1.0;
    int b = 1, flag = 1;
    int state = NEWTERM;
    terms.clear();
    stringstream ss(str);
    while(ss.get(ch) && state != ERR)
    {
        if(isdigit(ch))
        {
            ss.putback(ch);
            if(state == NEWTERM){
                ss >> a;
                state = COEFF;
            }
            else if(state == POWOP){
                ss >> b;
                state = EXP;
            }
            else
                state = ERR;
        }
        else if(ch == '^')
        {
            if(state == XVAR)
                state = POWOP;
            else
                state = ERR;
        }
        else if(ch == 'x')
        {
            if(state == COEFF)
                state = XVAR;
            else if(state == NEWTERM)
                state = XVAR;
            else
                state = ERR;
        }
        else if(ch == '+' || ch == '-')
        {
            if(state == NEWTERM)
                state = NEWTERM;
            else if(state == COEFF){
                terms.push_back(Term(a * flag, 0));
                state = NEWTERM;
            }
            else if(state == XVAR){
                terms.push_back(Term(a * flag, 1));
                state = NEWTERM;
            }
            else if(state == EXP){
                terms.push_back(Term(a * flag, b));
                state = NEWTERM;
            }
            else
                state = ERR;
            if(ch == '-')
                flag = -1;
            else
                flag = 1;
        }
    }
    if(state == COEFF){
        terms.push_back(Term(a * flag, 0));
        state = END;
    }
    else if(state == XVAR){
        terms.push_back(Term(a * flag, 1));
        state = END;
    }
    else if(state == EXP){
        terms.push_back(Term(a * flag, b));
        state = END;
    }
    else
        state = ERR;
    if(state == END)
        RemoveZeros();
    return state;
}
int main()
{
    string cmd, line;
    int id = 1;
    double x = 0.0, y = 0.0;
    Polynomial a, b;
    while(getline(cin, cmd) && cmd != "last"){
        if(cmd == "evaluate"){
            cin >> x >> std::ws;
            getline(cin, line);
            a.fromString(line);
            y = a.evaluate(x);
            cout << id++ << ": " << y << endl;
        }
        else{
            getline(cin, line);
            a.fromString(line);
            while(getline(cin, line) && line.size() > 0 && line != "-1"){
                if(cmd == "add"){
                    b.fromString(line);
                    a += b;
                }
                else if(cmd == "subtract"){
                    b.fromString(line);
                    a -= b;
                }
                else if(cmd == "multiply"){
                    b.fromString(line);
                    a *= b;
                }
            }
            cout << id++ << ": " << a << endl;
        }
    }
}

程序在VC2005下编译并运行正常,其中乘法效率较低,可以进行改进;下面为测试用例:

add

x+1

2x^2+1

3x+4

-1

evaluate

3

7x^3+2x^2-10

add

2x^2+1

-2x^2-1

-1

add

2x^3-5x+2

0

-1

multiply

x^2+2x+1

x^2+2x+1

x^2+2x+1

-1

evaluate

8

x^6+6x^5+15x^4+20x^3+15x^2+6x+1

multiply
-x+1
-x+1
-x+1
-1

last

如果觉得没问题,请尽快采纳。

热心网友 时间:2024-11-04 17:23

没问题很愿意帮你,私聊
声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。
E-MAIL:11247931@qq.com
说课包括哪些方面 说课内容包括()。 如何在手机百度上删除对话记录? 结核病是什么样的疾病? 曹丕17岁得了肺痨,明知自己命不长久,还要强争王位,是不是很自私呢?_百... 古代小说常出现的病名 急求一篇"生活小窍门"(500字)的作文 至今最有什么小妙招 健康的戒烟方法 笔记本电池锁死是什么原因引起的? C语言编程把这个子函数按指数升序的改成降序的,,,一元多项式的_百度... C语言,计算组合数C(A,B) 哪位大虾能提供个Labview 8.2的序列号和激活码? ...2011在NI License Activator中显示已经激活但打开后还是没有破解... LabVIEW8.6如何破解激活 如何养好大戬科植物,怎么区分龙骨和麒麟掌 蝴蝶泉值得去吗 梦见自家房屋倒蹋的预兆 是不是一定要过了六点以后才能属于早上?六点之前一直都属于凌晨或者是漆... Win10共享文件夹遇到错误0x80004005的解决方法 早上6点是凌晨 win7系统使用VirtualBox提示UnabletoloadR3module怎么解决 win7系统装不了打印机驱动怎么办 win7系统启动共享服务弹出错误代码Ox80004005怎么办 鸡吃酒糟对种蛋能孵化出小鸡吗? 母鸡吃酒糟会影响产蛋吗? 照母山万科城三期是哪个社区 你好我问一下吗冬天那个下蛋鸡吃那个酒糟好吗请回答我? 重庆万科星光天空之城产权年限多少年? 生蛋母鸡可以吃酒糟吗? 普拉多4.0排量中规版是什么意思? 咨询进口丰田2700普拉多中规版2014款丅x和vx区别有些什么区别 梦见白山羊咬我的肚子很疼的预兆 《太古焚天诀》txt下载在线阅读全文,求百度网盘云资源 《晧魔焚天诀》txt下载在线阅读全文,求百度网盘云资源 小学一年级 数学题 找规律 ()()() 3 ()()()() 19 ()() 17 ()() 小学数学找规律 1,4,10,19【】【】 在线== 谢了 干荷叶煮可以减肥吗 ...A和B能说一个数或两个数,直到谁说到20就是谁赢。这有什么规律... 干荷叶泡茶喝真的有减肥的效果吗? 旅游业发达每年接待数百万来自世界各地的游客用英文怎么说 数百万的英语 专家们都认为伦敦奥运会将会吸引数百万的游客用英语怎么翻译 每年有超过两百万的游客到巴黎旅游用英语用一般现在时怎么说? Finance rates和Lease rates有什么区别? 怎么用家庭教育来教育孩子啊? 北方人豪爽。南方人精明。山东人长的高。四川人爱吃辣。只是什么效应... 父母应该怎样用家庭教育来教育孩子啊? 父母利用家庭教育孩子好吗? 如何利用家庭教育?
  • 焦点

最新推荐

猜你喜欢

热门推荐