c++ project

my project is
Develop class Polynomial.This internal representation of a polynomial is an array of terms.Each term contains a coefficient and an exponent, hence you can have an array for coefficients and exponents as a member of your class(maximum number of terms will 100 however your class should also contain a member variable that indicates how many terms are there in a given instance of the polynomial class).
the term 2x^4 has a coefficient of 2 and exponent of 4. Develop a full class containing proper constructor and destructor functions as well as set and get functions. The class should also provide the following overloaded operator capabilities:
1 overload the addition operator (+) to add two polynomials.
2 overload the subtraction operator (-)to subtract two polynomials
3 overload the assignment operator to assign one polynomial to another
4 overload the multiplication operator(*) to multiply two polynomials
please help me in building this code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  #include<iostream>
using namespace std;


class Polynomial
{
private:
	int *Coefficient;
	int *Exponents;
	int Nterms;//number of terms
public:
	Polynomial();
	int getC(){return Coefficient};
	int getE(){return Exponents};
	void getUserInput();
	friend Polynomial operator+(const Polynomial&);
};

Polynomial operator+(Polynomial&A,Polynomial&B){ //Adds two polynomial objects when exponents become equal in the loop
Polynomial C;
for(int i=0;i<100;i++){
if(A.getE()[i]==B.getE()[i]){
C.getE()[i]=A.getE()[i];
C.getC()[i]=B.getE()[i]+A.getE()[i];
}

}
return C;
};

Polynomial operator-(Polynomial&A,Polynomial&B){//Sub two polynomial objects when exponents become equal in the loop
Polynomial C;
for(int i=0;i<100;i++){
if(A.getE()[i]==B.getE()[i]){
C.getE()[i]=A.getE()[i];
C.getC()[i]=B.getE()[i]-A.getE()[i];
}

}
return C;
};

//constructors
Polynomial::Polynomial()
{
	Nterms=0;
	Coefficient = new int[100];
	Exponents = new int[100];
	for (int i = 0; i < 100; i++)
	{
		Coefficient[i] = 0;
		Exponents[i] = 0;
	}
	
}


void Polynomial::getUserInput(){//Allows user to enter coef and expo
cout<<"Enter the Exponent of Polynomial: ";
cin>>Exponents[Nterms];
cout<<endl;
cout<<"Enter the Coefficient of Polynomial: ";
cin>>Coefficient[Nterms];
Nterms++;
}
What exactly you have problems with?


you can have an array for coefficients and exponents as a member of your class
"you can", but you might not!
Another take on your assigment (Requires C++11 support):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <vector>
#include <string>

struct Term
{
    int coeff;
    unsigned exp;
    Term(int c, unsigned e) : coeff(c), exp(e) {};
};

class Polynomial
{
  public:
    Polynomial() = default;
    Polynomial(const Polynomial&) = default;
    Polynomial(const std::vector<int>& c) : coeff(c) {};
    Polynomial(const Term t);
    Polynomial(const std::vector<Term>& t);

    std::string print() const;

    Polynomial& operator=(const Polynomial& other) = default;
//  ↓↓↓Too lazy to do that
//    Polynomial& operator*=(const Polynomial& other);
    Polynomial& operator-=(const Polynomial& other);
    Polynomial& operator+=(const Polynomial& other);
  private:
    std::vector<int> coeff;
};

Polynomial::Polynomial(const Term t) : coeff(t.exp + 1)
{
    coeff[t.exp] = t.coeff;
}

Polynomial::Polynomial(const std::vector<Term>& t)
{
    for(auto term: t)
        *this += term;
}

std::string Polynomial::print() const
{
    std::string temp;
    for(signed long long i = coeff.size() - 1; i >= 0; --i) {
        if (coeff[i] == 0)
            continue;
        if (temp != "")
            temp += " + ";
        if (coeff[i] != 1 || i == 0)
            temp += std::to_string(coeff[i]);
        if (i != 0)
            temp += "x^" + std::to_string(i);
    }
    return temp;
}

Polynomial& Polynomial::operator+=(const Polynomial& other)
{
    if(coeff.size() < other.coeff.size())
        coeff.resize(other.coeff.size());
    for(unsigned i = 0; i < std::min(coeff.size(), other.coeff.size()); ++i) {
        coeff[i] += other.coeff[i];
    }
    return *this;
}

Polynomial& Polynomial::operator-=(const Polynomial& other)
{
    if(coeff.size() < other.coeff.size())
        coeff.resize(other.coeff.size());
    for(unsigned i = 0; i < std::min(coeff.size(), other.coeff.size()); ++i) {
        coeff[i] -= other.coeff[i];
    }
    return *this;
}

const Polynomial operator+(const Polynomial& lhs, const Polynomial& rhs)
{
    Polynomial tmp(lhs);
    tmp += rhs;
    return tmp;
}

const Polynomial operator-(const Polynomial& lhs, const Polynomial& rhs)
{
    Polynomial tmp(lhs);
    tmp -= rhs;
    return tmp;
}

std::ostream& operator<<(std::ostream& lhs, const Polynomial& rhs)
{
    return lhs << rhs.print();
}


int main()
{
    Polynomial s({2, 1, 3}); //3x^2 + x + 3;
    std::cout << s << std::endl;
    Polynomial x(Term(8, 10)); //8x^10
    std::cout << x << std::endl;
    Polynomial r = s - x;
    std::cout << r << std::endl;
    r += Term(8, 10);
    std::cout << r;
}
3x^2 + x^1 + 2
8x^10
-8x^10 + 3x^2 + x^1 + 2
3x^2 + x^1 + 2
Notes: It is not efficient to store only large exponents in it. You need to overload operator += for Term to make it even remotely efficient. Using map of exponent β†’ coefficient would be more efficient in the long run.
Last edited on
thankyou very much
but there is a little problem
we have to use visual studio 2010
so we need the older version
can u edit it again please ?
and a little bit simpler so that I can understand it
Last edited on
Topic archived. No new replies allowed.