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

130
Lab2/array.h Normal file
View File

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

26
Lab2/contract.h Normal file
View File

@@ -0,0 +1,26 @@
//##################################################################
// LAB 2
// ADVANCED PROGRAMMING IN C++
// MATRIX/ARRAY WITH SHALLOW COPY
//==================================================================
// contract.h
// CLASS IMPLEMENTATION AND DEFINITION FOR
// THE CONTRACT
// Christian Ohlsson
// Daniel Alfredsson
// Karlstad 990907
//==================================================================
class Contract {
public:
int *buffer;
int size;
int owners;
Contract() {
size=0;

BIN
Lab2/lab_two Normal file

Binary file not shown.

16
Lab2/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;
}

110
Lab2/matrix.h Normal file
View File

@@ -0,0 +1,110 @@
//##################################################################
// 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 <stdlib.h>
#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);
}
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;
}

13
Lab2/read_me Normal file
View File

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