1.创建栈
typedef struct CharStack{
int top;
int data[SIZE];
}*CharStackPtr;
2.输出栈(跟踪数据)
void outputStack(CharStackPtr paraStack){
for (int i = 0; i <= paraStack -> top; i++){
printf("%c ",paraStack->data[i]);
}
printf("\r\n");
}
3.初始化
CharStackPtr charStackInit(){
CharStackPtr resultPtr = (CharStackPtr)malloc(sizeof(CharStack));
resultPtr->top = -1;
return resultPtr;
}
4.输入元素
void push(CharStackPtr paraStackPtr , int paraValue){
if (paraStackPtr->top >= SIZE - 1){
printf("Cannot push element : stack full.\n");
return;
}
paraStackPtr->top ++;
paraStackPtr->data[paraStackPtr->top] = paraValue;
}
5.取出元素
char put(CharStackPtr paraStackPtr){
if (paraStackPtr->top < 0){
printf("Cannot pop element : stack empty.\n");
return 0;
}
paraStackPtr->top --;
return paraStackPtr->data[paraStackPtr->top + 1];
}
6.括号匹配
bool bracketMatching(char* paraString, int paraLength) {
CharStackPtr tempStack = charStackInit();
push(tempStack,'#');
char tempChar,tempPopedChar;
for (int i = 0; i < paraLength; i++){
tempChar = paraString[i];
switch (tempChar){
case'(':
case'[':
case'{':
push(tempStack,tempChar);
break;
case')':
tempPopedChar = pop(tempStack);
if(tempPopedChar != '('){
return false;
} break;
case']':
tempPopedChar = pop(tempStack);
if (tempPopedChar != '['){
return false;
} break;
case'}':
tempPopedChar = pop(tempStack);
if (tempPopedChar != '{'){
return false;
} break;
default:
break;
}
}
tempPopedChar = pop(tempStack);
if (tempPopedChar != '#'){
return true;
}
return true;
}
7.括号测试
void bracketMatchingTest(){
char* tempExpression = "[12 + (1 - 3)] * 4";
bool tempMatch = bracketMatching(tempExpression,17);
printf("Is the expression '%s' bracket matching ? %d\n",tempExpression,tempMatch);
tempExpression = "( ) )";
tempMatch = bracketMatching(tempExpression,6);
printf("Is the expression '%s' bracket matching ? %d\n",tempExpression,tempMatch);
tempExpression = "()()(())";
tempMatch = bracketMatching(tempExpression,8);
printf("Is the expression '%s' bracket matching ? %d\n",tempExpression,tempMatch);
tempExpression = "({}[])";
tempMatch = bracketMatching(tempExpression,6);
printf("Is the expression '%s' bracket matching ? %d\n",tempExpression,tempMatch);
tempExpression = ")(";
tempMatch = bracketMatching(tempExpression,2);
printf("Is the expression '%s' bracket matching ? %d\n",tempExpression,tempMatch);
}
*** 将数组最后一位字符初始化为‘#’,括号匹配结束后数组里将会剩下‘#’,此时可说明括号匹配正确,否则说明只是中途一部分匹配正确而剩余有一半括号未匹配***
总代码
#include<stdio.h>
#include<malloc.h>
#define SIZE 10
typedef struct CharStack{
int top;
int data[SIZE];
}*CharStackPtr;
void outputStack(CharStackPtr paraStack){
for (int i = 0; i <= paraStack -> top; i++){
printf("%c ",paraStack->data[i]);
}
printf("\r\n");
}
CharStackPtr charStackInit(){
CharStackPtr resultPtr = (CharStackPtr)malloc(sizeof(CharStack));
resultPtr->top = -1;
return resultPtr;
}
void push(CharStackPtr paraStackPtr , int paraValue){
if (paraStackPtr->top >= SIZE - 1){
printf("Cannot push element : stack full.\n");
return;
}
paraStackPtr->top ++;
paraStackPtr->data[paraStackPtr->top] = paraValue;
}
char put(CharStackPtr paraStackPtr){
if (paraStackPtr->top < 0){
printf("Cannot pop element : stack empty.\n");
return 0;
}
paraStackPtr->top --;
return paraStackPtr->data[paraStackPtr->top + 1];
}
void pushPopTest(){
char ch;
CharStackPtr tempStack = charStackInit();
printf("After initialization , the stack is :");
outputStack(tempStack);
for (char ch = 'a'; ch < 'm'; ch ++){
printf("Pushing %c.\n",ch);
push(tempStack,ch);
outputStack(tempStack);
}
for (int i = 0; i < 3; i ++){
ch = pop(tempStack);
printf("Pop %c.\n",ch);
outputStack(tempStack);
}
printf("---pushiPopTest ends.\n");
}
bool bracketMatching(char* paraString, int paraLength) {
CharStackPtr tempStack = charStackInit();
push(tempStack,'#');
char tempChar,tempPopedChar;
for (int i = 0; i < paraLength; i++){
tempChar = paraString[i];
switch (tempChar){
case'(':
case'[':
case'{':
push(tempStack,tempChar);
break;
case')':
tempPopedChar = pop(tempStack);
if(tempPopedChar != '('){
return false;
} break;
case']':
tempPopedChar = pop(tempStack);
if (tempPopedChar != '['){
return false;
} break;
case'}':
tempPopedChar = pop(tempStack);
if (tempPopedChar != '{'){
return false;
} break;
default:
break;
}
}
tempPopedChar = pop(tempStack);
if (tempPopedChar != '#'){
return true;
}
return true;
}
void bracketMatchingTest(){
char* tempExpression = "[12 + (1 - 3)] * 4";
bool tempMatch = bracketMatching(tempExpression,17);
printf("Is the expression '%s' bracket matching ? %d\n",tempExpression,tempMatch);
tempExpression = "( ) )";
tempMatch = bracketMatching(tempExpression,6);
printf("Is the expression '%s' bracket matching ? %d\n",tempExpression,tempMatch);
tempExpression = "()()(())";
tempMatch = bracketMatching(tempExpression,8);
printf("Is the expression '%s' bracket matching ? %d\n",tempExpression,tempMatch);
tempExpression = "({}[])";
tempMatch = bracketMatching(tempExpression,6);
printf("Is the expression '%s' bracket matching ? %d\n",tempExpression,tempMatch);
tempExpression = ")(";
tempMatch = bracketMatching(tempExpression,2);
printf("Is the expression '%s' bracket matching ? %d\n",tempExpression,tempMatch);
}
int main(){
printf("这个代码我不理解!!!!\n");
bracketMatchingTest();
pushPopTest();
printf("现在会了吧!!!!\n");
return 0;
}
运行结构
After initialization , the stack is :
Pushing a.
a
Pushing b.
a b
Pushing c.
a b c
Pushing d.
a b c d
Pushing e.
a b c d e
Pushing f.
a b c d e f
Pushing g.
a b c d e f g
Pushing h.
a b c d e f g h
Pushing i.
a b c d e f g h i
Pushing j.
a b c d e f g h i j
Pushing k.
Cannot push element : stack full.
a b c d e f g h i j
Pushing l.
Cannot push element : stack full.
a b c d e f g h i j
Pop j.
a b c d e f g h i
Pop i.
a b c d e f g h
Pop h.
a b c d e f g
---pushiPopTest ends.
Is the expression '[12 + (1 - 3)] * 4' bracket matching ? 1
Is the expression '( ) )' bracket matching ? 0
Is the expression '()()(())' bracket matching ? 1
Is the expression '({}[])' bracket matching ? 1
Is the expression ')(' bracket matching ?