![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
-----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. . |
#3
| |||
| |||
|
|
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. |
#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" <vonchi_m (AT) yahoo (DOT) com> wrote in message news:udSLBPGYEHA.1000 (AT) TK2MSFTNGP12 (DOT) 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. |
#5
| |||
| |||
|
|
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. . |
#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" <vonchi_m (AT) yahoo (DOT) com> wrote in message news:udSLBPGYEHA.1000 (AT) TK2MSFTNGP12 (DOT) 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. |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |