Untitled
unknown
c_cpp
2 years ago
5.6 kB
9
Indexable
#include "lpoly.h"
#include <cmath>
lpoly::lpoly() {
_head_ptr = new node();
}
lpoly::lpoly(const lpoly & p){
_head_ptr = new node();
for (int i = 0; i <= p.degree();i++){
coeff_type copy = p[i];
if (copy!=0)
set(i,copy);
}
}
lpoly::lpoly(const coeff_type & c, size_type i){
_head_ptr = new node();
set(i,c);
}
lpoly::~lpoly(){
list_clear(_head_ptr);
}
//const members
int lpoly::degree() const{
int max = -1;
for (node * p = _head_ptr; p != nullptr; p = p->link()){
//std::cout << "current max is " << max;
if (int((p ->data()).i) > max && p->data().c != coeff_type()){
max = (p -> data()).i;
//std::cout << "new max is " << max << std::endl;
}
}
return max;
}
lpoly::coeff_type lpoly::operator[](size_type i) const{
const node * p;
for (p = _head_ptr; p != nullptr; p = p->link())
if ((p->data()).i == i)
return p->data().c;
return coeff_type();
}
lpoly::coeff_type lpoly::operator()(const coeff_type & v) const{
coeff_type sum = coeff_type();
for (node * p = _head_ptr; p!= nullptr; p = p -> link()){
sum += pow(v,p->data().i)*p->data().c;
}
return sum;
}
lpoly lpoly::derivative() const{
lpoly res = lpoly();
for (node * p = _head_ptr -> link(); p != nullptr; p = p -> link()){
term curr = p -> data();
res.set(curr.i - 1,curr.c * curr.i);
//std::cout << "(" << curr.c * curr.i<< ", " << curr.i-1 << ") has been set\n";
}
return res;
}
lpoly lpoly::integral() const{
lpoly res = lpoly();
for (node * p = _head_ptr; p != nullptr; p = p -> link()){
if ((p->data().c)/(p->data().i+1) != 0)
res.set(p->data().i + 1,(p->data().c)/(p->data().i+1));
}
return res;
}
void lpoly::print() const{
for (const node *p = _head_ptr; p != nullptr; p = p->link())
if (p->data().c != 0)
std::cout << (p->data()).c << "x^" << std::noshowpos << p-> data().i << " " << std::showpos;
std::cout << std::noshowpos;
}
//modification mmm
void lpoly::set(size_type i, const coeff_type & c){
node::value_type newterm =node::value_type(c,i);
term secondnewterm; // why does term not need node::
if (i == 0){
_head_ptr->set_data(newterm);
return;
}
node * n = _head_ptr -> link();
node::value_type current;
for (node * previous = _head_ptr; previous!= nullptr; previous = previous-> link()){
if (n != nullptr){
current = (n -> data());
}
else{
list_insert(previous, newterm);
return;
}
if (current.i > i){
list_insert(previous, newterm);
return;
}
else if (current.i == i){
n->set_data(newterm); // change data of term to set c
return;
}
n = n->link();
}
std::cout << "nothing happened in the for loop error\n" ;
}
void lpoly::operator = (const lpoly & source){
list_clear(_head_ptr);
_head_ptr = new node();
for (int i = 0; i <= source.degree(); i++){
if (source[i]!=0) set(i,source[i]);
}
}
void lpoly::operator += (const lpoly &p){
for (int i = 0; i <= std::max(p.degree(),degree()); i++){
coeff_type newcoeff = p[i] + (*this)[i];
set(i, newcoeff);
}
}
void lpoly::operator -= (const lpoly & p){
for (int i = 0; i <= std::max(p.degree(),degree()); i++){
coeff_type newcoeff =(*this)[i] - p[i];
set(i, newcoeff);
}
}
void lpoly::operator *= (const lpoly &p){
*this = *this * p;
}
void lpoly::operator /= (const lpoly & p){
}
void lpoly::negate(){
for (node * n = _head_ptr; n!= nullptr; n = n-> link()){
term newterm = term(-n->data().c, n->data().i);
n->set_data(newterm);
}
}
//NONMEMBER AND OVERLOADED + L
std::istream & operator >> (std::istream & is, lpoly & p){
lpoly::coeff_type coeff;
lpoly::size_type i;
std::string line;
std::getline(is,line);
std::stringstream linestream(line);
while (linestream >> coeff >> i){
p.set(i,coeff + p[i]);
}
return is;
}
std::ostream & operator << (std::ostream & os, const lpoly & p){
if (p.degree() == -1){
os << "zero poly";
return os;
}
p.print();
return os;
}
bool operator ==(const lpoly &a, const lpoly &b){
if (a.degree()!=b.degree())
return false;
for (int i = 0; i < a.degree(); i++){
if (a[i]!=b[i]) return false;
}
return true;
}
lpoly operator +(const lpoly & a, const lpoly & b){
lpoly res = lpoly(a);
res+=b;
return res;
}
lpoly operator -(const lpoly & a, const lpoly & b){
lpoly res = lpoly(a);
res-=b;
return res;
}
lpoly operator -(const lpoly & p){
lpoly res = lpoly(p);
res.negate();
return res;
}
lpoly operator *(const lpoly & a, const lpoly & b){
lpoly res = lpoly();
if (a.degree() == -1 || b.degree() == -1) return res;
for (int i = 0; i <= a.degree(); i++){
for (int j = 0; j <= b.degree(); j++){
lpoly::coeff_type coeff = lpoly::coeff_type();
coeff = a[i] * b[j];
lpoly::size_type x = 0;
x = i + j;
res.set(x, res[x] + coeff);
}
}
return res;
}
Editor is loading...
Leave a Comment