Like Tree1Likes
  • 1 Post By Adak

[HELP] Program that checks conic section

This is a discussion on [HELP] Program that checks conic section within the C Programming forums, part of the General Programming Boards category; How can I make a program that accepts equations then prints out if "CIRCLE", "ELLIPSE", "PARABOLA" or "HYPERBOLA". This is ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Location
    Caloocan, Metro Manila, Philippines
    Posts
    17

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

    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...
    Last edited by Arbyn Acosta; 09-19-2012 at 06:12 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,195
    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.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Aug 2012
    Location
    Caloocan, Metro Manila, Philippines
    Posts
    17
    Quote Originally Posted by grumpy View Post
    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...

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Write out EXACTLY how the different equations will be entered into the program, and I'll fix it, because I'm confused whether it will be using the '^' char (ie., 4x^2), or not (4x).

    Like this:
    Ellipse: yadayadaya
    Circle: yadayadayada
    Parabola: yadayadayada
    etc.

    You are correct. Your logic is too Rube Goldberg.
    Last edited by Adak; 09-19-2012 at 08:52 AM.
    dmh2000 likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 17
    Last Post: 03-15-2012, 05:30 AM
  2. Using Checks in C - Help!
    By saddists in forum C Programming
    Replies: 4
    Last Post: 12-26-2011, 10:14 PM
  3. Input Checks
    By Elements in forum C Programming
    Replies: 5
    Last Post: 04-25-2006, 07:41 PM
  4. Heuristic Checks
    By cloudy in forum C++ Programming
    Replies: 2
    Last Post: 02-12-2006, 03:08 PM
  5. If Program from the tutorial section
    By k001k@t in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2003, 07:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21