Finished Øving 9

master
Øyvind Skaaden 2019-03-13 23:01:06 +01:00
parent 9b8bd26daf
commit 92add1c7a2
9 changed files with 468 additions and 11 deletions

39
Oving 9/Oving 9/Dummy.cpp Normal file
View File

@ -0,0 +1,39 @@
#include "pch.h"
#include "Dummy.h"
void dummyTest()
{
Dummy a;
*a.num = 4;
Dummy b{ a };
Dummy c;
c = a;
cout << "a: " << *a.num << endl;
cout << "b: " << *b.num << endl;
cout << "c: " << *c.num << endl;
*b.num = 3;
*c.num = 5;
cout << "a: " << *a.num << endl;
cout << "b: " << *b.num << endl;
cout << "c: " << *c.num << endl;
cin.get();
}
Dummy::Dummy(const Dummy & other) : num{nullptr}
{
this->num = new int{};
*num = *other.num;
}
Dummy &Dummy::operator=(Dummy rhs)
{
swap(num, rhs.num);
return *this;
}

28
Oving 9/Oving 9/Dummy.h Normal file
View File

@ -0,0 +1,28 @@
#pragma once
#include <iostream>
using namespace std;
class Dummy
{
public:
int *num;
Dummy()
{
num = new int{ 0 };
}
Dummy(const Dummy &other);
Dummy &operator=(Dummy rhs);
~Dummy()
{
delete num;
}
};
void dummyTest();

View File

@ -3,5 +3,43 @@
void fillInFibonacciNumbers(int result[], int length)
{
int lo = 0;
int hi = 1;
int sum;
for (int i = 0; i < length; ++i)
{
result[i] = lo;
sum = lo + hi;
lo = hi;
hi = sum;
}
}
void printArray(int arr[], int length)
{
for (int i = 0; i < length; ++i)
{
cout << arr[i] << endl;
}
}
void createFibonacci()
{
int length;
cout << "How many fibonacci numbers? ";
cin >> length;
int *numbers = new int[length] {};
fillInFibonacciNumbers(numbers, length);
printArray(numbers, length);
delete[] numbers;
numbers = nullptr;
}

View File

@ -1,5 +1,8 @@
#pragma once
#include <iostream>
using namespace std;
void fillInFibonacciNumbers(int result[], int length);

190
Oving 9/Oving 9/Matrix.cpp Normal file
View File

@ -0,0 +1,190 @@
#include "pch.h"
#include "Matrix.h"
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
using namespace std;
/////////////
// Private //
/////////////
//Finds the longest number in the matrix
int Matrix::findLongestNumber() const
{
int longest = 0;
if (isValid())
{
int word;
ostringstream s;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
s << get(i, j);
word = s.str().length();
s.str("");
s.clear();
if (word > longest)
longest = word;
}
}
}
return longest;
}
////////////
// Public //
////////////
// Default
Matrix::Matrix()
{
matrix = nullptr;
rows = 0, cols = 0; // Illegal size
}
// General
Matrix::Matrix(const int nRows, const int nColumns)
{
// To make cleanup easier
rows = nRows;
cols = nColumns;
matrix = new double*[nRows] {}; // Create a list of pointers to array of ints
for (int i = 0; i < nRows; ++i)
{
matrix[i] = new double[nColumns] {0}; // Fill the list with an array of ints
for (int j = 0; j < nColumns; j++)
{
matrix[i][j] = 0;
}
}
}
// Identity-matrix
Matrix::Matrix(int nRows) : Matrix(nRows, nRows)
{
for (int i = 0; i < nRows; i++)
{
matrix[i][i] = 1; // Set the default diagonal to 1
}
}
// Set-get-functions
double Matrix::get(int row, int col) const
{
return matrix[row][col];
}
void Matrix::set(int row, int col, double value)
{
matrix[row][col] = value;
}
int Matrix::getRows() const
{
return rows;
}
int Matrix::getCols() const
{
return cols;
}
bool Matrix::isValid() const
{
if (matrix == nullptr || rows * cols == 0) // If the size is 0 in either direction, then the matrix is invalid
return false;
return true;
}
// Outsream-operator
ostream & operator<<(ostream & os, const Matrix & m)
{
if (m.isValid())
{
int width = m.findLongestNumber();
for (int i = 0; i < m.rows; i++)
{
for (int j = 0; j < m.cols; j++)
{
os << ' ' << setw(width) << m.get(i, j);
}
os << endl;
}
return os;
}
// If the matrix is non-valid, print this
return os << "The matrix is in an invalid state" << endl;
}
// Copy-constructor
Matrix::Matrix(const Matrix & rhs) : matrix{nullptr}
{
this->rows = rhs.rows; // Copy size
this->cols = rhs.cols; // Copy size
this->matrix = new double*[rhs.rows] {}; // Create a list of pointers to array of ints
for (int i = 0; i < this->rows; ++i)
{
matrix[i] = new double[this->cols] {0}; // Allocate the right amount of memory
for (int j = 0; j < this->cols; j++)
{
this->matrix[i][j] = rhs.matrix[i][j]; // Copy all values
}
}
}
// Copy-operator
Matrix & Matrix::operator=(Matrix rhs)
{
swap(rows, rhs.rows);
swap(cols, rhs.cols);
swap(matrix, rhs.matrix);
return *this;
}
// Add rhs to lhs (RightHandSide and LeftHandSide)
Matrix & Matrix::operator+=(const Matrix & rhs)
{
if (this->isValid() && rhs.isValid() && this->rows == rhs.rows && this->cols == rhs.cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
this->matrix[i][j] += rhs.get(i, j);
}
}
return *this;
}
this->matrix = nullptr;
return *this;
}
// Binary-add-operator
Matrix operator+(Matrix lhs, const Matrix & rhs)
{
lhs += rhs;
return lhs;
}
Matrix::~Matrix()
{
if (isValid())
{
for (int i = 0; i < rows; i++)
{
delete[] matrix[i]; // Delete all pointers to array of ints
}
delete[] matrix; // Delete all pointers to array of pointers
}
matrix = nullptr; // Sets the matrix to an illegal state
}

