Hi, commemorate.
The structure of your program looks a bit complicated, doesn’t it?
In one class (Polynomial) you declare an array of elements of another class (term), which is friend of the first class. The only advantage I can see, at the present stage of development, of this ‘friendship’ is that you don’t need to use some getter/setter methods.
An other point I should avoid is the usage of C-style arrays, since std::vectors are much, much more reliable.
Then, but that’s absolutely personal, I’d try to decide a style for giving names to the different elements of my code. For example, capitalized camel-case for classes, non capitalized camel-case for functions and all lowercase (with underscores or not) for variables, or another one that can suit your needs.
If you decided to give a chance to vectors, you could see how they can make your code easier to read. Here are some hints:
Term.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#ifndef TERM_H
#define TERM_H
class Term
{
public:
Term() = default;
double getCoef() const {return coef;}
int getExp() const {return exp;}
void setCoef(const double coef_arg) {coef = coef_arg;}
void setExp(const int exp_arg) {exp = exp_arg;}
private:
double coef {};
int exp {};
};
#endif // TERM_H
|
Terms.cpp:
Polynomial.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include <iostream>
#include <vector>
#include "Term.h"
class Polynomial
{
public:
Polynomial(const int);
void input();
void output();
private:
const int max_terms;
static int free;
int start {}, finish {};
static std::vector<Term> terms;
};
#endif // POLYNOMIAL_H
|
Polynomial.cpp:
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
|
#include "Polynomial.h"
int Polynomial::free = 0;
std::vector<Term> Polynomial::terms;
Polynomial::Polynomial(const int maxterms) : max_terms {maxterms}
{}
void Polynomial::input()
{
start = free;
for (int i = free; i < max_terms; i++)
{
std::cout << "Please, type the coefficient of polynomial "
"or 0 to stop: ";
double tmpdouble;
std::cin >> tmpdouble;
if (0 == tmpdouble)
{
free = i;
finish = i-1;
break;
}
Term tmpterm;
tmpterm.setCoef(tmpdouble);
terms.push_back(tmpterm);
}
for (int i = start; i <= finish; i++)
{
std::cout << "Please type the degree of polynomial: ";
int tmpint;
std::cin >> tmpint;
terms.at(i).setExp(tmpint);
}
}
void Polynomial::output()
{
for (int i = start; i <= finish ; i++)
{
std::cout << terms[i].getCoef() << "x^" << terms[i].getExp();
( i <= finish ) ? std::cout << "+" : std::cout << std::endl;
}
std::cout << "free: " << free << std::endl;
}
|
main.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <iostream>
#include <limits>
#include "Polynomial.h"
constexpr int MAXTERMS = 10;
int main()
{
Polynomial polyn(MAXTERMS);
polyn.input();
polyn.output();
std::cout << "\n\nPress ENTER to continue...\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return 0;
}
|
Please, type the coefficient of polynomial or 0 to stop: 2.2
Please, type the coefficient of polynomial or 0 to stop: 4.4
Please, type the coefficient of polynomial or 0 to stop: 6.6
Please, type the coefficient of polynomial or 0 to stop: 0
Please type the degree of polynomial: 3
Please type the degree of polynomial: 5
Please type the degree of polynomial: 7
2.2x^3+4.4x^5+6.6x^7+free: 3
|