在这个项目中,我希望创建一个多项式类。这是Polynomial.cpp文件的内容:
#include <iostream>
#include "Polynomial.h"
#include <stdexcept>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;
Polynomial::Polynomial(double c[], int size){
set(c,size);
}
Polynomial::Polynomial(const Polynomial &poly){
size = poly.size;
c = new double[size];
for(int i=0; i<size; i++){
c[i] = poly.c[i];
}
}
Polynomial::~Polynomial(){
delete [] c;
}
void Polynomial::set(double ca[], int s){
if(s < 1){
throw std::invalid_argument("Size must be larger or equal to 1.");
}
else{
size = s;
c = new double[size];
for(int i=0; i<size; i++){
c[i] = ca[i];
}
}
}
double* Polynomial::getPointer() const{
return c;
}
int Polynomial::getSize() const{
return size;
}
bool Polynomial::operator ==(const Polynomial& poly) const{
if(size != poly.size){
return false;
}
int i = 0;
int count = 0;
while ( i < size ) {
if ( c[i] == poly.c[i] ) {
count++;
}
i++;
}
if ( count != size) {
return false;
}
return true;
}
Polynomial Polynomial::operator +(const Polynomial& poly) const{
int d = fabs(size - poly.size); //absolute value of size difference
int bigger = size; //the size of the bigger polynomial
int smaller = poly.size; //the size of the smaller polynomial
if ( poly.size > size) {
bigger = poly.size;
smaller = size;
}
double r[bigger];
double s[bigger]; //new coef array for the smaller polynomial with zeros coefs added
if ( d != 0 ) {
if(smaller = poly.size){
for(int i=0; i < smaller; i++){
s[i] = poly.c[i];
}
for(int i=smaller; i < bigger; i++){
s[i] = 0;
}
for(int i=0; i < bigger; i++){
r[i] = c[i] + s[i];
}
}
else{
for(int i=0; i < smaller; i++){
s[i] = c[i];
}
for(int i=smaller; i < bigger; i++){
s[i] = 0;
}
for(int i=0; i < bigger; i++){
r[i] = poly.c[i] + s[i];
}
}
}
else {
for ( int i = 0; i < bigger; i++) {
r[i] = c[i] + poly.c[i];
}
}
return Polynomial(r,bigger);
}
ostream& operator<<(ostream &lhs, const Polynomial &poly){
string plus = "+";
bool valid = false; //check if the first term of the polynomial to be printed is valid or not, valid is true when term is non-zero
if(poly.getPointer()[poly.getSize()-1] < 0){
if(poly.getPointer()[poly.getSize()-1] != 0){
lhs << setprecision(1) << fixed << poly.getPointer()[poly.getSize()-1] << "x^" << poly.getSize()-1 << " ";
}
for(int i=poly.getSize()-2; i >= 0; i--){
if(poly.getPointer()[i] != 0 && i != 1 && i != 0){
if(poly.getPointer()[i] > 0){
lhs << setprecision(1) << fixed << plus << poly.getPointer()[i] << "x^" << i << " ";
}
else{
lhs << setprecision(1) << fixed << poly.getPointer()[i] << "x^" << i << " ";
}
}
else if(poly.getPointer()[i] != 0 && i == 1){
if(poly.getPointer()[i] > 0){
lhs << setprecision(1) << fixed << plus << poly.getPointer()[i] << "x" << " ";
}
else{
lhs << setprecision(1) << fixed << poly.getPointer()[i] << "x" << " ";
}
}
else if(poly.getPointer()[i] != 0 && i == 0){
if(poly.getPointer()[i] > 0){
lhs << setprecision(1) << fixed << plus << poly.getPointer()[i];
}
else{
lhs << setprecision(1) << fixed << poly.getPointer()[i];
}
}
}
}
else{
if(poly.getPointer()[poly.getSize()-1] != 0){
lhs << setprecision(1) << fixed << noshowpos << poly.getPointer()[poly.getSize()-1] << "x^" << poly.getSize()-1 << " ";
valid = true;
}
for(int i=poly.getSize()-2; i >= 0; i--){
if(poly.getPointer()[i] != 0 && i != 1 && i != 0){
if(poly.getPointer()[i] > 0 && valid == true){
lhs << setprecision(1) << fixed << plus << poly.getPointer()[i] << "x^" << i << " ";
}
else{
lhs << setprecision(1) << fixed << poly.getPointer()[i] << "x^" << i << " ";
valid = true;
}
}
else if(poly.getPointer()[i] != 0 && i == 1){
if(poly.getPointer()[i] > 0 && valid == true){
lhs << setprecision(1) << fixed << plus << poly.getPointer()[i] << "x" << " ";
}
else{
lhs << setprecision(1) << fixed << poly.getPointer()[i] << "x" << " ";
valid = true;
}
}
else if(poly.getPointer()[i] != 0 && i == 0){
if(poly.getPointer()[i] > 0 && valid == true){
lhs << setprecision(1) << fixed << plus << poly.getPointer()[i];
}
else{
lhs << setprecision(1) << fixed << poly.getPointer()[i];
valid = true;
}
}
}
}
return lhs;
}
这是我的测试程序:
#include <iostream>
#include "Polynomial.h"
#include <iomanip>
using namespace std;
int main() {
double c[] = {0,-5,3};
double d[] = {3,0,-5,0};
Polynomial p1(c,3);
Polynomial p2(d,4);
cout << "Polynomial p1 is: " << p1 << endl;
cout << "Degree of polynomial p1 is: " << noshowpos << p1.getDegree() << endl;
cout << "Polynomial p2 is: " << p2 << endl;
Polynomial p3(p1);
cout << "Polynomial p3 now is equal to p1 after copy constructor: " << p3 << endl;
bool same = p1 == p3;
cout << "Is p1 equal to p3? " << noshowpos << same << endl;
Polynomial p4 = p1 + p2;
cout << "Polynomial p4 is the sum of p1 and p2: " << p4 << endl;
所以我遇到的问题是这个if语句:
if(poly.getPointer()[poly.getSize()-1] != 0){
lhs << setprecision(1) << fixed << poly.getPointer()[poly.getSize()-1] << "x^" << poly.getSize()-1 << " ";
}
当我对p4 = p1 + p2执行算子+时,p4的double多项式数组为[3,-5,-2,0]。因此,此数组的p4 [size-1]的最后一个值为0。如您在上述if语句中所见,当我得到poly.getPointer()[poly.getSize()-1]值时,它等于0;但是,当我将它与if语句中的零进行比较时,即使if语句为false(0不等于0,因此也应跳过此if语句),它仍然会执行。我试过在IDE和Linux引擎上都运行它。但是,结果仍然相同。我不确定它是否与此调用set方法的构造函数有关:
Polynomial::Polynomial(double c[], int size){
set(c,size);
}
void Polynomial::set(double ca[], int s){
if(s < 1){
throw std::invalid_argument("Size must be larger or equal to 1.");
}
else{
size = s;
c = new double[size];
for(int i=0; i<size; i++){
c[i] = ca[i];
}
}
}
请帮助。谢谢。