45
Oving 9/Oving 9/Matrix.h Normal file
View File

@ -0,0 +1,45 @@
#pragma once
#include <iostream>
using namespace std;
class Matrix
{
double **matrix; // Array of pointers to pointers
int rows, cols;
int findLongestNumber() const;
public:
// Constructors
Matrix(); // default
Matrix(const int nRows, const int nColumns); // Most common
explicit Matrix(int nRows); // Identity matrix
// Set-get
double get(int row, int col) const;
void set(int row, int col, double value);
int getRows() const;
int getCols() const;
// Check if matrix is valid
bool isValid() const;
// Out-operator
friend ostream& operator<<(ostream& os, const Matrix& m);
// Copy-operators
Matrix(const Matrix &rhs);
Matrix &operator=(Matrix rhs);
// Other operators
Matrix &operator+=(const Matrix &rhs);
friend Matrix operator+(Matrix lhs, const Matrix &rhs);
// Destructor
~Matrix();
};

View File

@ -3,21 +3,119 @@
#include "pch.h"
#include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
#include "Fibonacci.h"
#include "Matrix.h"
#include "Dummy.h"
using namespace std;
int main()
{
cout << "Hello World!\n";
// createFibonacci();
/*
Matrix m1{ 10 };
Matrix m2{ m1 };
Matrix m3;
Matrix m4{ 4, 5 };
m3 = m2;
m1.set(0, 0, 2);
m3.set(0, 1, 10);
m2.set(0, 1, 42);
cout << m1 << endl;
cout << m2 << endl;
cout << m3 << endl;
m3 += m2;
cout << m3 << endl;
m3 += m4;
cout << m3 << endl;
*/
Matrix A{ 2,2 };
Matrix B{ 2,2 };
Matrix C{ 2,2 };
A.set(0, 0, 1);
A.set(0, 1, 2);
A.set(1, 0, 3);
A.set(1, 1, 4);
B.set(0, 0, 4);
B.set(0, 1, 3);
B.set(1, 0, 2);
B.set(1, 1, 1);
C.set(0, 0, 1);
C.set(0, 1, 3);
C.set(1, 0, 1.5);
C.set(1, 1, 2);
cout << A << endl;
cout << B << endl;
cout << C << endl;
A += B + C;
cout << A << endl;
cout << B << endl;
cout << C << endl;
#pragma region Task 3
/*
Når dummy kjører vil det skrives ut:
"a: 4"
"b: 4"
"c: 4"
"a: 4"
"b: 3"
"c: 5"
*/
//dummyTest(); // Stemmer ikke overens med ovenfor
/* Skriver ut
"a: 4"
"b: 4"
"c: 4"
"a: 5"
"b: 5"
"c: 5"
*/
/* 3c)
Etter kopikonstruktøren fungerer Dummy b
*/
/* 3d)
Etter =-operatøren fungerer programmet som forventet
*/
#pragma endregion
return 0;
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file

View File

@ -151,11 +151,15 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="Dummy.h" />
<ClInclude Include="Fibonacci.h" />
<ClInclude Include="Matrix.h" />
<ClInclude Include="pch.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="Dummy.cpp" />
<ClCompile Include="Fibonacci.cpp" />
<ClCompile Include="Matrix.cpp" />
<ClCompile Include="Oving 9.cpp" />
<ClCompile Include="pch.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>

View File

@ -21,6 +21,12 @@
<ClInclude Include="Fibonacci.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Matrix.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Dummy.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="pch.cpp">
@ -32,5 +38,11 @@
<ClCompile Include="Fibonacci.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Matrix.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Dummy.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>