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

Formula Validation

296 100+
Hello,

I have a expression like this -->

Expand|Select|Wrap|Line Numbers
  1. (a+b/c)
How to validate it ?

For eg: if the expression is (a+b/c then error should be thrown.
Apr 25 '08 #1
8 2336
Markus
6,050 Expert 4TB
Hello,

I have a expression like this -->

Expand|Select|Wrap|Line Numbers
  1. (a+b/c)
How to validate it ?

For eg: if the expression is (a+b/c then error should be thrown.
I dont understand.
How would the user give you the formula?
Apr 25 '08 #2
pankajit09
296 100+
I dont understand.
How would the user give you the formula?

The user will enter the formula in a textbox which is present in a form.
Apr 27 '08 #3
aktar
105 100+
Try this:

in your code will be :[PHP]$a = [the value];
$b = [the value];
$c= [the value];

$correct_answer = [the correct answer];[/PHP]now you can convert the formula text you receive from the user, like so:[PHP]($a+$b)/$c[/PHP]
then you can use a fuction like eval() to calculate a value based on the users input. Then its just a matter of matching it with the $correct_answer

like so:[PHP]eval('$user_answer = ($a+$b)/$c;');

if ($user_answer != $correct_answer){

//TRHOW ERROR!!!

}[/PHP]Regards
Apr 27 '08 #4
TheServant
1,168 Expert 1GB
If users are writing their own functions then you will need a lot of searching and counting in the strings, so before I spend time on that tell me if it's a set formula or not. If it is aktar's code looks good!
Apr 27 '08 #5
pankajit09
296 100+
If users are writing their own functions then you will need a lot of searching and counting in the strings, so before I spend time on that tell me if it's a set formula or not. If it is aktar's code looks good!

The formula will be limited to BODMAS like (x+y)/z

I just want to validate it .
May 15 '08 #6
TheServant
1,168 Expert 1GB
The formula will be limited to BODMAS like (x+y)/z
I just want to validate it .

So a user could enter something like x+(y*z)/2+y*x???

If so, this is complicated, and I am about to go home. Please confirm this is the case, because if users can enter their own formulae it means that you need to make lots and lots of rules to test each string by, and no point in wasting time on it if I have misunderstood.
May 15 '08 #7
pankajit09
296 100+
So a user could enter something like x+(y*z)/2+y*x???

If so, this is complicated, and I am about to go home. Please confirm this is the case, because if users can enter their own formulae it means that you need to make lots and lots of rules to test each string by, and no point in wasting time on it if I have misunderstood.

yes you are right.

Has someone did it ?
May 16 '08 #8
dlite922
1,584 Expert 1GB
yes you are right.

Has someone did it ?
BODMAS excercises?

http://www.abacustraining.biz/bodmasExercises.htm

What is an example of what the user exactly entering...

This:

Expand|Select|Wrap|Line Numbers
  1. 3*2(2+4/2)-13
OR

This:

Expand|Select|Wrap|Line Numbers
  1. x+y(z-z*2)/k
are you verifing the "syntax", IOW making sure the parenthesis are matched up and return a true or false,

or do you need to calculate the expression and give the a number result? or perhaps compare it with your number to see if correct?

If you're looking for the first example, then all you need to do is search for parenthesis. count all the left parenthesis and count all the right parenthesis, if the numbers don't match, they left a parenthesis out and their syntax is incorrect.

if second example, then use eval();
if using eval() BE VERY CAREFUL to filter the content, otherwise i can execute any PHP code on your Site!!

for example, use regex to check for characters other than -,+,*,/ 0-9 and a-z;

no semi-colon in content given to eval() should make it a litte safer. maybe user str_replace() and replace all semi-colons with an empty string.

Good luck,


Dan
May 16 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Ernie | last post by:
Hi, I spent more than an hour wondering why the following works in my Python 2.3.4 Linux box: formula = """ def dummy(): print "This is Ok!" """
2
by: celsius | last post by:
Hi folks, Al Bowers wrote this program on comp.lang.c Date: 2001-07-09 13:41:58 PST #include <stdio.h> int isleap (unsigned yr); static unsigned months_to_days (unsigned month); static long...
2
by: alex | last post by:
I need a more advanced formula than just an average for calculating items rating. I have: raitng value is on scale 1 to 10. s - sum of all ratings for an item n - number of rates (votes)
0
by: RJN | last post by:
Hi I have a main report and a sub report. I have a formula field on the main report and one on the sub report. I want the formula in the subreport to be evaluated after the formula in the main...
0
by: RJN | last post by:
Hi Sorry for posting this message again. I have a main report and a sub report. I have a formula field on the main report and one on the sub report. I want the formula in the subreport to be...
0
by: rjn | last post by:
Hi I have a main report in which I have inserted a sub report. I have a formula field on the main report and one on the sub report. I want the formula in the subreport to be evaluated after the...
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...
11
by: Brian VanPelt | last post by:
I am not a very experienced VB user, but I was trying to make a form that a user could input a formula for summation. For example, I would like the user to input the beginning and end values of...
1
by: barnzee | last post by:
Hi all, newbie here, but having a go I am trying to build a stock watchlist in excel 2007 with a dynamic link to a DDE server (paid for from a broker).There is no add-in or plug-in, I just CTL ALT...
30
by: Barry L. Bond | last post by:
Greetings! I just got a new Peet Brothers Ultimeter 2100 Weather Station. This new one has a way to display the heat index, if you press the "dew point" key twice. Being aware of all the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.