Ads 468x60px

Tuesday 13 December 2011

C++ program for matching braces in a parenthesised algebraic expression.

This is a simple C++ program which checks for matching braces in a parenthesised algebraic expression. If the match is successful, the function defined will return 1 or else return 0. The program goes as follows:

#include #include #include int match_paren(char str[20]) { int i, j; cout << "\n\tThe entered string is "; for (i = 0; str[i] != '\0'; i++) cout << str[i]; int stp = 0; // Let starting parenthesis is 0 int enp = 0; // and ending parenthesis is also 0 for (i = 0; str[i] != '\0'; i++) { if (str[i] == '(') stp = stp + 1; else if (str[i] == ')') enp = enp + 1; } if (stp == enp) return 1; else return 0; } void main() { clrscr(); int ctr; char nstr[20]; cout << "Enter any algebraic equation : "; gets(nstr); ctr = match_paren(nstr); if (ctr == 1) cout << "\nThe parenthesis are match equally"; else cout << "\nParenthesis are not matched"; } Source: www.LearnCandCpp.com

If you have any difficulty in understanding the programming concept of this code, just reply with your comment. I shall reply soon.

No comments:

Post a Comment