473,395 Members | 1,464 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,395 software developers and data experts.

creating algorithm for formula parsing

VM
I'm trying to find out what the best way would be to parse a formula so I
can then calculate it with its real values.
For example, a user may enter
((sys_empHours*db_empSal)/db_totalHours)
and I would need to parse all of that so that I can get the tokens that need
to be replaced with data (sys_empHours, db_empSal, etc...) and then plug
them back in.

Thanks.
Nov 16 '05 #1
8 6930
Well, barring any built in function, you will have to
worm through the string and check for validity on the
part since the last time you stopped when you hit a
delimiter.
-----Original Message-----
I'm trying to find out what the best way would be to parse a formula so Ican then calculate it with its real values.
For example, a user may enter
((sys_empHours*db_empSal)/db_totalHours)
and I would need to parse all of that so that I can get the tokens that needto be replaced with data (sys_empHours, db_empSal, etc...) and then plugthem back in.

Thanks.
.

Nov 16 '05 #2
Well, barring any built in function, you will have to
worm through the string and check for validity on the
part since the last time you stopped when you hit a
delimiter.
-----Original Message-----
I'm trying to find out what the best way would be to parse a formula so Ican then calculate it with its real values.
For example, a user may enter
((sys_empHours*db_empSal)/db_totalHours)
and I would need to parse all of that so that I can get the tokens that needto be replaced with data (sys_empHours, db_empSal, etc...) and then plugthem back in.

Thanks.
.

