Esempio n. 1
0
int main()
{

	// input string
	std::string str = "f(((g(jh)))))))";
	std::cout << "checking string: " << str;

	int mismatchPos = matchParen(str);

	if (mismatchPos < 0) {
		std::cout << "parenthesis matches";
	} else {
		std::cout << "\nmismatched at  : " << fillWhitespace(mismatchPos);
	}

	std::cout << std::endl;
	return 0;
}
Esempio n. 2
0
int findCentral (char *inputStr, size_t size) {
	char recurBuf[size+1];//add a place for '\0'
	char c;// tem;
	char case1c;
	int i = 0;
	int matchC, start, end;
	strncpy(recurBuf, inputStr, size);
    recurBuf[size] = '\0';

    // TODO : tested --  1+2 , (1+2)
    //        tested --  1.2 + 2.2
    //        tested --  (1+2) * 3
    //        failed --  ((3+5)/(2+2)-2)
	//cases not to recur: 1; (-1); -1.1  --> just one operand
	//cases needing recursion: 1+2; 1.1*2.2; (-1)*2; -1*2; -(1+2) ; ((1+2)*3)-4; 1*(2-3); (1+2)*(3-4)
	switch (judgeR (recurBuf, &i)) {
        case 1:	//mean one operand and no recursion needed
        {
            i = size-1;
            while (i >= 0) {
                case1c = recurBuf[i--];
                if (case1c == '-') {
                    ungetch(' ');
                }
                if (case1c != '(' && case1c != ')' && !isspace(case1c)) {// space should be adjusted
                    ungetch(case1c);
                }
            }
            ungetch(' ');
            return 1;
        }
        case 2:	//mean more than one
            //two situations: useful parenthesis ;; no () or useless ()
        {
            const int dui = i;
            matchC = matchParen(recurBuf, size,  &start, &end);
            if(matchC == -1){
                fprintf(stderr, "parenthesis are not in pair: %s, %s \n", recurBuf, inputStr);
                puts("You seems just entering part of expression, entering more?");
            }
            //means match! -- () exists
            else if (matchC == 0) {
                //useful ()
                if ( hasNum(recurBuf, 0, start-1) != -1 || hasNum(recurBuf, end, size) != -1 ) {
                    while (i < start){
                        c = recurBuf[i++];
                        if ((c == '+'
                                || c == '-') && hasNum(recurBuf, 0, i-1)!=-1 ){
                            ungetch(c);
                            ungetch(' ');
                            goto ufRecursion;
                        }
                    }
                    i = end;
                    while (i < size){
                        c = recurBuf[i++];
                        if ((c == '+'
                                || c == '-') && hasNum(recurBuf, i, size)!=-1 ){
                            ungetch(c);
                            ungetch(' ');
                            goto ufRecursion;
                        }
                    }
                    i = dui;
                    while (i < start){
                        c = recurBuf[i++];
                        if ((c == '*' || c == '/') && hasNum(recurBuf, 0, i-1)!=-1){
                            ungetch(c);
                            ungetch(' ');
                            goto ufRecursion;
                        }
                    }
                    i = end;
                    while (i < size){
                        c = recurBuf[i++];
                        if ((c == '*' || c == '/') && hasNum(recurBuf, i, size)!=-1){
                            ungetch(c);
                            ungetch(' ');
                            goto ufRecursion;
                        }
                    }
                }
                // useless ()
                else {
                    i = start;
                    while (--i >=0){
                        if (recurBuf[i] == '-'){
                            ungetch('-');
                            break;
                        }
                    }
                    ungetch(' ');
                    state &= findCentral(&recurBuf[start+1], end-start-2);
                    return 1;
                }
                return 0;//means the input is not right
            ufRecursion:
                state &= findCentral (&recurBuf[i], size-i);
                state &= findCentral (recurBuf, i-1);

            }// no ()
            else if (matchC == 1){
                //i = 0;
                while (i < size){
                    c = recurBuf[i++];
                    if ((c == '+'
                            || c == '-') && hasNum(recurBuf, 0, i-1)!=-1 ){
                        ungetch(c);
                        ungetch(' ');
                        goto nRecursion;
                    }
                }
                i = dui;
                while (i < size){
                    c = recurBuf[i++];
                    if ((c == '*' || c == '/') && hasNum(recurBuf, 0, i-1)!=-1){
                        ungetch(c);
                        ungetch(' ');
                        goto nRecursion;
                    }
                }
                return 0;//means input is not right
            nRecursion:
                state &= findCentral (&recurBuf[i], size-i);
                state &= findCentral (recurBuf, i-1);//the total size become less than size-1

            } else {
                printf("bad in matchParen: %d \n", matchC);
                return 0;
            }
            return 1;
        }
        default:
            return 0;
	}

}