Skip to content
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
22 changes: 13 additions & 9 deletions include/tformula.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
const int MaxLen=255;

const int MaxLen = 255;

class TFormula
{
private:
char Formula[MaxLen]; // исходная формула
char PostfixForm[MaxLen]; // постфиксная форма
public:
TFormula(char *form); // конструктор
int FormulaChecker(int Brackets[],int size); // проверка корректности скобок
int FormulaConverter(); // преобразование в постфиксную форму
double FormulaCalculator(); // вычисление формулы
private:
char Formula[MaxLen]; // исходная формула
char PostfixForm[MaxLen]; // постфиксная форма
public:
TFormula(char *form); // конструктор
int FormulaChecker(int brackets[], int size); // проверка корректности скобок
int FormulaConverter(); // преобразование в постфиксную форму
double FormulaCalculator(); // вычисление формулы

bool IsOperator(char str); // проверка оператора
int Priority(char str); // приоритет операции
};
12 changes: 6 additions & 6 deletions include/tstack.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
class TStack :public TDataRoot
{
private:
int top;
int top;
public:
TStack(int Size = DefMemSize);
~TStack() {};
void Put(const TData &Val);
TStack(int size);
TStack(const TStack &obj);
void Put(const TData &Val);
TData Get();
virtual TData TopElem();
virtual TData TopElem();

int IsValid();
void Print();
void Print();
};
64 changes: 32 additions & 32 deletions src/tdataroot.cpp
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
// ННГУ, ВМК, Курс "Методы программирования-2", С++, ООП
// ����, ���, ���� "������ ����������������-2", ++, ���
//
// tdataroot.cpp - Copyright (c) Гергель В.П. 28.07.2000 (06.08)
// Переработано для Microsoft Visual Studio 2008 Сысоевым А.В. (21.04.2015)
// tdataroot.cpp - Copyright (c) ������� �.�. 28.07.2000 (06.08)
// ������������ ��� Microsoft Visual Studio 2008 �������� �.�. (21.04.2015)
//
// Динамические структуры данных - базовый (абстрактный) класс - версия 3.1
// память выделяется динамически или задается методом SetMem
// ������������ ��������� ������ - ������� (�����������) ����� - ������ 3.1
// ������ ���������� ����������� ��� �������� ������� SetMem

#include <stdio.h>
#include "tdataroot.h"

TDataRoot::TDataRoot(int Size): TDataCom()
TDataRoot::TDataRoot(int Size) : TDataCom()
{
DataCount = 0;
MemSize = Size;
if (Size == 0) // память будет установлена методом SetMem
{
pMem = NULL;
MemType = MEM_RENTER;
}
else // память выделяется объектом
{
pMem = new TElem[MemSize];
MemType = MEM_HOLDER;
}
DataCount = 0;
MemSize = Size;
if (Size == 0) // ������ ����� ����������� ������� SetMem
{
pMem = NULL;
MemType = MEM_RENTER;
}
else // ������ ���������� ��������
{
pMem = new TElem[MemSize];
MemType = MEM_HOLDER;
}
} /*-------------------------------------------------------------------------*/

TDataRoot::~TDataRoot()
{
if (MemType == MEM_HOLDER)
delete [] pMem;
pMem = NULL;
if (MemType == MEM_HOLDER)
delete[] pMem;
pMem = NULL;
} /*-------------------------------------------------------------------------*/

void TDataRoot::SetMem(void *p, int Size) // задание памяти
void TDataRoot::SetMem(void *p, int Size) // ������� ������
{
if (MemType == MEM_HOLDER)
delete [] pMem; // ! информация не сохраняется
pMem = (TElem*) p;
MemType = MEM_RENTER;
MemSize = Size;
if (MemType == MEM_HOLDER)
delete[] pMem; // ! ���������� �� �����������
pMem = (TElem*)p;
MemType = MEM_RENTER;
MemSize = Size;
} /*-------------------------------------------------------------------------*/

bool TDataRoot::IsEmpty(void) const // контроль пустоты СД
bool TDataRoot::IsEmpty(void) const // �������� ������� ��
{
return DataCount == 0;
return DataCount == 0;
} /*-------------------------------------------------------------------------*/

bool TDataRoot::IsFull(void) const // контроль переполнения СД
bool TDataRoot::IsFull(void) const // �������� ������������ ��
{
return DataCount == MemSize;
} /*-------------------------------------------------------------------------*/
return DataCount == MemSize;
} /*-------------------------------------------------------------------------*/
206 changes: 206 additions & 0 deletions src/tformula.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
#define _CRT_SECURE_NO_WARNINGS
#include "tformula.h"
#include "tstack.h"
#include <cstring>
#include <iostream>

