for loop iterator

Jan 7, 2010 at 6:15am
consider this code I made.

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
  char input[80];
  char allCloseBrace[] = {']','}','>',')'};
  char allOpenBrace[] = {'[','{','<','('};
  int i=0,j,locClose = -1,tempClose = -1,tempOpen = -1;
  bool checkOpenBrace = false;
  cout<<"Input anything with random brackets: ";
  cin<<input;
while (true){
  //for loop that searches close brace
  for(i;i<80;i++){
     for(j=0;j<4;j++){
	if(input[i] == allCloseBrace[j]){
	   locClose = i;
	   kindBrace = j;
	   break;
	}
     }
    if(locClose == 0)
       cout<<"incorrect order";//or return false to exit the while loop;
    if(locClose>0)
       break;
  }
  if(locClose == -1)
     checkOpenBrace = true;

  if(checkOpenBrace == true){
      //checks if there is/are open brace if none output correct else incorrect
  }
  tempClose = locClose;
  //for loop that searches open brace
  for(k=locClose;k>=0;k++){
      //what I want here is a code that skips tempClose and tempOpen
      for(kj = countOpen;kj>=0;kj--){
          if(storeBrace[k]==allOpenBrace[kindBrace]){
	      tempOpen = k;
	      break;
	  }
      }
  }
  i = locClose + 1;
}

for example, the user input a[s{dp[asd]asdddsd]sd{sd}<d>(s)]
the first close bracket is the close bracket at input[10].
then the first open bracket before input[10] is input[6].
since the while loop is true it will loop again.
now the first close bracket must be at input[18].
for the loop that searches open brace, it will start searching at input[17] and stop at input[11] then continue at input[5].

My question is: Is there a simplier or shorter code doing this? without using the STL for algorithm (#include <algorithm>). Thanks.


EDIT: in general, what if I don't want the iterations of the for loop be executed at a certain number like
1
2
3
4
5
6
7
8
9
10
11
12
13
for(i=80;i>=0;i--){
  if(i==78)
     i =  75;
  if(i==56)
     i = 40;
  //and so on...  
}
//to be specific
for(i=locClose;i>=0;i--){
  if(i==tempClose)
    i = tempOpen - 1;
  /*where the value of locClose, tempClose, and tempOpen is changing depending on the user input*/   
}

Hmmm... in Mathematics i is a set of real numbers from 0 to 80 except for values (75,78],(40,56].
sorry I can't put that in Mathematical form.
Last edited on Jan 7, 2010 at 8:13am
Jan 7, 2010 at 12:58pm
If what you are trying to do is check to ensure proper bracketing, then your code is quite
wrong.

Any time you see an open brace/bracket/paren, push it onto a stack. Any time you see
a closing brace/bracket/paren, pop the top element off the stack and ensure that the
element popped is of the same "type" (brace/bracket/paren) as the closing character
you just found.

Expression is not well formed if:
* the top element does not match what you expect;
* you attempt to pop an element off an empty stack (too many closing chars)
* the stack is not empty after all input consumed (too few closing chars)
Jan 8, 2010 at 5:13am
Thanks jsmith. I already programmed a working algorithm for this one.
Topic archived. No new replies allowed.