Skip to content
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

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions hw6/bracket sequence/bracket sequence.sln
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
7 changes: 7 additions & 0 deletions hw6/bracket sequence/bracket sequence/Element.cpp
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;
}
9 changes: 9 additions & 0 deletions hw6/bracket sequence/bracket sequence/Element.h
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;
};
59 changes: 59 additions & 0 deletions hw6/bracket sequence/bracket sequence/Stack.cpp
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;
}

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; ничего плохого не сделает и проверка не нужна. Но как у Вас --- более читабельно и логично, поэтому тоже ок.

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;
}
26 changes: 26 additions & 0 deletions hw6/bracket sequence/bracket sequence/Stack.h
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;
};
75 changes: 75 additions & 0 deletions hw6/bracket sequence/bracket sequence/bracket sequence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#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]);
}
}
}
}
return s.isEmpty();
}

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;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

И даже это :)

4 changes: 4 additions & 0 deletions hw6/bracket sequence/bracket sequence/bracket sequence.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once
#include <string>

bool ifBracketSequenceIsCorrect(std::string const & str);
136 changes: 136 additions & 0 deletions hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?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" />
<ClCompile Include="tests.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Element.h" />
<ClInclude Include="bracket sequence.h" />
<ClInclude Include="Stack.h" />
<ClInclude Include="tests.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
Loading