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

Лабораторная работа №1, 2. Захарова Дарья #48

Open
wants to merge 10 commits 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
9,592 changes: 9,592 additions & 0 deletions ZaharovaDD/lab1/gtest/gtest-all.cc

Large diffs are not rendered by default.

20,063 changes: 20,063 additions & 0 deletions ZaharovaDD/lab1/gtest/gtest.h

Large diffs are not rendered by default.

170 changes: 170 additions & 0 deletions ZaharovaDD/lab1/include/list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
#pragma once
#include "node.h"
#include <iostream>

using namespace std;

//Шаблон класса Циклический список с головой
template<class TP>
class list
{
private:
node<TP>* head; //голова
node<TP>* Curr; //Указатель на текущий
public:
list(); //К. по умолчанию
~list(); //Деструктор
list(const list<TP>& a); //К.
void Clean(); //очистка списка

void InsertInOrder(const TP& a); //Вставить в упорядоченный список
void InsertAfter(node<TP>* a1, const TP& a2); //вставить после
void InsertToTail(const TP a); //вставить в конец

bool IsEmpty() const { return (head->next == head); }; //проверка на пустоту
//написать метод isfull
TP& GetCurr() const { return Curr->data; } //Получение текущего адреса
void Reset() { Curr = head->next; } //Установка на начало
void gonext() { Curr = Curr->next; }; //переход на следующее звено
bool IsOver() { if (Curr->next == head->next) return true; else return false; } //проверка на конец

const list<TP>& operator=(const list<TP>& a); //Перегрузка оператора =

bool operator==(const list<TP>& a) const; //Оператор сравнения ==
bool operator!=(const list<TP>& a) const { return !(*this == a); } //Оператор сравнения не равно
};


template <class TP> //К. по умолчанию
list<TP>::list()
{
head = new node<TP>();
Curr = head->next;
head->next = head;
}

template <class TP>
list<TP>::~list()
{
Clean();
delete head;
}


template <class TP> //К. копирования
list<TP>::list(const list<TP>& a)
{
head = new node<TP>;
node<TP>* E1 = a.head;
node<TP>* E2 = head;
if (E1->next == a.head)
{
head->next = head;
return;
}
while (E1->next != a.head)
{
E1 = E1->next;
E2->next = new node<TP>(E1->data);
E2 = E2->next;
}
E2->next = head;
Curr = head->next;
}

template <class TP> //Очистка списка
void list<TP>::Clean()
{
node<TP>* curr = head->next;
while (curr != head)
{
node<TP>* temp = curr->next;
delete curr;
curr = temp;
}
head->next = head;
}


template <class TP> //Вставить в упорядоченный список
void list<TP>::InsertInOrder(const TP& a)
{
if (IsEmpty())
{
head->next = new node<TP>(a);
head->next->next = head;
}
else
{
node<TP>* t = new node<TP>(a);
node<TP>* c = head;

while (c->next != head && (*(c->next) < *t))
{
c = c->next;
}
node<TP>* t1 = c->next;
c->next = t;
c->next->next = t1;
}
}

template <class TP> //вставить после
void list<TP>::InsertAfter(node<TP>* a1, const TP& a2)
{
if (head == NULL)
throw "empty list";
else
{
node<TP>* t = a1->next;
a1->next = new node<TP>(a2, t);
}
}

template<class TP> //вставить в конец
void list<TP> ::InsertToTail(const TP a)
{
Reset();
while (Curr->next != head)
gonext();
node<TP>* t = Curr->next;
Curr->next = new node<TP>(a);
Curr->next->next = t;
}

template <class TP> //присваивание
const list<TP>& list<TP>::operator=(const list<TP>& a)
{
Clean();
node<TP>* p_a = a.head;
node<TP>* p_curr = head;
while (p_a->next != a.head)
{
p_a = p_a->next;
p_curr->next = new node<TP>(p_a->data);
p_curr = p_curr->next;
}
p_curr->next = head;
Curr = head->next;
return *this;
}


template<class TP> //сравнение
bool list<TP>::operator==(const list<TP>& a) const
{
bool res = true;
if (this != &a)
{
node<TP>* t1 = a.head->next;
node<TP>* t2 = head->next;
while (t1->data == t2->data && t2 != head && t1 != a.head)
{
t1 = t1->next;
t2 = t2->next;
}
if (t2 != head || t1 != a.head)
res = false;
}
return res;
}
20 changes: 20 additions & 0 deletions ZaharovaDD/lab1/include/monom.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once
#include <math.h>


