C++ QUICK HELP! SHOULD BE REALLY EASY! For this program, what #includes do i nee
ID: 643851 • Letter: C
Question
C++ QUICK HELP! SHOULD BE REALLY EASY!
For this program, what #includes do i need for it to work on lines 4, 5, and 6.
And I keep getting this error on line 275 and 278
Error 1 error C2109: subscript requires array or pointer type
3 IntelliSense: expression must have pointer-to-object type
#ifndef POLY_H
#define POLY_H
#include
#include
#include
using namespace std;
class PolyNom
{
public:
PolyNom();
PolyNom operator + (const PolyNom&) const;
PolyNom operator - (const PolyNom&) const;
PolyNom operator * (const PolyNom&);
PolyNom operator = (const PolyNom&);
PolyNom& operator +=(const PolyNom&);
PolyNom& operator -=(const PolyNom&);
PolyNom& operator *=(const PolyNom&);
void enterTerms();
void printPolyNom() const;
int getNumOfTerms();
int getTermExpo(int);
int getTermCoeff(int);
void setCoeff(int, int);
~PolyNom();
private:
int numOfTerms;
int expo[100];
int coeff[100];
void polyNomCombine(PolyNom&);
};
#endif
PolyNom::PolyNom()
{
for (int t = 0; t < 100; t++)
{
coeff[t] = 0;
expo[t] = 0;
}
}
void PolyNom::printPolyNom() const
{
int start;
bool zero = false;
if (coeff[0])
{
cout << coeff[0];
start = 1;
zero = true;
}
else
{
if (coeff[1])
{
cout << coeff[1] << 'x';
if ((expo[1] != 0) && (expo[1] != 1))
cout << '^' << expo[1];
zero = true;
}
start = 2;
}
for (int x = start; x < 100; x++)
{
if (coeff[x] != 0)
{
cout << showpos << coeff[x] << noshowpos << 'x';
if ((expo[x] != 0) && (expo[x] != 1))
cout << '^' << expo[x];
zero = true;
}
}
if (!zero)
cout << '0';
cout << endl;
}
PolyNom PolyNom::operator = (const PolyNom &r)
{
expo[0] = r.expo[0];
coeff[0] = r.coeff[0];
for (int s = 1; (s < 100); s++)
{
if (r.expo[s] != 0)
{
expo[s] = r.expo[s];
coeff[s] = r.coeff[s];
}
else
{
if (expo[s] == 0)
break;
expo[s] = 0;
coeff[s] = 0;
}
}
return *this;
}
PolyNom PolyNom::operator + (const PolyNom &r) const
{
PolyNom temp;
bool expoExists;
int s;
temp.coeff[0] = coeff[0] + r.coeff[0];
for (s = 1; (s < 100) && (r.expo[s] != 0); s++)
{
temp.coeff[s] = r.coeff[s];
temp.expo[s] = r.expo[s];
}
for (int x = 1; x < 100; x++)
{
expoExists = false;
for (int t = 1; (t < 100) && (!expoExists); t++)
if (expo[x] == temp.expo[t])
{
temp.coeff[t] += coeff[x];
expoExists = true;
}
if (!expoExists)
{
temp.expo[s] = expo[x];
temp.coeff[s] += coeff[x];
s++;
}
}
return temp;
}
PolyNom& PolyNom::operator +=(const PolyNom &r)
{
*this = *this + r;
return *this;
}
PolyNom PolyNom::operator - (const PolyNom &r) const
{
PolyNom temp;
bool expoExists;
int s;
temp.coeff[0] = coeff[0] - r.coeff[0];
for (s = 1; (s < 100) && (expo[s] != 0); s++)
{
temp.coeff[s] = coeff[s];
temp.expo[s] = expo[s];
}
for (int x = 1; x < 100; x++)
{
expoExists = false;
for (int t = 1; (t < 100) && (!expoExists); t++)
if (r.expo[x] == temp.expo[t])
{
temp.coeff[t] -= r.coeff[x];
expoExists = true;
}
if (!expoExists)
{
temp.expo[s] = r.expo[x];
temp.coeff[s] -= r.coeff[x];
s++;
}
}
return temp;
}
PolyNom& PolyNom::operator -=(const PolyNom &r)
{
*this = *this - r;
return *this;
}
PolyNom PolyNom::operator * (const PolyNom &r)
{
PolyNom temp;
int s = 1;
for (int x = 0; (x < 100) && (x == 0 || r.coeff[x] != 0); x++)
for (int y = 0; (y < 100) && y == 0 || r.coeff[y] != 0; y++)
if ((expo[x] == 0) && (r.expo[y] == 0))
temp.coeff[0] += coeff[x] * r.coeff[y];
else
{
temp.coeff[s] = coeff[x] * r.coeff[y];
temp.expo[s] = expo[x] + r.expo[y];
s++;
}
polyNomCombine(temp);
return temp;
}
void PolyNom::polyNomCombine(PolyNom &w)
{
PolyNom temp = w;
int exp;
for (int x = 0; x < 100; x++)
{
w.coeff[x] = 0;
w.expo[x] = 0;
}
for (int x = 1; x < 100; x++)
{
exp = temp.expo[x];
for (int y = x + 1; y < 100; y++)
if (exp == temp.expo[y])
{
temp.coeff[x] += temp.coeff[y];
temp.expo[y] = 0;
temp.coeff[y] = 0;
}
}
w = temp;
}
PolyNom& PolyNom::operator *=(const PolyNom &r)
{
*this = *this * r;
return *this;
}
void PolyNom::enterTerms()
{
bool found = false;
int c, e, term;
cout << " Enter number of polynomial terms: ";
cin >> numOfTerms;
for (int n = 1; n <= numOfTerms; n++)
{
cout << " Enter a coefficient: ";
cin >> c;
cout << "Enter exponent: ";
cin >> e;
if (c != 0)
{
if (e == 0)
{
coeff[0] += c;
continue;
}
for (term = 1; (term < 100) && (coeff[term] != 0); term++)
if (e == expo[term])
{
coeff[term] += c;
expo[term] = e;
found = true;
}
if (!found)
{
coeff[term] += c;
expo[term] = e;
}
}
}
}
int PolyNom::getNumOfTerms()
{
return numOfTerms;
}
int PolyNom::getTermExpo(int term)
{
return expo[term];
}
int PolyNom::getTermCoeff(int term)
{
return coeff[term];
}
void PolyNom::setCoeff(int term, int coeff)
{
if (coeff[term] == 0)
cout << "No term at this here, can't set term." << endl;
else
coeff[term] = coeff;
}
PolyNom::~PolyNom()
{
}
int main()
{
PolyNom a, b, c, t;
a.enterTerms();
b.enterTerms();
t = a;
cout << "First polynomial is: ";
a.printPolyNom();
cout << "Second polynomial is: ";
b.printPolyNom();
cout << " Adding the polynomials yields: ";
c = a + b;
c.printPolyNom();
cout << " += the polynomials: ";
a += b;
a.printPolyNom();
cout << " Subtracting the polynomials: ";
a = t;
c = a - b;
c.printPolyNom();
cout << " -= the polynomials: ";
a -= b;
a.printPolyNom();
cout << " Multiplying the polynomials: ";
a = t;
c = a * b;
c.printPolyNom();
cout << " *= the polynomials: ";
a *= b;
a.printPolyNom();
cout << endl;
system("pause");
return 0;
}
Explanation / Answer
Note:
According to the problem, you have created a Header file.
Place the header file separately.
The header file contains the declaration of the class. Save it as POLY_H.h
Next, include header files and place the definitions of the functions, in another .cpp file and save it as POLY_H.cpp.
The following format shows the including the header files in POLY_H.cpp as,
#include "POLY_H.h"
#include <iostream>
using namespace std;
Finally, place the header files and main functions in another .cpp file say Polynomialfunctions.cpp.
Here is the following code with modification and saved separately.
If not, place the header file separately as POLY_H.h and include this header file, define the class functions and main function in one .cpp file.
But, place the #include headers on the beginning of the program then define the class functions followed by main function.
----------------------------------------------------------------------------------------------------------------------------
Program Code:
//POLY_H.h
#ifndef POLY_H
#define POLY_H
class PolyNom
{
public:
PolyNom();
PolyNom operator + (const PolyNom&) const;
PolyNom operator - (const PolyNom&) const;
PolyNom operator * (const PolyNom&);
PolyNom operator = (const PolyNom&);
PolyNom& operator +=(const PolyNom&);
PolyNom& operator -=(const PolyNom&);
PolyNom& operator *=(const PolyNom&);
void enterTerms();
void printPolyNom() const;
int getNumOfTerms();
int getTermExpo(int);
int getTermCoeff(int);
void setCoeff(int, int);
~PolyNom();
private:
int numOfTerms;
int expo[100];
int coeff[100];
void polyNomCombine(PolyNom&);
};
#endif
//POLY_H.cpp
#include "stdafx.h"
#include "POLY_H.h"
#include <iostream>
using namespace std;
PolyNom::PolyNom()
{
for (int t = 0; t < 100; t++)
{
coeff[t] = 0;
expo[t] = 0;
}
}
void PolyNom::printPolyNom() const
{
int start;
bool zero = false;
if (coeff[0])
{
cout << coeff[0];
start = 1;
zero = true;
}
else
{
if (coeff[1])
{
cout << coeff[1] << 'x';
if ((expo[1] != 0) && (expo[1] != 1))
cout << '^' << expo[1];
zero = true;
}
start = 2;
}
for (int x = start; x < 100; x++)
{
if (coeff[x] != 0)
{
cout << showpos << coeff[x] << noshowpos << 'x';
if ((expo[x] != 0) && (expo[x] != 1))
cout << '^' << expo[x];
zero = true;
}
}
if (!zero)
cout << '0';
cout << endl;
}
PolyNom PolyNom::operator = (const PolyNom &r)
{
expo[0] = r.expo[0];
coeff[0] = r.coeff[0];
for (int s = 1; (s < 100); s++)
{
if (r.expo[s] != 0)
{
expo[s] = r.expo[s];
coeff[s] = r.coeff[s];
}
else
{
if (expo[s] == 0)
break;
expo[s] = 0;
coeff[s] = 0;
}
}
return *this;
}
PolyNom PolyNom::operator + (const PolyNom &r) const
{
PolyNom temp;
bool expoExists;
int s;
temp.coeff[0] = coeff[0] + r.coeff[0];
for (s = 1; (s < 100) && (r.expo[s] != 0); s++)
{
temp.coeff[s] = r.coeff[s];
temp.expo[s] = r.expo[s];
}
for (int x = 1; x < 100; x++)
{
expoExists = false;
for (int t = 1; (t < 100) && (!expoExists); t++)
if (expo[x] == temp.expo[t])
{
temp.coeff[t] += coeff[x];
expoExists = true;
}
if (!expoExists)
{
temp.expo[s] = expo[x];
temp.coeff[s] += coeff[x];
s++;
}
}
return temp;
}
PolyNom& PolyNom::operator +=(const PolyNom &r)
{
*this = *this + r;
return *this;
}
PolyNom PolyNom::operator - (const PolyNom &r) const
{
PolyNom temp;
bool expoExists;
int s;
temp.coeff[0] = coeff[0] - r.coeff[0];
for (s = 1; (s < 100) && (expo[s] != 0); s++)
{
temp.coeff[s] = coeff[s];
temp.expo[s] = expo[s];
}
for (int x = 1; x < 100; x++)
{
expoExists = false;
for (int t = 1; (t < 100) && (!expoExists); t++)
if (r.expo[x] == temp.expo[t])
{
temp.coeff[t] -= r.coeff[x];
expoExists = true;
}
if (!expoExists)
{
temp.expo[s] = r.expo[x];
temp.coeff[s] -= r.coeff[x];
s++;
}
}
return temp;
}
PolyNom& PolyNom::operator -=(const PolyNom &r)
{
*this = *this - r;
return *this;
}
PolyNom PolyNom::operator * (const PolyNom &r)
{
PolyNom temp;
int s = 1;
for (int x = 0; (x < 100) && (x == 0 || r.coeff[x] != 0); x++)
for (int y = 0; (y < 100) && y == 0 || r.coeff[y] != 0; y++)
if ((expo[x] == 0) && (r.expo[y] == 0))
temp.coeff[0] += coeff[x] * r.coeff[y];
else
{
temp.coeff[s] = coeff[x] * r.coeff[y];
temp.expo[s] = expo[x] + r.expo[y];
s++;
}
polyNomCombine(temp);
return temp;
}
void PolyNom::polyNomCombine(PolyNom &w)
{
PolyNom temp = w;
int exp;
for (int x = 0; x < 100; x++)
{
w.coeff[x] = 0;
w.expo[x] = 0;
}
for (int x = 1; x < 100; x++)
{
exp = temp.expo[x];
for (int y = x + 1; y < 100; y++)
if (exp == temp.expo[y])
{
temp.coeff[x] += temp.coeff[y];
temp.expo[y] = 0;
temp.coeff[y] = 0;
}
}
w = temp;
}
PolyNom& PolyNom::operator *=(const PolyNom &r)
{
*this = *this * r;
return *this;
}
void PolyNom::enterTerms()
{
bool found = false;
int c, e, term;
cout << " Enter number of polynomial terms: ";
cin >> numOfTerms;
for (int n = 1; n <= numOfTerms; n++)
{
cout << " Enter a coefficient: ";
cin >> c;
cout << "Enter exponent: ";
cin >> e;
if (c != 0)
{
if (e == 0)
{
coeff[0] += c;
continue;
}
for (term = 1; (term < 100) && (coeff[term] != 0); term++)
if (e == expo[term])
{
coeff[term] += c;
expo[term] = e;
found = true;
}
if (!found)
{
coeff[term] += c;
expo[term] = e;
}
}
}
}
int PolyNom::getNumOfTerms()
{
return numOfTerms;
}
int PolyNom::getTermExpo(int term)
{
return expo[term];
}
int PolyNom::getTermCoeff(int term)
{
return coeff[term];
}
void PolyNom::setCoeff(int term, int coef)
{
if (coeff[term] == 0)
cout << "No term at this here, can't set term." << endl;
else
coeff[term] = coef;
}
PolyNom::~PolyNom()
{
}
// PolynimialFunction-class-header.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "POLY_H.h"
#include <iostream>
using namespace std;
int main()
{
PolyNom a, b, c, t;
a.enterTerms();
b.enterTerms();
t = a;
cout << "First polynomial is: ";
a.printPolyNom();
cout << "Second polynomial is: ";
b.printPolyNom();
cout << " Adding the polynomials yields: ";
c = a + b;
c.printPolyNom();
cout << " += the polynomials: ";
a += b;
a.printPolyNom();
cout << " Subtracting the polynomials: ";
a = t;
c = a - b;
c.printPolyNom();
cout << " -= the polynomials: ";
a -= b;
a.printPolyNom();
cout << " Multiplying the polynomials: ";
a = t;
c = a * b;
c.printPolyNom();
cout << " *= the polynomials: ";
a *= b;
a.printPolyNom();
cout << endl;
system("pause");
return 0;
}
Sample output:
Enter number of polynomial terms: 4
Enter a coefficient: 3
Enter exponent: 3
Enter a coefficient: 2
Enter exponent: 2
Enter a coefficient: 1
Enter exponent: 1
Enter a coefficient: 5
Enter exponent: 0
Enter number of polynomial terms: 4
Enter a coefficient: 2
Enter exponent: 3
Enter a coefficient: 1
Enter exponent: 2
Enter a coefficient: 6
Enter exponent: 1
Enter a coefficient: 4
Enter exponent: 0
First polynomial is:
5+3x^3+2x^2+1x
Second polynomial is:
4+2x^3+1x^2+6x
Adding the polynomials yields:
9+5x^3+3x^2+7x
+= the polynomials:
9+5x^3+3x^2+7x
Subtracting the polynomials:
1+1x^3+1x^2-5x
-= the polynomials:
1+1x^3+1x^2-5x
Multiplying the polynomials:
20+35x^3+19x^2+34x
*= the polynomials:
20+35x^3+19x^2+34x
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.