Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to convert an arithmetic string expression to an int result

Status
Not open for further replies.

CosmoGreco

Programmer
Sep 16, 2002
13
US
Hello Dear members of Tek-Tips.

It's not long ago since I start devoting my efforts to program application using C/C++. So I am not very familiar with all its capabilities yet.

I need to convert an entire arithmetic string expression to a result integer so C++ will calculate the expression for me without the need to develop a parser to track every single arithmetic operation.

In FoxPro for example, once I have a string with an arithmetic expression, I just do as in the following example and the result will be the final calculated expression number I want:

sVar = "10 * (10 - 5)"
iVar = &sVar

The contents of iVar after the above operation will be 50.

How do I do the same with C++?
The function atoi() will stop scanning the string right after it sees a non digit character so the example above will give me this result:

char szVar[]="10 * (10-5)";
int iVar = atoi(szVar);

The contents of iVar after the above operation will be 10 only.

Appreciate any ideas.

Regards,
Cosmo
 
>> In FoxPro for example ... etc.

Yes that's because in FoxPro you are using libraries that provide the functionality.

Basic C++ language does not come with higher level libraries such as the one you describe. There are 3rd party products you can use or you must write the code yourself.

Good luck
-pete
 
Thanks Pete for the comments.

I was expecting something like you mentioned. However, before wasting time and write my own parser to perform such task, I wanted to make sure I had no other alternative in C++ by getting some opinions from more experienced C++ programmers like yourself.

Thanks again!
Regards,
Cosmo
 
Try casting:
char szVar[]="10 * (10-5)";
int iVar = (int)(szVar);

 
I tried casting already and unfortunately it doesn't work the way we want.

If I do what you suggested, I will get a undefined result (I’m getting 1243024 instead of 50 for the given example).

Thanks anyway for trying to help!

Regards,
Cosmo
 
>> I will get a undefined result
>> char szVar[]="10 * (10-5)";
>> int iVar = (int)(szVar);

No way that will work, your just getting the memory address of szVar into the integer iVar.

-pete
 
With order of operations taken into account (including parenthesis), there is going to be a lot more than simple parsing involved. This sounds like a fun project! I'll have to work on it sometime!
 
Sure it would be fun project!

However I just could not sleep well thinking that I would be reinvent the wheel if I write a program for that. So I found this forum and the solution, ready to be picked up and I would like to share it with you:


The header files and other nice C/C++ routines can be download from:


If you wish to join this other cool forum, the address is:

Here another comment about the this problem with string operation:

C/C++ understands pretty well a math operation once it is a value type, not a string:

int iVar = 10 * (10 - 5); // not problems here.

If, somehow, I would be able to generate dynamic code, this problem would be solved:

Like:

szVar[] = “int iVar = 10 * (10 – 5)” ;

Then, as perhaps like a macro substitution, the above contents of the szVar would became a line of code.

This is quite possible to do, funny enough, in FoxPro that was completely written in C/C++. So there must be a way to do the same in C/C++.

FP can use a pointer to point to the contents of szVar so it sees it as an instruction. Basically this is a pointer to a piece of code, like a pointer to a function in C/C++. However, how to make the compiler sees a character string like a piece of code???

Why I don’t use this language then as it can do what I just described? Because there is no doubt C/C++ is much more powerful and is the language of choice for serious app development.

Regards,
Cosmo
 
Such things like dynamical parsing of formulas is property of interpreters, not of compilers. In case of interpreter (Fox is interpreter, isn't?) there is theoretical possibility to pass pieces of source code to the translator because the translator (interpreter) is present during all execution time. In case of compilers (like C or Pascal) the only way to do that are suitable run-time libraries.

> int iVar = 10 * (10 - 5); // not problems here.
calculated during translation phase by compiler to the static constant value.

> szVar[] = “int iVar = 10 * (10 – 5)” ;
compiled to a source text string; must be parsed during execution - at that time there is no compiler to do that.
 
Thanks Mingis for your reply.

FoxPro can be both - compiled and interpreter. Even if I have a compiled version of my app the above example would work fine in FP. I can indeed generate dynamically code with a compiled FP app at run-time.

Your comments are correct. However I did a test using the command-prompt parameters arg and *argv, which are obviously gathered at run-time and after the compilation stage and I was able to create a macro substitution using #define, which I understand is a preprocessor directive and occurs at compiling time too.

So during run time, whatever I input from the command-prompt, let’s say
“int iVar = 10 * (10 – 5)”, my const definition using #define, was indeed replaced by the entry I made from the command-prompt. Sounds like this constant is no longer a constant but a variable that can be altered with whatever entry from the command-prompt. I know that’s because in this case, what is being substituted is actually the address of that constant.

So if I had defined MYCONST using a #define macro and concatenate char by char from argv to MYCONST I could use it anywhere in my program as being the expression I entered from command-prompt.

The following piece of code is ridiculous alright, but using the debug, I could see indeed the substitution occurring:

Let’s say I have MYCONST define and initialized as “int iVar = 10 * (10-5)” through my concatenation macro routine gather from argv.

Then somewhere in my code I just write this:

MYCONST;

Yes! The substitution was done. The debug showed me int iVar=10*(10-5) instead MYCONST but as a string expression, not a valid line of code of course.

Regards,
Cosmo

 
Sounds like FoxPro links compiler parts to the executable allowing to parse source code at run-time stage. C does not offer such possibility by default.

Your experiments with macro substitution illustrates, that you can manipulate source code pieces during run-time, assigning it to a string variables, whatever, but there is pure text only, nothing more.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top