473,387 Members | 1,347 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.

Mathematical Formula Validator

296 100+
Hi

I want some mechanism to validate a mathematical formula.

For eg: (a+b is not valid since the closing bracket is missing.

Please help.
Jun 7 '07 #1
5 3875
ronnil
134 Expert 100+
you will want to need regular expressions.

start out here http://en.wikipedia.org/wiki/Regular_expression this covers the basics, afterwards you can find the functions working with regular expressions on http://www.php.net/manual/en
Jun 7 '07 #2
pbmods
5,821 Expert 4TB
Regular expressions will do the job, though for the sake of your sanity, you'll probably want to use a stack-based parser instead.

Run through your string and push an element every time you encounter an opening parenthesis/bracket. When you encounter a closing parenthesis/bracket, attempt to pop it off the stack. If the stack is empty, or if the next item on the stack doesn't match (e.g., trying to close a parenthesis before closing an inner bracket), you have an invalid string.
Jun 7 '07 #3
Motoma
3,237 Expert 2GB
Regular expressions will do the job, though for the sake of your sanity, you'll probably want to use a stack-based parser instead.

Run through your string and push an element every time you encounter an opening parenthesis/bracket. When you encounter a closing parenthesis/bracket, attempt to pop it off the stack. If the stack is empty, or if the next item on the stack doesn't match (e.g., trying to close a parenthesis before closing an inner bracket), you have an invalid string.
If all you are trying to do is check for parenthesis, just use the counting method: ( is +1, ) is -1. If at any point you are negative, you have a bad string. If you are at the end of the string, and you are not 0, you have a bad string.

If you need to do other types of validation, a transformation to polish prefix notation makes numbers quite easy to work with in code.
Jun 7 '07 #4
pankajit09
296 100+
I don't want only for parenthesis.

I want for every case.
Jun 12 '07 #5
Motoma
3,237 Expert 2GB
In that case you are going to need to set up a formal document delineating the requirements of your system. Once you have these requirements you can come back if you have issues meeting them.
Jun 12 '07 #6

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

Similar topics

5
by: VM | last post by:
If I have a string variable with a formula: string sMyformula = "3.3*5.2*5"; How can I convert this to a mathematical formula that the compiler can calculate? For all purposes, the formula in...
4
by: Wade | last post by:
Hi guys, I am building a string in code that is a mathematical formula, based on a lot of criteria specified by the user. Once the formula is complete, I want to evaluate it to get a result. ...
5
by: William Cruz | last post by:
I am currently working on a math project & I would like to know how to show the mathematical operators (symbols) on my equations. I know, or I think it has to do something with the Unicode but I...
5
by: jeremito | last post by:
I am extending python with C++ and need some help. I would like to convert a string to a mathematical function and then make this a C++ function. My C++ code would then refer to this function to...
0
by: Massi | last post by:
Hi everyone, I'm trying to build (on windows environment) a grid in which every cell contains a mathematical formula, the problem is the following: when I try to put in the cells some mathematical...
2
by: pankajit09 | last post by:
Hi I want some mechanism to validate a mathematical formula. For eg: (a+b is not valid since the closing bracket is missing. Please help.
1
by: divinity | last post by:
Hey The problem is as follows:- I have four text boxes namely, P, F, Q and A I want the value of A to be equal to the product of P, F and Q. The four textboxes are a part of a form, and...
7
by: clear1140 | last post by:
Good Day!!! Is it somehow possible to retrieve a string and then convert it into a mathematical expression? for example i have this string '14200 * 5' and then i want to convert this into a...
3
by: newbprogrammer | last post by:
I just started programming in c++ and i tried to explore and do programs on my own... So i wanted to do a program that would help me in my daily school work ... a program to solve quadratic...
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: 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:
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?
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
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.