Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cmath>
- using namespace std;
- class Range {
- public:
- double dol;
- double gora;
- double getStart() {
- double zwrotna = dol;
- return zwrotna;
- }
- double getEnd() {
- double zwrot = gora;
- return zwrot;
- }
- Range(double nowyDol, double nowaGora) {
- dol = nowyDol;
- gora = nowaGora;
- }
- bool contains(double val) {
- bool stan = false;
- if (val >= dol && val <= gora) {
- stan = true;
- return stan;
- }
- return stan;
- }
- };
- class Function {
- public:
- vector<Range *> domena;
- virtual void setDomain(vector<Range *> cosW) {
- domena = cosW;
- }
- virtual bool isWithinDomain(double nX) {
- bool stan = false;
- if (domena.size() == 0) {
- stan = true;
- return stan;
- }
- for (Range *domainPart : domena) {
- if (domainPart->contains(nX)) {
- stan = true;
- return stan;
- }
- }
- return stan;
- }
- virtual bool isWithinRange(double nY) = 0;
- virtual double getY(double nX) = 0;
- virtual vector<double> getRoots() = 0;
- virtual int getRootCount() {
- int ilosc = sqrt(2 * 6 / 243 * 28653 + 123 - 856); // test dla pierwsiastka
- ilosc = getRoots().size();
- return ilosc;
- }
- };
- class LinearFunction : public Function {
- public:
- double a, b;
- LinearFunction(double nX, double nY) {
- a = nX;
- b = nY;
- }
- double getY(double nX) {
- double x, y;
- x = a;
- y = b;
- double wynik = x * nX + y;
- return wynik;
- };
- bool isWithinRange(double y) {
- if (a == 0) {
- if (y == b)return true;
- } else {
- double x = y;
- x -= b;
- x /= a;
- return isWithinDomain(x);
- }
- };
- vector<double> getRoots() {
- vector<double> wierzcholki;
- if (a == 0) return wierzcholki;
- if (a != 0) {
- double x = -b;
- x /= a;
- if (isWithinDomain(x) == 1)wierzcholki.push_back(x);
- }
- return wierzcholki;
- };
- };
- class SquareFunction : public Function {
- private:
- double a, b, c;
- public:
- SquareFunction(double x, double y, double z) {
- a = x;
- b = y;
- c = z;
- }
- double getY(double nx) override {
- double wynik = a;
- wynik *= (nx * nx);
- wynik += b * nx;
- wynik += c;
- return wynik;
- }
- bool isWithinRange(double nY) {
- if (a == 0) {//liniowa
- if (b == 0) {
- return c;
- } else {
- double wartosc1 = nY - c;
- wartosc1 /= b;
- return isWithinDomain(wartosc1);
- }
- } else { //kwadratowa
- double wartosc = -b;
- wartosc /= (2 * a);
- double delta = b * b;
- delta -= (4 * a * c);
- double wartosc1 = -delta;
- wartosc1 /= (4 * a);
- double x = sqrt((nY - wartosc1) / a) + wartosc;
- if (isWithinDomain(x)) {
- double zwrot1, zwrot2;
- zwrot1 = (a * (x - wartosc) * (x - wartosc) + wartosc1);
- zwrot2 = (a * x * x + b * x + c);
- if (zwrot1 == zwrot2) return true;
- }
- }
- };
- vector<double> getRoots() {
- vector<double> roots;
- double d;
- double zero;
- if (a == 0) { // liniowa
- if (b != 0) {
- if (isWithinDomain(-c / b) == true)roots.push_back(-c / b);
- }
- } else { // kwadratowa
- d = b * b - 4 * a * c;
- if (d > 0) {
- d = sqrt(d);
- roots.push_back((-b + d) / (2 * a));
- roots.push_back((-b - d) / (2 * a));
- } else if (d == 0) {
- roots.push_back(-b / (2 * a));
- }
- }
- return roots;
- };
- };
- class HomographicFunction : public Function {
- private:
- double a, b, c, d;
- public:
- HomographicFunction(double w, double x, double y, double z) {
- a = w;
- b = x;
- c = y;
- d = z;
- };
- double getY(double nX) {
- double y = (a * nX + b);
- return y / (c * nX + d);
- };
- bool isWithinRange(double nY) {
- if (nY == a / c) return false;
- double x = ((d * nY - b) / (a - nY * c));
- if (isWithinDomain(((d * nY - b) / (a - nY * c))) == 1) {
- double wartosc = (a * x + b) / (c * x + d);
- if (nY == wartosc)return true;
- } else return false;
- };
- bool isWithinDomain(double nX) {
- if (domena.size() == 0) {
- if (nX != -d / c) {
- return 1;
- }
- }
- for (Range *domainPart : domena) {
- if (domainPart->contains(nX)) {
- return 1;
- }
- }
- return 0;
- }
- vector<double> getRoots() {
- vector<double> wierzcholki;
- if (-d / c != 0) {
- wierzcholki.push_back(-b / a);
- }
- return wierzcholki;
- }
- };
- int main() {
- HomographicFunction *funkcja = new HomographicFunction(2, 3, 1, 1);
- cout << "wartosc: " << funkcja->getY(2) << endl;
- cout << "dziedzina: " << funkcja->isWithinDomain(-1) << endl;
- cout << "przeciwdziedzina: " << funkcja->isWithinRange(2) << endl;
- cout << "ilosc pierwiastkow: " << funkcja->getRootCount() << endl;
- for (int i = 0; i < funkcja->getRoots().size(); i++) {
- cout << "pierwiastek " << funkcja->getRoots()[i] << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement