// Copyright (c) [email protected]
#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
标签:std,coefficient,return,多项式,C++,res,Polynomial,数据结构,data From: https://blog.csdn.net/wl886st/article/details/142236477