-
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
Program converts expressions to postfix form #10
Open
viktoriia-fomina
wants to merge
17
commits into
master
Choose a base branch
from
from-infix-to-postfix
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
23b03fd
project created
viktoriia-fomina 18244c7
almost finished
viktoriia-fomina 2cc239c
trying to make for brackets
viktoriia-fomina c68b942
trying to make with brakes
viktoriia-fomina ea69ae4
seems correct even with brackets
viktoriia-fomina da5e836
made better
viktoriia-fomina be25d6f
made better
viktoriia-fomina 7e2d02b
finished
viktoriia-fomina 4c22dfd
made kaka
viktoriia-fomina 3797388
made better
viktoriia-fomina 67d7431
added makefile
viktoriia-fomina 27ff3ce
renamed without spaced and added Makefile
viktoriia-fomina fa53dd5
adds
viktoriia-fomina bdfa4ab
adds
viktoriia-fomina 49a9804
lala
viktoriia-fomina bc2cfb6
Changed encoding
viktoriia-fomina 25a1cd1
now it works
viktoriia-fomina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
hw6/from infix to postfix/from infix to postfix/Element.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
130
hw6/from infix to postfix/from infix to postfix/Infix to postfix.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
#include "Infix to postfix.h" | ||
|
||
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) | ||
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. Тут возвращается всегда -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 == '/'); | ||
} |
8 changes: 8 additions & 0 deletions
8
hw6/from infix to postfix/from infix to postfix/Infix to postfix.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
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. Нельзя :) |
||
|
||
int infixToPostfix(string const & infix, string & toPostfix); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Вообще, кстати, использовать пробелы в именах файлов с исходниками плохая идея, потому что для некоторых систем сборки это может оказаться слишком внезапно. Ну и вообще, пробел обычно разделяет параметры в командной строке, прои пробелы в именах файлов надо специально думать при сборке из консоли.