TFormula::TFormula(char *form)
{
if (form != "")
{
std::strcpy(Formula, form);
std::strcpy(PostfixForm, "");

int i = 0;
int bracketCount = 0;
int closeBracket = 0;
int openBracket = 0;
while (Formula[i] != '\0')
{
if (Formula[i] == '(')
{
openBracket++;
bracketCount++;
}
else if (Formula[i] == ')')
{
closeBracket++;
bracketCount++;
}

i++;
}

for (int i = 0; i < closeBracket - openBracket; i++)
bracketCount++;

for (int i = 0; i < openBracket - closeBracket; i++)
bracketCount++;

int brackets[255];
if (FormulaChecker(brackets, 255) != 0)
{
for (int i = 0; i < bracketCount; i += 2)
std::cout << brackets[i] + 1 << " | " << brackets[i + 1] + 1 << std::endl;
}
else
{
this->FormulaConverter();
}
}
}
/*------------------------------------------------------------------------------*/
int TFormula::FormulaChecker(int brackets[], int size)
{
TStack mstack(size);

int bracketsCount = 0;
int i = 0;
int errors = 0;
int bracketIndex = 0;
while (Formula[i] != '\0')
{
if (Formula[i] == '(')
mstack.Put(bracketIndex++);

else if (Formula[i] == ')')
{
if (mstack.IsEmpty())
{
brackets[bracketsCount++] = -1;
brackets[bracketsCount++] = bracketIndex++;
errors++;
}

else
{
brackets[bracketsCount++] = mstack.Get();
brackets[bracketsCount++] = bracketIndex++;
}
}

i++;
}

while (!mstack.IsEmpty())
{
brackets[bracketsCount++] = mstack.Get();
brackets[bracketsCount++] = -1;
errors++;
}

return errors;
}
/*------------------------------------------------------------------------------*/
bool TFormula::IsOperator(char str)
{
return (str == '*' || str == '/'
|| str == '+' || str == '-');
}
/*------------------------------------------------------------------------------*/
int TFormula::Priority(char str)
{
if (str == '*' || str == '/') return 2;
if (str == '+' || str == '-') return 1;
return 0;
}
/*------------------------------------------------------------------------------*/
int TFormula::FormulaConverter()
{
TStack mstack(255);

int i = 0;
int postFix = 0;
while (Formula[i] != '\0')
{
if (isdigit(Formula[i]))
{
while (isdigit(Formula[i]))
{
PostfixForm[postFix++] = Formula[i];
i++;
}
PostfixForm[postFix++] = ' ';
i--;
}

else if (IsOperator(Formula[i]))
{
if (Priority(Formula[i]) > Priority(mstack.TopElem()) || Priority(Formula[i]) == 0 || mstack.IsEmpty())
mstack.Put(Formula[i]);

else
{
while (!mstack.IsEmpty() && Priority(mstack.TopElem()) >= Priority(Formula[i]) && mstack.TopElem() != '(')
PostfixForm[postFix++] = mstack.Get();

mstack.Put(Formula[i]);
}
}

if (Formula[i] == '(')
mstack.Put(Formula[i]);

if (Formula[i] == ')')
{
while (!mstack.IsEmpty())
{
if (mstack.TopElem() != '(')
PostfixForm[postFix++] = mstack.Get();

else
{
mstack.Get();
break;
}
}
}

i++;
}

while (!mstack.IsEmpty())
PostfixForm[postFix++] = mstack.Get();
PostfixForm[postFix] = '\0';

return 0;
}
/*------------------------------------------------------------------------------*/
double TFormula::FormulaCalculator()
{
TStack mstack(255);

int i = 0;
while (PostfixForm[i] != '\0')
{
if (isdigit(PostfixForm[i]))
mstack.Put(PostfixForm[i] - '0');

else if (IsOperator(PostfixForm[i]))
{
int rigthOperator = mstack.Get();
int leftOperator = mstack.Get();

switch (PostfixForm[i])
{
case'*':
mstack.Put(leftOperator * rigthOperator);
break;
case '/':
mstack.Put(leftOperator / rigthOperator);
break;
case '+':
mstack.Put(leftOperator + rigthOperator);
break;
case '-':
mstack.Put(leftOperator - rigthOperator);
break;
}
}

i++;
}

return mstack.Get();
}
/*------------------------------------------------------------------------------*/
Loading