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

Vector #58

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
71 changes: 53 additions & 18 deletions vectors/vector.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,60 @@
#include <iostream>
#include "vector.h"

using namespace std;
bool test_vector3d()
{
vector3d v1(2, 3, 1), v2(2, 4, 0), v3(0, 0, 0);

ostream& operator <<(ostream& os, const vector3d& v) {
return os << "{ " << v[0] << ", " << v[1] << ", " << v[2] << " }";
if ((v1 + v2) != vector3d(4, 7, 1))
{
cerr << "Wrong operation binary + \n";
return 0;
}

if ((v1 * 3) != vector3d(6, 9, 3))
{
cerr << "Wrong operation binary * on digit \n";
return 0;
}

if ((v2 / 2) != vector3d(1, 2, 0))
{
cerr << "Wrong operation binary / on digit \n";
return 0;
}

if (-v2 != vector3d(-2, -4, 0))
{
cerr << "Wrong operation unary - \n";
return 0;
}

if (!(v3) != vector3d(1, 1, 1) && !v2 != vector3d(0, 0, 0))
{
cerr << "Wrong operation unary ! \n";
return 0;
}
return 1;
}



int main(int argc, char** argv) {
vector3d v1, v2(12), v3(1, 3, 8);
v1[2] = 54;
//vector3d v4 = v1 + v2, v5 = v1 - v2, v6 = v1 * 0.5f;
//cout << "v4: " << v4 << endl << "v5: " << v5 << endl << "v6: " << v6 << endl;

printf("address of v1: 0x%p, size: %zu bytes\n", &v1, sizeof(v1));
printf("address of v1.data: 0x%p, size: %zu bytes\n", &v1.data, sizeof(v1.data));
printf("address of v1.data[-1]: 0x%p, size: %zu bytes\n", &v1.data[-1], sizeof(v1.data[-1]));
printf("address of v1.data[0]: 0x%p, size: %zu bytes\n", &v1.data[0], sizeof(v1.data[0]));
printf("address of v1.data[1]: 0x%p, size: %zu bytes\n", &v1.data[1], sizeof(v1.data[1]));
printf("address of v1.data[2]: 0x%p, size: %zu bytes\n", &v1.data[2], sizeof(v1.data[2]));
printf("address of v1.data[2000]: 0x%p, size: %zu bytes\n", &v1.data[2000], sizeof(v1.data[2000]));

return 0;
}

vector3d v1(12), v2(1, 3, 8),v3(0,0,0);
vector3d v4(v3);
v2[1] = 5;//���� �� ������ ��������, �� �����������
cout << v3 + v2;
cout << v2 * 5;
cout << v2 / 3;
cout << v2 ;
if (v2 != v1)
cout << "correct != operator" << endl;
if (test_vector3d())
return 0;
else
return 1;



}
61 changes: 58 additions & 3 deletions vectors/vector.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <ostream>
using namespace std;

class vector3d {
float data[3];
Expand All @@ -10,10 +11,64 @@ class vector3d {
vector3d(float value) { data[2] = data[1] = data[0] = value; }
vector3d(float a1, float a2, float a3) { data[0] = a1; data[1] = a2; data[2] = a3; }

float& operator[](int idx) { return data[idx]; }
float operator[](int idx) const { return data[idx]; }
vector3d operator+(const vector3d& other) {

return vector3d(data[0] + other.data[0],data[1] + other.data[1],data[2] + other.data[2]);
}

vector3d operator*(const float digit)
{
return vector3d(data[0] * digit,data[1] * digit,data[2] * digit);
}

vector3d operator/(const float digit)
{
return vector3d(data[0] / digit,data[1] / digit,data[2] / digit);
}
vector3d operator-()
{
return vector3d(-data[0], -data[1], -data[2]);
}
vector3d operator!()
{
if (data[0] == data[1] == data[2] == 0)
return vector3d(1, 1, 1);
return vector3d(0, 0, 0);
}
bool operator!=(const vector3d& v)
{
if (data[0] != v.data[0] || data[1] != v.data[1] || data[2] != v.data[2])
return 1;
return 0;
}

vector3d(const vector3d& v);

friend int main(int argc, char** argv);
friend ostream&operator<<(ostream& os, const vector3d& v);

float& operator[](int idx) { return data[idx]; }// �� ������ �������, ��� ��������� ��� ������
float operator[](int idx) const { return data[idx]; } // � ��� ������������ ������ �����, ������� ������������ ����� �������, ����� �� ������ ������, ������� const � �����=��-� ������ �� ������


};
ostream &operator<<(ostream& os, const vector3d& v)
{
os <<"{"<<v.data[0]<<"," << v.data[1] <<"," << v.data[2] << "}\n";
return os;

}

vector3d::vector3d(const vector3d& v)
{
data[0] = v.data[0];
data[1] = v.data[1];
data[2] = v.data[2];
cout << "constructor copy\n";
}






std::ostream& operator <<(std::ostream& os, const vector3d& v);