#include <stdio.h>
#include <malloc.h>
#define STACK_MAX_SIZE 10
/**
* Linear stack of integers.The key is date.
*/
typedef struct CharStack {
int top;
int data[STACK_MAX_SIZE];// The maximum length is fixed.//
} *CharStackPtr;
/**
*output the stack
*/
void outputStack(CharStackPtr paraStack) {
for (int i = 0; i<=paraStack->top; i++) {
printf("%c",paraStack->data[i]);
}//Of for .
printf("\r\n");
}//Of outputstack.
/*
*Initialize an empty char stack. No error checking for this function.
* @param paraStackPtr The pointer to the stack. It must be a pointer to change the stack.
* @param paraValues An int array storing all elements.
*/
CharStackPtr charStackInit() {
CharStackPtr resultPtr = (CharStackPtr)malloc(sizeof(struct CharStack));
resultPtr->top = -1;
return resultPtr;
}//Of StackInit.
/**
* push an element to the stack.
* @parm paraValue. the value to be pushed.
*/
void push(CharStackPtr paraStackPtr, int paraValue) {
// step1. space check.
if(paraStackPtr->top >= STACK_MAX_SIZE - 1){
printf("Cannot push element: stack full.\r\n");
return;
}
// step2. update the top.
paraStackPtr->top++;
// step3. push the element.
paraStackPtr->data[paraStackPtr->top] = paraValue;
} //Of push.
/**
* pop an element from the stack.
* @return the poped element.
*/
char pop(CharStackPtr paraStackPtr) {
//step 1. space check.
if (paraStackPtr->top < 0) {
printf("Cannot pop element: stack empty.\r\n");
return '\0';
}//Of if
//step 2. update the top.
paraStackPtr->top--;
//step 3. push element.
return paraStackPtr->data[paraStackPtr->top+1];
} //Of pop.
/**
* test the pop function.
*/
void pushPopTest()
{
printf("---- pushPopTest begins. ----\r\n");
char ch;
//Initialize
CharStackPtr tempStack = charStackInit();
printf("After initialization, the stack is: ");
outputStack(tempStack);
//Push
for(ch ='a'; ch<'m'; ch++) {
printf("Pushing %c.\r\n", ch);
push(tempStack, ch);
outputStack(tempStack);
}//Of push.
//pop
for(int i=0; i<3; i++) {
ch = pop(tempStack);
printf("pop %c\r\n",ch);
outputStack(tempStack);
}//Of for i.
printf("---- pushPopTest ends. ----\r\n");
}// Of pushPopTest
/**
* Is the bracket matching?
*
* @param paraString The given expression.
* @return Match or not.
*/
_Bool bracketMatching(char* paraString, int paraLength) {
// Step 1. Initialize the stack through pushing a '#' at the bottom.
CharStackPtr tempStack = charStackInit();
push(tempStack,'#');
char tempChar, tempPopedChar;
//step2. Process the string.
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 0;
} // Of if
break;
case ']':
tempPopedChar = pop(tempStack);
if (tempPopedChar != '[') {
return 0;
} // Of if
break;
case '}':
tempPopedChar = pop(tempStack);
if (tempPopedChar != '{') {
return 0;
} // Of if
break;
default:
// Do nothing.
break;
}//Of switch.
}//Of for i.
tempPopedChar = pop(tempStack);
if (tempPopedChar != '#') {
return 1;
} // Of if
return 1;
}//Of bracketMatching.
void bracketMatchingTest() {
char* tempExpression = "[2 + (1-3)] * 4";
int tempMatch = bracketMatching(tempExpression,17);
printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);
tempExpression = "( ) )";
tempMatch = bracketMatching(tempExpression, 6);
printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);
tempExpression = "()()(())";
tempMatch = bracketMatching(tempExpression, 8);
printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);
tempExpression = "({}[])";
tempMatch = bracketMatching(tempExpression, 6);
printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);
tempExpression = ")(";
tempMatch = bracketMatching(tempExpression, 2);
printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);
}//Of test.
void main() {
bracketMatchingTest();
}
对代码的理解和思考:
1.由于正常的多项式的书写规则,导致其书写过程的本身就已经表面了优先级,故可以直接将括号压入栈类,最上面的左括号即为优先级最高的,因此当再遇到右括号时,如果其为正常多项式,则必定与之匹配,这就是括号问题的基本思路。
2.关于代码细节上的收获,在定义栈的时候,注意是将top赋值为-1,因此在push时,需要先将top加1,更新Top的值,指向下一个空位,再进行赋值入栈。
而在括号匹配函数中,运用了switch函数,只要满足就一直执行的特点,使得代码看起来简洁易懂。并且在最开始push进‘#’字符作为标识,方便了操作。