Nov 16 '05 #3
// It ain't pretty but it does exactly what you asked for...
string sys_empHours = "40";
string db_empSal = "35000.00";
string db_totalHours = "80";
string userEntered = "((sys_empHours*db_empSal)/db_totalHours)";
string tokenReplace = userEntered.Replace("sys_empHours", sys_empHours);
tokenReplace = tokenReplace.Replace("db_empSal", db_empSal);
tokenReplace = tokenReplace.Replace("db_totalHours", db_totalHours);
// Now, if you want to actually perform the calculations in the user-entered
string, you've got a
// whole new thing coming. And it doesn't account for user-entered garbage
either, like
// ((sys_empHours*db_empSal)/db_totalHourssys_EmpHours), which would be
totally meaningless
// but .Replace() doesn't care about all that. Have fun.
// If you want to do a real parser that actually performs the math for you,
there are lots of
// examples around the Web in lots of languages. There are plenty in Pascal
and C (which should
// translate fairly readily to C#).

"VM" <vo******@yahoo.com> wrote in message
news:ud**************@TK2MSFTNGP12.phx.gbl...
I'm trying to find out what the best way would be to parse a formula so I
can then calculate it with its real values.
For example, a user may enter
((sys_empHours*db_empSal)/db_totalHours)
and I would need to parse all of that so that I can get the tokens that need to be replaced with data (sys_empHours, db_empSal, etc...) and then plug
them back in.

Thanks.

Nov 16 '05 #4
// It ain't pretty but it does exactly what you asked for...
string sys_empHours = "40";
string db_empSal = "35000.00";
string db_totalHours = "80";
string userEntered = "((sys_empHours*db_empSal)/db_totalHours)";
string tokenReplace = userEntered.Replace("sys_empHours", sys_empHours);
tokenReplace = tokenReplace.Replace("db_empSal", db_empSal);
tokenReplace = tokenReplace.Replace("db_totalHours", db_totalHours);
// Now, if you want to actually perform the calculations in the user-entered
string, you've got a
// whole new thing coming. And it doesn't account for user-entered garbage
either, like
// ((sys_empHours*db_empSal)/db_totalHourssys_EmpHours), which would be
totally meaningless
// but .Replace() doesn't care about all that. Have fun.
// If you want to do a real parser that actually performs the math for you,
there are lots of
// examples around the Web in lots of languages. There are plenty in Pascal
and C (which should
// translate fairly readily to C#).

"VM" <vo******@yahoo.com> wrote in message
news:ud**************@TK2MSFTNGP12.phx.gbl...
I'm trying to find out what the best way would be to parse a formula so I
can then calculate it with its real values.
For example, a user may enter
((sys_empHours*db_empSal)/db_totalHours)
and I would need to parse all of that so that I can get the tokens that need to be replaced with data (sys_empHours, db_empSal, etc...) and then plug
them back in.

Thanks.

Nov 16 '05 #5
Also you might want to look into RegularExpression class. I think it's called RegEx and that's pretty helpful. I just started taking a compiler course and this is the stuff we will be dealing with. To parse and analyze what the user entered we will be using lex and yacc, which I don't think there is an equivalent class in the CLR, but there are 3rd party tools out there.

Also if you don't want to get into compiler, you can just place the identifiers/variables as a node in a tree and the operators would be the parent of the nodes. Then you would do a in-order operation. You would need to make sure that you take into account operator presedence. Like multiply and divide would be higher than add and subtract.

I don't know if this helps, but it opens the doors to some other possiblities.

RAyRAy

"Michael C" wrote:
// It ain't pretty but it does exactly what you asked for...
string sys_empHours = "40";
string db_empSal = "35000.00";
string db_totalHours = "80";
string userEntered = "((sys_empHours*db_empSal)/db_totalHours)";
string tokenReplace = userEntered.Replace("sys_empHours", sys_empHours);
tokenReplace = tokenReplace.Replace("db_empSal", db_empSal);
tokenReplace = tokenReplace.Replace("db_totalHours", db_totalHours);
// Now, if you want to actually perform the calculations in the user-entered
string, you've got a
// whole new thing coming. And it doesn't account for user-entered garbage
either, like
// ((sys_empHours*db_empSal)/db_totalHourssys_EmpHours), which would be
totally meaningless
// but .Replace() doesn't care about all that. Have fun.
// If you want to do a real parser that actually performs the math for you,
there are lots of
// examples around the Web in lots of languages. There are plenty in Pascal
and C (which should
// translate fairly readily to C#).

"VM" <vo******@yahoo.com> wrote in message
news:ud**************@TK2MSFTNGP12.phx.gbl...
I'm trying to find out what the best way would be to parse a formula so I
can then calculate it with its real values.
For example, a user may enter
((sys_empHours*db_empSal)/db_totalHours)
and I would need to parse all of that so that I can get the tokens that

need
to be replaced with data (sys_empHours, db_empSal, etc...) and then plug
them back in.

Thanks.


Nov 16 '05 #6
Also you might want to look into RegularExpression class. I think it's called RegEx and that's pretty helpful. I just started taking a compiler course and this is the stuff we will be dealing with. To parse and analyze what the user entered we will be using lex and yacc, which I don't think there is an equivalent class in the CLR, but there are 3rd party tools out there.

Also if you don't want to get into compiler, you can just place the identifiers/variables as a node in a tree and the operators would be the parent of the nodes. Then you would do a in-order operation. You would need to make sure that you take into account operator presedence. Like multiply and divide would be higher than add and subtract.

I don't know if this helps, but it opens the doors to some other possiblities.

RAyRAy

"Michael C" wrote:
// It ain't pretty but it does exactly what you asked for...
string sys_empHours = "40";
string db_empSal = "35000.00";
string db_totalHours = "80";
string userEntered = "((sys_empHours*db_empSal)/db_totalHours)";
string tokenReplace = userEntered.Replace("sys_empHours", sys_empHours);
tokenReplace = tokenReplace.Replace("db_empSal", db_empSal);
tokenReplace = tokenReplace.Replace("db_totalHours", db_totalHours);
// Now, if you want to actually perform the calculations in the user-entered
string, you've got a
// whole new thing coming. And it doesn't account for user-entered garbage
either, like
// ((sys_empHours*db_empSal)/db_totalHourssys_EmpHours), which would be
totally meaningless
// but .Replace() doesn't care about all that. Have fun.
// If you want to do a real parser that actually performs the math for you,
there are lots of
// examples around the Web in lots of languages. There are plenty in Pascal
and C (which should
// translate fairly readily to C#).

"VM" <vo******@yahoo.com> wrote in message
news:ud**************@TK2MSFTNGP12.phx.gbl...
I'm trying to find out what the best way would be to parse a formula so I
can then calculate it with its real values.
For example, a user may enter
((sys_empHours*db_empSal)/db_totalHours)
and I would need to parse all of that so that I can get the tokens that

need
to be replaced with data (sys_empHours, db_empSal, etc...) and then plug
them back in.

Thanks.


Nov 16 '05 #7
Take a look at http://www.engr.mun.ca/~theo/Misc/exp_parsing.htm , from
which you will learn how
to write a simple recursive descent parser that will solve this problem.

<an*******@discussions.microsoft.com> wrote in message
news:26*****************************@phx.gbl...
Well, barring any built in function, you will have to
worm through the string and check for validity on the
part since the last time you stopped when you hit a
delimiter.
-----Original Message-----
I'm trying to find out what the best way would be to

parse a formula so I
can then calculate it with its real values.
For example, a user may enter
((sys_empHours*db_empSal)/db_totalHours)
and I would need to parse all of that so that I can get

the tokens that need
to be replaced with data (sys_empHours, db_empSal,

etc...) and then plug
them back in.

Thanks.
.

Nov 16 '05 #8
You might also want to look into generating code dynamically and
compiling it (very fast and efficient and extremely flexible). Heres
an example that could be modified to do what you want:
http://www.codeproject.com/csharp/matheval.asp

HTH
Kieran

RAyRAy <RA****@discussions.microsoft.com> wrote in message news:<F5**********************************@microso ft.com>...
Also you might want to look into RegularExpression class. I think it's called RegEx and that's pretty helpful. I just started taking a compiler course and this is the stuff we will be dealing with. To parse and analyze what the user entered we will be using lex and yacc, which I don't think there is an equivalent class in the CLR, but there are 3rd party tools out there.

Also if you don't want to get into compiler, you can just place the identifiers/variables as a node in a tree and the operators would be the parent of the nodes. Then you would do a in-order operation. You would need to make sure that you take into account operator presedence. Like multiply and divide would be higher than add and subtract.

I don't know if this helps, but it opens the doors to some other possiblities.

RAyRAy

"Michael C" wrote:
// It ain't pretty but it does exactly what you asked for...
string sys_empHours = "40";
string db_empSal = "35000.00";
string db_totalHours = "80";
string userEntered = "((sys_empHours*db_empSal)/db_totalHours)";
string tokenReplace = userEntered.Replace("sys_empHours", sys_empHours);
tokenReplace = tokenReplace.Replace("db_empSal", db_empSal);
tokenReplace = tokenReplace.Replace("db_totalHours", db_totalHours);
// Now, if you want to actually perform the calculations in the user-entered
string, you've got a
// whole new thing coming. And it doesn't account for user-entered garbage
either, like
// ((sys_empHours*db_empSal)/db_totalHourssys_EmpHours), which would be
totally meaningless
// but .Replace() doesn't care about all that. Have fun.
// If you want to do a real parser that actually performs the math for you,
there are lots of
// examples around the Web in lots of languages. There are plenty in Pascal
and C (which should
// translate fairly readily to C#).

"VM" <vo******@yahoo.com> wrote in message
news:ud**************@TK2MSFTNGP12.phx.gbl...
I'm trying to find out what the best way would be to parse a formula so I
can then calculate it with its real values.
For example, a user may enter
((sys_empHours*db_empSal)/db_totalHours)
and I would need to parse all of that so that I can get the tokens that need to be replaced with data (sys_empHours, db_empSal, etc...) and then plug
them back in.

Thanks.


Nov 16 '05 #9

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

Similar topics

17
by: savesdeday | last post by:
In my beginnning computer science class we were asked to translate a simple interest problem. We are expected to write an algorithm that gets values for the starting account balance B, annual...
2
by: Pasi Havia | last post by:
Hi, I am making a little program which calculates Wright's inbreeding coefficient. What I have a trouble with is converting the formula into a code. The formula is: Fx=? As I'm using...
5
by: George | last post by:
I have a problem creating triangles with this program it creates rectangles and squares but not triangles. For example I would like to create a triangle with the vertices (1,1), (31,1), (31,31) and...
5
by: Steve Lambert | last post by:
Hi Guys, I realise this might not be the correct group for this post so maybe you could redirect me accordingly. I have a coding requirement for which I suspect a standard algorithm exists. I...
3
by: Mike | last post by:
Hi, I have three tables in the following structure (simplified): Table 1: Containing the customers ------------------------------------------------- create table Customers ( int...
5
by: nedian | last post by:
Hello I am student and trying to learn access.I am having problem in making a program can any one help me? i wanted to make 2 tables in access and then create a link.First i am telling u what...
6
by: aarklon | last post by:
Hi folks, I found an algorithm for calculating the day of the week here:- http://www.faqs.org/faqs/calendars/faq/part1/index.html in the section titled 2.5 what day of the week was 2 august...
4
by: Bronson | last post by:
Hi, i'm looking for a fast way to find greatest multiple of a, which is smaller than b. Recently i've used b-b%a, but maybe it could be done faster. Any ideas? Przemek
2
by: richard_b_lloyd | last post by:
I've noticed that there are tons of tutories for creating search engines / search pages using vb.net. What all of these tutorials don't cover is how to parse the search text the user has enetered...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.