prak 5
s
// exception classes for various error types
#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
program awal
#include <cstdlib>
#include <iostream>
#include "xcept.h"
using namespace std;
template<class T>
class Array1D
{
// friend ostream& operator<<(ostream&, const Array1D<T>&);
public:
Array1D(int size = 0);
Array1D(const Array1D<T>& v);
~Array1D() {delete[] element;}
T& operator [](int i) const;
int Size(){return size;}
Array1D<T>& operator=(const Array1D<T>& v);
Array1D<T> operator+()const;
Array1D<T> operator+(const Array1D<T>& v)const;
Array1D<T> operator-()const;
Array1D<T> operator-(const Array1D<T>& v)const;
Array1D<T> operator*(const Array1D<T>& v)const;
Array1D<T> operator+=(const T& x);
Array1D<T>& ReSize (int sz);
void geser_kiri();
void geser_kanan();
private:
int size;
T*element;
};
template<class T>
Array1D<T>::Array1D(int sz)
{
if (sz < 0)throw BadInitializers();
size =sz;
element = new T[sz];
}
template<class T>
Array1D<T>::Array1D(const Array1D<T>& v)
{
size = v.size;
element = new T[size];
for (int i = 0; i<size; i++)
element [i]= v.element[i];
}
template<class T>
T& Array1D<T>::operator[](int i) const
{
if (i<0 || i >=size)throw OutOfBounds();
return element[i];
}
template<class T>
Array1D<T>&Array1D<T>::operator=(const Array1D<T>& v)
{
if (this != &v){
size= v.size;
delete[] element;
element = new T[size];
for (int i=0 ;i<size; i++)
element[i]= v.element[i];
}
return*this;
}
template<class T>
Array1D<T>Array1D<T>::operator+(const Array1D<T>& v)const
{
if (size != v.size) throw SizeMismatch();
Array1D<T> w(size);
for (int i =0; i<size;i++)
w.element[i]=element[i]+v.element[i];
return w;
}
template<class T>
Array1D<T>Array1D<T>::operator-(const Array1D<T>& v)const
{
if (size != v.size) throw SizeMismatch();
Array1D<T> w(size);
for (int i =0; i<size;i++)
w.element[i]=element[i]-v.element[i];
return w;
}
template<class T>
Array1D<T>Array1D<T>::operator-()const
{
Array1D<T> w(size);
for (int i =0; i<size;i++)
w.element[i]=-element[i];
return w;
}
template<class T>
Array1D<T>Array1D<T>::operator*(const Array1D<T>& v)const
{
if (size != v.size) throw SizeMismatch();
Array1D<T> w(size);
for (int i =0; i<size;i++)
w.element[i]=element[i]* v.element[i];
return w;
}
//template<class T>
Array1D<T>Array1D<T>::operator+=(const T& x)
{
for (int i =0; i<size; i++)
element[i] + = x;
return* this;
}
//template<class T>
ostream& operator<<(ostream& out, const Array1D<T>& x)
{
for (int i =0; i< x.size; i++)
out<<x.element[i]<<" ";
return out;
}
//template<class T>
Array1D<T>Array1D<T>::ReSize (int sz)
{
if (sz<0) throw BadInitializers();
size = sz;
element = new T[size];
return* this;
}
void main(void)
{
try {
Array1D<int>x(10),y,z;
for(int i=0;i<20;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 incremented 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;
}
Wednesday, 27 October 2010
Wednesday, 20 October 2010
ARRAY STATIS
prak 4
#include <cstdlib>
#include <iostream>
#define maks 5
using namespace std;
class Array1D{
friend ostream& operator <<(ostream&,const Array1D&);
friend istream&operator>>(istream&,Array1D&);
public:
Array1D();
void cetak();
void geser_kiri();
void geser_kanan();
void hapus_elemen();
private:
char A[maks];
};
Array1D::Array1D()
{
for(int i=0; i<maks; i++)
A[i]='0';
}
void Array1D::cetak()
{
for(int i=0; i< maks; i++)
cout <<A[i]<<"";
}
ostream& operator<<(ostream& out,const Array1D&x)
{
for(int i=0;i<maks;i++)
cout<<x.A[i]<<"";
cout<<endl;
return out;
}
istream& operator>>(istream& in,Array1D& x){
for(int i=0;i<5;i++){
cout<<"masukan nilai elemen posisi ke-"<<i+1<<":";
in>>x.A[i];
}
return in;
}
void Array1D::geser_kanan()
{
int n=maks;
int temp=A[n-1];
for(int i=n-1;i>=0;i--)
A[i+1]=A[i];
A[0]=temp;
}
void Array1D::geser_kiri()
{
int n=maks;
int temp=A[0];
for(int i=0;i<n;i++)
A[i]=A[i+1];
A[n-1]=temp;
}
void Array1D::hapus_elemen(){
int posisi;
cout<<"pilih indek berapa yang akan di hapus:";
cin>>posisi;
if(posisi>0 && posisi<=5)
A[posisi-1]='0';
else cout<<"nilai di luar range dari 1-5/n";
}
main(){
Array1D x;
cout<<"Array masih kosong:"<< x;
cin>>x;
cout<<"isi Array saat ini:"<<x;
x.geser_kiri();
cout<<"isi Array setelah digeser ke kiri:"<<x;
x.geser_kanan();
cout<<"isi Array setelah digeser ke kanan:"<<x;
x.hapus_elemen();
cout<<"setelah dihapus:"<<x;
system("PAUSE");
return EXIT_SUCCESS;
}
#include <cstdlib>
#include <iostream>
#define maks 5
using namespace std;
class Array1D{
friend ostream& operator <<(ostream&,const Array1D&);
friend istream&operator>>(istream&,Array1D&);
public:
Array1D();
void cetak();
void geser_kiri();
void geser_kanan();
void hapus_elemen();
private:
char A[maks];
};
Array1D::Array1D()
{
for(int i=0; i<maks; i++)
A[i]='0';
}
void Array1D::cetak()
{
for(int i=0; i< maks; i++)
cout <<A[i]<<"";
}
ostream& operator<<(ostream& out,const Array1D&x)
{
for(int i=0;i<maks;i++)
cout<<x.A[i]<<"";
cout<<endl;
return out;
}
istream& operator>>(istream& in,Array1D& x){
for(int i=0;i<5;i++){
cout<<"masukan nilai elemen posisi ke-"<<i+1<<":";
in>>x.A[i];
}
return in;
}
void Array1D::geser_kanan()
{
int n=maks;
int temp=A[n-1];
for(int i=n-1;i>=0;i--)
A[i+1]=A[i];
A[0]=temp;
}
void Array1D::geser_kiri()
{
int n=maks;
int temp=A[0];
for(int i=0;i<n;i++)
A[i]=A[i+1];
A[n-1]=temp;
}
void Array1D::hapus_elemen(){
int posisi;
cout<<"pilih indek berapa yang akan di hapus:";
cin>>posisi;
if(posisi>0 && posisi<=5)
A[posisi-1]='0';
else cout<<"nilai di luar range dari 1-5/n";
}
main(){
Array1D x;
cout<<"Array masih kosong:"<< x;
cin>>x;
cout<<"isi Array saat ini:"<<x;
x.geser_kiri();
cout<<"isi Array setelah digeser ke kiri:"<<x;
x.geser_kanan();
cout<<"isi Array setelah digeser ke kanan:"<<x;
x.hapus_elemen();
cout<<"setelah dihapus:"<<x;
system("PAUSE");
return EXIT_SUCCESS;
}
Wednesday, 13 October 2010
INHERITANCE DALAM C++
prak 3
#include <cstdlib>
#include <iostream>
using namespace std;
class bilangan{
friend ostream& operator<<(ostream&, const bilangan&);
friend istream& operator>>(istream&, bilangan&);
public:
bilangan(int a0=0, float b0=0.0):a(a0),b(b0){}
void banding_int(const bilangan&, const bilangan&);
bilangan& operator=(const bilangan&);
bilangan operator+(const bilangan&)const;
bilangan operator-()const;
protected:
int a;
float b;
};
ostream& operator<<(ostream& out, const bilangan& x){
out<<"Bagian integer: "<<x.a<<endl;
out<<"Bagian float: "<<x.b<<endl;
return out;
}
void bilangan::banding_int(const bilangan& x, const bilangan& y){
if(x.a>y.a)cout<<x.a<<"::x lebih besar dari"<<y.a<<"::y";
else cout<<x.a<<"::x lebih kecil dari"<<y.a<<"::y";
}
bilangan& bilangan::operator=(const bilangan& x){
a=x.a;
b=x.b;
return *this;
}
istream& operator>>(istream& in, bilangan& x){
cout<<"\nMasukkan bagian integer: ";
in>>x.a;
cout<<"Masukkan bagian float: ";
in>>x.b;
return in;
}
bilangan bilangan::operator+(const bilangan& x)const{
bilangan cc;
cc.a=a+x.a;
cc.b=b+x.b;
return cc;
}
bilangan bilangan::operator-()const{
bilangan x;
x.a=-a;
x.b=-b;
return x;
}
class bil_char:public bilangan{
friend ostream& operator<<(ostream&, const bil_char&);
public:
bil_char(int a0=0, int b0=0,char ch='x'):bilangan(a0,b0),c(ch){}
private:
char c;
};
ostream& operator<<(ostream& out,const bil_char& x){
out<<"Bagian integer: "<<x.a<<endl;
out<<"Bagian float: "<<x.b<<endl;
out<<"Bagian char: "<<x.c<<endl;
return out;
}
int main(int argc, char *argv[])
{
cout<<"AFRIQ YASIN RAMADHAN\n09018222\n\n";
bilangan s,t(-2,3.14),d;
cout<<"Nilai awal s\n"<<s;
cout<<"Nilai awal t dari deklarasi\n"<<t;
s=t;
cout<<"Setelah diassign t\n";
cout<<"Nilai s\n"<<s;
cout<<"Masukkan nilai-nilai objek d";
cin>>d;
cout<<"Setelah d+t=>\n"<<d+t;
cout<<"Nilai d dinegatifkan\n"<<-d;
bil_char ss;
cout<<"Nilai awal ss\n"<<ss;
system("PAUSE");
return EXIT_SUCCESS;
}
#include <cstdlib>
#include <iostream>
using namespace std;
class bilangan{
friend ostream& operator<<(ostream&, const bilangan&);
friend istream& operator>>(istream&, bilangan&);
public:
bilangan(int a0=0, float b0=0.0):a(a0),b(b0){}
void banding_int(const bilangan&, const bilangan&);
bilangan& operator=(const bilangan&);
bilangan operator+(const bilangan&)const;
bilangan operator-()const;
protected:
int a;
float b;
};
ostream& operator<<(ostream& out, const bilangan& x){
out<<"Bagian integer: "<<x.a<<endl;
out<<"Bagian float: "<<x.b<<endl;
return out;
}
void bilangan::banding_int(const bilangan& x, const bilangan& y){
if(x.a>y.a)cout<<x.a<<"::x lebih besar dari"<<y.a<<"::y";
else cout<<x.a<<"::x lebih kecil dari"<<y.a<<"::y";
}
bilangan& bilangan::operator=(const bilangan& x){
a=x.a;
b=x.b;
return *this;
}
istream& operator>>(istream& in, bilangan& x){
cout<<"\nMasukkan bagian integer: ";
in>>x.a;
cout<<"Masukkan bagian float: ";
in>>x.b;
return in;
}
bilangan bilangan::operator+(const bilangan& x)const{
bilangan cc;
cc.a=a+x.a;
cc.b=b+x.b;
return cc;
}
bilangan bilangan::operator-()const{
bilangan x;
x.a=-a;
x.b=-b;
return x;
}
class bil_char:public bilangan{
friend ostream& operator<<(ostream&, const bil_char&);
public:
bil_char(int a0=0, int b0=0,char ch='x'):bilangan(a0,b0),c(ch){}
private:
char c;
};
ostream& operator<<(ostream& out,const bil_char& x){
out<<"Bagian integer: "<<x.a<<endl;
out<<"Bagian float: "<<x.b<<endl;
out<<"Bagian char: "<<x.c<<endl;
return out;
}
int main(int argc, char *argv[])
{
cout<<"AFRIQ YASIN RAMADHAN\n09018222\n\n";
bilangan s,t(-2,3.14),d;
cout<<"Nilai awal s\n"<<s;
cout<<"Nilai awal t dari deklarasi\n"<<t;
s=t;
cout<<"Setelah diassign t\n";
cout<<"Nilai s\n"<<s;
cout<<"Masukkan nilai-nilai objek d";
cin>>d;
cout<<"Setelah d+t=>\n"<<d+t;
cout<<"Nilai d dinegatifkan\n"<<-d;
bil_char ss;
cout<<"Nilai awal ss\n"<<ss;
system("PAUSE");
return EXIT_SUCCESS;
}
Friday, 8 October 2010
KONSTRUKTOR DAN TEMPLATE
prak 2
#include <cstdlib>
#include <iostream>
class Operasi;
using namespace std;
//template <class T>
class Kompleks
{
friend class Operasi;
friend ostream& operator<<(ostream&, const Kompleks&);
friend istream& operator>>(istream&, Kompleks&);
public:
Kompleks(int s=0, int t=0):a(s),b(t){}
void cetak();
Kompleks operator-();
Kompleks operator-(const Kompleks&);
Kompleks operator+(const Kompleks&);
private:
int a;
int b;
};
//template <class T>
void Kompleks::cetak(){
if(b>0) cout << "Bilangan Kompleks : " << a << "+" << b << "i";
else cout << "Bilangan kompleks : " << a << b << "i";
cout << endl;
}
//template <class T>
Kompleks Kompleks::operator-(){
Kompleks x;
x.a=a;
x.b=-b;
return x;
}
//template <class T>
Kompleks Kompleks::operator-(const Kompleks& m){
Kompleks x;
x.a=a-m.a;
x.b=b-m.b;
return x;
}
//template <class T>
Kompleks Kompleks::operator+(const Kompleks& m){
Kompleks x;
x.a=a+m.a;
x.b=b+m.b;
}
//template <class T>
ostream& operator<<(ostream& out, const Kompleks& x){
if(x.b==0) out << '[' << x.a << ']';
else if (x.a==0&&x.b==1) out << '[' << "i" << ']';
else if (x.a==0&&x.b==-1) out << '[' << "-i" << ']';
else if (x.a==0&&x.b>1) out << '[' << x.b << "i" << ']';
else if (x.a==0&&x.b<-1) out << '[' << x.b << "i" << ']';
else if (x.b==1) out << '[' << x.a << "+" << "i" << ']';
else if (x.b>1) out << '[' << x.a << "+" << x.b << "i" << ']';
else if (x.b==-1) out << '[' << x.a << "-i" << ']';
else out << '[' << x.a << x.b << ']';
return out;
}
//template <class T>
istream& operator>>(istream& in, Kompleks& x){
cout << "Masukan bagian real : ";
in >> x.a;
cout << "Masukan bagian imajiner : ";
in >> x.b;
return in;
}
//template <class T>
class Operasi
{
public:
void cetak();
Kompleks jumlah(const Kompleks&, const Kompleks&);
Kompleks kali(const Kompleks&, const Kompleks&);
Kompleks kurang(const Kompleks&, const Kompleks&);
private:
int a;
int b;
};
void Operasi::cetak(){
if(b>0) cout << "Bilangan Kompleks : " << a << "+" << b << "i";
else cout << "Bilangan kompleks : " << a << b << "i";
cout << endl;
}
//template <class T>
Kompleks Operasi::jumlah(const Kompleks& m, const Kompleks& n){
Kompleks temp;
temp.a=m.a+n.a;
temp.b=m.b+n.b;
return temp;
}
//template <class T>
Kompleks Operasi::kurang(const Kompleks& m, const Kompleks& n){
Kompleks temp;
temp.a=m.a-n.a;
temp.b=m.b-n.b;
return temp;
}
//template <class T>
Kompleks Operasi::kali(const Kompleks& m, const Kompleks& n){
Kompleks temp;
temp.a=(m.a*n.a)-(m.b*n.b);
temp.b=(m.a*n.b)-(m.b*n.a);
return temp;
}
int main(int argc, char *argv[])
{
Kompleks x(2, 3), y(4, -4), t;
Operasi z;
cout << "Menggunakan cetak() : "; x.cetak();
cout << "Menggunakan Overloading : " << x;
cout << "Konjugat : " << -x;
y.cetak();
cout << "\nPenjumlahan menggunakan method : ";
t=z.jumlah(x, y);
t.cetak();
cout << "Penjumlahan menggunakan operator : ";
t=x+y;
cout << x << "+" << y << "=" << t;
cout << "\nPerkalian menggunakan method : ";
t=z.kali(x, y);
z.cetak();
cout << "\nPerkalian menggunakan operator : ";
//x*y;
//ut << x << "*" << y << "=" << t;
t-y;
cout << "\n" << x << "-" << y << "=" << t << endl;
Kompleks n;
cin >> n;
cout << n;
system("PAUSE");
return EXIT_SUCCESS;
}
#include <cstdlib>
#include <iostream>
class Operasi;
using namespace std;
//template <class T>
class Kompleks
{
friend class Operasi;
friend ostream& operator<<(ostream&, const Kompleks&);
friend istream& operator>>(istream&, Kompleks&);
public:
Kompleks(int s=0, int t=0):a(s),b(t){}
void cetak();
Kompleks operator-();
Kompleks operator-(const Kompleks&);
Kompleks operator+(const Kompleks&);
private:
int a;
int b;
};
//template <class T>
void Kompleks::cetak(){
if(b>0) cout << "Bilangan Kompleks : " << a << "+" << b << "i";
else cout << "Bilangan kompleks : " << a << b << "i";
cout << endl;
}
//template <class T>
Kompleks Kompleks::operator-(){
Kompleks x;
x.a=a;
x.b=-b;
return x;
}
//template <class T>
Kompleks Kompleks::operator-(const Kompleks& m){
Kompleks x;
x.a=a-m.a;
x.b=b-m.b;
return x;
}
//template <class T>
Kompleks Kompleks::operator+(const Kompleks& m){
Kompleks x;
x.a=a+m.a;
x.b=b+m.b;
}
//template <class T>
ostream& operator<<(ostream& out, const Kompleks& x){
if(x.b==0) out << '[' << x.a << ']';
else if (x.a==0&&x.b==1) out << '[' << "i" << ']';
else if (x.a==0&&x.b==-1) out << '[' << "-i" << ']';
else if (x.a==0&&x.b>1) out << '[' << x.b << "i" << ']';
else if (x.a==0&&x.b<-1) out << '[' << x.b << "i" << ']';
else if (x.b==1) out << '[' << x.a << "+" << "i" << ']';
else if (x.b>1) out << '[' << x.a << "+" << x.b << "i" << ']';
else if (x.b==-1) out << '[' << x.a << "-i" << ']';
else out << '[' << x.a << x.b << ']';
return out;
}
//template <class T>
istream& operator>>(istream& in, Kompleks& x){
cout << "Masukan bagian real : ";
in >> x.a;
cout << "Masukan bagian imajiner : ";
in >> x.b;
return in;
}
//template <class T>
class Operasi
{
public:
void cetak();
Kompleks jumlah(const Kompleks&, const Kompleks&);
Kompleks kali(const Kompleks&, const Kompleks&);
Kompleks kurang(const Kompleks&, const Kompleks&);
private:
int a;
int b;
};
void Operasi::cetak(){
if(b>0) cout << "Bilangan Kompleks : " << a << "+" << b << "i";
else cout << "Bilangan kompleks : " << a << b << "i";
cout << endl;
}
//template <class T>
Kompleks Operasi::jumlah(const Kompleks& m, const Kompleks& n){
Kompleks temp;
temp.a=m.a+n.a;
temp.b=m.b+n.b;
return temp;
}
//template <class T>
Kompleks Operasi::kurang(const Kompleks& m, const Kompleks& n){
Kompleks temp;
temp.a=m.a-n.a;
temp.b=m.b-n.b;
return temp;
}
//template <class T>
Kompleks Operasi::kali(const Kompleks& m, const Kompleks& n){
Kompleks temp;
temp.a=(m.a*n.a)-(m.b*n.b);
temp.b=(m.a*n.b)-(m.b*n.a);
return temp;
}
int main(int argc, char *argv[])
{
Kompleks x(2, 3), y(4, -4), t;
Operasi z;
cout << "Menggunakan cetak() : "; x.cetak();
cout << "Menggunakan Overloading : " << x;
cout << "Konjugat : " << -x;
y.cetak();
cout << "\nPenjumlahan menggunakan method : ";
t=z.jumlah(x, y);
t.cetak();
cout << "Penjumlahan menggunakan operator : ";
t=x+y;
cout << x << "+" << y << "=" << t;
cout << "\nPerkalian menggunakan method : ";
t=z.kali(x, y);
z.cetak();
cout << "\nPerkalian menggunakan operator : ";
//x*y;
//ut << x << "*" << y << "=" << t;
t-y;
cout << "\n" << x << "-" << y << "=" << t << endl;
Kompleks n;
cin >> n;
cout << n;
system("PAUSE");
return EXIT_SUCCESS;
}
Subscribe to:
Posts (Atom)