重载多项式C++ 中的 I/O 运算符
Overloading i/o operators in C++ for polynomials
我得到了这个项目,我必须重载 I/O 运算符来读写多项式。不幸的是,我似乎无法让它工作。
我有头文件:
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include <iostream>
using namespace std;
class Polynomial
{
public:
Polynomial();
Polynomial(int degree, double coef[]);
int degree;
double coef[ ];
friend istream& operator>>(istream&,Polynomial& );
friend ostream& operator<<(ostream&,const Polynomial&);
virtual ~Polynomial();
};
#endif // POLYNOMIAL_H
和 cpp 文件:
#include "Polynomial.h"
#include<iostream>
#include<string>
using namespace std;
Polynomial::Polynomial()
{
//ctor
}
Polynomial::~Polynomial()
{
//dtor
}
Polynomial::Polynomial(int d, double c[])
{
degree = d;
double coef [degree+1];
for(int i = 0; i < d+1; i++)
{
coef[i] = c[i];
}
}
istream& operator>>(istream& x, const Polynomial& p)
{
cout<<"The degree: ";
x>>p.degree;
for(int i = 0; i < p.degree+1; i++)
{
cout<<"The coefficient of X^"<<i<<"=";
x>>p.coef[i];
}
return x;
}
ostream& operator<<(ostream& out, const Polynomial& p)
{
out << p.coef[0];
for (int i = 1; i < p.degree; i++)
{
out << p.coef[i];
out << "*X^";
out << i;
}
return out;
}
在名称中,我试图读取多项式,然后写另一个多项式:
#include <iostream>
using namespace std;
#include "Polynomial.h"
int main()
{
Polynomial p1();
cin >> p1;
int degree = 2;
double coef [3];
coef[0]=1;
coef[1]=2;
coef[3]=3;
Polynomial p(degree, coef);
cout<<p;
return 0;
}
当我运行程序时,它只是冻结,我似乎找不到错误。 有什么想法吗?
Polynomial::Polynomial(int d, double c[])
{
degree = d;
double coef [degree+1];
for(int i = 0; i < d+1; i++)
{
coef[i] = c[i];
}
}
在这里,您创建本地数组coef
(顺便说一句,使用非标准C++),然后分配给它。您的成员coeff
没有初始化为任何有意义的东西(并且首先以现在的方式毫无意义)。
与其double coef[]
不如使用这样的std::vector
:
struct polynomial {
std::vector<double> coef;
// No need for member varaible degree, vector knows its lengths
polynomial (const std::vector<double> &coeffs) : coef (coeffs) {}
};
然后定义所有其他构造函数,你需要做一些有意义的事情。或者,您可以完全省略构造函数并直接分配系数向量。然后你可以例如这样的函数
int degree (const polynomial &p) {
return p.coef.size() - 1;
}
或
std::ostream &operator << (std::ostream &out, const polynomial p) {
if (p.coef.size() == 0) {
out << "0n";
return out;
}
out << p.coeff[0];
for (int i = 1; i < p.coef.size(); ++i)
out << " + " << p.coef[i] << "*X^i";
out << "n";
return out;
}
(1)
double coef[];
这是在堆栈上具有未调整大小/动态大小的数组的非标准方法。你最好给数组一些大小,或者让它成为一个指针并自己分配内存;或使用vector<double>
(二)您正在构造函数中创建一个局部变量,这将隐藏类中的成员变量。如果您使用的是指针方法,则需要在构造函数中正确分配它。无论使用哪种方法,理想情况下,都应将(成员)变量初始化为零coef
。
(三)
Polynomial p1();
这有效地声明了一个名为p1
的函数,该函数将返回一个Polynomial
而不是 tyoePolynomial
的变量。您可能希望使用:
Polynomial p1;
相关文章:
- 运算符 = 中的条件
- 如何处理运算符=中的常量成员?
- 返回 bool 作为 CPP 中 bool 运算符 [] 中的值
- 运算符中的不同类型? 具有无捕获,相同的签名,lambda
- C++中的字符串类: +运算符中的错误?
- 相当于C++运算符中的 Python
- 如何处理自定义输出运算符中的 iomanips?
- 处理输入流运算符>>中的无效输入
- 自定义容器中的括号运算符中的恒常性
- 我的重载运算符 == 中的 C++ 参数太少
- 比较运算符==中的对象指针
- 根据运算符中的枚举值推断类型
- C++ 输出流运算符 <<() 中的字符何时加宽?
- 正在使用C 三元运算符中的字符串常数,一种无效的非效率数组的使用
- 比较运算符 == 中的共享指针恒常性
- 指向重载前缀和后缀运算符中的类对象的指针
- 友元运算符中的变量范围
- 运算符中的"With respect to an indeterminately-sequenced function call"
- [ ] - 运算符中的加法和递增表达式
- 模板 &&运算符中的无效操作数+