C++一元多项式解析、计算、输出(数据结构作业),可直接运行

// Copyright (c) wyy-personal@outlook.com
#include <bits/stdc++.h>

class Polynomial {
   private:
    std::unordered_map<int, int> data_;

    void zero_value_optimization() {
        for (auto iter = data_.begin(); iter != data_.end();) {
            if (iter->second == 0) {
                iter = data_.erase(iter);
            } else {
                ++iter;
            }
        }
    }

   public:
    explicit Polynomial(const std::string& s) {
        data_ = {};
        static const std::regex pattern(R"(([+-]?\d*)(x)?(\^(\d+))?)");
        for (std::sregex_iterator it(s.begin(), s.end(), pattern), end; it != end; ++it) {
            std::smatch match = *it;
            if (match[0].str().empty()) {
                continue;
            }
            std::string coeffStr = match[1];
            std::string expStr = match[4];

            int coefficient = 1;
            if (!coeffStr.empty() && coeffStr != "+" && coeffStr != "-") {
                coefficient = std::stoi(coeffStr);
            } else if (coeffStr == "-") {
                coefficient = -1;
            }

            int exponent = 0;
            if (!expStr.empty()) {
                exponent = std::stoi(expStr);
            } else if (match[2].matched) {
                exponent = 1;
            }

            data_[exponent] += coefficient;
        }
    }

    Polynomial operator*(const Polynomial& right) const {
        Polynomial res("");
        for (auto&& [i, j] : this->data_) {
            for (auto&& [k, l] : right.data_) {
                res.data_[i + k] += j * l;
            }
        }
        res.zero_value_optimization();
        return res;
    }

    Polynomial operator-(const Polynomial& right) const {
        Polynomial res("");
        for (auto&& [i, j] : this->data_) {
            res.data_[i] += j;
        }
        for (auto&& [i, j] : right.data_) {
            res.data_[i] -= j;
        }
        res.zero_value_optimization();
        return res;
    }

    Polynomial operator+(const Polynomial& right) const {
        Polynomial res("");
        for (auto&& [i, j] : this->data_) {
            res.data_[i] += j;
        }
        for (auto&& [i, j] : right.data_) {
            res.data_[i] += j;
        }
        res.zero_value_optimization();
        return res;
    }

    Polynomial derivative() const {
        Polynomial res("");
        for (auto&& [i, j] : this->data_) {
            res.data_[i - 1] += i * j;
        }
        res.zero_value_optimization();
        return res;
    }

    std::string convert_to_string() const {
        std::string res{};
        std::vector<std::pair<int, int>> temp(data_.begin(), data_.end());
        std::ranges::sort(temp, [](auto left, auto right) { return left.first > right.first; });
        for (auto&& i : temp) {
            if (i.second != 0) {
                res += generate_term(i.second, i.first);
            }
        }
        if (res.front() == '+') {
            res.erase(res.begin());
        }
        return res;
    }

    std::string generate_term(int coefficient, int exponent) const {
        if (exponent == 0) {
            return std::format("{:+d}", coefficient);
        }
        if (exponent == 1) {
            if (coefficient == 1) {
                return "+x";
            }
            if (coefficient == -1) {
                return "-x";
            }
            return std::format("{:+d}x", coefficient);
        }
        if (coefficient == 1) {
            return std::format("+x^{:d}", exponent);
        }
        if (coefficient == -1) {
            return std::format("-x^{:d}", exponent);
        }
        return std::format("{:+d}x^{:d}", coefficient, exponent);
    }
};

using namespace std;

int main() {
    string str0, str1;
    cout << "请输入两行,每行各一个表达式:\n";
    cin >> str0 >> str1;
    Polynomial p0(str0), p1(str1);
    cout << format("您输入的表达式为:\np0 : {}\np1 : {}\n", p0.convert_to_string(), p1.convert_to_string());
    cout << "下面是计算结果:\n";
    cout << format("p0 + p1 = {}\n", (p0 + p1).convert_to_string());
    cout << format("p0 - p1 = {}\n", (p0 - p1).convert_to_string());
    cout << format("p1 - p0 = {}\n", (p1 - p0).convert_to_string());
    cout << format("p0 * p1 = {}\n", (p0 * p1).convert_to_string());
    cout << format("p0' = {}\n", p0.derivative().convert_to_string());
    cout << format("p1' = {}\n", p1.derivative().convert_to_string());
}

输入格式: 3x^2+2x+7 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值