int main(int argc, char *argv[]) { if (argc < 4) { fprintf(stderr, "Missing arguments\n"); exit(EXIT_FAILURE); } const double a = atof(argv[1]); const double b = atof(argv[2]); const double c = atof(argv[3]); Quad_root result; result = quad_solve(a, b, c); if (result.count > 0) { printf("x1 = %lf x2 = %lf\n", result.x1, result.x2); } else if (result.count == 0) { printf("No real roots\n"); } else { printf("Coef a = 0\n"); } return EXIT_SUCCESS; }
int main() try{ cout << "Quadratic equations are of the form: a*x*x + b*x + c = 0\n" << "Please enter a, b and c, the roots of the equation can be found.\n"; double a = 0; double b = 0; double c = 0; cin >> a >> b >> c; quad_solve(a, b, c); return 0; } catch (exception& e) { cerr << "Error: " << e.what() << '\n'; } catch (...) { cerr << "Unknown exception.\n"; }
/* given a quad-curve and a point (x,y), chop the quad at that point and return the new quad's offCurve point. Should only return false if the computed pos is the start of the curve (i.e. root == 0) */ static bool quad_pt2OffCurve(const SkPoint quad[3], SkScalar x, SkScalar y, SkPoint* offCurve) { const SkScalar* base; SkScalar value; if (SkScalarAbs(x) < SkScalarAbs(y)) { base = &quad[0].fX; value = x; } else { base = &quad[0].fY; value = y; } // note: this returns 0 if it thinks value is out of range, meaning the // root might return something outside of [0, 1) SkScalar t = quad_solve(base[0], base[2], base[4], value); if (t > 0) { SkPoint tmp[5]; SkChopQuadAt(quad, tmp, t); *offCurve = tmp[1]; return true; } else { /* t == 0 means either the value triggered a root outside of [0, 1) For our purposes, we can ignore the <= 0 roots, but we want to catch the >= 1 roots (which given our caller, will basically mean a root of 1, give-or-take numerical instability). If we are in the >= 1 case, return the existing offCurve point. The test below checks to see if we are close to the "end" of the curve (near base[4]). Rather than specifying a tolerance, I just check to see if value is on to the right/left of the middle point (depending on the direction/sign of the end points). */ if ((base[0] < base[4] && value > base[2]) || (base[0] > base[4] && value < base[2])) // should root have been 1 { *offCurve = quad[1]; return true; } } return false; }