将两个多项式与向量系数进行比较C++
Comparing two polynomials with vector's coefficients C++
此代码应该输出两个不同多项式的最大系数,但如果第一个大于第二个,则无法正确编译。
所以,如果第一个多项式是1-2x+4x^3,第二个多项式是-x+5x^2-3x^6。它将起作用,因为第二个多项式比第一个多项式大。
每当第一个多项式大于第二个时,它就会发布"矢量下标超出范围"错误
class Polynomial {
public:
Polynomial();
Polynomial(const vector<int>& coeffs);
int Degree() const;
int Coefficient(int k) const;
void print() const;
private:
vector<int>coefficient;
int main(){
//Variable and vector for inputs
vector<int> coefficient;
int input = 0;
//Welcome message
cout << "Welcome! Please input the coefficients of the first polynomial p(x).nWhen you are finished, enter -12345.n";
//While loop - if input isn't -12345, put the input into coefficient.
while (input != -12345){
cin >> input;
coefficient.push_back(input);
}
//Deletes -12345
coefficient.pop_back();
//Puts coefficient values into constructor
Polynomial first(coefficient);
//Prints first polynomial
cout << "nYour first polynomial is p(x) = ";
first.print();
//Prints degrees of first polynomial
cout << ".np(x) has degree " << first.Degree();
int degree1 = first.Degree();
//Prints transformation of first polynomial
cout << ".nThe transform of p(x) is ";
first.Transform();
//clears the values in coefficient for second polynomial inputs.
coefficient.clear();
//Inputs the second polynomial's coefficients.
cout << ".nnPlease input the coefficients of the second polynomial q(x).n";
//Had to use do-while because while loop wouldn't work.
do {
cin >> input;
coefficient.push_back(input);
} while (input != -12345);
//Deletes -12345
coefficient.pop_back();
//Puts coefficients into second polynomial
Polynomial second(coefficient);
//Prints second polynomial
cout << "nYour second polynomial is q(x) = ";
second.print();
cout << ".nq(x) has degree " << second.Degree();
int degree2 = second.Degree();
if (degree1 > degree2){
cout << ".nnThe coefficient of x^" << degree1 << " in p(x) is " << first.Coefficient(degree1);
cout << ".nThe coefficient of x^" << degree1 << " in q(x) is " << second.Coefficient(degree1);
}
else{
cout << ".nnThe coefficient of x^" << degree2 << " in p(x) is " << first.Coefficient(degree2);
cout << ".nThe coefficient of x^" << degree2 << " in q(x) is " << second.Coefficient(degree2);
}
int Polynomial::Degree() const{
int number = 0;
for (size_t i = 0, size = coefficient.size(); i < size; i++){
if (coefficient[i] != 0)
number = i;
}
return number;
}
int Polynomial::Coefficient(int k) const{
if (coefficient[k] != 0)
return coefficient[k];
else
return 0;
}
假设coefficient
向量只达到多项式的最高幂的索引(即,如果x^3
是最高幂,则它有4个元素),请尝试用替换函数
int Polynomial::Coefficient(int k) const{
if (k < coefficient.size())
return coefficient[k];
else
return 0;
}
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么比较运算符如此快速
- 我可以使用 g++ 进行三种比较 (<=>) 吗?
- 比较字符数组
- 将模板化的类型与C++中的某些类/类型进行比较
- C++自定义比较函数
- 如何比较自定义类的std::变体
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- std::设置自定义比较器
- 布尔比较运算符是如何在C++中工作的
- C++将目录中的所有文件与::filesystem进行比较
- shell排序中的交换和比较
- 如何在C++中比较两个char数组
- catch框架有没有办法比较流或文件
- 从文件中读取多个字节,并将它们存储在C++中进行比较
- 智能指针作为无序映射键,并通过引用进行比较
- 比较if语句中的数组值和int值
- 对于循环变体比较
- 使用自定义比较函数使用std::sort()对矢量字符串进行排序时出现问题
- 比较两个大小不等的映射c++