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. Кумин Алексей Александрович #33

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Binary file not shown.
9,592 changes: 9,592 additions & 0 deletions KuminAA/lab1-polinom/gtest/gtest-all.cc

Large diffs are not rendered by default.

20,063 changes: 20,063 additions & 0 deletions KuminAA/lab1-polinom/gtest/gtest.h

Large diffs are not rendered by default.

131 changes: 131 additions & 0 deletions KuminAA/lab1-polinom/include/list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#pragma once

#include <iostream>
#include "node.h"
using namespace std;

template<typename t>
class Ringlist
{
Node<t> *head;
Node<t> *curr;
public:
Ringlist();
Ringlist(const Ringlist<t> &l);
Ringlist<t>& operator= (const Ringlist<t> &l);
~Ringlist();

void InsertToOrdered(const t &a);

void reset() { curr = head->next; };
t& GetCurr() { return curr->data; };
void SetNext() { curr = curr->next; }
int isended() { return (curr->next == head->next); };

int operator==(const Ringlist<t>& l) const;
int operator!=(const Ringlist<t>& l) const { return !(*this == l); }
};

//-------------------------------------------------------------

template<typename t>
Ringlist<t>::Ringlist()
{
head = new Node<t>(NULL);
head->next = head;
curr = head;
}

template<typename t>
Ringlist<t>::Ringlist(const Ringlist<t> &l)
{
head = new Node<t>(l.head->data);
curr = head;
Node<t>* temp = l.head;
while (temp->next != l.head)
{
temp = temp->next;
curr->next = new Node<t>(temp->data);
curr = curr->next;
}
curr->next = head;
curr = head;
}

template<typename t>
Ringlist<t>::~Ringlist()
{
Node<t> *current = head->next;
Node<t> *temp;
while (current != head)
{
temp = current->next;
delete current;
current = temp;
}
head->next = head;
delete head;
}

template<typename t>
Ringlist<t>& Ringlist<t>::operator= (const Ringlist<t> &l)
{
Node<t> *current = head->next;
Node<t> *temp1;
while (current != head)
{
temp1 = current->next;
delete current;
current = temp1;
}
head->next = head;

Node<t> *temp = l.head;
head = new Node<t>(temp->data);
curr = head;
while (temp->next != l.head)
{
temp = temp->next;
curr->next = new Node<t>(temp->data);
curr = curr->next;
}
curr->next = head;
curr = head;

return *this;
}

template<typename t>
void Ringlist<t>::InsertToOrdered(const t &a)
{
curr = head;
while ((curr->next->data > a) && (curr->next != head))
curr = curr->next;

Node<t>* temp = curr->next;
curr->next = new Node<t>(a, temp);
}

template<typename t>
int Ringlist<t>::operator==(const Ringlist<t>& l) const
{
int k = 1;
if (this != &l)
{
Node<t> *temp = head->next;
Node<t> *temp1 = l.head->next;

while ((temp1 != l.head) && (temp != head) && temp->data == temp1->data)
{
temp = temp->next;
temp1 = temp1->next;
while ((temp->data == NULL) && (temp != head))
temp = temp->next;
while ((temp1->data == NULL) && (temp1 != l.head))
temp1 = temp1->next;
}
if (temp1 != l.head || temp != head)
k = 0;
}
return k;
}
24 changes: 24 additions & 0 deletions KuminAA/lab1-polinom/include/node.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

template <typename t>
class Node
{
public:
t data;
Node *next;
Node(t Data = NULL, Node *Next = NULL)
{
data = Data;
next = Next;
}
Node(const Node &l) { data = l.data; next = NULL; }

bool operator==(const Node<t> &l) const { return (data == l.data) && (next == l.next); };
bool operator!= (const Node<t>& l) const { return !(*this == l); }

bool operator< (const Node<t>& l) const { return (data < l.data); }
bool operator> (const Node<t>& l) const { return (data > l.data); }

};


63 changes: 63 additions & 0 deletions KuminAA/lab1-polinom/include/polinom.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#pragma once

#include "list.h"
#include <iostream>
#include <string>
using namespace std;


class Monom
{
public:
double coeff;
int abc;

Monom(double coeff1 = NULL, int abc1 = NULL) { coeff = coeff1; abc = abc1; }

bool operator< (const Monom& m) const { return (abc<m.abc); }
bool operator> (const Monom& m) const { return (abc>m.abc); }
bool operator==(const Monom& m) const
{
int k = 1;
if (coeff)
k = (abc == m.abc && coeff == m.coeff);
return k;
}
bool operator!=(const Monom& m) const { return !(*this == m); }
Monom& operator=(const Monom& m) { coeff = m.coeff; abc = m.abc; return *this; }
};


class Polinom
{
Ringlist<Monom> pol;
Ringlist<Monom> Parse(const string& s);

public:
Polinom();
Polinom(const string& str);
Polinom(const Polinom& p);
Polinom(const Ringlist<Monom>& p) { pol = p; }
Ringlist<Monom> AddSameAndInsert(const Monom& c, const Ringlist<Monom>& p);

Polinom& operator=(const Polinom& p);
Polinom operator+(const Polinom& p);
Polinom operator-(const Polinom& p)
{
Polinom temp(*this);
temp = temp + (-1) * p;
return temp;
}
Polinom operator*(const int& c);
Polinom operator*(const Polinom& p);
friend Polinom operator*(const int c, const Polinom& p)
{
Polinom temp(p);
temp = temp * c;
return temp;
}

friend ostream& operator<< (ostream& os, const Polinom& p);
int operator== (const Polinom& p) const { return pol == p.pol; }
int operator!= (const Polinom& p) const { return pol != p.pol; }
};
51 changes: 51 additions & 0 deletions KuminAA/lab1-polinom/sample/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "polinom.h"
#include <conio.h>

void main()
{
cout << "Input correct polinoms" << endl;
char c = 0;
string pol1;
string pol2;

while (!c || (c == ' '))
{
cout << "-------------------------" << endl;
cout << endl;
cout << "Enter first polinom:" << endl;
getline(cin, pol1);
if (pol1 == "")
getline(cin, pol1);
Polinom p1(pol1);
cout << "p1 = " << p1 << endl;

cout << "Enter second polinom:" << endl;
getline(cin, pol2);
if (pol2 == "")
getline(cin, pol2);
Polinom p2(pol2);
cout << "p2 = " << p2 << endl;

cout << "-------------------------" << endl;

Polinom res = p1 + p2;
cout << "p1 + p2 = " << res << endl;

cout << "-------------------------" << endl;

res = p1 - p2;
cout << "p1 - p2 = " << res << endl;

cout << "-------------------------" << endl;

res = p1 * p2;
cout << "p1 * p2 = " << res << endl;

cout << "-------------------------" << endl;

cout << "Press SPACE to retry" << endl;
cout << "Press any key to exit" << endl;

c = _getch();
}
}
Loading