Exercise 4.5 (10 points)
Write an program(c or c++) to receive a characterstring from the keyboard(the maximum length is 100), and testwhether the brackets ()?[]?{}in the string arematching.
typedef char SElemType;
Status BracketsMatching(char s[])
{
SqStack S;
char c;
char e;
InitStack(S);
for(i=0, c=s[i]; c!=’ ’; ++i, c=s[i])
{
if(c==’(’ || c==’[’ || c==’{’)
Push(S, c);
elseif (c==’)’ || c==’]’ ||c ==’}’)
{
add some codes here
}
}
}
int main()
{
char c;
char s[100];
int len=0;
c=getchar();
while(c!=’#’) // ‘#’ means the end of string
{
s[len++]=c;
c=getchar();
}
s[len]=’ ’;
if(BracketsMatching(s)==TRUE)
printf(“the brackets in the string are matching”);
else
printf(“the brackets in the string are not matching”);
}
Expert Answer
An answer will be send to you shortly. . . . .