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 start;
- double end;
- double getStart() {
- double zwrot = this->start;
- return zwrot;
- }
- double getEnd() {
- double zwrot = this->end;
- return zwrot;
- }
- Range(double s, double e) {
- start = s;
- end = e;
- }
- bool contains(double val) {
- if (val >= start && val <= end) return true;
- return false;
- }
- };
- class Function {
- public:
- vector<Range *> dziedzina;
- virtual void setDomain(vector<Range *> w) {
- dziedzina = w;
- }
- virtual bool isWithinDomain(double xx) {
- int ilosc=0;
- for(int i=0 ; i<dziedzina.size()-1 &&dziedzina.size()!=0 ; i++){
- ilosc++;
- }
- if (ilosc == 0) {
- return true;
- }
- for (Range *domainPart : dziedzina) {
- if (domainPart->contains(xx)) {
- return true;
- }
- }
- return false;
- }
- virtual bool isWithinRange(double y) = 0;
- virtual double getY(double x) = 0;
- virtual vector<double> getRoots() = 0;
- virtual int getRootCount() {
- int ilosc = 0;
- return getRoots().size();
- }
- };
- class LinearFunction : public Function {
- public:
- double a, b;
- LinearFunction(double x, double y) {
- a = x;
- b = y;
- }
- double getY(double x) {
- double x1, y1;
- x1 = a;
- y1 = b;
- return x1 * x + y1;
- };
- bool isWithinRange(double y) {
- if (a == 0) return y == b;
- else {
- double x = y - b;
- x /= a;
- return isWithinDomain(x);
- }
- };
- vector<double> getRoots() {
- vector<double> roots;
- if (a != 0) {
- double x = -b;
- x /= a;
- if (isWithinDomain(x))roots.push_back(x);
- }
- return roots;
- };
- };
- 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 x) override {
- double wartosc = a * x * x;
- wartosc += b * x;
- wartosc += c;
- return wartosc;
- }
- bool isWithinRange(double y) {
- if (a == 0) {//liniowa
- if (b == 0) {
- return c;
- } else {
- double p = y - c;
- p /= b;
- return isWithinDomain(p);
- }
- } else { //kwadratowa
- double p = -b;
- p /= (2 * a);
- double d = b * b;
- d -= (4 * a * c);
- double q = -d;
- q /= (4 * a);
- double x = sqrt((y - q) / a) + p;
- if (isWithinDomain(x)) {
- double zwrot1, zwrot2;
- zwrot1 = (a * (x - p) * (x - p) + q);
- 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) {
- double x = -(this->c) / this->b;
- if (isWithinDomain(x) == 1)roots.push_back(x);
- }
- } else { // kwadratowa
- d = b * b - 4 * a * c;
- if (d > 0) {
- d = sqrt(d);
- double x = (-b + d) / (2 * a);
- roots.push_back(x);
- x = (-b - d) / (2 * a);
- roots.push_back(x);
- } else if (d == 0) {
- double x = -b;
- x /= (2 * a);
- roots.push_back(x);
- }
- }
- 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 x) {
- double y = (a * x + b) / (c * x + d);
- return y;
- };
- bool isWithinRange(double y) override {
- if (y == a / c) return 0;
- double x = ((d * y - b) / (a - y * c));
- if (isWithinDomain(x) == 1) {
- double wartosc = (a * x + b) / (c * x + d);
- if (y == wartosc)return true;
- } else return false;
- };
- bool isWithinDomain(double x) {
- if (dziedzina.size() == 0 && x != -d / c) {
- return true;
- }
- for (Range *domainPart : dziedzina) {
- if (domainPart->contains(x)) {
- return true;
- }
- }
- return false;
- }
- vector<double> getRoots() {
- vector<double> roots;
- if (-d / c != 0) {
- roots.push_back(-b / a);
- }
- return roots;
- }
- };
- int main() {
- LinearFunction *liniowa = new LinearFunction(2,-3);
- cout<< liniowa->isWithinDomain(5)<< endl;
- cout<< liniowa->getRootCount()<< endl; //1
- cout<< liniowa->getRootCount()<< endl; //1
- cout<< liniowa->getY(1.5)<< endl; //0
- cout<< liniowa->isWithinRange(4)<< endl; //1
- vector<double> wektorek = liniowa->getRoots();
- cout<< wektorek[0]<< endl; //1.5
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement