-
June 18th, 2018, 11:37 PM
#1
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.
-
June 19th, 2018, 04:19 AM
#2
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
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
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
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
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 19th, 2018, 02:14 PM
#3
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:
So, that's 5 characters printed.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|