Thread: Loading & using equations

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    2

    Angry Loading & using equations

    I'm a beginner programmer stuck on how to implement this function for my program, any suggestions will be very welcome (save my hair).
    I want the user to input a mathematical equation e.g. y=m*x+c (using Visual C++).
    My problem is to then find a way that this can be a) used by the program, b) saved to file, c) opened and used from the saved file.
    As you can see this has me pretty much stumped......what variable type do i use that will recognise the mathematical symbols? Do i pass the input equation as a string vaiable and then write coding to pull out all mathematical symbols.......like pulling teeth
    Help basically.
    Thanks.

  2. #2
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    You should read the equation as string, and then start analysing it char by cha, ie: read the symbols, variables, etc...
    Then you check if it's in the form that you can solve.
    Then start solving...

    Where exactly are you stuck?
    none...

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    2
    Thought as much......will probably take a lot of work as the equations will include, powers, exponentials, nestled brackets etc.

    I've not started writing the code yet as i'm still in the planning stage on program functionalities and so structures.
    My problem will basically just be converting between the user input equation as a string and then converting to expressions. Once this is done implementing the equation will be simple. As for saving and loaing the data to/from file ...i'll again use the string format.

    Cheers for the help. If you know of any sample coding to analyse the string i would be more than keen to to view it.
    Cheers.

  4. #4
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    If I were you I'd pick a language that makes the job trivial :-)
    Code:
    #!/usr/bin/perl -w
    
    use strict;
    
    print "Enter an equation: ";
    chomp(my $equation = <STDIN>);
    
    print eval $equation;
    *Cela*

  5. #5
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    I'll give you some help...
    if the user inputs: 5+6
    and you read that into a string:
    Code:
    char *string;
    you will read string[0], and string[2] and use atoi() to convert it to int.
    then you read string[1] and use a swith statement to choose what operator to use.
    none...

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You could try to split the inputted equation into a binary tree. That way, the operations will be a lot easier to handle:
    Code:
    12*x + 4*x^2 = 2*x
    
             =
           /   \
          /     \
         +       *
       /   \    / \
      *     *  2   x
     / \   / \
    12  x 4   ^
             / \
            x   2
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cargo loading system
    By kyle rull in forum C Programming
    Replies: 1
    Last Post: 04-20-2009, 12:16 PM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Loading a bitmap (Without using glaux)
    By Shamino in forum Game Programming
    Replies: 7
    Last Post: 03-16-2006, 09:43 AM
  4. equations for a program
    By anthonye in forum C Programming
    Replies: 4
    Last Post: 06-19-2002, 04:38 AM
  5. Loading Screen
    By pinkcheese in forum Windows Programming
    Replies: 2
    Last Post: 04-06-2002, 11:48 PM