//main.c
#include<stdio.h>
#include"linklist.h"
#include"LinkStack.h"
#include<string.h>
#include<stdlib.h>
int main_1(void)
{
int i = 0;
int arr[10];
LinkStack* stack = NULL;
//创建栈
stack = SeqStack_Create();
/*判断栈创建的是否成功*/
if (stack == NULL)
{
return -1;
};
/*向栈中添加元素*/
for (int i = 0; i < 5; i++)
{
arr[i] = i + 1;
SeqStack_Push(stack, &arr[i]);
}
/*获取栈的尺寸*/
printf("栈的尺寸为:%d\n", SeqStack_Size(stack));
/*获取栈顶元素*/
printf("栈顶元素为:%d\n", *((int*)SeqStack_Top (stack)));
//删除栈中的元素
while (SeqStack_Size(stack)>0)
{
int* temp =(int*) SeqStack_Pop(stack);
printf("temp此时的值为:%d\n", *temp);
}
/*销毁栈*/
SeqStack_Destroy(stack);
return 0;
}
/*扫描代码*/
int isleft(const char* code)
{
int result = 0;
switch (*code)
{
case '<':
case'(':
case'[':
case'{':
result = 1;
break;
default:
result = 0;
break;
}
return result;
}
int isRight(const char* code)
{
int result = 0;
switch (*code)
{
case'>':
case'}':
case']':
case')':
result = 1;
break;
default:
result = 0;
break;
}
return result;
}
int MatchChar(char tmp_char_1, char tmp_char_2)
{
int result = 0;
switch (tmp_char_1)
{
case '<':
result = (tmp_char_2 == '>') ;
break;
case'{':
result = (tmp_char_2 == '}') ;
break;
case'[':
result = (tmp_char_2 == ']') ;
break;
case'(':
result = (tmp_char_2 == ')') ;
break;
default:
result = 0;
break;
}
return result;
}
int scanner(const char* code)
{
LinkStack* stack = NULL;
stack = SeqStack_Create();
char* tmp_char_1;
char* tmp_char_2;
int result;
while (code!= NULL)
{
tmp_char_1 = code;
if (isleft(code))
{
SeqStack_Push(stack,(void*)tmp_char_1);
}
if (isRight(code))
{
tmp_char_2 = (char*)SeqStack_Pop(stack);
/*匹配match*/
if (!(MatchChar(*tmp_char_2, *tmp_char_1))||tmp_char_2 ==NULL)
{
printf("匹配失败");
}
else
{
break;
}
}
code++;
}
if (SeqStack_Size(stack)!= 0)
{
printf("Sorry error");
}
else
{
printf("Successful match");
}
}
int main_2(void)
{
const char* code = "#include <stdio.h> int main() { int a[4][4]; int (*p)[4]; p = a[0]; return 0;} ";
scanner(code);
}
int main(void)
{
int return_num;
return_num = main_2();
return return_num;
}
//伪代码
创建栈;
为栈分配内存;
while(扫描的字符串非空)
{
if(该字符为普通字符)
{}
if(该字符为左括号)
{左符号压入栈}
if(该字符为右括号)
{左符号弹出栈}
if(左符号与右符号匹配)
{继续}
else(匹配不成功)
{报错停止循环}
}
//判断是否扫描完成全部字符串
//if(stack.size == 0)
{匹配成功}
else
{匹配失败,报错}
}