Dynamically create Identifiers?

This is a discussion on Dynamically create Identifiers? within the C Programming forums, part of the General Programming Boards category; I'm working on a little program to read mathematical expressions, solve the equations and print out the answers. The problem ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    1

    Question Dynamically create Identifiers?

    I'm working on a little program to read mathematical expressions, solve the equations and print out the answers.

    The problem is that it has to support variables.

    Example:

    a+2 = 3

    and in the future, while running the program, I may use the value of 'a' ,that's supposed to be calculated by the program from the expression above, to find the value of yet another variable ('b').

    Example:

    a+b = 5


    I've started to implement the program, but I can't figure out how to dynamically create identifiers with the names of the variables from the equations.

    Does anyone know how I can do this?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Sure -- you could have an array like the following
    Code:
    int variable[26];  /* For variables 'A' - 'Z' (case insensitive) */
    Or a data structure like this
    Code:
    struct variable_t {
        const char *name;
        double value;
    };
    and then an ordinary array, a dynamically allocated array, or some other data structure.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create a file from c++ program
    By Dan17 in forum C++ Programming
    Replies: 2
    Last Post: 05-08-2006, 04:25 PM
  2. A question about dynamically creating an array of objects
    By edd1986 in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2006, 11:30 AM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05:23 AM
  4. How to create a file association program?
    By eShain in forum Windows Programming
    Replies: 1
    Last Post: 03-06-2006, 11:15 AM
  5. How to Create reference to an array in C++
    By shiv_tech_quest in forum C++ Programming
    Replies: 2
    Last Post: 12-20-2002, 09:01 AM

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