Skip to content
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

Лабораторные работы #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
171 changes: 171 additions & 0 deletions lab1/include/Funcs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
#include"Stack.h"
#include <string>
#include"Funcs.h"

using namespace std;

int funcs::checkpriority(char x)
{
int res = 0;
if (x == '!') return 0;
if (((x >= 'A') && (x <= 'Z')) || ((x >= 'a') && (x <= 'z')))return 5;
if ((x == '/') || (x == '*')) return 3;
if ((x == '+') || (x == '-')) return 2;
if (x == '(')return 0;
if (x == ')')return 4;
throw"error";
}

void funcs::calculate(string string)
{
float op, op1, op2;
Stack<float> Tstack;
map<char, float> valuemap = readvalue(string);
for (const char& k : string)
{
if (((k >= 'A') && (k <= 'Z')) || ((k >= 'a') && (k <= 'z')))
{
op = valuemap[k];
Tstack.Push(op);
}

switch (k)
{
case'-':
{
op1 = Tstack.Pop();
op2 = Tstack.Pop();
Tstack.Push(op2 - op1);
break;
}
case'+':
{
op1 = Tstack.Pop();
op2 = Tstack.Pop();
Tstack.Push(op2 + op1);
break;
}
case'*':
{
op1 = Tstack.Pop();
op2 = Tstack.Pop();
Tstack.Push(op2*op1);
break;
}
case'/':
{
op1 = Tstack.Pop();
op2 = Tstack.Pop();
Tstack.Push(op2 / op1);
break;
}
}
}
cout << "���������: " << Tstack.Pop() << endl;


}


string funcs::postfixform(string string)
{
Stack<char> first;
Stack<char> second;
int priority, oppriority;
for (char el : string)
{
priority = checkpriority(el);
if (priority == 0)//������ ����� ������
{
second.Push(el);
}

if (priority == 5)//������ �������-������ � 1� ����
{
first.Push(el);
}
if (priority == 2)//������ �������� "+" ��� "-"
{
oppriority = checkpriority(second.checktop());
if (oppriority <= 2)//���� ��������� �������� �� ������� ����� ������ ���� ����� ���������� ��������-������ �������� �� 2� ����
{
second.Push(el);

}
else//����������� ��� �������� �� ��.2 � ������� ��������� ���� ���� ����� ��������� �������� � ������������� � ��.1
{
while (checkpriority(second.checktop()) >= 2)
{
first.Push(second.Pop());
}
second.Push(el);
}
}
if (priority == 3)//�.�. ������������ ���������-3,�� ������ ������ � ���� 3;
{
second.Push(el);
}
if (priority == 4)
{
while (checkpriority(second.checktop()) != 0)//������������� �������� �� ��.2 � ��.1 �� "("
{
first.Push(second.Pop());
}
second.Pop();//������� "(" �� ��.2
}
}
while (!second.IsEmpty())
{
first.Push(second.Pop());
}

std::string tmpstring;
int j = first.Size();
for (int i = 0; i <j; i++)//������������� �� ��.1 ������� �� ����� � ������
{
tmpstring = tmpstring + first.Pop();
}
string.clear();
j = tmpstring.size();
for (int i = 1; i <= j; i++)
{
char x = tmpstring[j - i];
string = string + x;
}
return string;
};

bool funcs::checkcorrectness(string a)
{
int count = 0;
char prev;
bool flag = false;
for (char k : a)
{
if (k == '(')
count++;

if (k == ')')
count--;
}
return (count == 0);

};

map<char, float>funcs::readvalue(string str)
{
map<char, float > res;
float value;
for (const char& c : str)
{
if (((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'))) {
if (res.count(c) == 0)
{
cout << "�������: " << c << endl;
cin >> value;
res.insert(pair<char, float>(c, value));
}
}
}
return res;
}
137 changes: 137 additions & 0 deletions lab1/include/List.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#pragma once
#include <iostream>
#include "Node.h"
using namespace std;


template<typename Tkey>
class List {
private:
Node<Tkey>*root;//��������� �� ������ ������� ������
public:
List();//����������� �� ���������
List(const List&);//����������� �����������
~List();//����������
void insertend(Tkey key);//������� � ����� ������
void insertbegin(Tkey key);//������� � ������ ������
void remove(Tkey key);//�������� ��������
Node<Tkey>* search(Tkey key);//����� ��������
int Size()const;//���-�� ��������� ������
Node<Tkey>* GetRoot();//�������� ������ �� ������ ������� ������
};
template <typename Tkey>
List<Tkey>::List()
{
root = nullptr;
};

template <typename Tkey>
List<Tkey>::~List()
{
delete root;
};

template <typename Tkey>
void List<Tkey>::insertbegin(Tkey key)
{
Node<Tkey>*nNode = new Node<Tkey>;
nNode->Key = key;
nNode->pNext = root;
root = nNode;
};
template <typename Tkey>
void List<Tkey>::insertend(Tkey key)
{
if (root == nullptr)
{
insertbegin(key);
return;
}
Node*tmp = root;
while (tmp->pNext != nullptr)
{
tmp = tmp->pNext;
}
tmp->pNext = new Node<Tkey>;
tmp->pNext->key = key;
tmp->pNext->pNext = nullptr;
};

template <typename Tkey>
void List<Tkey>::remove(Tkey key)
{
if (root == nullptr) return;
Node<Tkey>*node = root;
if (root->Key == key)
{
root = root->pNext;
delete node;
return;
}
while ((node->pNext != nullptr) && (node->pNext->Key != key))
{
node = node->pNext;
}

if (node->pNext == nullptr)
{
throw "Element not found";
}
Node<Tkey>*node1 = node->pNext;
node->pNext = node1->pNext;
delete node1;

};
template <typename Tkey>
Node<Tkey>* List<Tkey>::search(Tkey key)
{
if (root == nullptr) return;
Node<Tkey>*node = root;
while ((node->pNext != nullptr) && (node->pNext->key != key))
{
node = node->pNext;
}
if (node->pNext == nullptr)
{
throw "Element not found";
}

return node->pNext;
};

template <typename Tkey>
Node<Tkey>* List<Tkey>::GetRoot()
{
return root;
};

template <typename Tkey>
int List<Tkey>::Size()const
{
Node<Tkey>*node = root;
int count = 0;
while (node != nullptr)
{
node = node->pNext;
count++;
}
return count;
};

template <typename Tkey>
List<Tkey>::List(const List& a)
{
root = new Node<Tkey>;
Node<Tkey>*copy = root;//��������� ��� ��������� ������ ������
Node<Tkey>*node = a->GetRoot();//��������� ��� ��������� ������� ������
while (node->pNext != nullptr)
{
copy->Key = node->Key;
copy->pNext = new Node<Tkey>;
copy = copy->pNext;
node = node->pNext;
}
copy->Key = node->Key;
copy->pNext = new Node<Tkey>;
copy->pNext = nullptr;
};
8 changes: 8 additions & 0 deletions lab1/include/Node.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once
template <typename Tkey>
class Node
{
public:
Tkey Key;
Node*pNext;
};
Loading