программы c++ (843584)
Текст из файла
Vector.h
#pragma once
#include
#include
#include
class Vector
{
private:
int count;
int* mass;
public:
Vector(int N);
Vector(const Vector& V);
~Vector();
Vector operator=(const Vector& V);
Vector operator+(const Vector& V) const;
Vector operator-(const Vector& V);
Vector operator*(const int& V);
int operator*(const Vector& V) const;
friend std::ostream& operator<<(std::ostream& os, const Vector& V);
Vector.cpp
#include "pch.h"
#include "Vector.h"
#include
Vector::Vector(int N)
{
count = N;
mass = new int[count];
for (int i = 0; i < count; i++)
{
mass[i] = rand() % 10;
}
}
Vector::~Vector()
{
delete[] mass;
}
Vector::Vector(const Vector& V)
{
count = V.count;
mass = new int[count];
for (int i = 0; i < count; i++)
{
mass[i] = V.mass[i];
}
}
Vector Vector:: operator=(const Vector& V)
{
count = V.count;
delete[] mass;//óäàëÿåì ïàìÿòü
mass = new int[count];// âûäåëÿåì ïàìÿòü ïîä òî ÷òî áóäåì çàïèñûâàòü
for (int i = 0; i < count; i++)//
{
mass[i] = V.mass[i];
}
return *this;
}
Vector Vector:: operator+(const Vector& V) const
{
Vector x(count);
x.mass = new int[x.count];
for (int i = 0; i < count; i++)
{
x.mass[i] = mass[i] + V.mass[i];
}
return x;
}
Vector Vector :: operator-(const Vector& V)
{
Vector x(count);
x.mass = new int[x.count];
for (int i = 0; i < count; i++)
{
x.mass[i] = mass[i] - V.mass[i];
}
return x;
}
int Vector::operator*(const Vector& V) const
{
int res = 0;
for (int i = 0; i < count; i++)
{
res += mass[i] * V.mass[i];
}
return res;
}
Vector Vector::operator*(const int& V)
{
Vector x(count);
x.mass = new int[x.count];
for (int i = 0; i < count; i++)
{
x.mass[i] = mass[i] * V;
}
return x;
}
std::ostream& operator<<(std::ostream& os, const Vector& V)
{
os << "(";
for (int i = 0; i < V.count - 1; i++)
{
os << V.mass[i] << " , ";
}
os << V.mass[V.count - 1] << ")" << std::endl;
return os;
}
Исходный код
#include
#include "Vector.h"
using namespace std;
int main()
{
std::srand(time(NULL));
Vector a(3);
cout << "a" << a << endl;
Vector b(3);
cout << "b" << b << endl;
Vector c(3);
cout << "c" << c << endl;
a = b;
cout << "1. " << a << endl;
cout << "2. " << c + b << endl;
cout << "3. " << c - b << endl;
cout << "4. " << c * b << endl;
cout << "5. " << c*4 << endl;
cin.get();
return 0;
}
// BaseList.h
#pragma once
template
class BaseList
{
protected:
int listSize;
public:
BaseList() {}
virtual ~BaseList() {}
int size() const { return listSize; }
bool isEmpty() const { return !listSize; }
virtual BaseList& Sort(const T& value) = 0;
virtual BaseList& addInHead(const T& value) = 0;
virtual BaseList& addInTail(const T& value) = 0;
virtual BaseList& removeFromHead() = 0;
virtual BaseList& removeFromTail() = 0;
virtual T& head() = 0;
virtual const T& head() const = 0;
virtual T& tail() = 0;
virtual const T& tail() const = 0;
};
// LinkedList.h
#pragma once
#include "BaseList.h"
#include
template
class LinkedList : public BaseList
{
private:
struct Node
{
T data;
Node *next;
};
Node* buildNode(const T& value,
Node *left = nullptr, // nullptr- нулевой указатель
Node *right = nullptr)
{
Node *newNode = new Node;
newNode->next = right;
newNode->data = value;
if (left)
{
left->next = newNode;
}
return newNode;
}
Node* findParentNode(Node *head, Node *node)
{
if (head == node)
{
return nullptr;
}
Node *temp = head;// указатель на голову
while (temp)
{
if (temp->next == node)
{
return temp;
}
temp = temp->next;
}
return nullptr;
}
Node* addAfter(const T& value, Node *node)
{
return buildNode(value, node, node->next);
}
Node* addBefore(const T& value, Node *node)
{
return buildNode(value, findParentNode(headNode, node), node);
}
void remove(Node *node)
{
if (node)
{
Node *parent = findParentNode(headNode, node);
if (parent)
{
parent->next = node->next;
}
node->next = nullptr;
delete node;
}
}
Node* headNode;
Node* tailNode;
public:
LinkedList();
~LinkedList();
BaseList& addInHead(const T& value);
BaseList& addInTail(const T& value);
BaseList& removeFromHead();
BaseList& removeFromTail();
BaseList& remove(const T& value);
BaseList& Sort(const T& value);
T& head() { return headNode->data; }
const T& head() const { return headNode->data; }
T& tail() { return tailNode->data; }
const T& tail() const { return tailNode->data; }
template
friend std::ostream& operator«(std::ostream& os, const LinkedList& list);
};
template
LinkedList::LinkedList()
{
headNode = tailNode = nullptr;
listSize = 0;
}
template
BaseList& LinkedList::addInHead(const T& value)
{
if (!isEmpty())
{
headNode = tailNode = addBefore(value, headNode);
}
else
{
headNode = tailNode = buildNode(value);
}
listSize++;
return *this;
}
template
BaseList& LinkedList::addInTail(const T& value)
{
if (!isEmpty())
{
tailNode = addAfter(value, tailNode);
}
else
{
headNode = tailNode = buildNode(value);
}
listSize++;
return *this;
}
template
BaseList& LinkedList::removeFromHead()
{
if (isEmpty())
{
return *this;
}
if (listSize == 1)
{
remove(headNode);
headNode = tailNode = nullptr;
}
else
{
Node* temp = headNode;
headNode = headNode->next;
remove(temp);
}
listSize--;
return *this;
}
template
BaseList& LinkedList::removeFromTail()
{
if (isEmpty())
{
return *this;
}
if (listSize == 1)
{
remove(headNode);
headNode = tailNode = nullptr;
}
else
{
Node* parent = findParentNode(headNode, tailNode);
tailNode = parent;
remove(tailNode->next);
}
listSize--;
return *this;
}
template
LinkedList::~LinkedList()
{
while (!isEmpty())
{
removeFromHead();
}
}
template
std::ostream& operator«(std::ostream & os, const LinkedList& list)
{
if (list.isEmpty())
{
os « "List is empty!" « std::endl;
return os;
}
LinkedList::Node *temp = list.headNode;
while (temp)
{
os « temp->data « " ";
temp = temp->next;
}
return os;
}
template
BaseList& LinkedList::Sort(const T& value)
{
if (isEmpty())
{
headNode = tailNode = buildNode(value);
listSize++;
return *this;
}
if (value data)
{
headNode = addBefore(value, headNode);
listSize++;
return *this;
}
Node *temp = headNode;
while (temp)
{
if (value data)
{
addBefore(value, temp);
listSize++;
return *this;
}
temp = temp->next;
}
tailNode = addAfter(value, tailNode);
listSize++;
return *this;
}
template
BaseList& LinkedList::Remove(const T& value)
{
if (value == headNode->data)
{
Node* temp = headNode;
headNode = headNode->next;
remove(temp);
listSize--;
return *this;
}
else
{
Node* temp = headNode;
while (temp)
{
if (temp->data == value)
{
Node* parent = findParentNode(headNode, temp);
parent->next = temp->next;
remove(temp);
listSize--;
return*this;
}
temp = temp->next;
}
}
}
//Source.cpp
#include "LinkedList.h"
int main()
{
LinkedList list;
std::cout « list « std::endl;
list.Sort(2).Sort(1).Sort(4).Sort(3) Sort(5);
std::cout « list « std::endl;
list.Remove(3);
std::cout « list « std::endl;
std::cin.get();
return 0;
}
Характеристики
Тип файла документ
Документы такого типа открываются такими программами, как Microsoft Office Word на компьютерах Windows, Apple Pages на компьютерах Mac, Open Office - бесплатная альтернатива на различных платформах, в том числе Linux. Наиболее простым и современным решением будут Google документы, так как открываются онлайн без скачивания прямо в браузере на любой платформе. Существуют российские качественные аналоги, например от Яндекса.
Будьте внимательны на мобильных устройствах, так как там используются упрощённый функционал даже в официальном приложении от Microsoft, поэтому для просмотра скачивайте PDF-версию. А если нужно редактировать файл, то используйте оригинальный файл.
Файлы такого типа обычно разбиты на страницы, а текст может быть форматированным (жирный, курсив, выбор шрифта, таблицы и т.п.), а также в него можно добавлять изображения. Формат идеально подходит для рефератов, докладов и РПЗ курсовых проектов, которые необходимо распечатать. Кстати перед печатью также сохраняйте файл в PDF, так как принтер может начудить со шрифтами.