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
50 changes: 31 additions & 19 deletions include/TFormula.h
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
#ifndef TFORMULA_H
#define TFORMULA_H
#include "TStack.h"
const int MaxLength = 255;
const int OPER = 6;
#include <string>
const int MaxLength = 256; // ������������ ����� �������� �������
const int OPER = 6; // ����� ���������� �������� � ���������
struct Operations {
char operation;
int priority;
};
struct Frmla {
int size; // ����� �������
char Frml[MaxLength]; // ���� �������
};
class TFormula {
private:

int infSize;
int postSize;
char Infix[MaxLength];
char Postfix[MaxLength];
Frmla infix; // ��������� ���� ������ �����
Frmla postfix; // ����������� ����� ������

char operations[OPER];
int priorities[OPER];

void SetOpTable();
void FormulaConverter();
bool FormulaChecker(char form[], int size);
Operations ops[OPER]; // ��������, ������� ����� ��������� � �������
void SetOpTable(); // �����, �������� ���� ��������� ������

public:
TFormula();
TFormula(char const form[]);
void SetInfixForm(char const form[]);
char const* GetInfixFormula() const;
char const* GetPostfixFormula() const;
double Calculate();
};
TFormula(); // ����������� �� ���������
TFormula(char* form); // ����������� �������������� ����
TFormula(std::string const& form); // ����������� �������������� ����
void SetInfixForm(char* form); // �������, ����������� �������� �������� ��������� �������
void SetInfixForm(std::string const& form); // �������, ����������� �������� �������� ��������� �������

void FormulaConverter(); // ����������� ������� � ����������� �����
int FormulaChecker(int* Brackets, int& size) const; // �������� ������������ ���������� �������� �������
char const* GetInfixFormula() const; // ���������� ��������� ����� ������
char const* GetPostfixFormula() const; // ���������� ����������� ����� ������
int const GetInfixSize() const { return infix.size; }
int const GetPostfixSize() const { return postfix.size; }

double Calculate(); // ���������� �������� ���������
};

#endif
76 changes: 40 additions & 36 deletions include/TStack.h
Original file line number Diff line number Diff line change
@@ -1,58 +1,61 @@
#ifndef TSTACK_H
#define TSTACK_H
#include <iostream>
const int maxMemSize = 100;
const int maxMemSize = 100; // ������������ ����� �����

template<typename T>
class TStack
{
class TStack {
protected:
T* pMem;
int MemSize;
int DataCount;
T* pMem; // ������ ��������� �����
int MemSize; // ������ �����
int DataCount; // ���������� ��������� �����
public:
TStack(int size = maxMemSize);
TStack(TStack const& st);
~TStack();
TStack(int size = maxMemSize); // ����������� � �����������/�� ��������
TStack(TStack const& st); // ����������� �����������
~TStack(); // ����������

TStack& operator=(TStack const& st);
bool isEmpty() const;
bool isFull() const;
void Put(const T& Val);
virtual T Get();
T CheckLast();
TStack& operator=(TStack const& st); // �������� ������������
bool isEmpty() const; // �������� ����� �� �������
bool isFull() const; // �������� ����� �� �������
void Put(const T& Val); // ��������� ������ �������� � ����
T Get(); // ������������ �������� �� �����
T const& CheckLast() const; // �������� ���������� �������� �����
};

//..............................................................................................//
template<typename T>
TStack<T>::TStack(int size) {
TStack<T>::TStack(int size) { // ����������� � �����������(�� ���������)
if (size < 0 || size > maxMemSize)
throw std::out_of_range("���� ������� ������������ �������� ����� ������������ �����");
if (size == 0)
size = maxMemSize;
/*if (size == 0)
size = maxMemSize;*/
MemSize = size;
DataCount = 0;
pMem = new T[MemSize];
for (int i = 0; i < MemSize; ++i) {
pMem[i] = 0;
}
}

//...............................................................................................//
template<typename T>
TStack<T>::~TStack() {
TStack<T>::~TStack() { // ����������
if (pMem != NULL) {
delete[] pMem;
pMem = NULL;
}
}
//...............................................................................................//
template<typename T>
TStack<T>::TStack(TStack const& st) {
TStack<T>::TStack(TStack const& st) { //����������� �����������
MemSize = st.MemSize;
DataCount = st.DataCount;
pMem = new T[MemSize];
for (int i = 0; i < DataCount; ++i) {
pMem[i] = st.pMem[i];
}
}

//................................................................................................//
template<typename T>
TStack<T>& TStack<T>::operator=(TStack const& st) {
TStack<T>& TStack<T>::operator=(TStack const& st) { // �������� ������������
if (this != &st) {
if (MemSize != st.MemSize) {
delete[] pMem;
Expand All @@ -66,36 +69,37 @@ TStack<T>& TStack<T>::operator=(TStack const& st) {
}
return *this;
}

//.................................................................................................//
template<typename T>
bool TStack<T>::isEmpty() const { return (DataCount == 0); }

bool TStack<T>::isEmpty() const { return (DataCount == 0); } // �������� �� �������
//.................................................................................................//
template<typename T>
bool TStack<T>::isFull() const { return (DataCount == MemSize); }

bool TStack<T>::isFull() const { return (DataCount == MemSize); } // �������� �� �������
//.................................................................................................//
template<typename T>
void TStack<T>::Put(const T& Val) {
void TStack<T>::Put(const T& Val) { // ���������� �������� � ����
if (isFull())
throw std::out_of_range("���� �����!");
else {
DataCount++;
pMem[DataCount - 1] = Val;
}
}

//.................................................................................................//
template<typename T>
T TStack<T>::Get() {
T TStack<T>::Get() { // ������������ �������� �� �����
if (isEmpty())
throw std::out_of_range("���� ����!");
else {
T tmp = pMem[DataCount - 1];
DataCount--;
return(tmp);
return(pMem[DataCount]);
}
}

//..................................................................................................//
template<typename T>
T TStack<T>::CheckLast() {
T const& TStack<T>::CheckLast() const{ // �������� ���������� �������� �����
if (isEmpty())
throw std::out_of_range("���� ����!");
return(pMem[DataCount - 1]);
}

Expand Down
10 changes: 7 additions & 3 deletions samples/TMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@
#include "TStack.h"
#include "TFormula.h"
#include <locale.h>
#include <cstdlib>
#include <string>

int main(int argc, char** argv) {
setlocale(LC_ALL, "RUSSIAN");
TFormula fml;
std::cout << "������� ���������: ";
char a[255];
std::cin >> a;
std::string a;
std::getline(std::cin, a);
fml.SetInfixForm(a);

TFormula b = fml;
std::cout << "�������, ���������� � ��������� �����: " << b.GetInfixFormula() << std::endl;
std::cout << "� �����������: " << b.GetPostfixFormula() << std::endl;
std::cout << "���������: " << fml.Calculate() << '\n';
return 0;
}
Loading