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
Binary file added PiginAP/lab3-postfix/DOC/Lab-1(Postfix).pdf
Binary file not shown.
186 changes: 186 additions & 0 deletions PiginAP/lab3-postfix/include/List.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
#ifndef LIST_H
#define LIST_H

#include "Obj.h"
#include <iostream>

template <typename L>
class List
{
private:
Obj<L>* root;
public:
List(); //+
List(const List<L>& list); //+
~List(); //+
void push_back(const L &l); //+
void push_up(const L &l); //+
void push_under(L key, const L &l); //+
L pop_up(); //+
L pop_back(); //+
size_t get_size()const; //+
List<L>& operator=(const List<L>& list); //+
};
//==========================================================||==========================================================
template <typename L>
List< L >::List() : root(NULL) {}
//==========================================================
template<typename L>
List<L>::~List()
{
while (root)
{
pop_up();
}
}
//==========================================================
template<typename L>
List<L>::List(const List<L>& list):root(NULL)
{
if (list.root)
{
root = new Obj<L>(list.root->val);
Obj<L>* buf = root;
Obj<L>* buffer = list.root->next;
while (buffer)
{
buf->next = new Obj<L>(buffer->val);
buf = buf->pNext;
buffer = buffer->pNext;
}
}
}
//==========================================================
template<typename L>
void List<L>::push_back(const L & l)
{
Obj<L>* temp = NULL;
if (temp = new Obj<L>(l))
{
if (root == NULL) {
root = temp;
}
else {
Obj<L>* buf = root;
while (buf->pNext)
{
buf->pNext;
}
buf->pNext = temp;}
}
else {
throw "List is FULL";}
}
//==========================================================
template<typename L>
void List<L>::push_up(const L & l)
{
Obj<L>* obj = NULL;
if (obj = new Obj<L>(l))
{
Obj<L>* buf = root;
root = obj;
obj->pNext = buf;
}
else {
throw "ERROR List is FULL";
}

}
//==========================================================
template<typename L>
void List<L>::push_under(L key, const L & l)
{
Obj<L>* Obj = NULL;
if (Obj = new Obj<L>(l))
{
if (!root->pNext)
{
root->pNext = Obj;
}
else {
Obj<L>* buf = root;
while (buf->val != key)
{
buf = buf->pNext;
}
Obj<L>* temp = buf->pNext;
buf->pNext = Obj;
buf->pNext->pNext = temp;}

}
else {
throw "List is FULL";}
}
//==========================================================
template<typename L>
L List<L>::pop_up()
{
if (!root) { throw "List is Empty"; }
Obj<L>* buf = root;
L dat = buf->val;
root = buf->pNext;
delete buf;
return dat;
}
//==========================================================
template<typename L>
L List<L>::pop_back()
{

if (!root) { throw "List is Empty"; }
else if (!root->next)
{
L val = root->val;
delete root;
root = NULL;
return val;
}
else
{
Obj<L>* buf = root;
while (buf->next->next)
{
buf = buf->next;
}
L val = buf->next->val;
Obj<L>* temp = buf->next;
buf->next = NULL;
delete temp;
return val;
}
return L();
}
//==========================================================
template<typename L>
inline size_t List<L>::get_size() const
{
size_t size = 0;
Obj<L>* root_buf = root;
while (root_buf)
{
root_buf = root_buf->pNext;
size++;
}
return size;
}
//==========================================================
template<typename L>
List<L>& List<L>::operator=(const List<L>& list)
{
if (list.root)
{
root = new Obj<L>(list.root->val);
Obj<L>* buf = root;
Obj<L>* buffer = list.root->next;
while (buffer)
{
buf->next = new Obj<L>(buffer->val);
buf = buf->next;
buffer = buffer->next;
}
}
return *this;
}
//==========================================================
#endif
15 changes: 15 additions & 0 deletions PiginAP/lab3-postfix/include/Obj.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef OBJ_H
#define OBJ_H

#include <iostream>
template <typename L>

class Obj
{
public:
Obj() :pNext(NULL) {};
Obj(const L& value) :pNext(NULL), val(value) {};
Obj* pNext;
L val;
};
#endif
19 changes: 19 additions & 0 deletions PiginAP/lab3-postfix/include/Postfix.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef POSTFIX_H
#define POSTFIX_H

#include "Stack.h"
#include <iostream>

using namespace std;

class Postfix
{
private:
static bool operand(const char str); //+
static int priority(char x); //+
static double calc_op(double one, double two, char op);
public:
static string create_form(string inp_text); //+
static double calc_form(string form); //+
};
#endif
82 changes: 82 additions & 0 deletions PiginAP/lab3-postfix/include/Stack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#ifndef STACK_H
#define STACK_H

#include "List.h"

template<typename T>
class Stack
{
private:
List<T> list;
public:
Stack();
Stack(const Stack<T>& stack);
~Stack();
void push(const T& t);
T pop();
bool IsEmpty()const;
bool IsFull()const;

};
//==========================================================||==========================================================
template<typename T>
Stack<T>::Stack() {}
//==========================================================
template<typename T>
Stack<T>::~Stack()
{
list.~List();
}
//==========================================================
template<typename T>
Stack<T>::Stack(const Stack<T>& stack){
list = List<T>(stack.list);
}
//==========================================================
template<typename T>
void Stack<T>::push(const T & t)
{
list.push_up(t);
}
//==========================================================
template<typename T>
T Stack<T>::pop()
{
T dat = T();
try
{
dat = list.pop_up();
}
catch (const char* ex)
{
return T();
}
return dat;
}
//==========================================================
template<typename T>
bool Stack<T>::IsEmpty()const
{
if (list.get_size() == 0)
{
return true;
}
return false;
}
//==========================================================
template<typename T>
bool Stack<T>::IsFull()const
{
try
{
list.push_up(T());
}
catch (const char* ex)
{
return true;
}
list.pop();
return false;
}
//==========================================================
#endif
27 changes: 27 additions & 0 deletions PiginAP/lab3-postfix/sample/sample_list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <iostream>
#include <string>
#include "Postfix.h"

using namespace std;


int main()
{
Stack<char> Stack_one;
Stack<char> Stack_two;
string inp_text;

cout << "Expression: ";
cin >> inp_text;

string form = Postfix::create_form(inp_text);
cout << "Post. form: " << form << endl;
double calc_form = Postfix::calc_form(form);
if(calc_form == 0)
{
return 0;
}
cout << calc_form << endl;

return 0;
}
Loading