dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Retrieving Mathematical formulae and manipulating them with C++?

  1. #1
    Join Date
    Dec 2010
    Posts
    114

    Retrieving Mathematical formulae and manipulating them with C++?

    Code:
    #define formula_( operand0, operator0, operand1 ) printf_s("%d %s %d", ##operand0, ##operator0, ##operand1 )
      
    int red = 1;
    int green = 2;
    int blue = 3;
    
    int v = formula_(red, "+", blue);
    How come the answer is five? What is the best way to store mathematical formulae? This is basically run-time thing, I can't build them in.
    thanks
    Jack
    Last edited by luckiejacky; June 18th, 2018 at 11:41 PM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,464

    Re: Retrieving Mathematical formulae and manipulating them with C++?

    What compiler/os are you using? On my Windows 7 system using MS VS2017 15.7.3 c++, the output is

    Code:
    1 + 3
    PS However, this macro expansion is ill-defined. ## is the token-pasting (or merging) operator. When used the ##<name> is removed and the tokens preceding it and following it are concatenated with the value of <name> to form a new token which must be valid. Hence ##<name> cannot be the first or last token in the macro definition.

    Consider

    Code:
    #define paster( n ) printf_s( "token" #n " = %d", token##n )  
    int token9 = 9;
    using as

    Code:
    paster(9);
    gives

    Code:
    printf_s( "token" "9" " = %d", token9 );
    as #n is expanded to be "9" and token##n is expanded to be token9

    which by the rules of string literal concatenation then becomes

    Code:
    printf_s( "token9 = %d", token9 );
    In the code in post #1, the ##<name> (##operand0 etc) has nothing to which to merge and so is ill-formed. On MS VS2017, this is the same as

    Code:
    #define formula_( operand0, operator0, operand1 ) printf_s("%d %s %d", operand0, operator0, operand1 )
    
    int red = 1;
    int green = 2;
    int blue = 3;
    
    int v = formula_(red, "+", blue);
    which produces the same output

    Code:
    1 + 3
    PPS Do you mean something like this

    Code:
    #define formula_( operand0, operator0, operand1 ) printf_s("%d %s %d = %d", operand0, #operator0, operand1, operand0##operator0##operand1 )
    
    int red = 1;
    int green = 2;
    int blue = 3;
    
    int v = formula_(red, +, blue);
    which gives the output

    Code:
    1 + 3 = 4
    Last edited by 2kaud; June 19th, 2018 at 07:21 AM. Reason: PPS
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.7)

  3. #3
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,115

    Re: Retrieving Mathematical formulae and manipulating them with C++?

    How come the answer is five?
    The reason is simple, the return value of printf_s() is the number of characters printed.
    You are printing:
    Code:
    1 + 3
    So, that's 5 characters printed.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)