Home > Back-end >  Wrong again...
Wrong again...

Time:10-24

/* 1.8 h homework problems 

H. if allowed to contain 3 brackets in expression: parentheses, square brackets and braces,
Design an algorithm in a sequential expression in parentheses is properly matched stack judgment,
Void Bracketmatch (const char * c)

*/
# include & lt; iostream>
# include & lt; Cstring>
# include & lt; String>

using namespace std;
The class stack {
Private:
Int maxsize;
int top;
Char * st.

Public:



The stack (int size)
{
Maxsize=size;
Top=1;
St=new char [maxsize];
}

Void push (char item)
{
St [+ + top]=item;
}

Char top1 ()
{
Return st [top];
}

Void the pop ()
{
Top -;
}

Bool empty ()
{
Return the top==1;
}
};
/*
TODO: plan a stack algorithm USES the sequential expression in c is correct matching brackets,
Output: 1. The traversal expression c, in the process of matching error cout & lt; 2. After traversal, the stack is empty, the match is correct, cout & lt; <"The match right!" If not null, the cout & lt; */
Void Bracketmatch (const char * c)//matching brackets
{
Bool bj=true;
Char t;
Stack s1 (strlen (c) + 1);
For (unsigned int I=0; I & lt; Strlen (c); I++)
{
T=c [I];
The switch (t)
{
Case '('
S1. Push (t);
break;
Case [' : '
S1. Push (t);
break;
Case '{' :
S1. Push (t);
break;
Case ') :

If (s1) top1 ()=='(')
{
S1. Pop ();
break;
}
The else
{
Cout & lt; Bj=false;
break;
}
Case '] ':
If (s1) top1 ()=='[')
{
S1. Pop ();
break;
}
The else
{
Cout & lt; Bj=false;
break;
}
Case '} ':
If (s1) top1 ()=='{')
{
S1. Pop ();
break;
}
The else
{
Cout & lt; Bj=false;
break;
}
Default:
break;
}
}
If (bj)
{
If (s1. The empty ())
{
Cout & lt; <"The match right!" }
The else
Cout & lt; }
}

Int main ()
{

String s;
Getline (cin, s);
Bracketmatch (s.c _str ());
return 0;
}


Results no

This task requires the matching function, other place should be is not allowed to change

Here are a few cases

Case 1:
Assume that the input is:
{1 * [3 * 2 + (2-1)]}
The output is:
Match the right!

Case 2:
Assume that the input is:
{} {[] () () () () [] {{{}}}
The output is:
{matching error

Use case 3:
Assume that the input is:
{1 * [3 * 2 + (2, 1)}]
The output is:
} matching error

Use case 4:
Assume that the input is:
(1 * (3 - (3 * [3% (4=7 [5 t}}} i9] 73] 24] 5-6) +) 6 *) 5% {* 3 ^ {{2!!!!! (
The output is:
} matching error

Use case 5:
Assume that the input is:
)]} {] (
The output is:
) matching error

CodePudding user response:

Look at your use case, it should be print only mismatch errors for the first time, so don't match, after printing will exit the loop is good

 void Bracketmatch (const char * c)//matching brackets 
{
Bool bj=true;
Char t;
Stack s1 (strlen (c) + 1);
For (unsigned int I=0; I & lt; Strlen (c); I++)
{
T=c [I];
The switch (t)
{
Case '('
S1. Push (t);
break;
Case [' : '
S1. Push (t);
break;
Case '{' :
S1. Push (t);
break;
Case ') :

If (s1) top1 ()=='(')
{
S1. Pop ();
break;
}
The else
{
Cout & lt; Bj=false;
break;
}
Case '] ':
If (s1) top1 ()=='[')
{
S1. Pop ();
break;
}
The else
{
Cout & lt; Bj=false;
break;
}
Case '} ':
If (s1) top1 ()=='{')
{
S1. Pop ();
break;
}
The else
{
Cout & lt; Bj=false;
break;
}
Default:
break;
}
if (! Bj) break;//add it is ok, as long as there is wrong, it is no longer continue to find matching
}
If (bj)
{
If (s1. The empty ())
{
Cout & lt; <"The match right!" }
The else
Cout & lt; }
}

CodePudding user response:

reference 1st floor qybao response:
see your use case, should be print only mismatch errors for the first time, so meet do not match, it is good to print and then exit the loop

 void Bracketmatch (const char * c)//matching brackets 
{
Bool bj=true;
Char t;
Stack s1 (strlen (c) + 1);
For (unsigned int I=0; I & lt; Strlen (c); I++)
{
T=c [I];
The switch (t)
{
Case '('
S1. Push (t);
break;
Case [' : '
S1. Push (t);
break;
Case '{' :
S1. Push (t);
break;
Case ') :

If (s1) top1 ()=='(')
{
S1. Pop ();
break;
}
The else
{
Cout & lt; Bj=false;
break;
}
Case '] ':
If (s1) top1 ()=='[')
{
S1. Pop ();
break;
}
The else
{
Cout & lt; Bj=false;
break;
}
Case '} ':
If (s1) top1 ()=='{')
{
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related