# [HELP] Program that checks conic section

• 09-19-2012
Arbyn Acosta
[HELP] Program that checks conic section
How can I make a program that accepts equations then prints out if "CIRCLE", "ELLIPSE", "PARABOLA" or "HYPERBOLA". This is my first try. The problem is that!!! It won't work. I think this is my 3rd work.

When I enter x^2+y^2 it says ELLIPSE. Its supposed to be a CIRCLE

Quote:

If x² and y² both have the same coefficients like x² + y² or 3x² + 3y², then it is a circle.

If x² and y² both have different coefficients that have the same sign, like 4x² + 9y², or x² + 16y², then it is an ellipse.

If x² and y² have different signs, like 25x² - 9y², or 16y² - x², then it is a hyperbola.

If the equation has either x² and y², but not both, then it is a parabola.

Code:

```#include <ctype.h>#include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) {     char eq[256];     bool x_exist = false, y_exist = false, neg_exist = false;     int x, y;     fgets(eq, 256, stdin);     for (int i = 0, end = strlen(eq); i < end; i++)     {         if (eq[i] == 'x')         {             x_exist == true;             if (eq[i - 1] == '-' && neg_exist == false)             {                 x = 1;                 neg_exist = true;             }             else if (eq[i - 1] == '+')                 x = 1;             else if (isdigit(eq[i - 1]))             {                 x = atoi(&eq[i - 1]);                 if (eq[i - 1] == '-' && neg_exist == false)                 {                     x = 1;                     neg_exist = true;                 }                 else if (eq[i - 1] == '+')                     x = 1;             }         }         else if (eq[i] == 'y')         {             y_exist == true;             if (eq[i - 1] == '-' && neg_exist == false)             {                 y = 1;                 neg_exist = true;             }             else if (eq[i - 1] == '+')                 y = 1;             else if (isdigit(eq[i - 1]))             {                 y = atoi(&eq[i - 1]);                 if (eq[i - 1] == '-' && neg_exist == false)                 {                     y = 1;                     neg_exist = true;                 }                 else if (eq[i - 1] == '+')                     y = 1;             }         }     }     if ((!y_exist && x_exist) || (y_exist && !x_exist))         printf("PARABOLA");     else if (x == y)         printf("CIRCLE");     else if (x != y)         printf("ELLIPSE");     else if (neg_exist)         printf("HYPERBOLA");     return 0; }```
I'm also thinking that my way is not the best way because it uses TOO much conditions. I think...
• 09-19-2012
grumpy
You're confusing yourself by using x (in the expressions you type) as a variable that characterises a conic, and x (in your code) to be a coefficient. Similarly for y.

There is also the problem that your rules will not detect the fact that "4x^2 -4xy + 4y^2 = 3" describes an ellipse. Conics aren't necessarily symmetric around the x or y axis.
• 09-19-2012
Arbyn Acosta
Quote:

Originally Posted by grumpy
You're confusing yourself by using x (in the expressions you type) as a variable that characterises a conic, and x (in your code) to be a coefficient. Similarly for y.

There is also the problem that your rules will not detect the fact that "4x^2 -4xy + 4y^2 = 3" describes an ellipse. Conics aren't necessarily symmetric around the x or y axis.

I know that it wont detect an equation like "4x^2 -4xy + 4y^2 = 3" the problem is that I don't know what to do. That is why I'm asking and I am not confused by using x as a variable and as a coefficient...
• 09-19-2012