Praktikum 5


Berikut ini hasil program dari praktikum 5,, sebenernya programnya udah di edit dirumah berhubungan pada waktu praktikum tdak bs di kompile (program blum jadi),, 😀 tp mga aja gak terjadi eror waktu posting,, tapi kalo masih ada yang salah mohon dikoreksi yaa,,

Berikut programnya,

array xcept,

#ifndef __xcept
#define __xcept
//#include <except.h>
//#include <new.h>
// bad initializers
class BadInitializers {
public:
BadInitializers() {}
};
// insufficient memory
class NoMem {
public:
NoMem() {}
};
// change new to throw NoMem instead of xalloc
void my_new_handler()
{
throw NoMem();
};
//new_handler Old_Handler_ = set_new_handler(my_new_handler);
// improper array, find, insert, or delete index
// or deletion from empty structure
class OutOfBounds {
public:
OutOfBounds() {}
};
// use when operands should have matching size
class SizeMismatch {
public:
SizeMismatch() {}
};
// use when zero was expected
class MustBeZero {
public:
MustBeZero() {}
};
// use when zero was expected
class BadInput {
public:
BadInput() {}
};
#endif

array 1D,

#ifndef __array1d
#define __array1d
using namespace std;
class Array1D{
friend ostream& operator << (ostream&, const Array1D&);
public:
Array1D(int size=0);
Array1D(const Array1D& v);
~Array1D(){delete [] element;}
int& operator[](int i)const;
int Size(){return size;}
Array1D& operator=(const Array1D& v);
Array1D operator+()const;
Array1D operator+(const Array1D& v)const;
Array1D operator-()const;
Array1D operator-(const Array1D& v)const;
Array1D operator*(const Array1D& v)const;
Array1D& operator+=(const int& x);
Array1D& Resize(int sz);
private:
int size;
int * element;
};
Array1D::Array1D(int sz){
if(sz<0)throw BadInitializers();
size=sz;
element=new int[sz];
}
Array1D::Array1D(const Array1D& v){
size=v.size;
element=new int[size];
for(int i=0; i<size; i++)
element[i]=element[i];
}
int& Array1D::operator[](int i)const{
if(i<0||i>=size)throw OutOfBounds();
return element[i];
}
Array1D& Array1D::operator=(const Array1D& v){
if(this !=&v){
size=v.size;
delete[]element;
element=new int[size];
for(int i=0;i<size;i++)
element[i]=element[i];
}
return * this;
}
Array1D Array1D::operator+(const Array1D& v)const{
if (size != v.size) throw SizeMismatch();
Array1D w(size);
for(int i=0; i<size; i++)
w.element[i]=element[i]+v.element[i];
return w;
}
Array1D Array1D::operator-(const Array1D& v)const{
if(size != v.size) throw SizeMismatch();
Array1D w(size);
for(int i=0; i<size; i++)
w.element[i]=element[i]-v.element[i];
return w;
}
Array1D Array1D::operator-()const{
Array1D w(size);
for(int i=0; i<size; i++)
w.element[i]=-element[i];
return w;
}
Array1D Array1D::operator*(const Array1D& v)const{
if(size != v.size) throw SizeMismatch();
Array1D w(size);
for(int i=0; i<size; i++)
w.element[i]=element[i]=v.element[i];
return w;
}
Array1D& Array1D::operator+=(const int& x){
for(int i=0; i<size;i++)
element[i]+=x;
return* this;
}
ostream& operator<<(ostream& out, const Array1D& x){
for(int i=0; i<x.size; i++)
out << x.element[i] << ” “;
return out;
}
Array1D& Array1D::Resize(int sz){
if(sz<0) throw BadInitializers();
delete[]element;
size=sz;
element=new int[size];
return* this;
}
#endif

untuk mainnya,

#include <cstdlib>
#include <iostream>
#include “xcept.h”
#include “array1d.h”
using namespace std;
int main(int argc, char *argv[])
{
try{
Array1D X(10), Y, Z;
for(int i=0; i<10; i++)
X[i]=i;
cout << “X[3] = ” << X[3] << endl;
cout << “X is ” << X << endl;
Y=X;
cout << “Y is ” << Y << endl;
X += 2;
cout << “X increment by 2 is ” << X << endl;
Z=(Y+X)*Y;
cout << “(Y+X)*Y is ” << Z << endl;
cout << “-(Y+X)*Y is ” << -Z << endl;
}
catch(…){
cerr << “An Exception has occured” << endl;}
system(“PAUSE”);
return EXIT_SUCCESS;
}

Demikian programnya,smoga bermanfaat,, ^_^

Tinggalkan komentar