camelCase
The Case of the Mysterious Camel.
- Reaction score
- 362
I decided to try writing a calculator like the one on Google and it works perfectly; almost.
It reads mathematical expressions correctly until I try and nest parentheses for function calls..
For example:
Input: cos(0)
Output String: (cos(0))
Evaluates To: 1
Input: cos((0))
Output String:
Evaluates To: 0
I've tracked the bug down to this line:
The lines commented as "Open", "Args" and "Close" are the problem.
As you can see, I do not allow nested parentheses for function calls.
I didn't allow them because I wouldn't know when the end of the argument list would be.
So..
Is there a way, using regex, to specify.. Uh..
"I want there to be the same amount of open and close parentheses inside the arg list" ?
The "Open" line is the beginning of the argument list and the "Close" line is the end of the argument list. So, the "Args" line is all that needs changing, I guess.
I'm just not sure if regex can count characters and stuff.
It reads mathematical expressions correctly until I try and nest parentheses for function calls..
For example:
Input: cos(0)
Output String: (cos(0))
Evaluates To: 1
Input: cos((0))
Output String:
Evaluates To: 0
I've tracked the bug down to this line:
Code:
static regex expression_token(
string("(\\d+\\.\\d+)|")+ //Number followed by period followed by number
"(\\d+\\.)|"+ //Number followed by period
"(\\.\\d+)|"+ //Period followed by number
"(\\d+)|"+ //Number with no period
"(\\()|"+ //Open parentheses
"(\\))|"+ //Close parentheses
"([^\\dA-Za-z]+)|"+ //An operator
"([A-Za-z0-9_]*)"+ //Func name
"\\("+ //Open <---
"([^\\(\\)]*)"+ //Args <--
"\\)" //Close <--
);
The lines commented as "Open", "Args" and "Close" are the problem.
As you can see, I do not allow nested parentheses for function calls.
I didn't allow them because I wouldn't know when the end of the argument list would be.
So..
Is there a way, using regex, to specify.. Uh..
"I want there to be the same amount of open and close parentheses inside the arg list" ?
The "Open" line is the beginning of the argument list and the "Close" line is the end of the argument list. So, the "Args" line is all that needs changing, I guess.
I'm just not sure if regex can count characters and stuff.