Reading Equations from a Data File

This is a discussion on Reading Equations from a Data File within the C Programming forums, part of the General Programming Boards category; Hello All, I would like to read equations from a datafile and use them in my main program. For example, ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    3

    Reading Equations from a Data File

    Hello All,

    I would like to read equations from a datafile and use them in my main program.
    For example, my data file might look like:

    sin(i%360) + 2*sin(j%360)
    2*cos(i%360) - sin(j%360)

    and so on.
    So each line will be a separate equation.

    Variable i and j will be defined and values set in the main application.

    I can read the lines using something like this:
    Code:
    	FILE *file_ptr;
    	char record[MAX_REC_SIZE + 1]; 
    	file_ptr = fopen("EQS.DAT","r");	
    	while(fgets(record,MAX_REC_SIZE+1,file_ptr) != NULL)
    		fputs(record,stdout);
    	fclose(file_ptr);
    and see the equation written to the screen.

    I am stuck though on how to assign this to a variable within my program so that I can put it to use.
    For example, say i have a variable called eq1 of type float.
    How do I assign the value of record (above, a line from the data file) to this float.

    Thanks for any advice,
    p
    Last edited by parisgraphics; 06-27-2008 at 10:00 PM. Reason: forgot code tags

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    You can't. You have to somehow build a way to interpret the equation and come up with the results yourself.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    And it's not an easy thing to do with plain C. There are some tools like bison, that help you parse an expression. That means separate an expression.

    1)If you want to evaluate ANY expression than you need a lot of work. If you want to evaluate an expression without parenthesis then it might be simpler. Like you can read one one character and store it to a string. When you find an operator sympol you store it somewhere else. You do this again and have a second string.
    Lets say you have:
    i * j
    you ll separate them into three strings.
    You ll probably want to create a struct for variables like
    Code:
    struct var {
        char *name;
        float value;
    }
    So then you wait for the variables values to be set. You ll search for example every struct if it has the name "i". If it does you put on the "value" the appropriate value. And then you evaluate the expression. You could do that by having a linked list with the structs:
    Code:
    struct var {
        char *name;
        float value;
        struct var *next;
    }
    Even better to also include the operators
    Code:
    struct token {
       char *name;
       float value;
       struct var *next;
       char operator;
    }
    So when the operator == 0 it is a variable. When the operator != 0 it is an operator. So you read a variable "value" read the next token that should be an operator, read the third "value" evaluate the expression, have something like
    sum += the value you just evaluated
    and do this until struct var *next == NULL.

    If you put even a single parenthesis then everything is more complicated. You cannot evaluate them by that. The parenthesis will be evaulated first.There was another topic about trees and boolean expressions that I explained a way to do so.

    At last, if you include sin, ln, abs and such, you need to read them, pass the name into a function with a switch-case and call the appropriate function in C.

    For each line you ll do the above.

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    3

    Thanks and maybe a different approach

    Thanks for the replies!

    C_ntua I see what you are saying and now thinking that maybe a better option for me would be to create some kind of UI at the start of the application - maybe a menu - which allows the user to build the equations on the spot. As they make choices the equation is built correctly. I probably should have started this way but didn't realize that reading them from a text file would be so involved.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Just be careful with parenhtesis. Lets say you have a UI. Then someone wants to evaluate:
    sin(ln2+2^(2+1))
    The UI will save you the time parsing the expression. But you will have to store the data in a correct way for them to be evaluated.

    So there is no way to calculate on the spot things, because the evaluation might depend on something afterwards.

    MORE important, if somebody types:
    1 + 2/2
    then you have to wait for the 2/2 to be evaluated before you do the +. If you do it "on-the-fly" then you ll get
    1+1=3 / 2 = 1.5
    Not what you want.
    So I don't think a UI will simplify a lot things. It will save you the parsing, but you wont save the time storing the data in specific linked structures in order to evaluate the equation at the end.

    If you exclude parenthesis then your program would be easier to write but much weaker.

    So:
    1) Decide what is best, parser or UI
    2) Make an appropriate struct, more likely linked structs, that can store the information and evaluate an expression. An example, a "normal" tree. Each parent two childs. Each node will also have two values. An operator and a value in the case it is a number. You will evaluate first whatever has two child with both pointers NULL. Then you get their values and the operator (stored accordingly) and calculate. Store the value on the parent and make NULL its pointer. Etc etc
    3) Think on how the data will be stored in the above linked struct when you parse or when the user uses the UI

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    3

    Good points

    Hi C_ntua,

    Thanks - good points!
    I will try out a few approaches and see how things turn out :-)
    I'm developing for a platform where asking the user to type anything is a pain (gp2x hand held console).
    What I'm starting to imagine now is using SDL to create a GUI of guided menu options and building the equations based on what the user clicks. It will be confined a bit by the options and not as open ended but that's ok.
    Now that I've mentioned the platform someone might wonder - why are you using trig functions on a platform with no FPU?!? Actually I'm not. I am using LUTs but didn't see the point of bringing that into the discussion earlier since bottom line what I want is a way for the user to define the equations used in my app.

    Again - thanks for the good infos!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. reading data from a file
    By brianptodd in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2003, 05:50 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

Tags for this Thread


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