# 7-6 symbol pairing (24 points)   This problem uses c + + to do bird food, the amount of code will be less, but the c language is not much more complex. Of course, if it's the c + + boss when I didn't say, anyway, I also traverse when I do it in c + +, and I also traverse when I do it in c language. In short, I can only traverse. The teacher asked to use c language, so let's finish it in c language.
The idea of my code is:
1. First traverse the whole string, and the symbols that encounter symbol conditions are stored in an array temp.
2. The array implements a stack. When traversing the stored character array temp, the left symbol is put into the stack and the right symbol is out of the stack. The inconsistent symbol flag=0. You can directly jump out of the loop without continuing to traverse temp.
3. After the value of flag changes or the temp array is traversed, proceed to the judgment step.
(1) If the temp array is traversed and the stack is empty, top = = 0 (the initial value of top in my code is 0, or - 1, depending on your preference) and the flag value is still 1, then yes is a perfect match without error.
(2) If the traversal has ended (for example, flag=0), top= 0, that is, the stack is not empty. At this time, the default left symbol is not paired. Don't ask me why. Ask the person who asked the question. I tried it manually and attached the operation results: Obviously, after the code runs, the symbol pairing should be? -], However, the principle is that when traversing to ']', if the symbol does not match the '{' in the stack, it will directly flag=0; therefore, this is why as long as the stack is not empty after traversal, it will directly default to the left symbol.
(3) The last case is easier to understand, that is, the stack is empty and there is no left symbol to go in, but there is a right symbol behind temp, so there is a problem with the right symbol.
4. The last step is to output the results according to the requirements of the topic. Wuhu!

```//Lin Hou with a bad mind
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int top=0;//Make stack top
int main()
{
char stack;//Stores the symbol for the left half
char temp;//Store all symbols to be checked in the string
char str;//Keep the input characters for each line
int j=0;
int flag=1;//Final judgment
while(gets(str))
{
if(str=='.')break;//Subject conditions, the next carriage return judgment can be fully ac without adding
for(int i=0;i<strlen(str);i++)//gets can only check one line, so str stores one line of characters respectively before judging
{
if(str[i]=='('||str[i]=='['||str[i]=='{'||str[i]==')'||str[i]==']'||str[i]=='}')
{
temp[j++]=str[i];
}
else if(str[i]=='/'&&str[i+1]=='*')
{
temp[j++]='a';
i++;
}
else if(str[i]=='*'&&str[i+1]=='/')
{
temp[j++]='b';
i++;
}
}

}//So far, all the symbols to be checked have been saved in temp. Next, we only need to use the knowledge of stack to judge the characters in temp
char error;//Define a character, followed by.
for(int i=0;i<j;i++)//Traverse the temp array.
{
if(temp[i]=='('||temp[i]=='['||temp[i]=='{'||temp[i]=='a')//Store the left values of a pair of symbols in the stack array
{
stack[top++]=temp[i];
}
if(temp[i]==')')//When you encounter the symbols on the right half, find a way to realize the stack function in the array stack
{
if(stack[top-1]=='(')top--;//The last stored symbols in the stack array meet the conditions, and then top --. The next time you encounter the symbols on the left half, the original value will be overwritten by the new value, and the stack function is realized
else//If there are unequal situations, the results will come out directly
{
error=temp[i];
flag=0;//The conditions are no longer met
break;
}

}
if(temp[i]==']')//The following is the case
{
if(stack[top-1]=='[')top--;
else
{
error=temp[i];
flag=0;
break;
}
}
if(temp[i]=='}')
{
if(stack[top-1]=='{')top--;
else
{
error=temp[i];
flag=0;
break;
}
}
if(temp[i]=='b')
{
if(stack[top-1]=='a')top--;
else
{
error=temp[i];
flag=0;
break;
}
}

}
if(flag==1&&top==0)printf("YES");//If there is no error, the characters in the stack have been paired. At this time, flag=1,top=-
else//When flag=0, there are two situations. One is the error of top!=0, that is, the unpaired symbol is in the middle. At this time, the title seems to be the default left symbol pairing, that is, the column is like (-)?
{
printf("NO\n");
if(top!=0)//
{
if(stack[top-1]=='(')printf("%c-?",stack[top-1]);//If you pay attention to the requirements of the output format, there will be no problem
if(stack[top-1]=='[')printf("%c-?",stack[top-1]);
if(stack[top-1]=='{')printf("%c-?",stack[top-1]);
if(stack[top-1]=='a')printf("/*-?");
}
else//At this time, top==0, that is, the symbols in the stack are paired, but there is another symbol that is not paired. At this time, there is a problem with the symbol on the right
{
if(error==')')printf("?-%c",error);
if(error==']')printf("?-%c",error);
if(error=='}')printf("?-%c",error);
if(error=='b')printf("?-*/");
}
}
return 0;
}
```

Tags: C data structure

Posted on Tue, 19 Oct 2021 02:00:18 -0400 by nikkio3000