-
Notifications
You must be signed in to change notification settings - Fork 0
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
Bracket sequence #9
base: master
Are you sure you want to change the base?
Changes from 9 commits
94609dc
efc2041
4d07785
2bb51d9
60fbd81
54c7a52
9533022
41f713d
4146470
927b90a
9e72313
52d86bf
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 15 | ||
VisualStudioVersion = 15.0.27703.1 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "bracket sequence", "bracket sequence\bracket sequence.vcxproj", "{E0E7D27A-9576-458F-B076-FBF3A2DEB9C8}" | ||
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 | ||
{E0E7D27A-9576-458F-B076-FBF3A2DEB9C8}.Debug|x64.ActiveCfg = Debug|x64 | ||
{E0E7D27A-9576-458F-B076-FBF3A2DEB9C8}.Debug|x64.Build.0 = Debug|x64 | ||
{E0E7D27A-9576-458F-B076-FBF3A2DEB9C8}.Debug|x86.ActiveCfg = Debug|Win32 | ||
{E0E7D27A-9576-458F-B076-FBF3A2DEB9C8}.Debug|x86.Build.0 = Debug|Win32 | ||
{E0E7D27A-9576-458F-B076-FBF3A2DEB9C8}.Release|x64.ActiveCfg = Release|x64 | ||
{E0E7D27A-9576-458F-B076-FBF3A2DEB9C8}.Release|x64.Build.0 = Release|x64 | ||
{E0E7D27A-9576-458F-B076-FBF3A2DEB9C8}.Release|x86.ActiveCfg = Release|Win32 | ||
{E0E7D27A-9576-458F-B076-FBF3A2DEB9C8}.Release|x86.Build.0 = Release|Win32 | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {A3CDAC60-BE3A-42CD-AD96-73C4C1E3F3A4} | ||
EndGlobalSection | ||
EndGlobal |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#include "Element.h" | ||
|
||
Element::Element(char data = '\0') | ||
{ | ||
this->data = data; | ||
this->next = nullptr; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#pragma once | ||
|
||
struct Element | ||
{ | ||
explicit Element(char data); | ||
|
||
char data; | ||
Element* next; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include "Stack.h" | ||
#include "Element.h" | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
Stack::Stack() | ||
{ | ||
head = nullptr; | ||
} | ||
|
||
Stack::~Stack() | ||
{ | ||
while (!isEmpty()) | ||
{ | ||
auto* temp = head; | ||
head = temp->next; | ||
temp->next = nullptr; | ||
delete temp; | ||
} | ||
} | ||
|
||
void Stack::push(char data) | ||
{ | ||
auto* newElement = new Element(data); | ||
if (!isEmpty()) | ||
{ | ||
newElement->next = head; | ||
} | ||
head = newElement; | ||
} | ||
|
||
bool Stack::pop() | ||
{ | ||
if (isEmpty()) | ||
{ | ||
return false; | ||
} | ||
auto* temp = head->next; | ||
head->next = nullptr; | ||
delete head; | ||
head = temp; | ||
return true; | ||
} | ||
|
||
char Stack::peek() const | ||
{ | ||
if (!isEmpty()) | ||
{ | ||
return head->data; | ||
} | ||
cout << "Stack is empty\n"; | ||
return '\0'; | ||
} | ||
|
||
bool Stack::isEmpty() const | ||
{ | ||
return head == nullptr; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#pragma once | ||
|
||
struct Element; | ||
|
||
struct Stack | ||
{ | ||
// ����������� | ||
Stack(); | ||
|
||
// ���������� | ||
~Stack(); | ||
|
||
// ���������� �������� � ������ | ||
void push(char data); | ||
|
||
// �������� �������� �� ������ | ||
bool pop(); | ||
|
||
// ���������� �� �������� ������� | ||
char peek() const; | ||
|
||
// ��������� �������� �� ���� ������ | ||
bool isEmpty() const; | ||
private: | ||
Element* head; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#include "bracket sequence.h" | ||
#include "Stack.h" | ||
|
||
bool isBracket(char const symbol); | ||
bool ifClosingBracket(char const symbol); | ||
bool ifOpeningBracket(char const symbol); | ||
bool ifPairOfBrackets(char const openingBracket, char const closingBracket); | ||
|
||
bool ifBracketSequenceIsCorrect(std::string const & str) | ||
{ | ||
Stack s; | ||
for (int i = 0; i < str.size(); ++i) | ||
{ | ||
if (isBracket(str[i])) | ||
{ | ||
if (s.isEmpty()) | ||
{ | ||
if (!ifOpeningBracket(str[i])) | ||
{ | ||
return false; | ||
} | ||
s.push(str[i]); | ||
} | ||
else | ||
{ | ||
if (ifClosingBracket(str[i])) | ||
{ | ||
if (!ifPairOfBrackets(s.peek(), str[i])) | ||
{ | ||
return false; | ||
} | ||
s.pop(); | ||
} | ||
else | ||
{ | ||
s.push(str[i]); | ||
} | ||
} | ||
} | ||
} | ||
if (!s.isEmpty()) | ||
{ | ||
return false; | ||
} | ||
return true; | ||
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. Это надо в одну строчку писать 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. Напоминаю :) |
||
} | ||
|
||
bool isBracket(char const symbol) | ||
{ | ||
return (symbol == '(' || symbol == ')' || symbol == '[' || symbol == ']' || | ||
symbol == '{' || symbol == '}'); | ||
} | ||
|
||
bool ifClosingBracket(char const symbol) | ||
{ | ||
return (symbol == ')' || symbol == ']' || symbol == '}'); | ||
} | ||
|
||
bool ifOpeningBracket(char const symbol) | ||
{ | ||
return (symbol == '(' || symbol == '[' || symbol == '{'); | ||
} | ||
|
||
bool ifPairOfBrackets(char const openingBracket, char const closingBracket) | ||
{ | ||
if (openingBracket == '(' && closingBracket == ')') | ||
{ | ||
return true; | ||
} | ||
if (openingBracket == '[' && closingBracket == ']') | ||
{ | ||
return true; | ||
} | ||
if (openingBracket == '{' && closingBracket == '}') | ||
{ | ||
return true; | ||
} | ||
return false; | ||
} | ||
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. И даже это :) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#pragma once | ||
#include <string> | ||
|
||
bool ifBracketSequenceIsCorrect(std::string const & str); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project DefaultTargets="Build" ToolsVersion="15.0" 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>15.0</VCProjectVersion> | ||
<ProjectGuid>{E0E7D27A-9576-458F-B076-FBF3A2DEB9C8}</ProjectGuid> | ||
<RootNamespace>bracketsequence</RootNamespace> | ||
<WindowsTargetPlatformVersion>10.0.17134.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>v141</PlatformToolset> | ||
<CharacterSet>MultiByte</CharacterSet> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>false</UseDebugLibraries> | ||
<PlatformToolset>v141</PlatformToolset> | ||
<WholeProgramOptimization>true</WholeProgramOptimization> | ||
<CharacterSet>MultiByte</CharacterSet> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>true</UseDebugLibraries> | ||
<PlatformToolset>v141</PlatformToolset> | ||
<CharacterSet>MultiByte</CharacterSet> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>false</UseDebugLibraries> | ||
<PlatformToolset>v141</PlatformToolset> | ||
<WholeProgramOptimization>true</WholeProgramOptimization> | ||
<CharacterSet>MultiByte</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 /> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | ||
<ClCompile> | ||
<WarningLevel>Level3</WarningLevel> | ||
<Optimization>Disabled</Optimization> | ||
<SDLCheck>true</SDLCheck> | ||
<ConformanceMode>true</ConformanceMode> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Console</SubSystem> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
<ClCompile> | ||
<WarningLevel>Level3</WarningLevel> | ||
<Optimization>Disabled</Optimization> | ||
<SDLCheck>true</SDLCheck> | ||
<ConformanceMode>true</ConformanceMode> | ||
</ClCompile> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | ||
<ClCompile> | ||
<WarningLevel>Level3</WarningLevel> | ||
<Optimization>MaxSpeed</Optimization> | ||
<FunctionLevelLinking>true</FunctionLevelLinking> | ||
<IntrinsicFunctions>true</IntrinsicFunctions> | ||
<SDLCheck>true</SDLCheck> | ||
<ConformanceMode>true</ConformanceMode> | ||
</ClCompile> | ||
<Link> | ||
<EnableCOMDATFolding>true</EnableCOMDATFolding> | ||
<OptimizeReferences>true</OptimizeReferences> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<ClCompile> | ||
<WarningLevel>Level3</WarningLevel> | ||
<Optimization>MaxSpeed</Optimization> | ||
<FunctionLevelLinking>true</FunctionLevelLinking> | ||
<IntrinsicFunctions>true</IntrinsicFunctions> | ||
<SDLCheck>true</SDLCheck> | ||
<ConformanceMode>true</ConformanceMode> | ||
</ClCompile> | ||
<Link> | ||
<EnableCOMDATFolding>true</EnableCOMDATFolding> | ||
<OptimizeReferences>true</OptimizeReferences> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemGroup> | ||
<ClCompile Include="Element.cpp" /> | ||
<ClCompile Include="bracket sequence.cpp" /> | ||
<ClCompile Include="main.cpp" /> | ||
<ClCompile Include="Stack.cpp" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ClInclude Include="Element.h" /> | ||
<ClInclude Include="bracket sequence.h" /> | ||
<ClInclude Include="Stack.h" /> | ||
</ItemGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | ||
<ImportGroup Label="ExtensionTargets"> | ||
</ImportGroup> | ||
</Project> |
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.
В принципе, если стек пуст, то head у него nullptr, так что
newElement->next = head;
ничего плохого не сделает и проверка не нужна. Но как у Вас --- более читабельно и логично, поэтому тоже ок.