2024-3-18

This commit is contained in:
2024-03-18 20:18:12 +08:00
parent 8933bff3c7
commit 59e0405033
51 changed files with 5263 additions and 0 deletions

View File

@@ -0,0 +1,69 @@
/***链栈实现括号匹配***/
#include<string>
#include<iostream>
using namespace std;
#include"LinkStack.h"
//算法3.11 括号的匹配
bool match(string exp)
{
//检验表达式(表达式以"#"结束)中所含"["和"]"、"("和")"是否匹配如果匹配则返回true否则返回false。
//表达式以“#”结束
SNode<char>* S;
InitStack(S);
int flag = 1; // 标记查找结果以控制循环及返回结果
char ch;
char e, x;
int i = 0;
ch = exp[i++]; // 读入第一个字符
while (ch != '#' && flag)
{
switch (ch)
{
case '[':
case '(': // 若是左括号,则将其压入栈
cout << "左括号进栈!" << endl;
Push(S, ch);
break;
case ')': // 若是右括号“)”,则根据栈顶元素的值分情况考虑
GetTop(S, e);
if (!StackEmpty(S) && e == '(') // 若栈非空且栈顶元素是“(”,则匹配成功
{
Pop(S, x);
cout << "右括号出栈!" << endl;
}
else
flag = 0; // 若栈空或栈顶元素不是“(”,则非法
break;
case ']': // 若是右括号“]”,则根据栈顶元素的值分情况考虑
GetTop(S, e);
if (!StackEmpty(S) && e == '[') // 若栈顶元素是“[”,则匹配成功
Pop(S, x);
else
flag = 0; // 若栈空或栈顶元素不是“[”,则非法
break;
}//switch
ch = exp[i++]; //继续读入下一个字符
}//while
if (StackEmpty(S) && flag) // 栈空且标志为true,括号匹配返回true
return true;
else // 否则,括号不匹配,返回false
return false;
}//match
int main()
{
int flag;
string exp;
cout << "请输入待匹配的表达式,以“#”结束:" << endl;
cin >> exp;
flag = match(exp);
if (flag)
cout << "括号匹配成功!" << endl;
else
cout << "括号匹配失败!" << endl;
return 0;
}//end of main function

View File

@@ -0,0 +1,138 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{bc507152-80ab-4a4f-9006-339728d357ca}</ProjectGuid>
<RootNamespace>BracketsMatch</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="LinkStack.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="BracketsMatch.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="源文件">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="头文件">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="资源文件">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="LinkStack.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="BracketsMatch.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,92 @@
template <class DT>
struct SNode //结点
{
DT data; //数据域,存储数据元素值
SNode* next;//指针域,指向下一个结点
};
//【算法3.6】
template <class DT>
void InitStack(SNode<DT>*& S)//创建空链栈
{
S = NULL;
}
//【算法3.7】
template <class DT>
void DestroyStack(SNode<DT>* (&S))//释放链栈
{
SNode<DT>* p;
while (S)//从头结点开始,依次释放结点
{
p = S;
S = S->next;
delete p;
}
//L=NULL;//头结点指向空
}
//【算法3.8】
template<class DT>
bool Push(SNode<DT>*& S, DT e)
{
SNode<DT>* p;
p = new SNode<DT>;
if (!p) return false; //创建失败,结束运行
p->data = e; // 新结点赋值
p->next = S; //结点S链接到p结点之后
S = p;
return true; // 插入成功返回true
}
//【算法3.9】
template<class DT>
bool Pop(SNode<DT>*& S, DT& e)
{
SNode<DT>* p;
if (S == NULL) return false;
p = S;
e = p->data;
S = S->next;
delete p;
return true; // 删除成功返回true
}
//【算法3.10】
template<class DT>
bool GetTop(SNode<DT>* S, DT& e)
{
SNode<DT>* p;
if (S == NULL) return false;
p = S;
e = p->data;
return true; // 删除成功返回true
}
//测栈空
template<class DT>
bool StackEmpty(SNode<DT>* S)
{
if (S == NULL)
return true;
else
return false;
}
//显示栈内容
template<class DT>
void DispStack(SNode<DT>* S)
{
SNode<DT>* p;
p = S;
while (p)
{
cout << p->data << "\t";
p = p->next;
}
cout << endl;
}