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 {
- private:
- double start, end;
- public:
- double getStart() {
- return start;
- }
- double getEnd() {
- return end;
- }
- Range(double start, double end) {
- this->start = start;
- this->end = end;
- }
- bool contains(double val) {
- return val >= start && val <= end;
- }
- };
- class Function {
- private:
- //std::vector<Range *> domain;
- public:
- std::vector<Range *> domain;
- virtual void setDomain(std::vector<Range *> ranges) { domain = ranges; }
- virtual bool isWithinDomain(double x) {
- if (domain.size() == 0) {
- return true;
- }
- for (Range *domainPart : domain) {
- if (domainPart->contains(x)) {
- return true;
- }
- }
- return false;
- }
- virtual bool isWithinRange(double y) = 0;
- virtual double getY(double x) = 0;
- virtual std::vector<double> getRoots() = 0;
- virtual int getRootCount() {
- return getRoots().size();
- };
- };
- class LinearFunction : public Function {
- private:
- double a, b;
- public:
- LinearFunction(double x, double y) {
- this->a = x;
- this->b = y;
- }
- double getY(double x) override {
- return this->a * x + this->b;
- };
- bool isWithinRange(double y) override {
- if (this->a == 0) {
- return y == this->b;
- } else {
- double x = (y - this->b) / this->a;
- return isWithinDomain(x);
- }
- };
- vector<double> getRoots() override {
- vector<double> roots;
- if (a != 0) {
- double x = -(this->b) / this->a;
- if (isWithinDomain(x) == 1)roots.push_back(x);
- }
- return roots;
- };
- /*int getRootCount() override {
- return this->a == 0 ? 0 : 1;
- };*/
- };
- //-------------------------------
- class SquareFunction : public Function {
- private:
- double a, b, c;
- public:
- SquareFunction(double x, double y, double z) {
- this->a = x;
- this->b = y;
- this->c = z;
- }
- double getY(double x) override {
- double a = this->a;
- double b = this->b;
- double c = this->c;
- return a * x * x + b * x + c;
- }
- bool isWithinRange(double y) override {
- if (a == 0) {//liniowa
- if (b == 0) {
- return y == this->c;
- } else {
- double x = (y - this->c) / this->b;
- return isWithinDomain(x);
- }
- } else { //kwadratowa
- double p = -b / (2 * a);
- double delta = b * b - 4 * a * c;
- double q = -delta / (4 * a);
- double x = sqrt((y - q) / a) + p;
- if (isWithinDomain(x) == 1) {
- return (a * (x - p) * (x - p) + q) == (a * x * x + b * x + c);
- }
- }
- };
- vector<double> getRoots() override {
- vector<double> roots;
- double delta;
- double zero;
- /*
- if (a != 0) {
- double x = -(this->b) / this->a;
- if (isWithinDomain(x) == 1)roots.push_back(x);
- }
- */
- if (a == 0) { // liniowa
- if (b != 0) {
- double x = -(this->c) / this->b;
- if (isWithinDomain(x) == 1)roots.push_back(x);
- }
- } else { // kwadratowa
- delta = b * b - 4 * a * c;
- delta = sqrt(delta);
- if (delta > 0) {
- double x = (-b + delta) / (2 * a);
- roots.push_back(x);
- x = (-b - delta) / (2 * a);
- roots.push_back(x);
- } else if (delta == 0) {
- double x = -b / (2 * a);
- roots.push_back(x);
- }
- }
- return roots;
- };
- };
- ///------------------
- class HomographicFunction : public Function {
- private:
- double a, b, c, d;
- public:
- HomographicFunction(double a, double b, double c, double d) {
- this->a = a;
- this->b = b;
- this->c = c;
- this->d = d;
- };
- double getY(double x) {
- double a = this->a;
- double b = this->b;
- double c = this->c;
- double d = this->d;
- return (a * x + b) / (c * x + d);
- };
- bool isWithinRange(double y) override {
- if (y == a / c) return 0;
- double x = ((d * y - b) / (a * c - y * c));
- if (isWithinDomain(x) == 1);
- return y == (a * x + b) / (c * x + d);
- };
- bool isWithinDomain(double x) {
- if (domain.size() == 0 && x != -d / c) {
- return true;
- }
- for (Range *domainPart : domain) {
- if (domainPart->contains(x)) {
- return true;
- }
- }
- return false;
- }
- vector<double> getRoots() override {
- vector<double> roots;
- double x;
- if (-d / c != 0) {
- x = -b / a;
- roots.push_back(x);
- }
- if (-d / c == 0 && -b / a == 0) {
- x = -b / a;
- roots.push_back(x);
- }
- return roots;
- };
- };
- ///---------------
- 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