-
May 12th, 2011, 06:06 PM
#1
Literal command
Hi guys I´m trying to execute a formula, for example pow(x,2), comming from an Edit1 in the main form of my program and show result in an Edit2. However I don´t know how to convert this String in a literal instruction that the program can execute;
please suppose that we have:
#include <math.h> ...etc
double x=3;
String formula = pow(x,2);
Edit2->Text=formula;
// This works and Edit2 shows a value of 9
However when the String is obtained from an Edit, this is what happens:
double x = 3;
String formula = Edit1->Text;
Edit2->Text=formula;
// This doesn´t work and Edit2 shows the formula as a characters string, I mean "pow(x,2)"
How can I get that?
Many thank in advance
-
May 12th, 2011, 08:17 PM
#2
Re: Literal command
Originally Posted by jgabase
double x=3;
String formula = pow(x,2);
Edit2->Text=formula;
What language is this? What is "String"?
So you expected C++ to take that string and understand what it means? What "pow" means to you may mean something different to someone else. Maybe it means to draw a picture of someone socking someone in the face ("pow!").
Seriously, there is no built-in or library function to turn a string into an expression, and then solve the expression. You have to write a function to do this (a parser), so that C++ understands what that string means and then execute that code.
Regards,
Paul McKenzie
-
May 13th, 2011, 10:13 AM
#3
Re: Literal command
Let me explain it in other way... please imagine that I want to create the function:
float y FUNCTION (void)
{
float x=3;
y = pow(x,2); // Previously I called the header: #include <math.h>
return y; // In this case the function will return "9" (3 to the power of 2)
}
Now imagine that instead of using this formula "y = pow(x,2);" I would like to use other that the user will write in an Edit component in the form of the program. What should be the procedure to get this? Is there a way for the compiler to execute a literal instruction?
float y FUNCTION (void)
{
float x=3;
............... <--- Insert here the formula comming from an Edit I.E "y = pow(x,3);"
return y; // In this case the function should return 27
}
best regards
-
May 13th, 2011, 11:30 AM
#4
Re: Literal command
As Paul wrote you have to write a parser to accomplish this.
-
May 19th, 2011, 04:12 PM
#5
Re: Literal command
opss... I understand now many thanks.
Tags for this Thread
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
|