C++ regex

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:
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.
 

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
iterate through the list and count them, or create a linked list and push each "(" into the front of it, then remove them as you run into ")"s, if you have any "(" left over or run into a ")" when there are no "("s in the linked list then they have an invalid ammount of parenthesis

I myself was looking at creating a calculator that would handle un-known variables, etc, as text, basically an algebraic calculator, that was my "first C++ project" and that was the solution i came up with for the same problem, its nice because using that method you can link each parenthesis to the correct counterpart without iterating through it a bunch of times, just once. O(n) rather than O(n*log(n)) which is the more simple method of iterating through it and expanding each parenthesis then iterating again
 

camelCase

The Case of the Mysterious Camel.
Reaction score
362
My current solution is O(n); it just doesn't handle nested parentheses in function calls =x
(Unless regex isn't a linear algorithm..)
Guess I'll have to change my approach for this, then..

Oh, well.
Thanks.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top