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

Program converts expressions to postfix form #10

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
31 changes: 31 additions & 0 deletions hw6/from infix to postfix/from infix to postfix.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}") = "from infix to postfix", "from infix to postfix\from infix to postfix.vcxproj", "{54D5BC53-220D-4FE3-BB72-1FF3E987C3AD}"
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
{54D5BC53-220D-4FE3-BB72-1FF3E987C3AD}.Debug|x64.ActiveCfg = Debug|x64
{54D5BC53-220D-4FE3-BB72-1FF3E987C3AD}.Debug|x64.Build.0 = Debug|x64
{54D5BC53-220D-4FE3-BB72-1FF3E987C3AD}.Debug|x86.ActiveCfg = Debug|Win32
{54D5BC53-220D-4FE3-BB72-1FF3E987C3AD}.Debug|x86.Build.0 = Debug|Win32
{54D5BC53-220D-4FE3-BB72-1FF3E987C3AD}.Release|x64.ActiveCfg = Release|x64
{54D5BC53-220D-4FE3-BB72-1FF3E987C3AD}.Release|x64.Build.0 = Release|x64
{54D5BC53-220D-4FE3-BB72-1FF3E987C3AD}.Release|x86.ActiveCfg = Release|Win32
{54D5BC53-220D-4FE3-BB72-1FF3E987C3AD}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {05DC8ABC-44E6-489E-859E-8BB93557CE7C}
EndGlobalSection
EndGlobal
13 changes: 13 additions & 0 deletions hw6/from infix to postfix/from infix to postfix/Element.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "Element.h"

Element::Element(char data = '\0')
{
this->data = data;
this->next = nullptr;
}

Element::~Element()
{
this->data = '\0';
this->next = nullptr;
}
9 changes: 9 additions & 0 deletions hw6/from infix to postfix/from infix to postfix/Element.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

struct Element
{
Element(char data);
~Element();
char data;
Element* next;
};
130 changes: 130 additions & 0 deletions hw6/from infix to postfix/from infix to postfix/Infix to postfix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#include "Infix to postfix.h"

Choose a reason for hiding this comment

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

Вообще, кстати, использовать пробелы в именах файлов с исходниками плохая идея, потому что для некоторых систем сборки это может оказаться слишком внезапно. Ну и вообще, пробел обычно разделяет параметры в командной строке, прои пробелы в именах файлов надо специально думать при сборке из консоли.


bool isOperator(char const symbol);
bool isRoundBracket(char const symbol);
void doIfOperator(Stack & s, string & toPostrix, int & currentIndex, char const currentSymbol);
int doIfRoundBracket(Stack & s, string & toPostfix, int & currentIndex, char const currentSymbol);
int popAllOperators(Stack & s, string & toPostfix, int & currentIndex);
bool isPlusOrMinus(char const symbol);
bool isMultOrDiv(char const symbol);

int infixToPostfix(string const & infix, string & toPostfix)

Choose a reason for hiding this comment

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

Тут возвращается всегда -1 или 0, имеет смысл bool его сделать

{
Stack s;
int currentIndex = 0;
for (int i = 0; i < (int)infix.size(); ++i)
{
if (!isOperator(infix[i]) && !isRoundBracket(infix[i]))
{
toPostfix += infix[i];
++currentIndex;
}
else
{
if (isOperator(infix[i]))
{
doIfOperator(s, toPostfix, currentIndex, infix[i]);
}
else
{
if (doIfRoundBracket(s, toPostfix, currentIndex, infix[i]) == -1)
{
cout << "Infix expression is incorrect" << endl;
return -1;
}
}
}
}
while (!s.isEmpty())
{
if (popAllOperators(s, toPostfix, currentIndex) == -1)
{
return -1;
}
}
return 0;
}

bool isOperator(char const symbol)
{
return (symbol == '+' || symbol == '-' || symbol == '*' || symbol == '/');
}

bool isRoundBracket(char const symbol)
{
return (symbol == '(' || symbol == ')');
}

void doIfOperator(Stack & s, string & toPostfix, int & currentIndex, char const currentSymbol)
{
if (s.isEmpty())
{
s.push(currentSymbol);
}
else
{
if (isMultOrDiv(currentSymbol) && isPlusOrMinus(s.peek()) || s.peek() == ')')
{
s.push(currentSymbol);
}
else if (s.peek() == '(')
{
s.push(currentSymbol);
}
else
{
// ������� ������ ����� ��� ���, ��������� ������� �� ����� - �������� ��� ������
// ������� ������� ���� ��� �����, ��������� ������� �� ����� - ��������
toPostfix += s.peek();
++currentIndex;
s.pop();
s.push(currentSymbol);
}
}
}

int doIfRoundBracket(Stack & s, string & toPostfix, int & currentIndex, char const currentSymbol)
{
if (currentSymbol == ')' && s.isEmpty())
{
return -1;
}
else if (currentSymbol == ')' && isOperator(s.peek()))
{
toPostfix += s.peek();
++currentIndex;
s.pop();
if (s.peek() == '(')
{
s.pop();
}
}
else
{
// ���� ������ �������, �� ������ ������ �� � ����
s.push(currentSymbol);
}
return 0;
}

int popAllOperators(Stack & s, string & toPostfix, int & currentIndex)
{
if (isRoundBracket(s.peek()))
{
return -1;
}
toPostfix += s.peek();
++currentIndex;
s.pop();
return 0;
}

bool isPlusOrMinus(char const symbol)
{
return (symbol == '+' || symbol == '-');
}

bool isMultOrDiv(char const symbol)
{
return (symbol == '*' || symbol == '/');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once
#include <string>
#include <iostream>
#include "Stack.h"

using namespace std;

Choose a reason for hiding this comment

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

Нельзя :)


int infixToPostfix(string const & infix, string & toPostfix);
65 changes: 65 additions & 0 deletions hw6/from infix to postfix/from infix to postfix/Stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "Stack.h"
#include "Element.h"

Stack::Stack()
{
head = nullptr;
}

Stack::~Stack()
{
auto* temp = head;
while (!isEmpty())
{
temp = head;
head = temp->next;
temp->next = nullptr;
delete temp;
temp = nullptr;
}
}

void Stack::push(char data)
{
auto* newElement = new Element(data);
if (!isEmpty())
{
newElement->next = head;
}
head = newElement;
}

int Stack::pop()
{
if (!isEmpty())
{
auto* temp = head->next;
head->next = nullptr;
delete head;
head = nullptr;
head = temp;
return 0;
}
else
{
return -1;
}
}

char Stack::peek()
{
if (!isEmpty())
{
return head->data;
}
else
{
cout << "Stack is empty\n";
return '\0';
}
}

bool Stack::isEmpty()
{
return head == nullptr;
}
28 changes: 28 additions & 0 deletions hw6/from infix to postfix/from infix to postfix/Stack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once
#include <iostream>

using namespace std;

struct Element;

struct Stack
{
// �����������
Stack();
// ����������
~Stack();
// ����������� �����������

// �������� ������������

// ���������� �������� � ������
void push(char data);
// �������� �������� �� ������
int pop();
// ���������� �� �������� �������
char peek();
// ��������� �������� �� ���� ������
bool isEmpty();
private:
Element * head;
};
Loading