Thread: IDEA: Equation solver

  1. #1
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    IDEA: Equation solver

    The task is to write an equation solver, which takes a string expression containing a mathematical formula, then calculating its roots.
    For simplicitys sake, it doesn't have to be able to solve equations larger than grade 2 (that's extra bonus points if it does ).

    The letter X (capital x) will be the unknown variable.

    The string expression can contain extra spaces, so your function must handle this properly.

    Bonus points are awarded if the function can handle parantheses and division, but this is not required.

    Examples of string input:
    Code:
    "X = 5"
    "2*X = 12 - 4"
    "X+8 = 78-X + 3*X"
    "X^2 = 4*2 + 8"
    "5*X^2 + 12*X + 8 = -X + 3*X^2 - 3 - X +8*X + 15"
    "X =                -8"
    "            X     =8         "
    The syntax of this function will look like this:

    void SolveEquation(const char* Equation, char* ResultBuffer);

    Where Equation is the string containing the equation, and ResultBuffer is the buffer where you store the answer after it has been calculated. This buffer is allocated outside the function, so your function can assume that there is enough space to store your answer.
    You decide the syntax of this answer yourself. It could look something like this:
    Code:
    "r1 = ???, r2 = ???"
    "Answer: ???, ???"
    "??? ???"
    Whatever, you decide!


    Alternative solution:

    If you find this too easy, you can make a function with the syntax:

    void SolveEquation(const char* Equation, char Variable, char* ResultBuffer);

    Equation and ResultBuffer is the same as before. Variable is a character representing the unknown variable, and all other variables encountered in the Equation expression will be treated as unknown constants.

    Examples (X is the unknown for everyone here):
    Code:
    "X^2 + a*X + b = 0"
    "3*a*X^2 + 4*b*X + 3*a + c = b"
    Last edited by Magos; 10-21-2002 at 02:51 PM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  2. #2
    Registered User V1P3R V3N0M's Avatar
    Join Date
    Jan 2003
    Posts
    22
    grrrrrr nice thing... I'll try it out.. watch out the n00b trying it out!! (ME!)



    lets code!! no time to sleep... already 7h44 am anyway :P

    V1P3R V3N0M
    [email protected]

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Yes, this would make a good contest. I have done something similar before, though:
    http://www.cprogramming.com/cboard/s...uation+solving
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help needed! Finding Root of an Equation?
    By reader in forum C Programming
    Replies: 4
    Last Post: 06-13-2008, 10:10 AM
  2. Replies: 15
    Last Post: 11-04-2006, 04:06 AM
  3. Cubic equation program not giving me right answers
    By face_master in forum C++ Programming
    Replies: 8
    Last Post: 08-24-2006, 05:42 PM
  4. New idea on conveting byte to bits/bits to byte
    By megablue in forum C Programming
    Replies: 10
    Last Post: 10-26-2003, 01:16 AM
  5. IDEA: Equation Parser
    By ygfperson in forum Contests Board
    Replies: 0
    Last Post: 08-12-2002, 11:31 PM