Problem p00079 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

double EPS = 1e-10;
const double PI = acos(-1);

double add(double a, double b) {
  if (abs(a + b) < EPS * (abs(a) + abs(b)))
    return 0;
  return a + b;
}

struct point {
  double x, y;
  point() {}
  point(double x, double y) : x(x), y(y) {}

  point operator+(point p) { return point(add(x, p.x), add(y, p.y)); }

  point operator-(point p) { return point(add(x, -p.x), add(y, -p.y)); }

  point operator*(double d) { return point(x * d, y * d); }

  point operator/(double d) { return point(x / d, y / d); }

  bool operator==(const point &p) const {
    return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
  }
};

struct segment {
  point a, b;
};

double dot(point a, point b) { return (a.x * b.x + a.y * b.y); }

double cross(point a, point b) { return (a.x * b.y - a.y * b.x); }

double vec_abs(point a) { return sqrt(a.x * a.x + a.y * a.y); }

double dist(point a, point b) {
  return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2));
}

int ccw(point p0, point p1, point p2) {
  point a = p1 - p0;
  point b = p2 - p0;
  if (cross(a, b) > EPS)
    return +1;
  if (cross(a, b) < EPS)
    return -1;
  if (dot(a, b) < -EPS)
    return +2;
  if (a.x * a.x + a.y * a.y < b.x * b.x + b.y * b.y)
    return -2;
  return 0;
}

vector<segment> construct_edge(point *p, int n) {
  vector<segment> seg, seg2;
  segment temp;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i != j) {
        temp.a = p[i];
        temp.b = p[j];
        seg.push_back(temp);
      }
    }
  }

  int t = seg.size();
  for (int i = 0; i < t; i++) {
    int fg = 0;
    for (int j = 0; j < t; j++) {
      if (i != j && vec_abs(seg[i].b - seg[j].a) <= EPS) {
        if (ccw(seg[i].a, seg[i].b, seg[j].b) == 1) {
          fg = 1;
          break;
        }
      }
    }
    if (!fg)
      seg2.push_back(seg[i]);
  }
  return seg2;
}

/*
double areaPol(point* ps, int n){
  vector<segment>t=construct_edge(ps,n);

  int tsz=t.size();
  double area=0;
  for(int i=0;i<tsz;i++){
    area+=abs((t[i].a.x-t[i].b.x)*(t[i].a.y+t[i].b.y));
  }
  return abs(area)/2.0;
}
*/
/*
double areaPol(point* ps, int n){
  vector<segment>t=construct_edge(ps,n);

  int tsz=t.size();
  point c;
  c.x=c.y=0;
  for(int i=0;i<n;i++)c=c+ps[i];
  c=c/n;

  double area=0;
  for(int i=0;i<tsz;i++){
    double a=dist(t[i].a,t[i].b);
    double b=dist(t[i].a,c);
    double d=dist(t[i].b,c);
    double z=(a+b+d)/2;
    area+=sqrt(z*(z-a)*(z-b)*(z-d));
  }
  return area;
}
*/

int main(void) {
  point p[21];
  int n = 0;
  char k;
  double x, y;

  while (cin >> x >> k >> y) {
    if (cin.eof())
      break;
    p[n].x = x;
    p[n].y = y;
    n++;
  }
  p[n] = p[0];
  double s;
  for (int j = 0; j < n; j++)
    s += (p[j].x - p[j + 1].x) * (p[j].y + p[j + 1].y);
  printf("%.6f\\n", abs(s / 2));

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

double EPS = 1e-10;
const double PI = acos(-1);

double add(double a, double b) {
  if (abs(a + b) < EPS * (abs(a) + abs(b)))
    return 0;
  return a + b;
}

struct point {
  double x, y;
  point() {}
  point(double x, double y) : x(x), y(y) {}

  point operator+(point p) { return point(add(x, p.x), add(y, p.y)); }

  point operator-(point p) { return point(add(x, -p.x), add(y, -p.y)); }

  point operator*(double d) { return point(x * d, y * d); }

  point operator/(double d) { return point(x / d, y / d); }

  bool operator==(const point &p) const {
    return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
  }
};

struct segment {
  point a, b;
};

double dot(point a, point b) { return (a.x * b.x + a.y * b.y); }

double cross(point a, point b) { return (a.x * b.y - a.y * b.x); }

double vec_abs(point a) { return sqrt(a.x * a.x + a.y * a.y); }

int ccw(point p0, point p1, point p2) {
  point a = p1 - p0;
  point b = p2 - p0;
  if (cross(a, b) > EPS)
    return +1;
  if (cross(a, b) < EPS)
    return -1;
  if (dot(a, b) < -EPS)
    return +2;
  if (a.x * a.x + a.y * a.y < b.x * b.x + b.y * b.y)
    return -2;
  return 0;
}

vector<segment> construct_edge(point *p, int n) {
  vector<segment> seg, seg2;
  segment temp;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i != j) {
        temp.a = p[i];
        temp.b = p[j];
        seg.push_back(temp);
      }
    }
  }

  int t = seg.size();
  for (int i = 0; i < t; i++) {
    int fg = 0;
    for (int j = 0; j < t; j++) {
      if (i != j && vec_abs(seg[i].b - seg[j].a) <= EPS) {
        if (ccw(seg[i].a, seg[i].b, seg[j].b) == 1) {
          fg = 1;
          break;
        }
      }
    }
    if (!fg)
      seg2.push_back(seg[i]);
  }
  return seg2;
}

double areaPol(point *ps, int n) {
  vector<segment> t = construct_edge(ps, n);

  int tsz = t.size();
  double area = 0;
  for (int i = 0; i < tsz; i++) {
    area += cross(t[i].a, t[i].b) / 2;
  }
  return abs(area);
}

int main(void) {
  point p[20];
  int n = -1;

  for (; ~scanf("%lf,%lf", &p[n].x, &p[n].y); n++)
    ;

  printf("%.6f\\n", areaPol(p, n));

  return 0;
}