Esempio n. 1
0
bool parse(FILE *fp, char bracket)
{
	static int ok = true;
	int c;

	while(ok && (c = fgetc(fp)) != EOF) {
		if(bad_bracket(c, bracket)) {
			ok = false;
		}
		if(proper_closing_bracket(c, bracket)) {
			/* printf("%d: '%c'\n",cnt, (char)c); */
			cnt--;
			return true;
		}

		if(is_open_bracket((char)c)) {
			cnt++;
			/* printf("%d: '%c'\n",cnt, (char)c); */
			if(!parse(fp, (char)c)) {
				ok = false;
			}
		}
	}

	return ok;
}
int main() {
    std::string data;

    std::getline(std::cin, data);
    data = trim(data);
    std::stack<char> brackets_stack;
    int count = 0;
    for (size_t idx = 0; idx < data.size(); ++idx) {
        char c = data[idx];
        if (is_close_bracket(c) || is_open_bracket(c)) {
            if (is_open_bracket(c)) brackets_stack.push(c);
            if (is_close_bracket(c)) {
                if (brackets_stack.empty() ||
                    static_cast<int>(c) - static_cast<int>(brackets_stack.top()) >= 3 ||
                    static_cast<int>(c) - static_cast<int>(brackets_stack.top()) <= 0) {
                    std::cout << count;
                    return 0;
                }
                if (!brackets_stack.empty() &&
                    static_cast<int>(c) - static_cast<int>(brackets_stack.top()) < 3 &&
                    static_cast<int>(c) - static_cast<int>(brackets_stack.top()) > 0) {
                    brackets_stack.pop();
                }
                
            }
            count++;
        }
    }
    
    if (brackets_stack.empty())
        std::cout << "CORRECT";
    else
        std::cout << count;

    return 0;

}
Esempio n. 3
0
static char *is_hebrew_command(FriBidiChar *text,int *length)
{
	char const *tag_txt,*env_txt;
	
	static char buffer[MAX_COMMAND_LEN*2+3];
	/* The command can consist at most of name of tag,
	 * environment and breakets */
	
	FriBidiChar *end_ptr;
	if(*text!='\\') {
		return NULL;
	}
	
	end_ptr=text+1;
	if((tag_txt = is_tag_begin_or_end(&end_ptr))!=NULL
		&& is_open_bracket(&end_ptr)
		&& (env_txt=is_heb_environment(&end_ptr))!=NULL
		&& is_close_bracket(&end_ptr))
	{
		*length=end_ptr-text;
		strcpy(buffer,"\\");
		strcat(buffer,tag_txt);
		strcat(buffer,"{");
		strcat(buffer,env_txt);
		strcat(buffer,"}");
		return buffer;
	}
	
	end_ptr=text+1;
	if((tag_txt=is_heb_tag(&end_ptr))!=NULL){
		*length=end_ptr-text;
		strcpy(buffer,"\\");
		strcat(buffer,tag_txt);
		return buffer;
	}
	return NULL;
}