Hello,
I really need your help.
I am writing a program to create a polynomial using linked list. The program is to perform different operations such as addition, subtraction, multiplication. I am however having problems setting the coefficients of this polynomial. I have an idea on how to implement the operations but i'm having problem setting the coefficient.
e.g for a polynomial as 3x^2 + 5x + 1. i have to set the coefficients as
p.set(0, 1)
p.set(1, 5)
p.set(2, 3).
poly p(2) to represent a second order polynomial.
Below is the code that i have written so far. I am having a bunch of errors which i have posted below the code. any help or assistance is greatly appreciated.
//Header file
# include <iostream>
using namespace std;
class Lnode{
public:
Lnode();
Lnode(int, double);
double coefficient;
int exponent;
Lnode *next;
};
Lnode::Lnode(){
coefficient = 0.0;
exponent = 0;
next = NULL;
}
Lnode::Lnode(int exp, double coef){
coefficient = coef;
exponent = exp;
next = NULL;
}
class poly{
public:
poly();
poly(int);
void setCoef(int exp, double coef);
void print();
private:
int size;
Lnode *head, *tail;
};
# include <iostream>
# include "Polynomials.h"
using namespace std;
poly::poly(){
head = tail = NULL;
}
poly::poly(int n){
int i;
for(i=0; i<=n+1; i++)
Lnode *newnode = new Lnode;
}
void poly::setCoef(int exp, double coef){
Lnode *newnode = new Lnode(exp, coef);
newnode->next = head;
head = newnode;
if (tail==NULL)
tail = head;
size++;
}
void poly::print(){
Lnode *current;
current = head;
while(current != NULL){
cout<<current->coefficient<<"x^"<<current->exponent<<" ";
current = current->next;
}
}
//Test File
# include <iostream>
# include "Polynomials.h"
# include "poly.cpp"
using namespace std;
void main() {
poly p(3);
a.setCoef(0,1.1);
a.setCoef(1,2.1);
a.print();
}
// i set my coefficients to double and not integer as i put above.
These are the error that i am getting:
Error 1 error C2011: 'Lnode' : 'class' type redefinition
Error 2 error C2027: use of undefined type 'Lnode'
Error 3 error C2059: syntax error : ')'
Error 4 error C2143: syntax error : missing ';' before '{'
Error 5 error C2447: '{' : missing function header (old-style formal list?)
Error 6 error C2027: use of undefined type 'Lnode'
Error 7 error C2062: type 'int' unexpected
Error 8 error C2143: syntax error : missing ';' before '{'
Error 9 error C2447: '{' : missing function header (old-style formal list?)
Error 10 error C2011: 'poly' : 'class' type redefinition
Error 11 error C2027: use of undefined type 'poly'
Error 12 error C2059: syntax error : ')'
Error 13 error C2143: syntax error : missing ';' before '{'
Error 15 error C2027: use of undefined type 'poly'
Error 16 error C2062: type 'int' unexpected
Error 17 error C2143: syntax error : missing ';' before '{'
Error 18 error C2447: '{' : missing function header (old-style formal list?)
Error 19 error C2027: use of undefined type 'poly'
Error 20 error C2027: use of undefined type 'Lnode'
Error 21 error C2227: left of '->{ctor}' must point to class/struct/union/generic type
Please, please help me. Thank you in advance for your help.