first commit
This commit is contained in:
118
Lab5/array.h
Normal file
118
Lab5/array.h
Normal 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(); }
|
||||
|
||||
44
Lab5/except.h
Normal file
44
Lab5/except.h
Normal 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;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
112
Lab5/lab5.dsp
Normal file
112
Lab5/lab5.dsp
Normal 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
Lab5/lab5.dsw
Normal file
29
Lab5/lab5.dsw
Normal 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
Lab5/matrix.cpp
Normal file
24
Lab5/matrix.cpp
Normal 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
|
||||
|
||||
120
Lab5/matrix.h
Normal file
120
Lab5/matrix.h
Normal 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++)
|
||||
|
||||
Reference in New Issue
Block a user