Letterbomber
02-24-2009, 09:11 PM
normally i would never come for help here, but ive worked for 8-9 hours on this and cant figure it out. google's no help either. what i have to do is this:
Write a class for Polynomial.
You class will contain the following functions:
A constructor
A destructor - you must free any allocated data.
A function to evaluate a polynomial: double Eval(double x)
An overload to the stream insertion operator to print a polynomial
An overload to the stream extraction operator to read a polynomial
A main program which will read in a polynomial of degree 10, print the
resulting polynomial, evaluate it and print the answer.
print polynomials in a format like this:
coef*x^exp + coef*x^exp ...
where coef is the coefficient of the ith term and exp is it's exponent.
if the coef is zero, don't print the term.
example:
10*x^5 + 5*x^4 - 7*x^2 + 5
note: if the coef is negative, print a minus, not a plus.
input format:
10,5 5,4 -7,2 5,0
where the first number is the coefficient and the second is the power of x to
which it applies.
so basically i have to print the polynomial in that format, and then print the result for a degree of x.
what i have so far:
my .h file:
class Polynomial;
class Term{
friend class Polynomial;
private:
float coef;
int exp;
};
class Polynomial{
public:
Polynomial();
Polynomial Add(Polynomial poly);
Polynomial Mult(Polynomial poly);
float Eval(float f);
double eval(double x);
private:
Term *termArray;
int capacity;
int terms;
}
my .cpp file:
#include iostream
#include math.h
#include "Polynomial1.h"
Polynomial(){
termArray= new Term[4];
capacity= 4;
terms= 0;
}
poly x;
cin>>x;
istream &operator>>(istream &in, poly x)
int a;
double b, c;
for(int i=0, i*less than sign*a, i++){
cin>>b>>c;
x.NewTerm(b,c);
}
return in;
}
void copy(Term *a, Term *b, Term *c){
while (a*less than sign*b) *(c++)= *(a++);
}
double Poly::eval(double x){
double ans= 0;
for(int i=0; i*less than sign*terms; i++){
ans= ans+termArray[i].coef*pow(x, termArray[i].exp)
}
return ans;
}
i think i got an idea on the class and how to print the result, but i'm completely lost when it comes to getting it print out in that format. any programmers out there that can help? i hate to seem lazy but i'm completely stuck and i dont know what else to do.
edit: almost forgot my errors:
Polynomial1.cpp:6: error: new types may not be defined in a return type
Polynomial1.cpp:6: note: (perhaps a semicolon is missing after the definition of ‘Polynomial’)
Polynomial1.cpp: In function ‘Polynomial Polynomial()’:
Polynomial1.cpp:7: error: ‘termArray’ was not declared in this scope
Polynomial1.cpp:8: error: ‘capacity’ was not declared in this scope
Polynomial1.cpp:9: error: ‘terms’ was not declared in this scope
Polynomial1.cpp: At global scope:
Polynomial1.cpp:12: error: ‘poly’ does not name a type
Polynomial1.cpp:14: error: expected constructor, destructor, or type conversion before ‘>>’ token
Polynomial1.cpp:15: error: expected constructor, destructor, or type conversion before ‘&’ token
Polynomial1.cpp:18: error: expected unqualified-id before ‘for’
Polynomial1.cpp:22: error: expected unqualified-id before ‘return’
Polynomial1.cpp:23: error: expected declaration before ‘}’ token
btw, i am a n00b when it comes to this stuff.
Write a class for Polynomial.
You class will contain the following functions:
A constructor
A destructor - you must free any allocated data.
A function to evaluate a polynomial: double Eval(double x)
An overload to the stream insertion operator to print a polynomial
An overload to the stream extraction operator to read a polynomial
A main program which will read in a polynomial of degree 10, print the
resulting polynomial, evaluate it and print the answer.
print polynomials in a format like this:
coef*x^exp + coef*x^exp ...
where coef is the coefficient of the ith term and exp is it's exponent.
if the coef is zero, don't print the term.
example:
10*x^5 + 5*x^4 - 7*x^2 + 5
note: if the coef is negative, print a minus, not a plus.
input format:
10,5 5,4 -7,2 5,0
where the first number is the coefficient and the second is the power of x to
which it applies.
so basically i have to print the polynomial in that format, and then print the result for a degree of x.
what i have so far:
my .h file:
class Polynomial;
class Term{
friend class Polynomial;
private:
float coef;
int exp;
};
class Polynomial{
public:
Polynomial();
Polynomial Add(Polynomial poly);
Polynomial Mult(Polynomial poly);
float Eval(float f);
double eval(double x);
private:
Term *termArray;
int capacity;
int terms;
}
my .cpp file:
#include iostream
#include math.h
#include "Polynomial1.h"
Polynomial(){
termArray= new Term[4];
capacity= 4;
terms= 0;
}
poly x;
cin>>x;
istream &operator>>(istream &in, poly x)
int a;
double b, c;
for(int i=0, i*less than sign*a, i++){
cin>>b>>c;
x.NewTerm(b,c);
}
return in;
}
void copy(Term *a, Term *b, Term *c){
while (a*less than sign*b) *(c++)= *(a++);
}
double Poly::eval(double x){
double ans= 0;
for(int i=0; i*less than sign*terms; i++){
ans= ans+termArray[i].coef*pow(x, termArray[i].exp)
}
return ans;
}
i think i got an idea on the class and how to print the result, but i'm completely lost when it comes to getting it print out in that format. any programmers out there that can help? i hate to seem lazy but i'm completely stuck and i dont know what else to do.
edit: almost forgot my errors:
Polynomial1.cpp:6: error: new types may not be defined in a return type
Polynomial1.cpp:6: note: (perhaps a semicolon is missing after the definition of ‘Polynomial’)
Polynomial1.cpp: In function ‘Polynomial Polynomial()’:
Polynomial1.cpp:7: error: ‘termArray’ was not declared in this scope
Polynomial1.cpp:8: error: ‘capacity’ was not declared in this scope
Polynomial1.cpp:9: error: ‘terms’ was not declared in this scope
Polynomial1.cpp: At global scope:
Polynomial1.cpp:12: error: ‘poly’ does not name a type
Polynomial1.cpp:14: error: expected constructor, destructor, or type conversion before ‘>>’ token
Polynomial1.cpp:15: error: expected constructor, destructor, or type conversion before ‘&’ token
Polynomial1.cpp:18: error: expected unqualified-id before ‘for’
Polynomial1.cpp:22: error: expected unqualified-id before ‘return’
Polynomial1.cpp:23: error: expected declaration before ‘}’ token
btw, i am a n00b when it comes to this stuff.