first commit

This commit is contained in:
2026-03-05 13:16:26 +01:00
commit 1b2bf174e8
164 changed files with 35594 additions and 0 deletions

106
Lab1/array.h Normal file
View File

@@ -0,0 +1,106 @@
//##################################################################
// LAB 1
// ADVANCED PROGRAMMING IN C++
// MATRIX/ARRAY WITH DEEP COPY
//==================================================================
// array.h
// CLASS IMPLEMENTATION AND DEFINITION FOR
// THE ARRAY
// Christian Ohlsson
// Daniel Alfredsson
// Karlstad 990907
//==================================================================
#include <iostream.h>
class Array {
private:
int size; //The size of the Array
int *buffer; //Pointer to an int-buffer
public:
~Array();
Array(const int s=0);
Array(const Array &src);
void operator = (const Array &src);
Array Array::operator +(const Array &src);
int &operator [] (int index) const;
int getSize() const;
};
//################################################################
// Constructor
// Creates a new Array
//================================================================
Array::Array(const int s=0) {
buffer = new int[s];
size= s;
}
//################################################################
// Constructor
// Copys an array
//================================================================
Array::Array(const Array &src) {
size = src.size;
buffer = new int[size];
for(int i=0 ; i<size ; i++)
buffer[i] = src.buffer[i];
}
//################################################################
// Operator overload
// Allows to assign a Array to another
//================================================================
void Array::operator = (const Array &src) {
delete [] buffer;
size = src.size;
buffer = new int[size];
for(int i=0 ; i<size ; i++)
buffer[i] = src.buffer[i];
}
//################################################################
// Operator overload
// Returns a element in the Array
//================================================================
int &Array::operator [] (int index) const{
return buffer[index];
}
//################################################################
// Operator overload
// Allows to add a Array to another
//================================================================
Array Array::operator + (const Array &src) {
Array tmp(size);
for(int i=0 ; i<size ; i++)
tmp.buffer[i] = buffer[i] + src.buffer[i];
return (tmp);
}
//################################################################
// Destructor
// Deallocates memory, used by the Array
//================================================================
Array::~Array() {
delete [] buffer;
}
//################################################################
// Operator overload
// Allows input in the Array, using STDIN
//================================================================
istream &operator >> (istream &is, Array &v) {
for(int l=0 ; l<v.getSize() ; l++)
is >> v[l];
return is;
}
//################################################################
// Operator overload
// Prints out an Array, using STDOUT
//================================================================
ostream &operator << (ostream &os, const Array &v) {
for(int k=0 ; k<v.getSize() ; k++)
os << v[k];
return os;
}
//################################################################
// Function getSize()
// Returns the size of the Array
//================================================================
int Array::getSize() const{
return size;
}

BIN
Lab1/lab_one Normal file

Binary file not shown.

16
Lab1/matrix.cpp Normal file
View File

@@ -0,0 +1,16 @@
//matrix.cpp
#include "matrix.h"
int main() {
Array a (2);
Matrix b (2,2);
cout << "input Array: " << endl;
cin>>a;
cout << "input Matrix: " << endl;
cin >>b;
//b[2] = a;
b[1] = a + b[1];
Matrix c(b);
b[1][1] = a[1];
cout << "c:\n" <<c << "a:\n"<< a << "\nb:\n" << b;
return 0;
}

111
Lab1/matrix.h Normal file
View File

@@ -0,0 +1,111 @@
//##################################################################
// LAB 1
// ADVANCED PROGRAMMING IN C++
// MATRIX/ARRAY WITH DEEP COPY
//==================================================================
// matrix.h
// CLASS IMPLEMENTATION AND DEFINITION FOR
// THE MATRIX
// Christian Ohlsson
// Daniel Alfredsson
// Karlstad 990907
//==================================================================
#include "array.h"
class Matrix {
private:
int cols, rows; //# of cols and rows in the matrix
Array *myMatrix; //Array-pointer to our matrix
public:
void Matrix::operator = (const Matrix &src);
int getCols() const { return cols;};
int getRows() const { return rows;};
Array &Matrix::operator [] (int index) const;
Matrix(const Matrix &src);
Matrix(int cols, int rows);
~Matrix();
};
//################################################################
// Constructor
// Creates a new Matrix with 'c' columns and 'r' rows
//================================================================
Matrix::Matrix(int c, int r) {
myMatrix = new Array[r];
for(int i=0; i < c; i++)
myMatrix[i] = Array(c);
cols = c;
rows = r;
}
//################################################################
// Constructor
// Creates a new copy of a Matrix
//================================================================
Matrix::Matrix(const Matrix &src) {
cols = src.cols;
rows = src.rows;
int i,j;
myMatrix = new Array[rows];
for(i=0; i < cols; i++)
myMatrix[i] = Array(cols);
for(i=0 ; i < cols ; i++)
for(j=0 ; j < rows ; j++)
myMatrix[i][j] = src.myMatrix[i][j];
}
//################################################################
// Destructor
// De-allocates memory for a Matrix
//================================================================
Matrix::~Matrix() {
delete [] myMatrix;
}
//################################################################
// Operator overload
// Sets one Matrix equal to another
//================================================================
void Matrix::operator = (const Matrix &src) {
delete [] myMatrix;
cols = src.cols;
rows = src.rows;
myMatrix = new Array[rows];
for(int i=0 ; i<cols ; i++)
for(int j=0 ; j<rows ; j++)
myMatrix[i][j] = src.myMatrix[i][j];
}//################################################################
// Operator overload
// Reyurns a row in a Matrix, and checks for bad indexes.
//================================================================
Array &Matrix::operator [] (int index) const {
if(index>=rows || index<0) {
cout <<"\nIndex out of bounds! Quitting...\n";
exit(0);
}
else
return myMatrix[index];
}
//################################################################
// Operator overload
// Allow inserts in a Matrix, using STDIN
//================================================================
istream &operator >> (istream &is, Matrix &v) {
int i,j;
for(i=0 ; i<v.getCols() ; i++)
for (j=0 ; j<v.getRows() ; j++)
is >> v[i][j];
return is;
}
//################################################################
// Operator overload
// Prints the Matrix on the screen, using STDUOT
//================================================================
ostream &operator << (ostream &os, const Matrix &v) {
int i,j;
for(i=0 ; i < v.getCols() ; i++)
for(j=0 ; j < v.getRows() ; j++)
os << i << "," << j<< " = " << v[i][j] << "\n";
return os;
}

12
Lab1/read_me Normal file
View File

@@ -0,0 +1,12 @@
Avancerad programmering i C++
==========================================
Denna lab skapades under Linux med
kommandot:
g++ -Wall -o lab_one *.cpp