我是C ++的新手(以及一般的OOP)。我正在编写一个对2个多项式执行某些数学运算的类。这是我的代码:
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
using namespace std;
class Poly
{
private:
int order; // the order of the polynomial
int *coeff; // pointer to an array of coefficients
// size of the coefficient array is predicated on [order + 1]
public:
// Poly(); // the default constructor
int setOrderAndCoeff(); // sets the order and coefficients
int display(); // displays the resutling polynomial
void addition(Poly P1, Poly P2); // adds 2 polynomials
void subtraction (Poly P1, Poly P2); // subtracts 2 polynomials
void multiplication (Poly P1, Poly P2); // multiplies 2 polynomials
// ~Poly(); // the destructor
};
//Poly::Poly()
//{
// order = 0;
// *coeff = 0;
//}
int Poly::display()
{
int i;
int j;
for (i = order; i >= 0; i--)
{
cout << coeff[i] << "x^" << i;
if ((i - 1) != -1)
{
cout << "+";
}
}
cout << "\n";
return 0;
}
int Poly::setOrderAndCoeff()
{
int i;
cout << "Please enter the order of the polynomial: ";
cin >> order;
coeff = new int[order + 1];
for (i = order; i >= 0; i--)
{
cout << "Please enter the coefficient of x^" << i << " :";
cin >> coeff[i];
}
return 0;
}
void Poly::addition(Poly P1, Poly P2)
{
int max;
int i;
max = (P1.order > P2.order) ? P1.order : P2.order;
int *add = new int[max + 1];
if (P1.order == P2.order)
{
for (i = P1.order; i >= 0; i--)
{
add[i] = P1.coeff[i] + P2.coeff[i];
}
}
if (P1.order > P2.order)
{
for (i = P1.order; i > P2.order; i--)
{
add[i] = P1.coeff[i];
}
for (i = P2.order; i >= 0; i--)
{
add[i] = P1.coeff[i] + P2.coeff[i];
}
}
if (P1.order < P2.order)
{
for (i = P2.order; i > P1.order; i--)
{
add[i] = P2.coeff[i];
}
for (i = P1.order; i >= 0; i--)
{
add[i] = P1.coeff[i] + P2.coeff[i];
}
}
cout << "\nAddition:";
display();
cout << "\n";
}
void Poly::subtraction(Poly P1, Poly P2)
{
int max;
int i;
max = (P1.order > P2.order) ? P1.order : P2.order;
int *sub = new int[max + 1];
if (P1.order == P2.order)
{
for (i = P1.order; i >= 0; i--)
{
sub[i] = P1.coeff[i] - P2.coeff[i];
}
}
if (P1.order > P2.order)
{
for (i = P1.order; i > P2.order; i--)
{
sub[i] = P1.coeff[i];
}
for (i = P2.order; i >= 0; i--)
{
sub[i] = P1.coeff[i] - P2.coeff[i];
}
}
if (P1.order < P2.order)
{
for (i = P2.order; i > P1.order; i--)
{
sub[i] = -P2.coeff[i];
}
for (i = P1.order; i >= 0; i--)
{
sub[i] = P1.coeff[i] - P2.coeff[i];
}
}
cout << "\nSubtraction:";
display();
cout << "\n";
}
void Poly::multiplication(Poly P1, Poly P2)
{
int i;
int j;
int max;
max = P1.order + P2.order;
int *mult = new int[max + 1];
for (i = P1.order; i >= 0; i--)
for (j = P2.order; j >= 0; j--)
{
mult[i + j] += P1.coeff[i] * P2.coeff[i];
}
cout << "\nMultiplication:";
display();
}
int main()
{
int choice;
Poly P1, P2, P3;
cout << "-------- Instructions --------" << endl;
cout << "For polynomial 1... " << endl;
P1.setOrderAndCoeff();
cout << endl;
cout << "For polynomial 2... " << endl;
P2.setOrderAndCoeff();
while (1)
{
cout << "\n******** Menu Selection ********" << endl;
cout << "1: Addition\n2: Subtraction\n3: Mutiplication\n0: Exit" << endl;
cout << "Please enter your choice (1, 2, 3 or 0):";
cin >> choice;
switch (choice)
{
case 1:
cout << "\n-------- Addition --------\n";
cout << "Polynomial 1: ";
P1.display();
cout << "Polynomial 2: ";
P2.display();
P3.addition(P1, P2);
cout << "--------------------------\n";
break;
case 2:
cout << "\n-------- Subtraction --------\n";
cout << "Polynomial 1: ";
P1.display();
cout << "Polynomial 2: ";
P2.display();
P3.subtraction(P1, P2);
cout << "--------------------------\n";
break;
case 3:
cout << "\n-------- Multiplication --------\n";
cout << "Polynomial 1: ";
P1.display();
cout << "Polynomial 2: ";
P2.display();
P3.multiplication(P1, P2);
cout << "--------------------------\n";
break;
case 0:
cout << "The program will now terminate. Thank you." << endl;
exit(0);
default:
cout << endl;
cout << "You have entered an invalid selection." << endl;
cout << "Please enter a positive integer between 0 and 3.";
cout << endl;
}
}
return 0;
}
所有内容都会编译,并提示用户输入订单和一些系数。不幸的是,addition(),subttraction()和multiplication()函数不会返回结果多项式。
我知道我错过了一些非常基本的东西,但我无法弄清楚它是什么!
提前感谢您提供任何指导!
PS:我甚至需要默认的构造函数吗?
编辑(根据建议的修订更新的代码)
以下是基于建议的修订代码:
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
using namespace std;
class Poly
{
private:
int order; // the order of the polynomial
int *coeff; // pointer to an array of coefficients
// size of the coefficient array is predicated on [order + 1]
int *add;
int *sub;
int *mult;
public:
// Poly(); // the default constructor
int setOrderAndCoeff(); // sets the order and coefficients
int display(int *data, int count); // displays the resutling polynomial
void addition(Poly P1, Poly P2); // adds 2 polynomials
void subtraction (Poly P1, Poly P2); // subtracts 2 polynomials
void multiplication (Poly P1, Poly P2); // multiplies 2 polynomials
// ~Poly(); // the destructor
};
int Poly::display(int *data, int count)
{
for (i = cout; i >= 0; i--)
{
cout << data[i] << "x^" << i;
if ((i - 1) != -1)
{
cout << "+";
}
}
cout << "\n";
return 0;
}
int Poly::setOrderAndCoeff()
{
int i;
cout << "Please enter the order of the polynomial: ";
cin >> order;
coeff = new int[order + 1];
for (i = order; i >= 0; i--)
{
cout << "Please enter the coefficient of x^" << i << " :";
cin >> coeff[i];
}
return 0;
}
void Poly::addition(Poly P1, Poly P2)
{
int max;
int i;
max = (P1.order > P2.order) ? P1.order : P2.order;
add = new int [max + 1];
if (P1.order == P2.order)
{
for (i = P1.order; i >= 0; i--)
{
add[i] = P1.coeff[i] + P2.coeff[i];
}
}
if (P1.order > P2.order)
{
for (i = P1.order; i > P2.order; i--)
{
add[i] = P1.coeff[i];
}
for (i = P2.order; i >= 0; i--)
{
add[i] = P1.coeff[i] + P2.coeff[i];
}
}
if (P1.order < P2.order)
{
for (i = P2.order; i > P1.order; i--)
{
add[i] = P2.coeff[i];
}
for (i = P1.order; i >= 0; i--)
{
add[i] = P1.coeff[i] + P2.coeff[i];
}
}
cout << "\nAddition:";
display(add, count);
cout << "\n";
}
void Poly::subtraction(Poly P1, Poly P2)
{
int max;
int i;
max = (P1.order > P2.order) ? P1.order : P2.order;
int *sub = new int[max + 1];
if (P1.order == P2.order)
{
for (i = P1.order; i >= 0; i--)
{
sub[i] = P1.coeff[i] - P2.coeff[i];
}
}
if (P1.order > P2.order)
{
for (i = P1.order; i > P2.order; i--)
{
sub[i] = P1.coeff[i];
}
for (i = P2.order; i >= 0; i--)
{
sub[i] = P1.coeff[i] - P2.coeff[i];
}
}
if (P1.order < P2.order)
{
for (i = P2.order; i > P1.order; i--)
{
sub[i] = -P2.coeff[i];
}
for (i = P1.order; i >= 0; i--)
{
sub[i] = P1.coeff[i] - P2.coeff[i];
}
}
cout << "\nSubtraction:";
display(sub, count);
cout << "\n";
}
void Poly::multiplication(Poly P1, Poly P2)
{
int i;
int j;
int max;
max = P1.order + P2.order;
int *mult = new int[max + 1];
for (i = P1.order; i >= 0; i--)
for (j = P2.order; j >= 0; j--)
{
mult[i + j] += P1.coeff[i] * P2.coeff[i];
}
cout << "\nMultiplication:";
display(mult, count);
}
int main()
{
int choice;
Poly P1, P2, P3;
cout << "-------- Instructions --------" << endl;
cout << "For polynomial 1... " << endl;
P1.setOrderAndCoeff();
cout << endl;
cout << "For polynomial 2... " << endl;
P2.setOrderAndCoeff();
while (1)
{
cout << "\n******** Menu Selection ********" << endl;
cout << "1: Addition\n2: Subtraction\n3: Mutiplication\n0: Exit" << endl;
cout << "Please enter your choice (1, 2, 3 or 0):";
cin >> choice;
switch (choice)
{
case 1:
cout << "\n-------- Addition --------\n";
cout << "Polynomial 1: ";
P1.display(add, count);
cout << "Polynomial 2: ";
P2.display(add, count);
P3.addition(P1, P2);
cout << "--------------------------\n";
break;
case 2:
cout << "\n-------- Subtraction --------\n";
cout << "Polynomial 1: ";
P1.display(sub, count);
cout << "Polynomial 2: ";
P2.display(sub, count);
P3.subtraction(P1, P2);
cout << "--------------------------\n";
break;
case 3:
cout << "\n-------- Multiplication --------\n";
cout << "Polynomial 1: ";
P1.display(mult, count);
cout << "Polynomial 2: ";
P2.display(sub, count);
P3.multiplication(P1, P2);
cout << "--------------------------\n";
break;
case 0:
cout << "The program will now terminate. Thank you." << endl;
exit(0);
default:
cout << endl;
cout << "You have entered an invalid selection." << endl;
cout << "Please enter a positive integer between 0 and 3.";
cout << endl;
}
}
return 0;
}
我仍然遇到一些构建错误,即:
1)我没有在
中定义int Poly::display(int *data, int count)
2)在开关的情况1中添加是未声明的标识符(与sub和mult的情况一样,但它的错误相同)
3)in
void Poly::addition(Poly P1, Poly P2`)
无法将参数2转换为int
提前感谢大家一起来看看。
-Ryan
答案 0 :(得分:1)
您如何期望函数不返回值来返回值?当你有
void addition(Poly P1, Poly P2);
此函数无法返回值。作为第一步,您应该将其更改为
Poly addition(Poly P1, Poly P2);
并在函数中添加适当的return
语句。但它仍然不是通常在C ++中完成的方式。您不希望按值传递Poly
对象,因为它很昂贵,而且根据您当前的定义,它会泄漏内存。你应该使用
Poly addition(const Poly& P1, const Poly& P2);
并且作为最后 - 但最重要的 - 评论。不要使用指针。替换你的
int *coeff;
与
std::vector<int> coeff;
只有当您知道自己在做什么以及在低级实现中时,才应使用手动内存分配。为了您的目的,std::vector
要好得多。
答案 1 :(得分:0)
我认为returning
代表displaying
。问题是sub
,add
和mult
在Poly::display
函数中不可见。很简单,尝试将此变量添加为Poly
类的成员。
class Poly
{
private:
int order; // the order of the polynomial
int *coeff; // pointer to an array of coefficients
int *add, *sub, *mult;
// ...
};
然后你不必在每个函数中声明它们,例如Poly::addition(Poly P1, Poly P2)
你可以改变:
int *add = new int[max + 1];
通过
add = new int[max + 1]; // Or
// this->add = new int[max + 1];
// if you prefer.
编辑:您正在功能coeff
中显示display
内容,并且coeff
在您拨打任何操作时都不会更改。
您必须对函数display
进行一些修改,最简单的方法是重新定义显示以接受2个参数:
class Poly
{
puplic:
// ...
void display(int *data, int count);
// ...
};
int Poly::display(int *data, int count)
{
for (int i = count; i >= 0; i--)
{
cout << data[i] << "x^" << i;
if ((i - 1) != -1)
{
cout << "+";
}
}
cout << "\n";
return 0;
}
然后在(例如)Poly::addition
:
void Poly::addition(Poly P1, Poly P2)
{
//...
display(add, count) // Here count is the ammount if elements in data.
}