idk why these stuffs get stashed for so long and I didn't ever commit them

This commit is contained in:
2025-11-06 09:43:54 +08:00
parent 5dbdfef1a1
commit e01c041259
232 changed files with 22806 additions and 1256 deletions

View File

@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="BracketsMatch" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/BracketsMatch" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/BracketsMatch" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
<Add option="-fexceptions" />
</Compiler>
<Unit filename="BracketsMatch.cpp" />
<Extensions />
</Project>
</CodeBlocks_project_file>

View File

@@ -0,0 +1,107 @@
/***链栈实现括号匹配***/
#include<iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef char SElemType;
typedef int Status;
typedef struct SNode{
int data;
struct SNode *next;
}SNode,*LinkStack;
Status InitStack(LinkStack &S)
{
S = NULL;
return OK;
}
bool StackEmpty(LinkStack S)
{
if(!S)
return true;
return false;
}
Status Push(LinkStack &S,SElemType e)
{
SNode *p = new SNode;
if(!p)
{
return OVERFLOW;
}
p->data = e;
p->next = S;
S = p;
return OK;
}
Status Pop(LinkStack &S,SElemType &e)
{
SNode *p;
if(!S)
return ERROR;
e = S->data;
p = S;
S = S->next;
delete p;
return OK;
}
Status GetTop(LinkStack &S,SElemType &e)
{
if(!S)
return ERROR;
e = S->data;
return OK;
}
//括号的匹配
Status Matching(LinkStack S)
{
//检验表达式中所含括号是否正确匹配如果匹配则返回true否则返回false。
//表达式以“#”结束
int flag=1; //标记查找结果以控制循环及返回结果
char c;
SElemType x;
cin>>c; //读入第一个字符
while(c!='#' && flag)
{
switch (c){
case '[':
case '(': //若是左括号,则将其压入栈
Push(S,c);
break;
case ')' : //若是右括号“)”,则根据当前栈顶元素的值分情况考虑
GetTop(S,x);
if (!StackEmpty(S) && x=='(' ) //若栈非空且栈顶元素是“(”,则匹配成功
Pop(S,x);
else
flag=0; //若栈空或栈顶元素不是“(”,则非法
break;
case ']' : //若是右括号“]”,则根据当前栈顶元素的值分情况考虑
GetTop(S,x);
if (!StackEmpty(S) && x=='[' ) //若栈顶元素是“[”,则匹配成功
Pop(S,x);
else
flag=0; //若栈空或栈顶元素不是“[”,则非法
break;
}//switch
cin>>c; //继续读入下一个字符
}//while
if (StackEmpty(S) && flag )
return 1;
else
return 0;
}//Matching
int main()
{
LinkStack S;
InitStack(S);
cout<<"请输入待匹配的表达式,以“#”结束:"<<endl;
int flag = (int)Matching(S);
if(flag)
cout<<"括号匹配成功!"<<endl;
else
cout<<"括号匹配失败!"<<endl;
return 0;
}

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
<File name="BracketsMatch.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="2162" topLine="83" />
</Cursor>
</File>
</CodeBlocks_layout_file>