Created
May 19, 2012 18:59
-
-
Save artekem/2731964 to your computer and use it in GitHub Desktop.
matrixes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdafx.h" | |
#include <iomanip> | |
#include "Mac.h" | |
Mac::Mac() : Vec(), N(0), M(0) {} | |
Mac::Mac(int rows, int columns) : Vec() | |
{ | |
setSize(rows, columns); | |
} | |
Mac::Mac(const Mac &matrix) : N(matrix.N), M(matrix.M) | |
{ | |
setName(matrix.Naz); | |
setSize(N, M); | |
if (A) | |
{ | |
for (int i = 0; i < n; i++) | |
{ | |
A[i] = matrix.A[i]; | |
} | |
} | |
else | |
{ | |
n = 0; | |
} | |
} | |
/* | |
* Pobiera liczbe wierszy i kolumn macierzy oraz alokuje pamiec | |
*/ | |
void Mac::setSize(int rows, int columns) | |
{ | |
N = rows; | |
M = columns; | |
n = N * M; | |
A = new (nothrow) int[n]; | |
} | |
/* | |
* Zwraca liczbe wierszy | |
*/ | |
int Mac::getRows() | |
{ | |
return N; | |
} | |
/* | |
* Zwraca liczbe kolumn | |
*/ | |
int Mac::getColumns() | |
{ | |
return M; | |
} | |
/* | |
* Dodawanie macierzy | |
*/ | |
Mac Mac::operator+(const Mac &matrix) const | |
{ | |
Mac total; | |
total.N = (N > matrix.N) ? N : matrix.N; | |
total.M = (M > matrix.M) ? M : matrix.M; | |
total.setName("suma"); | |
total.setSize(total.N, total.M); | |
if (total.A) | |
{ | |
int i, j, k, m; | |
for (i = k = m = 0; i < N; ++i) | |
{ | |
for (j = 0; j < M; ++j, ++k, ++m) | |
{ | |
total.A[k] = A[m]; | |
} | |
for (; j < total.M; ++j, ++k) | |
{ | |
total.A[k] = 0; | |
} | |
} | |
for (; i < total.N; ++i) | |
{ | |
for(j=0; j<total.M; ++j,++k) | |
{ | |
total.A[k] = 0; | |
} | |
} | |
for (i = m = 0; i < matrix.N; i++) | |
{ | |
for (j = 0, k = i * total.M; j < matrix.M; ++j, ++k, ++m) | |
{ | |
// Wlasciwa operacja dodawania | |
total.A[k] += matrix.A[m]; | |
} | |
} | |
} | |
/*else | |
{ | |
total.n = total.N = total.M = 0; | |
}*/ | |
return total; | |
} | |
/* | |
* Odejmowanie macierzy | |
*/ | |
Mac Mac::operator-(const Mac &matrix) const | |
{ | |
Mac difference; | |
difference.N = (N > matrix.N) ? N : matrix.N; | |
difference.M = (M > matrix.M) ? M : matrix.M; | |
difference.setName("roznica"); | |
difference.setSize(difference.N, difference.M); | |
if (difference.A) | |
{ | |
int i, j, k, m; | |
for (i = k = m = 0; i < N; ++i) | |
{ | |
for (j = 0; j < M; ++j, ++k, ++m) | |
{ | |
difference.A[k] = A[m]; | |
} | |
for (; j < difference.M; ++j, ++k) | |
{ | |
difference.A[k] = 0; | |
} | |
} | |
for (; i < difference.N; ++i) | |
{ | |
for (j = 0; j<difference.M; ++j,++k) | |
{ | |
difference.A[k] = 0; | |
} | |
} | |
for (i = m = 0; i < matrix.N; i++) | |
{ | |
for (j = 0, k = i * difference.M; j < matrix.M; ++j, ++k, ++m) | |
{ | |
// Wlasciwa operacja odejmowania | |
difference.A[k] -= matrix.A[m]; | |
} | |
} | |
} | |
else | |
{ | |
difference.n = difference.N = difference.M = 0; | |
} | |
return difference; | |
} | |
/* | |
* Operator indeksacji | |
*/ | |
int &Mac::operator()(int i, int j) | |
{ | |
/* if (A == 0) | |
{ | |
// Zwraca zmienna statyczna x z klasy Vec | |
return x; | |
} | |
if (i >= N || i < 0 || j >= M || j < 0 ) | |
{ | |
return x; | |
} | |
*/ | |
return A[i * M + j]; | |
} | |
/* | |
* Mnozenie dwoch macierzy przez siebie | |
*/ | |
Mac Mac::operator*(Mac &v) const | |
{ | |
int Y; | |
Mac s; | |
s.N = N; | |
s.M = v.M; | |
Y = (M < v.N) ? M : v.N; | |
s.setName("mnozenie"); | |
s.setSize(s.N, s.M); | |
if (s.A) | |
{ | |
int i, j, k, m, r; | |
int y; | |
for (r = i = 0; i < s.N; ++i) | |
{ | |
for (y = j = 0; j < s.M; ++j, ++r) | |
{ | |
for (m = 0, k = i * M; m < Y; ++k, ++m) | |
{ | |
y += A[k] * v(m, j); | |
} | |
s.A[r] = y; | |
} | |
} | |
} | |
else | |
{ | |
s.n = s.N = s.M = 0; | |
} | |
return s; | |
} | |
/* | |
* Mnozenie macierzy przez skalar | |
*/ | |
Mac Mac::operator*(int scalar) const | |
{ | |
Mac matrix(*this); | |
matrix.setName("skalar"); | |
for (int i = 0; i < matrix.n; i++) | |
{ | |
matrix.A[i] *= scalar; | |
} | |
return matrix; | |
} | |
/* | |
* Implementacja pobierania danych macierzy | |
*/ | |
void Mac::setData(istream &input) | |
{ | |
cout << "Nazwa macierzy (maksymalnie 10 znakow): "; | |
input >> Naz; | |
cout << "Liczba wierszy macierzy: "; | |
input >> N; | |
cout << "Liczba kolumn macierzy: "; | |
input >> M; | |
n = N * M; | |
delete [] A; | |
A = new (nothrow) int[n]; | |
if (A == 0) | |
{ | |
N = M = n = 0; | |
} | |
// Wprowadzanie poszczegolnych elementow macierzy | |
for (int i = 0; i < n; i++) | |
{ | |
cout << Naz << "[" << (i / M + 1) << ", " << (i % M + 1)<< "] = "; | |
input >> A[i]; | |
} | |
} | |
/* | |
* Implementacja zwracania danych macierzy | |
*/ | |
void Mac::getData(ostream &output) const | |
{ | |
//int precision = output.precision(p); | |
//int width = precision + 6; | |
output << Naz << endl; | |
for (int i = 0; i < n; i++) | |
{ | |
if (i % M) | |
{ | |
output << ", "; | |
} | |
else if (i == 0) | |
{ | |
output << "[ "; | |
} | |
else | |
{ | |
output << " ]\n[ "; | |
} | |
// Wyswietlanie wlasciwego elementu macierzy | |
output << fixed << A[i]; | |
} | |
output << " ]\n\n"; | |
} | |
/* | |
* Zwraca macierz Nx1 z najmniejszymi elementami w wierszu | |
*/ | |
Mac Mac::getSmallestEllements() | |
{ | |
int j; | |
int smallest; | |
Mac matrix; | |
matrix.setName("najmn"); | |
matrix.setSize(N, 1); | |
smallest = A[0]; | |
for (int i = 0; i < N; i++) | |
{ | |
for (j = 0; j < M; j++) | |
{ | |
if (smallest > A[i * M + j]) | |
{ | |
smallest = A[i * M + j]; | |
} | |
} | |
matrix.A[i] = smallest; | |
smallest = A[i * M + j + 1]; | |
} | |
return matrix; | |
} | |
/* | |
* Zwraca macierz Nx1 z najwiekszymi elementami w wierszu | |
*/ | |
Mac Mac::getBiggestEllements() | |
{ | |
int j; | |
int biggest; | |
Mac matrix; | |
matrix.setName("najw"); | |
matrix.setSize(N, 1); | |
biggest = A[0]; | |
for (int i = 0; i < N; i++) | |
{ | |
for (j = 0; j < M; j++) | |
{ | |
if (biggest < A[i * M + j]) | |
{ | |
biggest = A[i * M + j]; | |
} | |
} | |
matrix.A[i] = biggest; | |
biggest = A[i * M + j + 1]; | |
} | |
return matrix; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// semestralnyDlg.cpp : implementation file | |
// | |
#include "stdafx.h" | |
#include "semestralny.h" | |
#include "semestralnyDlg.h" | |
#include "afxdialogex.h" | |
#include "Vec.h" | |
#include "Mac.h" | |
#ifdef _DEBUG | |
#define new DEBUG_NEW | |
#endif | |
// CAboutDlg dialog used for App About | |
class CAboutDlg : public CDialogEx | |
{ | |
public: | |
CAboutDlg(); | |
// Dialog Data | |
enum { IDD = IDD_ABOUTBOX }; | |
protected: | |
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support | |
// Implementation | |
protected: | |
DECLARE_MESSAGE_MAP() | |
}; | |
CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD) | |
{ | |
} | |
void CAboutDlg::DoDataExchange(CDataExchange* pDX) | |
{ | |
CDialogEx::DoDataExchange(pDX); | |
} | |
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx) | |
END_MESSAGE_MAP() | |
// CsemestralnyDlg dialog | |
CsemestralnyDlg::CsemestralnyDlg(CWnd* pParent /*=NULL*/) | |
: CDialogEx(CsemestralnyDlg::IDD, pParent) | |
, mac_A11(0) | |
, mac_A12(0) | |
, mac_A13(0) | |
, mac_A14(0) | |
, mac_A15(0) | |
, mac_A21(0) | |
, mac_A22(0) | |
, mac_A23(0) | |
, mac_A24(0) | |
, mac_A25(0) | |
, mac_A31(0) | |
, mac_A32(0) | |
, mac_A33(0) | |
, mac_A34(0) | |
, mac_A35(0) | |
, mac_A41(0) | |
, mac_A42(0) | |
, mac_A43(0) | |
, mac_A44(0) | |
, mac_A45(0) | |
, mac_A51(0) | |
, mac_A52(0) | |
, mac_A53(0) | |
, mac_A54(0) | |
, mac_A55(0) | |
, mac_B11(0) | |
, mac_B12(0) | |
, mac_B13(0) | |
, mac_B14(0) | |
, mac_B15(0) | |
, mac_B21(0) | |
, mac_B22(0) | |
, mac_B23(0) | |
, mac_B24(0) | |
, mac_B25(0) | |
, mac_B31(0) | |
, mac_B32(0) | |
, mac_B33(0) | |
, mac_B34(0) | |
, mac_B35(0) | |
, mac_B41(0) | |
, mac_B42(0) | |
, mac_B43(0) | |
, mac_B44(0) | |
, mac_B45(0) | |
, mac_B51(0) | |
, mac_B52(0) | |
, mac_B53(0) | |
, mac_B54(0) | |
, mac_B55(0) | |
, option(0) | |
, rowsA(2) | |
, columnsA(2) | |
, rowsB(2) | |
, columnsB(2) | |
{ | |
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); | |
} | |
void CsemestralnyDlg::DoDataExchange(CDataExchange* pDX) | |
{ | |
CDialogEx::DoDataExchange(pDX); | |
DDX_Text(pDX, IDC_EDIT1_1, mac_A11); | |
DDX_Text(pDX, IDC_EDIT1_2, mac_A12); | |
DDX_Text(pDX, IDC_EDIT1_3, mac_A13); | |
DDX_Text(pDX, IDC_EDIT1_4, mac_A14); | |
DDX_Text(pDX, IDC_EDIT1_5, mac_A15); | |
DDX_Text(pDX, IDC_EDIT2_1, mac_A21); | |
DDX_Text(pDX, IDC_EDIT2_2, mac_A22); | |
DDX_Text(pDX, IDC_EDIT2_3, mac_A23); | |
DDX_Text(pDX, IDC_EDIT2_4, mac_A24); | |
DDX_Text(pDX, IDC_EDIT2_5, mac_A25); | |
DDX_Text(pDX, IDC_EDIT3_1, mac_A31); | |
DDX_Text(pDX, IDC_EDIT3_2, mac_A32); | |
DDX_Text(pDX, IDC_EDIT3_3, mac_A33); | |
DDX_Text(pDX, IDC_EDIT3_4, mac_A34); | |
DDX_Text(pDX, IDC_EDIT3_5, mac_A35); | |
DDX_Text(pDX, IDC_EDIT4_1, mac_A41); | |
DDX_Text(pDX, IDC_EDIT4_2, mac_A42); | |
DDX_Text(pDX, IDC_EDIT4_3, mac_A43); | |
DDX_Text(pDX, IDC_EDIT4_4, mac_A44); | |
DDX_Text(pDX, IDC_EDIT4_5, mac_A45); | |
DDX_Text(pDX, IDC_EDIT5_1, mac_A51); | |
DDX_Text(pDX, IDC_EDIT5_2, mac_A52); | |
DDX_Text(pDX, IDC_EDIT5_3, mac_A53); | |
DDX_Text(pDX, IDC_EDIT5_4, mac_A54); | |
DDX_Text(pDX, IDC_EDIT5_5, mac_A55); | |
DDX_Text(pDX, IDC_EDITB1_1, mac_B11); | |
DDX_Text(pDX, IDC_EDITB1_2, mac_B12); | |
DDX_Text(pDX, IDC_EDITB1_3, mac_B13); | |
DDX_Text(pDX, IDC_EDITB1_4, mac_B14); | |
DDX_Text(pDX, IDC_EDITB1_5, mac_B15); | |
DDX_Text(pDX, IDC_EDITB2_1, mac_B21); | |
DDX_Text(pDX, IDC_EDITB2_2, mac_B22); | |
DDX_Text(pDX, IDC_EDITB2_3, mac_B23); | |
DDX_Text(pDX, IDC_EDITB2_4, mac_B24); | |
DDX_Text(pDX, IDC_EDITB2_5, mac_B25); | |
DDX_Text(pDX, IDC_EDITB3_1, mac_B31); | |
DDX_Text(pDX, IDC_EDITB3_2, mac_B32); | |
DDX_Text(pDX, IDC_EDITB3_3, mac_B33); | |
DDX_Text(pDX, IDC_EDITB3_4, mac_B34); | |
DDX_Text(pDX, IDC_EDITB3_5, mac_B35); | |
DDX_Text(pDX, IDC_EDITB4_1, mac_B41); | |
DDX_Text(pDX, IDC_EDITB4_2, mac_B42); | |
DDX_Text(pDX, IDC_EDITB4_3, mac_B43); | |
DDX_Text(pDX, IDC_EDITB4_4, mac_B44); | |
DDX_Text(pDX, IDC_EDITB4_5, mac_B45); | |
DDX_Text(pDX, IDC_EDITB5_1, mac_B51); | |
DDX_Text(pDX, IDC_EDITB5_2, mac_B52); | |
DDX_Text(pDX, IDC_EDITB5_3, mac_B53); | |
DDX_Text(pDX, IDC_EDITB5_4, mac_B54); | |
DDX_Text(pDX, IDC_EDITB5_5, mac_B55); | |
DDX_Radio(pDX, IDC_RADIO1, option); | |
DDX_Text(pDX, IDC_EDIT_rows, rowsA); | |
DDV_MinMaxInt(pDX, rowsA, 1, 5); | |
DDX_Text(pDX, IDC_EDIT_columns, columnsA); | |
DDV_MinMaxInt(pDX, columnsA, 1, 5); | |
DDX_Text(pDX, IDC_EDIT_rowsB, rowsB); | |
DDV_MinMaxInt(pDX, rowsB, 1, 5); | |
DDX_Text(pDX, IDC_EDIT_columnsB, columnsB); | |
DDV_MinMaxInt(pDX, columnsB, 1, 5); | |
} | |
BEGIN_MESSAGE_MAP(CsemestralnyDlg, CDialogEx) | |
ON_WM_SYSCOMMAND() | |
ON_WM_PAINT() | |
ON_WM_QUERYDRAGICON() | |
ON_BN_CLICKED(IDOK, &CsemestralnyDlg::OnBnClickedOk) | |
ON_BN_CLICKED(IDC_BUTTON1, &CsemestralnyDlg::OnBnClickedButton1) | |
END_MESSAGE_MAP() | |
// CsemestralnyDlg message handlers | |
BOOL CsemestralnyDlg::OnInitDialog() | |
{ | |
CDialogEx::OnInitDialog(); | |
// Add "About..." menu item to system menu. | |
// IDM_ABOUTBOX must be in the system command range. | |
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); | |
ASSERT(IDM_ABOUTBOX < 0xF000); | |
CMenu* pSysMenu = GetSystemMenu(FALSE); | |
if (pSysMenu != NULL) | |
{ | |
BOOL bNameValid; | |
CString strAboutMenu; | |
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX); | |
ASSERT(bNameValid); | |
if (!strAboutMenu.IsEmpty()) | |
{ | |
pSysMenu->AppendMenu(MF_SEPARATOR); | |
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); | |
} | |
} | |
// Set the icon for this dialog. The framework does this automatically | |
// when the application's main window is not a dialog | |
SetIcon(m_hIcon, TRUE); // Set big icon | |
SetIcon(m_hIcon, FALSE); // Set small icon | |
// TODO: Add extra initialization here | |
return TRUE; // return TRUE unless you set the focus to a control | |
} | |
void CsemestralnyDlg::OnSysCommand(UINT nID, LPARAM lParam) | |
{ | |
if ((nID & 0xFFF0) == IDM_ABOUTBOX) | |
{ | |
CAboutDlg dlgAbout; | |
dlgAbout.DoModal(); | |
} | |
else | |
{ | |
CDialogEx::OnSysCommand(nID, lParam); | |
} | |
} | |
// If you add a minimize button to your dialog, you will need the code below | |
// to draw the icon. For MFC applications using the document/view model, | |
// this is automatically done for you by the framework. | |
void CsemestralnyDlg::OnPaint() | |
{ | |
if (IsIconic()) | |
{ | |
CPaintDC dc(this); // device context for painting | |
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0); | |
// Center icon in client rectangle | |
int cxIcon = GetSystemMetrics(SM_CXICON); | |
int cyIcon = GetSystemMetrics(SM_CYICON); | |
CRect rect; | |
GetClientRect(&rect); | |
int x = (rect.Width() - cxIcon + 1) / 2; | |
int y = (rect.Height() - cyIcon + 1) / 2; | |
// Draw the icon | |
dc.DrawIcon(x, y, m_hIcon); | |
} | |
else | |
{ | |
CDialogEx::OnPaint(); | |
} | |
} | |
// The system calls this function to obtain the cursor to display while the user drags | |
// the minimized window. | |
HCURSOR CsemestralnyDlg::OnQueryDragIcon() | |
{ | |
return static_cast<HCURSOR>(m_hIcon); | |
} | |
void CsemestralnyDlg::OnBnClickedOk() | |
{ | |
} | |
void CsemestralnyDlg::OnBnClickedButton1() | |
{ | |
UpdateData(); | |
Mac matrixA(rowsA, columnsA); | |
Mac matrixB(rowsB, columnsB); | |
int rows_bigger = (rowsA > rowsB) ? rowsA : rowsB; | |
int columns_bigger = (columnsA > columnsB) ? columnsA : columnsB; | |
Mac suma(rows_bigger, columns_bigger); | |
matrixA.setCoordinate(0, mac_A11); | |
matrixA.setCoordinate(1, mac_A12); | |
matrixA.setCoordinate(2, mac_A21); | |
matrixA.setCoordinate(3, mac_A22); | |
matrixB.setCoordinate(0, mac_B11); | |
matrixB.setCoordinate(1, mac_B12); | |
matrixB.setCoordinate(2, mac_B21); | |
matrixB.setCoordinate(3, mac_B22); | |
suma = matrixA; | |
CString test; | |
test.Format("%d", suma(0,0)); | |
((CEdit*)GetDlgItem(IDC_TEST))->SetWindowText(test); | |
ofstream plik_wy("suma.txt"); | |
if (plik_wy.is_open()) | |
{ | |
// Suma | |
plik_wy << matrixB; | |
} | |
plik_wy.close(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdafx.h" | |
#include "Vec.h" | |
using namespace std; | |
Vec::Vec() : n(0), A(0) | |
{ | |
setName("0"); | |
} | |
Vec::Vec(char *name) : n(0), A(0) | |
{ | |
setName(name); | |
} | |
Vec::Vec(const Vec &vector) : n(vector.n) | |
{ | |
setName(vector.Naz); | |
setLength(n); | |
if (A) | |
{ | |
for (int i = 0; i < n; i++) | |
{ | |
A[i] = vector.A[i]; | |
} | |
} | |
else | |
{ | |
n = 0; | |
} | |
} | |
Vec::~Vec() | |
{ | |
delete [] A; | |
n = 0; | |
A = 0; | |
} | |
/* | |
* Ustawia nazwe wektora | |
*/ | |
void Vec::setName(const char *name) | |
{ | |
strcpy_s(Naz, name); | |
} | |
/** | |
* Ustawia rozmiar wektora i rezerwuje na niego miejsce w pamieci | |
*/ | |
void Vec::setLength(int size) | |
{ | |
n = size; | |
if (n > 0) | |
{ | |
A = new int[n]; | |
} | |
} | |
/** | |
* Zapisuje wartosc pojedynczej wspolrzednej do tablicy wektora | |
*/ | |
void Vec::setCoordinate(int i, int value) | |
{ | |
A[i] = value; | |
} | |
/* | |
* Ustawia precyzje wyprowadzania wynikow | |
*/ | |
/*void Vec::setPrecision(int n) | |
{ | |
if (n >= 0) | |
{ | |
p = n; | |
} | |
}*/ | |
/* | |
* Zwraca obecna precyzje wyprowadzania wynikow | |
*/ | |
/*int Vec::getPrecision() | |
{ | |
return p; | |
}*/ | |
/* | |
* Zwraca dlugosc wektora | |
*/ | |
int Vec::getLength() | |
{ | |
return n; | |
} | |
/* | |
* Operator dodawania | |
*/ | |
Vec Vec::operator+(const Vec &vector) const | |
{ | |
int length; | |
Vec total; | |
// Dlugosc dluzszego wektora | |
length = (n > vector.n) ? n : vector.n; | |
total.setName("s"); | |
total.setLength(length); | |
if (total.A) | |
{ | |
int i; | |
for (i = 0; i < n; i++) | |
{ | |
total.A[i] = A[i]; | |
} | |
for (; i < vector.n; i++) | |
{ | |
total.A[i] = 0; | |
} | |
for (i = 0; i < vector.n; i++) | |
{ | |
total.A[i] += vector.A[i]; | |
} | |
} | |
else | |
{ | |
total.n = 0; | |
} | |
return total; | |
} | |
/* | |
* Operator odejmowania | |
*/ | |
Vec Vec::operator-(const Vec &vector) const | |
{ | |
int length; | |
Vec difference; | |
// Dlugosc dluzszego wektora | |
length = (n > vector.n) ? n : vector.n; | |
difference.setName("r"); | |
difference.setLength(length); | |
if (difference.A) | |
{ | |
int i; | |
for (i = 0; i < n; i++) | |
{ | |
difference.A[i] = A[i]; | |
} | |
for (; i < vector.n; i++) | |
{ | |
difference.A[i] = 0; | |
} | |
for (i = 0; i < vector.n; i++) | |
{ | |
difference.A[i] -= vector.A[i]; | |
} | |
} | |
else | |
{ | |
difference.n = 0; | |
} | |
return difference; | |
} | |
/* | |
* Operator mnozenia skalarnego | |
*/ | |
int Vec::operator*(const Vec &vector) const | |
{ | |
int total = 0; | |
int length = (n < vector.n) ? n : vector.n; | |
for (int i = 0; i < length; i++) | |
{ | |
total += A[i] * vector.A[i]; | |
} | |
return total; | |
} | |
/* | |
* Operator mnozenia przez liczbe | |
*/ | |
Vec Vec::operator*(int scalar) const | |
{ | |
Vec vector(*this); | |
vector.setName("mno"); | |
for (int i = 0; i < vector.n; i++) | |
{ | |
vector.A[i] *= scalar; | |
} | |
return vector; | |
} | |
/* | |
* Operator mnozenia przez liczbe z odwrotnymi parametrami | |
*/ | |
Vec operator*(int scalar, const Vec &vector) | |
{ | |
return vector * scalar; | |
} | |
/* | |
* Operator indeksacji | |
*/ | |
int &Vec::operator[](int i) const | |
{ | |
/*if (A == 0 || i < 0 || i >= n) | |
{ | |
return x; | |
} | |
*/ | |
return A[i]; | |
} | |
/* | |
* Operator konwersji z typu Vec do int | |
*/ | |
Vec::operator int() const | |
{ | |
double vector_length = 0; | |
for (int i = n - 1; i >= 0; i--) | |
{ | |
vector_length += A[i] * A[i]; | |
} | |
return sqrt(vector_length); | |
} | |
/* | |
* Operator wprowadzania | |
*/ | |
istream &operator>>(istream &input, Vec &vector) | |
{ | |
vector.setData(input); | |
return input; | |
} | |
/* | |
* Operator wyprowadzania | |
*/ | |
ostream &operator<<(ostream &output, const Vec &vector) | |
{ | |
vector.getData(output); | |
return output; | |
} | |
/* | |
* Implementacja pobierania danych wektora | |
*/ | |
void Vec::setData(istream &input) | |
{ | |
if (*Naz == 0) | |
{ | |
cout << "Nazwa wektora (maksymalnie 10 znakow): "; | |
input >> Naz; | |
} | |
if (n == 0) | |
{ | |
cout << "Dlugosc wektora: "; | |
input >> n; | |
} | |
// Usuniecie pustej tablicy ze wspolrzednymi wektora | |
delete [] A; | |
A = new (nothrow) int[n]; | |
if (A == 0) | |
{ | |
n = 0; | |
} | |
for (int i = 0; i < n; i++) | |
{ | |
cout << Naz << "[" << i + 1 << "] = "; | |
input >> A[i]; | |
} | |
} | |
/* | |
* Implementacja zwracania danych wektora | |
*/ | |
void Vec::getData(ostream &output) const | |
{ | |
output << Naz << " = ["; | |
if (A) | |
{ | |
output << A[0]; | |
for (int i = 1; i < n; i++) | |
{ | |
output << ", " << A[i]; | |
} | |
} | |
output << "]"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment