CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2004
    Posts
    138

    Converting a string into a line of code.

    If I have a CString str; and say I have a function double y(double x). Suppose the string in str is "x*cos(x)", and I want the function double y(double x) to return what is written in str, is this at all feasible using MFC or any other standard utility libraries?

  2. #2
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Converting a string into a line of code.

    You mean that you want 'y' to return a string instead of a double?
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  3. #3
    Join Date
    Oct 1999
    Location
    ks
    Posts
    523

    Re: Converting a string into a line of code.

    if i read you right you can simply let the y fn return a double and then convert the double into a CString:

    z = y(x);

    CSting str_of_z;

    str_of_z.Format("%10.6f",z);

  4. #4
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: Converting a string into a line of code.

    Quote Originally Posted by Fatboy
    If I have a CString str; and say I have a function double y(double x). Suppose the string in str is "x*cos(x)", and I want the function double y(double x) to return what is written in str, is this at all feasible using MFC or any other standard utility libraries?
    There is no standard tools for evaluating arithmetic expressions. Searching web, you can find some handy tools, examples, etc. Try googling for something like 'arithmetic expression parser' or something similar. However, thing I would recommend, is LUA interpreter. It is very handy, lightweight tool which may be used for parsing expressions. You might consider it too big to just parse expressions, but I think that its advantages are worth it: It supports variables, built-in basic math functions, custom functions, is portable (I compiled and used it on Windows and two Linux distros).
    I tried also boost spirit library, but it is totally illegible for me

    Cheers,
    Hob
    B+!
    'There is no cat' - A. Einstein

    Use [code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  5. #5
    Join Date
    Jan 2007
    Posts
    102

    Re: Converting a string into a line of code.

    Hahaha,

    That's funny, only Hobson understood Fatboy's question.
    Fatboy would like to create an application something similar like Matlab (but I guess a very small one).
    Peace!

  6. #6
    Join Date
    Aug 2004
    Posts
    138

    Re: Converting a string into a line of code.

    Yes, a very very small Matlab, or you can view it as a something more than a graphic calculator. Thanks guys for your help, and respect to Hobson for understanding my question, I know it was hard to decipher!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured