I need help. The parentheses of an expression.

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#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);
	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 <iostream>
int main (int argc, char **argv){
	using namespace 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 ( unsigned short k = 0, y; k < n.size();++k )
		for ( y = 0; y < n[k].size(); ++y)
			cout<<n[k][y]<<endl;
	
	return 0;
}
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.