class monom
{
public:
double cff; //�����. ����� �������
int svr; //�������� �������

monom(const monom& a) { cff = a.cff; svr = a.svr; }
monom(const double index_cff = 0.0, const unsigned int index_svr = 0) { cff = index_cff; svr = index_svr; } //�.
const monom& operator=(const monom& a) { cff = a.cff; svr = a.svr; return *this; } //�. ������������
bool operator< (const monom& a) const { return (svr<a.svr); } //�. ���������
bool operator> (const monom& a) const { return (svr>a.svr); } //�. ���������
bool operator==(const monom& a) const { return ((svr == a.svr) && (cff == a.cff)); } //����������� ==
bool operator!=(const monom& a) const { return !(*this == a); } //����������� !=


};
16 changes: 16 additions & 0 deletions ZaharovaDD/lab1/include/node.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

//Шаблон класса "Элемент списка"
template<class TP>
class node
{
public:
TP data; //Данные в эл.
node<TP>* next; //Указатель на следующий эл.

node() { next = NULL; }; //К. по умолчанию
node(TP a1, node<TP>* a2 = NULL) { data = a1; next = a2; }; //К. с параметром
bool operator< (const node<TP>& a) const { return (data < a.data); }; //О. сравнения
bool operator> (const node<TP>& a) const { return (data > a.data); }; //О. сравнения
};

6 changes: 6 additions & 0 deletions ZaharovaDD/lab1/include/polinom.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// ����� ������� - ������������� ����������� ������ �������
// ������������ �������� ������, ������� ������, �������� ������,
// ����� ������, �������� ���������(������� ������������� �������),
// ��������� ���������

#include "monom.h"
33 changes: 33 additions & 0 deletions ZaharovaDD/lab1/include/polynom.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include <iostream>
#include <string>
#include <algorithm>
#include "monom.h"
#include "list.h"

#define OFFSET 120 //Код символа 'x'

class polynom
{
private:
list<monom> list_monom; //список мономов
list<monom> such(list <monom> & op); //объединение подобные

public:
polynom(const polynom& p);
const polynom& operator=(const polynom &p);
polynom(list<monom> &in_list) : list_monom(in_list) {} //К. по списку
polynom(const string op = ""); //К. по строке

polynom operator-(const polynom& p) const { return *this + p*(-1); } //Бинарный минус
polynom operator-() const { return (-1)*(*this); } //Унарный минус
bool operator==(const polynom& p) const { return list_monom == p.list_monom; } //О. сравнения
bool operator!=(const polynom& p) const { return list_monom != p.list_monom; } //О. сравнения
polynom operator+(const polynom& p) const; //О. сложения полиномов
polynom operator*(const polynom& p) const; //О. умножения полиномов
polynom operator*(const double k) const; //Умножение на константу
friend polynom operator*(const double k, const polynom& p) { return p*k; } //Умножение на константу с другой стороны

friend std::ostream& operator<<(std::ostream &str, const polynom &p); //О. вставки в поток
};
Binary file added ZaharovaDD/lab1/otchet_polynom.docx
Binary file not shown.
5 changes: 5 additions & 0 deletions ZaharovaDD/lab1/samples/main_polinom.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "polinom.h"

void main()
{
}
66 changes: 66 additions & 0 deletions ZaharovaDD/lab1/samples/main_polynom.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "monom.h"
#include "polynom.h"
#include <iostream>

using namespace std;

void main()
{
int flag = 1;
int k = 0;
while (flag == 1)
{
system("cls");//�������� �����
cout << "Write first polynom" << endl;
string s1;
cin >> s1;
polynom S1(s1);
cout << "Write second polynom" << endl;
string s2;
cin >> s2;
polynom S2(s2);
cout << "Choose operation" << endl;
cout << "1 +" << endl;
cout << "2 -" << endl;
cout << "3 *" << endl;
cout << "4 * const" << endl;
cin >> k;
switch (k)
{
case 1:
{
polynom res = S1 + S2;
cout << res;
break;
}
case 2:
{
polynom res = S1 - S2;
cout << res;
break;
}
case 3:
{
polynom res = S1 * S2;
cout << res;
break;
}
case 4:
{
string con;
cout << " Write const" << endl;
cin >> con;
polynom C(con);
polynom res = C*S1;
cout << res;
break;
}
default:
break;
}

cout << " \n Repead : 1" << endl;
cin >> flag;

}
}
Loading