コード例 #1
0
ファイル: read-command.c プロジェクト: simzou/cs111-lab1a
// 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';
}
コード例 #2
0
ファイル: main.c プロジェクト: landm2000/sokoban
void moveMan(u32 kDown,u32 kHeld,int level,LEVELDATA *this_level_data){
    if (kDown & KEY_UP || kHeld & KEY_UP) {
        if (my > 0) {
            char *upline_orig = level_data[level].levelDataLine[my-1];
            char *upline      = this_level_data->levelDataLine[my-1];
            if(upline[mx] == BOXX_CHAR &&
               my > 1){
                upline_orig = level_data[level].levelDataLine[my-2];
                upline      = this_level_data->levelDataLine[my-2];
                if (upline[mx] == FLOR_CHAR ||
                    upline[mx] == MARK_CHAR) {
                    if (kDown & KEY_UP) {
                        upline[mx] = BOXX_CHAR;
                        if (upline_orig[mx] == MARK_CHAR) {
                            push_char_stack(mx,my-2,BOXK_CHAR);
                        }else{
                            push_char_stack(mx,my-2,BOXX_CHAR);
                        }
                        upline = this_level_data->levelDataLine[my-1];
                        upline[mx] = MANN_CHAR;
                        upline = this_level_data->levelDataLine[my];
                        upline[mx] = FLOR_CHAR;
                        upline_orig = level_data[level].levelDataLine[my];
                        if (upline_orig[mx] == MARK_CHAR) {
                            push_char_stack(mx,my,MARK_CHAR);
                        }else {
                            push_char_stack(mx,my,FLOR_CHAR);
                        }
                        push_char_stack(mx,my-1,  MANN_CHAR);
                        my--;
                    }
                }
            }else if (upline[mx]      == FLOR_CHAR ||
                      upline_orig[mx] == MARK_CHAR) {
                if (hcount > 0) {
                    hcount--;
                }else{
                    upline = this_level_data->levelDataLine[my];
                    upline[mx] = FLOR_CHAR;
                    upline_orig = level_data[level].levelDataLine[my];
                    if (upline_orig[mx] == MARK_CHAR) {
                        push_char_stack(mx,my,MARK_CHAR);
                    }else {
                        push_char_stack(mx,my,FLOR_CHAR);
                    }
                    push_char_stack(mx,my-1,  MANN_CHAR);
                    my--;
                    hcount = H_COUNT_MAX;
                }
            }
        }
    }else if ((kDown & KEY_DOWN) || (kHeld & KEY_DOWN)) {
        if (my < this_level_data->height-1) {
            char *upline_orig = level_data[level].levelDataLine[my+1];
            char *upline = this_level_data->levelDataLine[my+1];
            if(upline[mx] == BOXX_CHAR &&
               my < this_level_data->height-2){
                upline_orig = level_data[level].levelDataLine[my+2];
                upline      = this_level_data->levelDataLine[my+2];
                if (upline[mx] == FLOR_CHAR ||
                    upline[mx] == MARK_CHAR) {
                    if (kDown & KEY_DOWN){
                        upline[mx] = BOXX_CHAR;
                        if (upline_orig[mx] == MARK_CHAR) {
                            push_char_stack(mx,my+2,BOXK_CHAR);
                        }else{
                            push_char_stack(mx,my+2,BOXX_CHAR);
                        }
                        upline = this_level_data->levelDataLine[my+1];
                        upline[mx] = MANN_CHAR;
                        upline = this_level_data->levelDataLine[my];
                        upline[mx] = FLOR_CHAR;
                        upline_orig = level_data[level].levelDataLine[my];
                        if (upline_orig[mx] == MARK_CHAR) {
                            push_char_stack(mx,my,MARK_CHAR);
                        }else {
                            push_char_stack(mx,my,FLOR_CHAR);
                        }
                        push_char_stack(mx,my+1,  MANN_CHAR);
                        my++;
                    }
                }
            }else if (upline[mx]      == FLOR_CHAR ||
                upline_orig[mx] == MARK_CHAR) {
                if (hcount > 0) {
                    hcount--;
                }else{
                    upline = this_level_data->levelDataLine[my];
                    upline[mx] = FLOR_CHAR;
                    upline_orig = level_data[level].levelDataLine[my];
                    if (upline_orig[mx] == MARK_CHAR) {
                        push_char_stack(mx,my,MARK_CHAR);
                    }else{
                        push_char_stack(mx,my,FLOR_CHAR);
                    }
                    push_char_stack(mx,my+1,  MANN_CHAR);
                    my++;
                    hcount = H_COUNT_MAX;
                }
            }
        }
    }else if ((kDown & KEY_LEFT) || (kHeld & KEY_LEFT)) {
        if (mx > 0) {
            char *upline_orig = level_data[level].levelDataLine[my];
            char *upline      = this_level_data->levelDataLine[my];
            if (upline[mx-1] == BOXX_CHAR &&
                mx > 1){
                if (upline[mx-2] == FLOR_CHAR ||
                    upline[mx-2] == MARK_CHAR) {
                    if (kDown & KEY_LEFT) {
                        upline[mx] = FLOR_CHAR;
                        upline[mx-1] = MANN_CHAR;
                        upline[mx-2] = BOXX_CHAR;
                        if (upline_orig[mx] == MARK_CHAR) {
                            push_char_stack(mx--,my,MARK_CHAR);
                        }else {
                            push_char_stack(mx--,my,FLOR_CHAR);
                        }
                        push_char_stack(mx,my,  MANN_CHAR);
                        if (upline_orig[mx-1] == MARK_CHAR){
                            push_char_stack(mx-1,my,BOXK_CHAR);
                        }else{
                            push_char_stack(mx-1,my,BOXX_CHAR);
                        }
                    }
                }
            }else if (upline[mx-1] == FLOR_CHAR ||
                      upline[mx-1] == MARK_CHAR) {
                if (hcount > 0) {
                    hcount--;
                }else{
                    upline[mx] = FLOR_CHAR;
                    if (upline_orig[mx] == MARK_CHAR) {
                        push_char_stack(mx--,my,MARK_CHAR);
                    }else{
                        push_char_stack(mx--,my,FLOR_CHAR);
                    }
                    push_char_stack(mx,my,  MANN_CHAR);
                    hcount = H_COUNT_MAX;
                }
            }
        }
    }else if ((kDown & KEY_RIGHT) || (kHeld & KEY_RIGHT)) {
        if (my < this_level_data->width-1) {
            char *upline_orig = level_data[level].levelDataLine[my];
            char *upline = this_level_data->levelDataLine[my];
            if (upline[mx+1] == BOXX_CHAR &&
                mx < this_level_data->width-2){
                if (upline[mx+2] == FLOR_CHAR ||
                    upline[mx+2] == MARK_CHAR) {
                    if (kDown & KEY_RIGHT){
                        upline[mx]   = FLOR_CHAR;
                        upline[mx+1] = MANN_CHAR;
                        upline[mx+2] = BOXX_CHAR;
                        if (upline_orig[mx] == MARK_CHAR) {
                            push_char_stack(mx++,my,MARK_CHAR);
                        }else{
                            push_char_stack(mx++,my,FLOR_CHAR);
                        }
                        push_char_stack(mx,my,  MANN_CHAR);
                        if (upline_orig[mx+1] == MARK_CHAR){
                            push_char_stack(mx+1,my,BOXK_CHAR);
                        }else{
                            push_char_stack(mx+1,my,BOXX_CHAR);
                        }
                    }
                }
            }else if (upline[mx+1] == FLOR_CHAR ||
                      upline[mx+1] == MARK_CHAR) {
                upline[mx] = FLOR_CHAR;
                if (hcount > 0) {
                    hcount--;
                }else{
                    if (upline_orig[mx] == MARK_CHAR) {
                        push_char_stack(mx++,my,MARK_CHAR);
                    }else{
                        push_char_stack(mx++,my,FLOR_CHAR);
                    }
                    push_char_stack(mx,my,  MANN_CHAR);
                    hcount = H_COUNT_MAX;
                }
            }
        }
    }
}