C++ Bc. 12 cpp: Porovnání verzí
m (Stránka C plus plus Bc. 12 cpp přemístěna na stránku C++ Bc. 12 cpp) |
m (+kategorie programovani, c++) |
||
Řádek 60: | Řádek 60: | ||
[ [[C plus plus Bc. 12|Zpět]] ] | [ [[C plus plus Bc. 12|Zpět]] ] | ||
[[Kategorie:Programování]] | |||
[[Kategorie:C++]] |
Verze z 2. 9. 2006, 10:45
#include <iostream> #include <sstream> #include <algorithm> #include <vector> #include <cmath> struct Chyba { template <typename T> Chyba(T t) : text(t) {} std::string text; }; struct Bod { double x, y; }; typedef std::vector<Bod> Polygon; double plocha(const Polygon& p) { const int N = p.size(); if (N == 0) throw Chyba("polygon musi mit minimalne 3 vrcholy"); bool uzavreny = p[0].x == p[N-1].x && p[0].y == p[N-1].y; if ((uzavreny && N <= 3) || (!uzavreny && N <= 2)) throw Chyba("polygon musi mit minimalne 3 vrcholy"); double s = 0; if (!uzavreny) s += (p[N-1].x - p[0].x)*(p[N-1].y + p[0].y); for (int i=0; i<N-1; i++) s += (p[i+1].x - p[i].x)*(p[i+1].y + p[i].y); return std::abs(s/2); } int main() { using namespace std; Polygon p; istringstream input("1 1 0 2 -1 1 -1 -1 1 -1"); Bod b; while (input >> b.x >> b.y) p.push_back(b); try { cout << "Plocha = " << plocha(p); } catch(Chyba &ch) { cout << ch.text << endl; } }
[ Zpět ]