[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...