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
21 changes: 21 additions & 0 deletions Arefev_DA/lab1/include/Function.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef FUNCTION_H
#define FUNCTION_H
#include "TLink.h"
#include "TList.h"
#include "TStack.h"
#include <iostream>
#include <map>
#include <string>
using namespace std;
class st
{
public:
static string postfix_form(string);
static float calculations(string);
static string correct(string s);
private:
static int prioretet(char);
static map<char, float> readvalue(const string&);
};

#endif
12 changes: 12 additions & 0 deletions Arefev_DA/lab1/include/TLink.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef TLINK_H
#define TLINK_H

template <typename T>
class TLink
{
public:
T Key;
TLink*pNext;
};

#endif
172 changes: 172 additions & 0 deletions Arefev_DA/lab1/include/TList.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
#ifndef TLIST_H
#define TLIST_H
#include "TLink.h"
#include <iostream>
using namespace std;

template <typename T>
class TList
{
private:
TLink<T>*root;
public:
TList();
~TList();
TList(const TList& a);
void insert_start(T key);
void insert_end(T key);
void insert_before(T key, T keyNext);
void insert_after(T key, T keyprev);
TLink<T>* Search(T key);
void Remove(T key);
TLink<T>* Top()const;
};
template <typename T>
TList<T>::TList()
{
root = 0;
};

template <typename T>
TList<T>::~TList()
{
TLink<T>* node = root;
while (root != 0)
{
root = root->pNext;
delete node;
}
};

template <typename T>
TList<T>::TList(const TList& a)
{
if (a.root == 0) return 0;
TLink<T>*node = a.root;
root->Key = node->key;
root->pNext = 0;
node = node->pNext;
TLink<T>*tmp = root;
while (root != 0)
{
tmp->pNext = new TLink<T>;
tmp->pNext->pNext = 0;
tmp = tmp->pNext;
node = node->pNext;
}

}

template <typename T>
void TList<T>::insert_start(T key)
{
TLink<T>*n = new TLink<T>;
n->Key = key;
n->pNext = root;
root = n;
};

template <typename T>
void TList<T>::insert_end(T key)
{
if (root == 0)
{
root = new TLink<T>;
root->key = key;
root->pNext = 0;
return;
}
while (root->pNext != 0)
{
root = root->pNext;
}
root->pNext = new TLink<T>;
root->pNext->key = key;
root->pNext->pNext = 0;
};

template <typename T>
void TList<T>::insert_before(T key, T keyNext)
{
if (root == 0)
return;
if (root->key == keyNext)
{
Add(key);
return;
}
TList<T>*Nroot = root;
while ((Nroot->pNext != 0) && (Nroot->pNext->key != keyNext))
Nroot = Nroot->pNext;
if (Nroot->pNext == 0)
{
throw "Error! There is no such key";
}
TList<T>*n = new Node;
n->key = key;
n->pNext = Nroot->pNext;
Nroot->pNext = n;
};

template <typename T>
void TList<T>::insert_after(T key, T keyprev)
{
while (root != 0 && root->key != keyprev)
root = root->pNext;
if (root == 0)
{
throw "Error! There is no such element";
}
TList<T>*n = new Node;
n->key = key;
n->pNext = root->pNext;
root->pNext = n;
};


template <typename T>
TLink<T>* TList<T>::Search(T key)
{
TList<T>*Nroot = root;
while (Nroot != 0)
{
if (Nroot->key == key)
{
return Nroot;
}
Nroot = Nroot->pNext;
}
throw "Error! There is no such element";
};


template <typename T>
void TList<T>::Remove(T key)
{
if (root == 0) return;
TLink<T>*node = root;
if (root->Key == key)
{
root = root->pNext;
delete node;
return;
}
while ((node->pNext != 0) && (node->pNext->Key != key))
node = node->pNext;
if (node->pNext == 0)
{
throw "Error! There is no such element";
}
TLink<T>*node1 = node->pNext;
node->pNext = node1->pNext;
delete node1;
};

template <typename T>
TLink<T>* TList<T>::Top()const
{
return root;
};


#endif
80 changes: 80 additions & 0 deletions Arefev_DA/lab1/include/TStack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#ifndef TSTACK_H
#define TSTACK_H
#include "TList.h"
#include <iostream>
using namespace std;

template <typename T>
class TStack
{
private:
TList<T>*elems;
public:
TStack();
~TStack();
TStack(const TStack<T>& a);
void Push(T a);
T Pop();
bool IsFull()const;
bool IsEmpty()const;
};



template <typename T>
TStack<T>::TStack()
{
elems = new TList<T>();
};

template <typename T>
TStack<T>::~TStack()
{
delete elems;
};

template <typename T>
TStack<T>::TStack(const TStack<T>& a)
{
elems = new TList(*(a.elems));
};

template <typename T>
void TStack<T>::Push(T a)
{
if (IsFull())
throw "Error! Stack is full";
elems->insert_start(a);
};

template <typename T>
T TStack<T>::Pop()
{
if (IsEmpty())
throw "Error! Stack is empty";
T key = elems->Top()->Key;
elems->Remove(key);
return key;
};

template <typename T>
bool TStack<T>::IsEmpty()const
{
return elems->Top() == 0;
};

template <typename T>
bool TStack<T>::IsFull()const
{
T Key = -1;
try
{
elems->insert_start(Key);
elems->Remove(Key);
}
catch (...) { return true; }
return false;

};

#endif
33 changes: 33 additions & 0 deletions Arefev_DA/lab1/samples/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "Function.h"
#include <iostream>
#include <string>
using namespace std;

void main()
{
setlocale(LC_ALL, "Russian");
string s, a;
float Result;
cout << "������� ���������: " << endl;
getline(cin, s);
try
{
s = st::correct(s);
}
catch (const char*ex)
{
cout << endl << ex << endl;
return;
}
a = st::postfix_form(s);
try
{
Result = st::calculations(a);
}
catch (const char*ex)
{
cout << endl << ex << endl;
return;
}
cout << endl << "���������: " << s <<" = " << Result << endl;
}
Loading