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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
#include <algorithm>
#include <iostream>
#include <limits>
#include <tuple>
#include <vector>
struct Point {
Point(double xArg, double yArg)
: x{xArg}, y{yArg}
{}
double x, y;
};
bool property1 (const std::vector<Point>& points);
bool areEqual(const Point& point1, const Point& point2);
bool property2 (const std::vector<Point>& points);
bool barycenter (const std::vector<Point>& points);
Point calculateAverage(const std::vector<Point> &points);
std::vector<Point> buildOneVector(const std::vector<double> &vec);
std::vector<std::vector<Point>>
buildTestVectors(std::vector<std::vector<Point>>& testvecs);
void waitForEnter();
std::vector<Point> askUserForData();
void performControls(const std::vector<Point>& points);
int main ()
{
std::vector<std::vector<Point>> normvect;
buildTestVectors(normvect);
// First step: check if our vectors are normalized
for(size_t i{0}; i<normvect.size(); i++) {
std::cout << "\nChecking vector " << i+1 << ":\n";
performControls(normvect.at(i));
}
waitForEnter();
// Second step: ask for new vectors and check
int useransw = 0;
do {
std::cout << "\nNormalized vectors checker\n==========================\n"
<< "What do you want to do?"
<< "\n0) exit?"
<< "\n1) insert data into vector?"
<< "\nyour choice: ";
std::cin >> useransw;
std::vector<Point> points;
switch (useransw) {
case 0:
std::cout << "\nGoodby, then.\n";
break;
case 1:
points = askUserForData();
performControls(points);
break;
default:
break;
}
} while(useransw != 0);
return 0;
}
// Returns true if at least two elements into vector are different
bool property1 (const std::vector<Point>& points)
{
for(size_t i{0}; i<points.size()-1; i++) {
// std::cout << "\nComparing point[" << i << "].x (" << points.at(i).x
// << "), point[" << i << "].y (" << points.at(i).y
// << ") and point[" << i+1 << "].x (" << points.at(i+1).x
// << "), point[" << i+1 << "].y" << points.at(i+1).y
// << " --> They are ";
if(!areEqual(points.at(i), points.at(i+1))) {
// std::cout << "different.\n";
return true;
}/* else {
std::cout << "equal.\n";
}*/
}
return false;
}
bool areEqual(const Point& point1, const Point& point2)
{
return std::tie(point1.x, point1.y) == std::tie(point2.x, point2.y);
}
// Returns true if the sum of all the x-coordinates is equal to the sum
// of all the y-coordinates.
bool property2 (const std::vector<Point>& points)
{
double sum_x = 0, sum_y = 0;
for(const auto point: points) {
sum_x += point.x;
sum_y += point.y;
}
// std::cout << "\nSum of all the x: " << sum_x
// << "; sum of all the y: " << sum_y << '\n';
return sum_x == sum_y;
}
// Returns true if the barycenter point does not belong to the passed vector
bool barycenter(const std::vector<Point>& points)
{
Point baryc = calculateAverage(points);
// std::cout << "Barycenter: baryc.x = " << baryc.x << "; baryc.y = "
// << baryc.y << '\n';
auto findresult = std::find_if(std::begin(points), std::end(points),
[&baryc] (const Point& p) {
return (baryc.x == p.x) && (baryc.y == p.y);
});
return findresult == std::end(points);
}
Point calculateAverage(const std::vector<Point>& points)
{
Point average(0.0, 0.0);
for(const auto point : points) {
average.x += point.x;
average.y += point.y;
}
average.x /= points.size();
average.y /= points.size();
return average;
}
std::vector<Point> buildOneVector(const std::vector<double>& vec)
{
std::vector<Point> pointvec;
// int j = 0;
for(size_t i{0}; i<vec.size(); i+=2) {
pointvec.emplace_back(vec.at(i), vec.at(i+1));
// std::cout << "pointvec.at(" << j << ").x: " << pointvec.at(j).x
// << "; pointvec.at(" << j << ").y: " << pointvec.at(j).y << '\n';
// j++;
}
std::cout << '\n';
return pointvec;
}
std::vector<std::vector<Point>>
buildTestVectors(std::vector<std::vector<Point>>& testvecs)
{
// INPUT
// 3 0 0 0 0 0 0
// 4 1 0 1 1 1 0 1 0
// 3 0 1 0 -1 0 0
// 3 0 1 1 0 1 1
// 4 0 0 1 0 0 1 0 0
// 3 0 0 1 1 0 0
// First vector.
std::vector<double> coord = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
testvecs.push_back(buildOneVector(coord));
// Second vector.
coord.empty();
coord = {1.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0};
testvecs.push_back(buildOneVector(coord));
// Third vector.
coord.empty();
coord = {0.0, 1.0, 0.0, -1.0, 0.0, 0.0};
testvecs.push_back(buildOneVector(coord));
// Forth vector.
coord.empty();
coord = {0.0, 1.0, 1.0, 0.0, 1.0, 1.0};
testvecs.push_back(buildOneVector(coord));
// Fifth vector.
coord.empty();
coord = {0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0};
testvecs.push_back(buildOneVector(coord));
// Sixth vector.
coord.empty();
coord = {0.0, 0.0, 1.0, 1.0, 0.0, 0.0};
testvecs.push_back(buildOneVector(coord));
// waitForEnter();
return testvecs;
}
void waitForEnter()
{
std::cout << "\nPress ENTER to continue...\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
std::vector<Point> askUserForData()
{
std::cout << "Please, tell me how many points do you want to insert: ";
int num_of_points = 0;
std::cin >> num_of_points;
std::cout << "\nPlease insert data as a couple of double separated "
"by a space.";
Point tmppoint(0.0, 0.0);
std::vector<Point> points;
for(int i{0}; i<num_of_points; i++) {
std::cout << "\npoint coordinates x y: ";
std::cin >> tmppoint.x >> tmppoint.y;
points.push_back(tmppoint);
}
std::cout << "\n\nVector built.\n";
return points;
}
void performControls(const std::vector<Point>& points)
{
int passed_exams = 0;
if(property1(points)) {
std::cout << "1) there are at least two different elements: yes!\n";
passed_exams++;
} else {
std::cout << "1) *** Property 1 does not apply.\n";
return;
}
if(property2(points)) {
std::cout << "2) sum of all the x is equal to sum of all the y: yes!\n";
passed_exams++;
} else {
std::cout << "2) *** Property 2 does not apply.\n";
return;
}
if(barycenter(points)) {
std::cout << "3) barycenter doesn't belong to vector: yes!\n";
passed_exams++;
} else {
std::cout << "3) *** Property 3 does not apply.\n";
return;
}
// Pointless check, but... like placeholder for future improvements
if(2 < passed_exams)
std::cout << "this vector is normalized.\n";
}
|