用c++写一个求多项式问题
时间: 2023-04-06 09:00:40 浏览: 60
您好,以下是用 C 语言编写求多项式问题的示例代码:
#include <stdio.h>
int main() {
int n, x, i, j, sum = 0;
printf("请输入多项式的最高次数:");
scanf("%d", &n);
int a[n+1];
printf("请输入多项式的各项系数:");
for (i = 0; i <= n; i++) {
scanf("%d", &a[i]);
}
printf("请输入 x 的值:");
scanf("%d", &x);
for (i = 0; i <= n; i++) {
int temp = 1;
for (j = 0; j < i; j++) {
temp *= x;
}
sum += a[i] * temp;
}
printf("多项式的值为:%d\n", sum);
return 0;
}
希望能够帮到您!
相关问题
用c++写一个一元稀疏多项式计算器
好的,以下是用C++实现一元稀疏多项式计算器的代码,包括多项式的加法、减法、乘法、求导和积分计算:
```c++
#include <iostream>
#include <unordered_map>
#include <cmath>
using namespace std;
// 定义多项式的项
struct Term {
int coef; // 系数
int exp; // 指数
Term(int c = 0, int e = 0) : coef(c), exp(e) {}
};
// 定义多项式类
class Polynomial {
public:
Polynomial() {}
~Polynomial() {}
// 加法
Polynomial operator+(const Polynomial& other) const {
Polynomial res;
for (auto& term : terms) {
res.terms[term.first] += term.second;
}
for (auto& term : other.terms) {
res.terms[term.first] += term.second;
}
return res;
}
// 减法
Polynomial operator-(const Polynomial& other) const {
Polynomial res;
for (auto& term : terms) {
res.terms[term.first] += term.second;
}
for (auto& term : other.terms) {
res.terms[term.first] -= term.second;
}
return res;
}
// 乘法
Polynomial operator*(const Polynomial& other) const {
Polynomial res;
for (auto& term1 : terms) {
for (auto& term2 : other.terms) {
int exp = term1.first + term2.first;
int coef = term1.second * term2.second;
res.terms[exp] += coef;
}
}
return res;
}
// 求导
Polynomial derivative() const {
Polynomial res;
for (auto& term : terms) {
if (term.first > 0) {
res.terms[term.first - 1] += term.second * term.first;
}
}
return res;
}
// 积分
Polynomial integral() const {
Polynomial res;
for (auto& term : terms) {
res.terms[term.first + 1] += term.second / (term.first + 1);
}
return res;
}
// 打印多项式
void print() const {
bool first = true;
for (auto& term : terms) {
if (term.second == 0) continue;
if (term.second > 0 && !first) {
cout << "+";
}
first = false;
cout << term.second;
if (term.first > 0) {
cout << "x";
if (term.first > 1) {
cout << "^" << term.first;
}
}
}
cout << endl;
}
// 添加项
void add_term(int coef, int exp) {
terms[exp] += coef;
}
private:
unordered_map<int, int> terms; // 哈希表存储多项式的每一项
};
int main() {
Polynomial p1, p2, res;
// 第一个多项式
p1.add_term(2, 3);
p1.add_term(3, 2);
p1.add_term(4, 1);
p1.add_term(5, 0);
cout << "p1(x) = ";
p1.print();
// 第二个多项式
p2.add_term(1, 4);
p2.add_term(2, 3);
p2.add_term(3, 2);
p2.add_term(4, 1);
p2.add_term(5, 0);
cout << "p2(x) = ";
p2.print();
// 加法
res = p1 + p2;
cout << "p1(x) + p2(x) = ";
res.print();
// 减法
res = p1 - p2;
cout << "p1(x) - p2(x) = ";
res.print();
// 乘法
res = p1 * p2;
cout << "p1(x) * p2(x) = ";
res.print();
// 求导
res = p1.derivative();
cout << "dp1(x)/dx = ";
res.print();
// 积分
res = p1.integral();
cout << "∫p1(x)dx = ";
res.print();
return 0;
}
```
以上代码中,我们使用了`unordered_map`来存储多项式的每一项,其中键为指数,值为系数。在多项式的加减乘法中,我们遍历两个多项式的哈希表,对于相同指数的项进行系数相加/相减/相乘,并将结果存储到结果多项式的哈希表中。在求导和积分计算中,我们遍历多项式的哈希表,对于每一项进行求导/积分操作,并将结果存储到结果多项式的哈希表中。最后,我们使用`print`函数打印出多项式的表达式。
用C++代码写两个一元稀疏多项式相加
以下是用 C 代码实现两个一元稀疏多项式相加的例子:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_TERMS 20
typedef struct {
int coef;
int expo;
} PolynomialTerm;
typedef struct {
PolynomialTerm terms[MAX_TERMS];
int size;
} Polynomial;
Polynomial addPolynomials(Polynomial p1, Polynomial p2) {
Polynomial result;
int i = 0, j = 0, k = 0;
while (i < p1.size && j < p2.size) {
if (p1.terms[i].expo > p2.terms[j].expo) {
result.terms[k++] = p1.terms[i++];
} else if (p1.terms[i].expo < p2.terms[j].expo) {
result.terms[k++] = p2.terms[j++];
} else {
result.terms[k].expo = p1.terms[i].expo;
result.terms[k++].coef = p1.terms[i++].coef + p2.terms[j++].coef;
}
}
while (i < p1.size)
result.terms[k++] = p1.terms[i++];
while (j < p2.size)
result.terms[k++] = p2.terms[j++];
result.size = k;
return result;
}
int main() {
Polynomial p1, p2, result;
p1.size = p2.size = result.size = 0;
// 输入第一个多项式
printf("Enter the number of terms in the first polynomial: ");
scanf("%d", &p1.size);
printf("Enter the coefficient and exponent of each term:\n");
for (int i = 0; i < p1.size; i++) {
scanf("%d %d", &p1.terms[i].coef, &p1.terms[i].expo);
}
// 输入第二个多项式
printf("\nEnter the number of terms in the second polynomial: ");
scanf("%d", &p2.size);
printf("Enter the coefficient and exponent of each term:\n");
for (int i = 0; i < p2.size; i++) {
scanf("%d %d", &p2.terms[i].coef, &p2.terms[i].expo);
}
// 计算结果多项式并输出
result = addPolynomials(p1, p2);
printf("\nResultant polynomial after addition:\n");
for (int i = 0; i < result.size; i++) {
printf("%dx^%d ", result.terms[i].coef, result.terms[i].expo);
if (i != result.size - 1) {
printf("+ ");
}
}
printf("\n");
return 0;
}
```
这个程序首先定义了两个结构体:`PolynomialTerm` 表示多项式中的一项,包括系数和指数;`Polynomial` 表示整个多项式,包括多个 `PolynomialTerm` 和 `size` 表示多项式中包含的项数。
然后定义了一个函数 `addPolynomials`,用于将两个多项式相加。这个函数首先定义了一个新的多项式 `result`,然后使用类似于归并排序的方式将两个多项式中的项按照指数排序,然后将相同指数的项的系数相加得到新的系数,最后将结果存入 `result` 中,并返回。
在 `main` 函数中,先输入两个多项式,然后调用 `addPolynomials` 函数计算结果,并输出。