### I need help. The parentheses of an expression.

EDIT: No one's responding, maybe cus of lack of documentation?

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152`` ``````#include #include //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 > &n){ n.resize(1); n[0].resize(1); unsigned short 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. else if ( s[k] == ')' ){ --x; } else //Store what's within the parentheses. n[y][x] += s[k]; } } #include int main (int argc, char **argv){ using namespace std; vector< vector > 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 ( unsigned short k = 0, y; k < n.size();++k ) for ( y = 0; y < n[k].size(); ++y) cout<
Last edited on
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.
Topic archived. No new replies allowed.