473,320 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

convert the content of a string to an expression to check its correctness

Hi,

suppose you have the class Class1 and the main program:

class Class1
{
public:
Class1();
~Class1();

private:
int a;
int b;
}

void main()
{
Class1 cl = Class1();

cl.a = 4;
cl.b = 6;

char str[] = "a>0 && b<5"
}

I want to check if the expression which is contained to the str[] is
true or false.
With other word i want to check the following:

if (a>0 && b<5)
{
// do something
}
else
{
// do something else
}

the problem is that i have the expression in a string, how can i
convert the content od the string to a expression and check if the
expression is TRUE or FALSE?

Thanks in advance for any help.
Jul 22 '05 #1
7 1814
On 18 Jul 2004 14:40:42 -0700, spiros <ka****@ceid.upatras.gr> wrote:
Hi,

suppose you have the class Class1 and the main program:

class Class1
{
public:
Class1();
~Class1();

private:
int a;
int b;
}

void main()
{
Class1 cl = Class1();

cl.a = 4;
cl.b = 6;

char str[] = "a>0 && b<5"
}

I want to check if the expression which is contained to the str[] is
true or false.
With other word i want to check the following:

if (a>0 && b<5)
{
// do something
}
else
{
// do something else
}

the problem is that i have the expression in a string, how can i
convert the content od the string to a expression and check if the
expression is TRUE or FALSE?

Thanks in advance for any help.


It's not easy. You need to write a parser. Whole books have been written
on this subject and its far too big a topic to be explained in a newsgroup.

Your best bet is to get hold of some sample code, study it carefully and
then adapt it to your particular needs. For instance you could get hold of
The C++ Programming Language (3rd edition) by Bjarne Stroustrup which has
a simple arithmetic expression parser in chapter 6.

john
Jul 22 '05 #2
On 18 Jul 2004 14:40:42 -0700, ka****@ceid.upatras.gr (spiros) wrote:
Hi,

suppose you have the class Class1 and the main program:

class Class1
{
public:
Class1();
~Class1();

private:
int a;
int b;
}

void main()
{
Class1 cl = Class1();

cl.a = 4;
cl.b = 6;

char str[] = "a>0 && b<5"
}

I want to check if the expression which is contained to the str[] is
true or false.
With other word i want to check the following:

if (a>0 && b<5)
{
// do something
}
else
{
// do something else
}

the problem is that i have the expression in a string, how can i
convert the content od the string to a expression and check if the
expression is TRUE or FALSE?


If you need to evaluate the expressions at runtime, you'll have to
write a parser, which for full C++ functionality will require many
thousands of lines of code (or you could use a library like
boost.Spirit to reduce this substantially). A far better solution is
to use a scripting language that supports this kind of thing - I think
Python is a popular choice. Python and C++ play together quite well,
see http://www.boost.org/libs/python/doc/index.html

Tom
Jul 22 '05 #3
spiros posted:
int main()
{
Class1 cl = Class1();

cl.a = 4;
cl.b = 6;

char str[] = "a>0 && b<5"
}

I want to check if the expression which is contained to the str[] is true or false.
With other word i want to check the following:

if (a>0 && b<5)
{
// do something
}
else
{
// do something else
}

the problem is that i have the expression in a string, how can i convert the content od the string to a expression and check if the expression is TRUE or FALSE?

Are you taking input from the user?
-JKop
Jul 22 '05 #4
JKop <NU**@NULL.NULL> wrote in message news:<Zi*****************@news.indigo.ie>...
spiros posted:
int main()
{
Class1 cl = Class1();

cl.a = 4;
cl.b = 6;

char str[] = "a>0 && b<5"
}

I want to check if the expression which is contained to

the str[] is
true or false.
With other word i want to check the following:

if (a>0 && b<5)
{
// do something
}
else
{
// do something else
}

the problem is that i have the expression in a string,

how can i
convert the content od the string to a expression and

check if the
expression is TRUE or FALSE?

Are you taking input from the user?
-JKop


No, i don't take any input from the user, the string is available at compile time.

Any idea?

Thanks,
Spiros.
Jul 22 '05 #5
spiros posted:
No, i don't take any input from the user, the string is available at compile time.

Any idea?

Thanks,
Spiros.

Well if the string is available at compile time... then why
is it a string in the first place?! Why not an inline
function?

bool Stuff(int a, int b)
{
return a>0 && b<5;
}
-JKop
Jul 22 '05 #6
JKop posted:

No, i don't take any input from the user, the string is available at compile time.

Any idea?

Thanks,
Spiros.

Well if the string is available at compile time... then

why is it a string in the first place?! Why not an inline
function?

bool Stuff(int a, int b)
{
return a>0 && b<5;
}
-JKop

And if that ain't elaborate enough for you:

Have loads of these little functions:

bool Suff(int a, int b)
{
return a> 0 && b < 5;
}

bool Choc(int a, int b)
{
return (a - 5) > 2 ? b + 3 : b + 4;
}

bool Monk(int a, int b)
{
return a < -2 || b;
}
But then, in your actual calling code, you can refer to
them by the same name:

int main()
{
bool (*Current)(int,int) = Monk;

Current(5,6);

Current = Stuff;

Current(7,9);

Current = Choc;

Current(5,-2);

}

I just don't see what possible "problem" would make you
have to make strings out of them!
-JKop
Jul 22 '05 #7
On Tue, 20 Jul 2004 19:52:52 GMT, JKop <NU**@NULL.NULL> wrote:


I just don't see what possible "problem" would make you
have to make strings out of them!


OP doesn't know how to use pointers to member functions perhaps?

john
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: JimMenees | last post by:
I know there's an easy answer to this which is escaping me: -------------------------------------------------------------------------- ----------------------- I have clickable thumb images which...
0
by: Jimmy Cerra | last post by:
I recently came up with a cool little stylesheet for definition lists. There is a small demostration for the impatient . I hope this helps someone. Here's how I did it. Definition lists are...
11
by: rajarao | last post by:
hi I want to remove the content embedded in <script> and </script> tags submitted via text box. My java script should remove the content embedded between <script> and </script> tag. my current...
7
by: >>Shailesh | last post by:
CODE: $stuff = '<html><head><title>stuff</title></head><body><b>Hello</b> World</body></html>'; header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
30
by: ceeques | last post by:
Hi I am a novice in C. Could you guys help me solve this problem - I need to convert integer(and /short) to string without using sprintf (I dont have standard libray stdio.h). for...
10
by: sposes | last post by:
Im very much a newbie but perhaps somehone can help me. Ive been searching for a way to convert a std::string to a unsigned char* The situation is I have a function that wants a unsigned char*...
10
by: Steve Pope | last post by:
The first of the following functions compiles, the second gives what I think is a spurious error: "cannot convert `const char' to `char *' in assignment". void foo(int m) { char *str; if (m...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.