first commit

This commit is contained in:
crille committed 2026-03-05 13:16:26 +01:00
commit 1b2bf174e8
164 files changed
+35594

No files matched your search

+118
View File
@@ -0,0 +1,118 @@
//##################################################################␍
// LAB 5␍
// ADVANCED PROGRAMMING IN C++␍
// MATRIX/ARRAY WITH TRY-CATCH␍
//==================================================================␍
// array.h␍
// CLASS IMPLEMENTATION AND DEFINITION FOR␍
// THE ARRAY␍
// Christian Ohlsson␍
// Daniel Alfredsson␍
// Karlstad 990927␍
//==================================================================␍
␍
#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) { ␍
try {␍
buffer = new int[s];␍
}catch(MemoryError &e){ e.Print(); }␍
size= s;␍
}␍
//################################################################␍
// Constructor␍
// Copys an array␍
//================================================================␍
Array::Array(const Array &src) {␍
size = src.size;␍
try {␍
buffer = new int[size];␍
}catch(MemoryError &e){ e.Print(); }␍
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;␍
try {␍
buffer = new int[size];␍
}catch(MemoryError &e){ e.Print(); }␍
␍
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{␍
if (index<0 || index >= size)␍
throw RangeError();␍
return buffer[index];␍
}␍
//################################################################␍
// Operator overload␍
// Allows to add a Array to another␍
//================================================================␍
Array Array::operator + (const Array &src) {␍
if(src.size != getSize())␍
throw SizeError();␍
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;␍
}␍
␍
␍
+44
View File
@@ -0,0 +1,44 @@
//##################################################################␍
// LAB 5␍
// ADVANCED PROGRAMMING IN C++␍
// MATRIX/ARRAY WITH TRY-CATCH␍
//==================================================================␍
// except.h␍
// CLASS IMPLEMENTATION AND DEFINITION FOR␍
// ALL ERRORS␍
// Christian Ohlsson␍
// Daniel Alfredsson␍
// Karlstad 990927␍
//==================================================================␍
␍
#ifndef _EXCEPT_H_␍
#define _EXCEPT_H_␍
#include <iostream.h>␍
␍
class Error{␍
public:␍
virtual void Print() = 0;␍
};␍
␍
class RangeError:public Error{␍
public:␍
RangeError() {};␍
void Print() { cout<<"Index out of range.\n"; }␍
␍
};␍
␍
class MemoryError:public Error{␍
public:␍
MemoryError() {};␍
void Print() { cout<<"Could not create object.\n"; }␍
␍
};␍
␍
class SizeError:public Error{␍
public:␍
SizeError() {};␍
void Print() { cout<<"Size doesnt match.\n"; }␍
␍
};␍
␍
#endif
+112
View File
@@ -0,0 +1,112 @@
# Microsoft Developer Studio Project File - Name="Lab5" - Package Owner=<4>
# Microsoft Developer Studio Generated Build File, Format Version 6.00
# ** DO NOT EDIT **
# TARGTYPE "Win32 (x86) Console Application" 0x0103
CFG=Lab5 - Win32 Debug
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
!MESSAGE use the Export Makefile command and run
!MESSAGE
!MESSAGE NMAKE /f "Lab5.mak".
!MESSAGE
!MESSAGE You can specify a configuration when running NMAKE
!MESSAGE by defining the macro CFG on the command line. For example:
!MESSAGE
!MESSAGE NMAKE /f "Lab5.mak" CFG="Lab5 - Win32 Debug"
!MESSAGE
!MESSAGE Possible choices for configuration are:
!MESSAGE
!MESSAGE "Lab5 - Win32 Release" (based on "Win32 (x86) Console Application")
!MESSAGE "Lab5 - Win32 Debug" (based on "Win32 (x86) Console Application")
!MESSAGE
# Begin Project
# PROP AllowPerConfigDependencies 0
# PROP Scc_ProjName ""
# PROP Scc_LocalPath ""
CPP=cl.exe
RSC=rc.exe
!IF "$(CFG)" == "Lab5 - Win32 Release"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 0
# PROP BASE Output_Dir "Release"
# PROP BASE Intermediate_Dir "Release"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 0
# PROP Output_Dir "Release"
# PROP Intermediate_Dir "Release"
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD BASE RSC /l 0x41d /d "NDEBUG"
# ADD RSC /l 0x41d /d "NDEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
!ELSEIF "$(CFG)" == "Lab5 - Win32 Debug"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 1
# PROP BASE Output_Dir "Debug"
# PROP BASE Intermediate_Dir "Debug"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 1
# PROP Output_Dir "Debug"
# PROP Intermediate_Dir "Debug"
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD BASE RSC /l 0x41d /d "_DEBUG"
# ADD RSC /l 0x41d /d "_DEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
!ENDIF
# Begin Target
# Name "Lab5 - Win32 Release"
# Name "Lab5 - Win32 Debug"
# Begin Group "Source Files"
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
# Begin Source File
SOURCE=.\matrix.cpp
# End Source File
# End Group
# Begin Group "Header Files"
# PROP Default_Filter "h;hpp;hxx;hm;inl"
# Begin Source File
SOURCE=.\array.h
# End Source File
# Begin Source File
SOURCE=.\except.h
# End Source File
# Begin Source File
SOURCE=.\matrix.h
# End Source File
# End Group
# Begin Group "Resource Files"
# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
# End Group
# End Target
# End Project
+29
View File
@@ -0,0 +1,29 @@
Microsoft Developer Studio Workspace File, Format Version 6.00
# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!
###############################################################################
Project: "Lab5"=".\Lab5.dsp" - Package Owner=<4>
Package=<5>
{{{
}}}
Package=<4>
{{{
}}}
###############################################################################
Global:
Package=<5>
{{{
}}}
Package=<3>
{{{
}}}
###############################################################################
+24
View File
@@ -0,0 +1,24 @@
//matrix.cpp␍
#include "matrix.h"␍
int main() {␍
Array a(2); //Sätt till 3 om du vill ha SizeError␍
Matrix b (2,2);␍
cout << "input Array: " << endl;␍
cin>>a;␍
cout << "input Matrix: " << endl;␍
cin >>b;␍
␍
try {␍
// b[2] = a; //Ta med om du vill ha RangeError␍
b[1] = a + b[1];␍
} catch(Error &e) { e.Print(); }␍
␍
Matrix c(b);␍
␍
try {␍
b[1][1] = a[1];␍
} catch(Error &e) { e.Print(); }␍
␍
cout << "c:\n" <<c << "a:\n"<< a << "\nb:\n" << b;␍
return 0;␍
}␍
+120
View File
@@ -0,0 +1,120 @@
//##################################################################␍
// LAB 5␍
// ADVANCED PROGRAMMING IN C++␍
// MATRIX/ARRAY WITH TRY-CATCH␍
//==================================================================␍
// matrix.h␍
// CLASS IMPLEMENTATION AND DEFINITION FOR␍
// THE MATRIX␍
// Christian Ohlsson␍
// Daniel Alfredsson␍
// Karlstad 990907␍
//==================================================================␍
␍
#include <stdlib.h>␍
#include "except.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) {␍
try {␍
myMatrix = new Array[r];␍
}catch(MemoryError &e){ e.Print(); }␍
␍
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;␍
try {␍
myMatrix = new Array[rows];␍
}catch(MemoryError &e){ e.Print(); }␍
␍
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;␍
try {␍
myMatrix = new Array[rows];␍
}catch(MemoryError &e){ e.Print(); }␍
␍
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) ␍
throw RangeError();␍
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;␍
}␍
␍
␍
␍
␍