Exemple #1
0
// assume input is string starting with '('
void get_string_up_to_matching_parens(char* const string, char* matched) {
  int i = 1;
  // initialize stack with the open parens, when stack is empty, initial parens
  // was matched
  char_stack* c_stack = malloc(sizeof(struct char_stack));
  push_char_stack('(', c_stack);

  while (!char_stack_empty(c_stack) && string[i] != EOF && string[i] != '\0') {
    // printf("%c", string[i]);
    if (string[i] == '(') {
      push_char_stack('(', c_stack);
    }
    else if (string[i] == ')') {
      pop_char_stack(c_stack);
    }
    // if I have string = (abc) I want to store matched = abc
    // so matched[0] = string[1] and so on
    matched[i-1] = string[i];
    i++;
  }

  if (!char_stack_empty(c_stack)) {
    error(1,0,"no matching close parentheses found");
  }
  // if I have string = (abc) I want to store matched = abc
  // at the end when ) is matched i = 5 and we need to subtract 2 to 
  // overwrite the the close parentheses
  matched[i-2] = '\0';
}
Exemple #2
0
//when popping one char in charstack, two nums will be popped.
void pop_pro(stacknum *list_num, stackchar *list_char)
{
	int pop_num1,pop_num2,res;
	char pop_char1;
	pop_num1=pop_num_stack(list_num);//pop the first elem
	pop_num2=pop_num_stack(list_num);//pop the second elem
	pop_char1=pop_char_stack(list_char);//pop the top operator

	res=jisuan( pop_char1, pop_num1,  pop_num2);//calcu the res and push it into the numstack
	push_num_stack(list_num, res);
}