#include <vector>
#include <string>
//This function only works on cases: 2*(3*(4+(4*4) ) ), not on: 2*(3+4)/(2+4)....
//I need it to work on the second, in a way such that each parentheses can be easily accessed in a vector
void get_parentheses(std::string s, std::vector< std::vector<std::string> > &n){
n.resize(1);
n[0].resize(1);
unsignedshort y,x,k;
for( y = x = k = 0; k < s.size(); ++k ){
//New parentheses found, goto next element of n
if ( s[k] == '(' ){
++x;
//If element not exist, create it
if ( n[y].size() == x ){
n[y].resize( n[0].size() +1 );
n[y][ n[y].size() -1 ] = "";
}
}
//No longer in this parentheses, return to the previous element of n.
elseif ( s[k] == ')' ){
--x;
}
else //Store what's within the parentheses.
n[y][x] += s[k];
}
}
#include <iostream>
int main (int argc, char **argv){
usingnamespace std;
vector< vector<string> > n;
get_parentheses("2*(3*(4+4*(5+3)))",n);
//Notice each parentheses only contain one other parentheses.
//How would i obtain the parentheses 2*(3/(4-9)/(4*5))?
//In a way such that i can decode them nearly or easier than
//n[y][x], anyone of you have source code i could look at ?
//Just ignore the multidimensional vector, i know it is not needed
//To obtain the parentheses within the string that is passed to
//get_parentheses().
for ( unsignedshort k = 0, y; k < n.size();++k )
for ( y = 0; y < n[k].size(); ++y)
cout<<n[k][y]<<endl;
return 0;
}
Just be clear, the problem isn't obtaining the parentheses.
The problem is to organize them in a vector or a similar concept, such that it is possible to
access for instance all the parentheses of the second parentheses in a way similar to n[y] or n[y][x] <- Something like that.