
Другие языки программирования и технологии
Помогите решить задачу в С++

(x^2 + y^2 < 15^2) || (x^2 + y^2 > 25^2)
Задачи на то, лежит ли точка там и там стали слишком популярны в последнее время, поэтому я сделала шаблон, в котором я просто меняю условия в int main, которым должна соответствовать область
#include <iostream>
#include <vector>
#include <limits>
#include <functional>
#include <algorithm>
#include <cmath>
struct Point{
double x = 0;
double y = 0;
};
struct Bound{
std::function<bool(Point)> func = [](Point){return false;};
double x0 = std::numeric_limits<double>::min();
double x1 = std::numeric_limits<double>::max();
bool including = false;
Bound(){};
Bound(const std::function<bool(Point)> f);
Bound(const std::function<bool(Point)> f, double a, double b);
Bound(const std::function<bool(Point)> f, double a, double b, bool including);
};
std::istream& operator>>(std::istream& os, Point& p);
bool is_in_bounds(const Point& p, const std::vector<Bound> bounds);
bool is_in_bounds(const Point& p, const std::vector<Bound> bounds, bool reverse);
int main(){
Point p;
std::cin >> p;
std::vector<Bound> bounds;
Bound b = Bound();
b.x1 = -25;
bounds.push_back(b);
b = Bound();
b.x0 = 25;
bounds.push_back(b);
bounds.push_back(Bound([](Point p){
return p.y * p.y + p.x * p.x <= 25 * 25;
}, -25, 25, true));
bounds.push_back(Bound([](Point p){
return p.y * p.y + p.x * p.x >= 15 * 15;
}, -15, 15, true));
std::cout << (is_in_bounds(p, bounds, true) ? "Точка лежит в заштрихованной области" : "Точка не лежит в заштрихованной области") << std::endl;
return 0;
}
Bound::Bound(const std::function<bool(Point)> f){
func = f;
}
Bound::Bound(const std::function<bool(Point)> f, double a, double b){
func = f;
x0 = a;
x1 = b;
}
Bound::Bound(const std::function<bool(Point)> f, double a, double b, bool including){
func = f;
x0 = a;
x1 = b;
this->including = including;
}
std::istream& operator>>(std::istream& is, Point& p){
is >> p.x >> p.y;
return is;
}
bool is_in_bounds(const Point& p, const std::vector<Bound> bounds){
bool res = true;
std::for_each(bounds.cbegin(), bounds.cend(), [&res, &p](Bound bound){
if((p.x > bound.x0 && p.x < bound.x1) || (bound.including && (p.x == bound.x1 || p.x == bound.x0)))
res &= bound.func(p);
});
return res;
}
bool is_in_bounds(const Point& p, const std::vector<Bound> bounds, bool reverse){
return reverse ? !is_in_bounds(p, bounds) : is_in_bounds(p, bounds);
}
#include <iostream>
#include <vector>
#include <limits>
#include <functional>
#include <algorithm>
#include <cmath>
struct Point{
double x = 0;
double y = 0;
};
struct Bound{
std::function<bool(Point)> func = [](Point){return false;};
double x0 = std::numeric_limits<double>::min();
double x1 = std::numeric_limits<double>::max();
bool including = false;
Bound(){};
Bound(const std::function<bool(Point)> f);
Bound(const std::function<bool(Point)> f, double a, double b);
Bound(const std::function<bool(Point)> f, double a, double b, bool including);
};
std::istream& operator>>(std::istream& os, Point& p);
bool is_in_bounds(const Point& p, const std::vector<Bound> bounds);
bool is_in_bounds(const Point& p, const std::vector<Bound> bounds, bool reverse);
int main(){
Point p;
std::cin >> p;
std::vector<Bound> bounds;
Bound b = Bound();
b.x1 = -25;
bounds.push_back(b);
b = Bound();
b.x0 = 25;
bounds.push_back(b);
bounds.push_back(Bound([](Point p){
return p.y * p.y + p.x * p.x <= 25 * 25;
}, -25, 25, true));
bounds.push_back(Bound([](Point p){
return p.y * p.y + p.x * p.x >= 15 * 15;
}, -15, 15, true));
std::cout << (is_in_bounds(p, bounds, true) ? "Точка лежит в заштрихованной области" : "Точка не лежит в заштрихованной области") << std::endl;
return 0;
}
Bound::Bound(const std::function<bool(Point)> f){
func = f;
}
Bound::Bound(const std::function<bool(Point)> f, double a, double b){
func = f;
x0 = a;
x1 = b;
}
Bound::Bound(const std::function<bool(Point)> f, double a, double b, bool including){
func = f;
x0 = a;
x1 = b;
this->including = including;
}
std::istream& operator>>(std::istream& is, Point& p){
is >> p.x >> p.y;
return is;
}
bool is_in_bounds(const Point& p, const std::vector<Bound> bounds){
bool res = true;
std::for_each(bounds.cbegin(), bounds.cend(), [&res, &p](Bound bound){
if((p.x > bound.x0 && p.x < bound.x1) || (bound.including && (p.x == bound.x1 || p.x == bound.x0)))
res &= bound.func(p);
});
return res;
}
bool is_in_bounds(const Point& p, const std::vector<Bound> bounds, bool reverse){
return reverse ? !is_in_bounds(p, bounds) : is_in_bounds(p, bounds);
}
Похожие вопросы
- Помогите решить задачу на Турбо паскале
- Помогите решить задачу.
- Помогите решить задачу на программирование!
- Народ, слезно прошу помочь решить задачу по информатике (програмирование), я просто ноль в этом(((
- Помогите решить задачу на Basic
- Помогите решить задачи на Си! Пожалуйста!!!
- помогите решить задачи
- Помогите решить задачи по Паскалю
- Помогите решить задачу в паскале
- помогите решить задачу на паскале: напечатать "столбиком" значения sin2, sin3, ..sin 20.