-
Notifications
You must be signed in to change notification settings - Fork 0
Balance brackets #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Balance brackets #15
Changes from all commits
1a83386
2549f2f
14417f4
8d6727e
e7e22d6
2f9e2a0
a04a9a8
ac6b390
015e87e
5d2e300
d85663e
6a502c1
cf515a4
77c9d15
0dfb7c2
b720cdc
22af234
c713a82
9a45a27
ae4b6a9
e226c08
13aa804
954da9f
a121942
fe152de
1e1980f
bb748f8
022b692
7f6d2c3
7fe9a12
ad099c8
f6c2352
6c04a72
5f1e13d
4fee312
cb50cd2
8e7b5a1
f50a25c
bcf969b
12dbf94
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| | ||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| # Visual Studio Version 16 | ||
| VisualStudioVersion = 16.0.31410.357 | ||
| MinimumVisualStudioVersion = 10.0.40219.1 | ||
| Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "BalanceBrackets", "BalanceBrackets\BalanceBrackets.vcxproj", "{2095AD51-1509-4999-A54D-827860784952}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|x64 = Debug|x64 | ||
| Debug|x86 = Debug|x86 | ||
| Release|x64 = Release|x64 | ||
| Release|x86 = Release|x86 | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {2095AD51-1509-4999-A54D-827860784952}.Debug|x64.ActiveCfg = Debug|x64 | ||
| {2095AD51-1509-4999-A54D-827860784952}.Debug|x64.Build.0 = Debug|x64 | ||
| {2095AD51-1509-4999-A54D-827860784952}.Debug|x86.ActiveCfg = Debug|Win32 | ||
| {2095AD51-1509-4999-A54D-827860784952}.Debug|x86.Build.0 = Debug|Win32 | ||
| {2095AD51-1509-4999-A54D-827860784952}.Release|x64.ActiveCfg = Release|x64 | ||
| {2095AD51-1509-4999-A54D-827860784952}.Release|x64.Build.0 = Release|x64 | ||
| {2095AD51-1509-4999-A54D-827860784952}.Release|x86.ActiveCfg = Release|Win32 | ||
| {2095AD51-1509-4999-A54D-827860784952}.Release|x86.Build.0 = Release|Win32 | ||
| EndGlobalSection | ||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
| GlobalSection(ExtensibilityGlobals) = postSolution | ||
| SolutionGuid = {241DB504-44F6-401D-9485-FE90894D17CA} | ||
| EndGlobalSection | ||
| EndGlobal |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| #include "BalanceBrackets.h" | ||
| #include "../../Stack/Stack/Stack.h" | ||
|
|
||
| bool isOpeningBracket(char parentheses) | ||
| { | ||
| return parentheses == '(' || parentheses == '{' || parentheses == '['; | ||
| } | ||
|
|
||
| bool isClosingBracket(char paretheses) | ||
| { | ||
| return paretheses == ')' || paretheses == '}' || paretheses == ']'; | ||
| } | ||
|
|
||
| bool openingAndClosingOfTheSameType(char openingParetheses, char closingParetheses) | ||
| { | ||
| return openingParetheses != '(' && closingParetheses == ')' | ||
| || openingParetheses != '{' && closingParetheses == '}' | ||
| || openingParetheses != '[' && closingParetheses == ']'; | ||
| } | ||
|
|
||
| bool checkCorrectOrderBrackets(const char* expressionFromParentheses, int* errorCode) | ||
| { | ||
| Stack* head = createStack(); | ||
| int counter = 0; | ||
| while (expressionFromParentheses[counter] != '\0') | ||
| { | ||
| if (isOpeningBracket(expressionFromParentheses[counter])) | ||
| { | ||
| int error = 0; | ||
| push(&head, expressionFromParentheses[counter], &error); | ||
| if (error == 2) | ||
| { | ||
| *errorCode = 2; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Кстати, можно было тогда уж в push сразу errorCode передавать :) Но тогда стоило бы его сделать нулём в начале функции и гарантировать, что как только он не 0, мы тут же выходим. |
||
| deleteStack(&head); | ||
| return false; | ||
| } | ||
| } | ||
| else if (isClosingBracket(expressionFromParentheses[counter])) | ||
| { | ||
| int error = 0; | ||
| char topOfTheStack = (char)pop(&head, &error); | ||
| if (error == 1) | ||
| { | ||
| *errorCode = 1; | ||
| deleteStack(&head); | ||
| return false; | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
| } | ||
| if (openingAndClosingOfTheSameType(topOfTheStack, expressionFromParentheses[counter])) | ||
| { | ||
| *errorCode = 1; | ||
| deleteStack(&head); | ||
| return false; | ||
| } | ||
| } | ||
| counter++; | ||
| } | ||
| if (isEmpty(head)) | ||
| { | ||
| deleteStack(&head); | ||
| return true; | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
| } | ||
| *errorCode = 1; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Я бы тут 0 оставил. Функция корректно отработала, просто скобочная последовательность не корректна (на то Вы true/false и возвращаете) |
||
| deleteStack(&head); | ||
| return false; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| #pragma once | ||
| #include <stdbool.h> | ||
|
|
||
| // Function to check the correct position of the brackets | ||
| bool checkCorrectOrderBrackets(const char* expressionFromParentheses, int* errorCode); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| <?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>16.0</VCProjectVersion> | ||
| <Keyword>Win32Proj</Keyword> | ||
| <ProjectGuid>{2095ad51-1509-4999-a54d-827860784952}</ProjectGuid> | ||
| <RootNamespace>BalanceBrackets</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>v142</PlatformToolset> | ||
| <CharacterSet>Unicode</CharacterSet> | ||
| </PropertyGroup> | ||
| <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> | ||
| <ConfigurationType>Application</ConfigurationType> | ||
| <UseDebugLibraries>false</UseDebugLibraries> | ||
| <PlatformToolset>v142</PlatformToolset> | ||
| <WholeProgramOptimization>true</WholeProgramOptimization> | ||
| <CharacterSet>Unicode</CharacterSet> | ||
| </PropertyGroup> | ||
| <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> | ||
| <ConfigurationType>Application</ConfigurationType> | ||
| <UseDebugLibraries>true</UseDebugLibraries> | ||
| <PlatformToolset>v142</PlatformToolset> | ||
| <CharacterSet>Unicode</CharacterSet> | ||
| </PropertyGroup> | ||
| <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> | ||
| <ConfigurationType>Application</ConfigurationType> | ||
| <UseDebugLibraries>false</UseDebugLibraries> | ||
| <PlatformToolset>v142</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" /> | ||
| <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | ||
| <LinkIncremental>true</LinkIncremental> | ||
| </PropertyGroup> | ||
| <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | ||
| <LinkIncremental>false</LinkIncremental> | ||
| </PropertyGroup> | ||
| <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
| <LinkIncremental>true</LinkIncremental> | ||
| </PropertyGroup> | ||
| <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
| <LinkIncremental>false</LinkIncremental> | ||
| </PropertyGroup> | ||
| <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> | ||
| <ClCompile Include="..\..\Stack\Stack\Stack.c" /> | ||
| <ClCompile Include="BalanceBrackets.c" /> | ||
| <ClCompile Include="BalanceBracketsTest.c" /> | ||
| <ClCompile Include="Main.c" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <ClInclude Include="..\..\Stack\Stack\Stack.h" /> | ||
| <ClInclude Include="BalanceBrackets.h" /> | ||
| <ClInclude Include="BalanceBracketsTest.h" /> | ||
| </ItemGroup> | ||
| <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | ||
| <ImportGroup Label="ExtensionTargets"> | ||
| </ImportGroup> | ||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| <?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> | ||
| <ClCompile Include="BalanceBrackets.c"> | ||
| <Filter>Исходные файлы</Filter> | ||
| </ClCompile> | ||
| <ClCompile Include="BalanceBracketsTest.c"> | ||
| <Filter>Исходные файлы</Filter> | ||
| </ClCompile> | ||
| <ClCompile Include="..\..\Stack\Stack\Stack.c"> | ||
| <Filter>Исходные файлы</Filter> | ||
| </ClCompile> | ||
| <ClCompile Include="Main.c"> | ||
| <Filter>Исходные файлы</Filter> | ||
| </ClCompile> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <ClInclude Include="BalanceBrackets.h"> | ||
| <Filter>Файлы заголовков</Filter> | ||
| </ClInclude> | ||
| <ClInclude Include="..\..\Stack\Stack\Stack.h"> | ||
| <Filter>Файлы заголовков</Filter> | ||
| </ClInclude> | ||
| <ClInclude Include="BalanceBracketsTest.h"> | ||
| <Filter>Файлы заголовков</Filter> | ||
| </ClInclude> | ||
| </ItemGroup> | ||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| #include "BalanceBracketsTest.h" | ||
| #include "BalanceBrackets.h" | ||
|
|
||
| bool balanceBracketsTest() | ||
| { | ||
| int errors[14] = {0}; | ||
| return checkCorrectOrderBrackets("((15 - x) - (13 + 45) * ( 1 0 - 1 6))", &errors[0]) | ||
| && checkCorrectOrderBrackets("(([(){()}][]{}{}{()}))", &errors[1]) | ||
| && checkCorrectOrderBrackets("([[]({})]({}(())))", &errors[2]) | ||
| && checkCorrectOrderBrackets("(x * x) * ( c - a)", &errors[3]) | ||
| && checkCorrectOrderBrackets("[[]]{{}}(())", &errors[4]) | ||
| && checkCorrectOrderBrackets("(({{{}[]}[]}[]))", &errors[5]) | ||
| && checkCorrectOrderBrackets("{}()[]", &errors[6]) | ||
| && !checkCorrectOrderBrackets("{{", &errors[7]) | ||
| && !checkCorrectOrderBrackets("{())[][]}", &errors[8]) | ||
| && !checkCorrectOrderBrackets("(x-a)*(c+a))", &errors[9]) | ||
| && !checkCorrectOrderBrackets("(]])()()[][]{}{{}}", &errors[10]) | ||
| && !checkCorrectOrderBrackets("{()[]{}{{))))))(}", &errors[11]) | ||
| && !checkCorrectOrderBrackets("{{}}(((", &errors[12]) | ||
| && !checkCorrectOrderBrackets("(", &errors[13]) | ||
| && errors[0] == 0 | ||
| && errors[1] == 0 | ||
| && errors[2] == 0 | ||
| && errors[3] == 0 | ||
| && errors[4] == 0 | ||
| && errors[5] == 0 | ||
| && errors[6] == 0 | ||
| && errors[7] == 1 | ||
| && errors[8] == 1 | ||
| && errors[9] == 1 | ||
| && errors[10] == 1 | ||
| && errors[11] == 1 | ||
| && errors[12] == 1 | ||
| && errors[13] == 1; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| #pragma once | ||
| #include <stdbool.h> | ||
|
|
||
| // Function for testing a function that checks for the correct position of the brackets | ||
| bool balanceBracketsTest(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| #include "BalanceBrackets.h" | ||
| #include "BalanceBracketsTest.h" | ||
| #include <stdio.h> | ||
|
|
||
| int main() | ||
| { | ||
| if (!balanceBracketsTest()) | ||
| { | ||
| printf("Test failed"); | ||
| return -1; | ||
| } | ||
| char expressionFromParentheses[250] = { '\0' }; | ||
| printf("enter the expression that you want to check for the correct placement of brackets\n"); | ||
| scanf_s("%[^\n]s", expressionFromParentheses, (unsigned)sizeof(expressionFromParentheses)); | ||
| int errorCode = 0; | ||
| checkCorrectOrderBrackets(expressionFromParentheses, &errorCode); | ||
| if (errorCode == 1) | ||
| { | ||
| printf("The balance of the brackets is incorrect"); | ||
| return 0; | ||
| } | ||
| if (errorCode == 2) | ||
| { | ||
| printf("Insufficient memory"); | ||
| return -1; | ||
| } | ||
| printf("The balance of the brackets is correct"); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Кажется, что лучше бы подошло "notOpeningAndClosingOfTheSameType", или в самой функции поправить и в месте вызова "!" писать