2009-08-10

Обьект хандлагат програмчлал

#include 
#include 

using namespace std;

class Fraction {
 unsigned int num;
 unsigned int denom;
 public:
  Fraction() { }
  Fraction(unsigned int , unsigned int );
  Fraction(const Fraction & );
  bool operator==(const Fraction & z) {
   return (z.num == num && z.denom == denom);
  }
  bool operator<(const Fraction & z) const {
   if ((float)num/denom < (float)z.num/z.denom)
    return true;
   else
    return false;
  }
  void setDenom(int new_denom) {
   denom = new_denom;
  }
  unsigned int getNum() const {
   return num;
  }
  unsigned int getDenom() const {
   return denom;
  }
};

Fraction::Fraction(unsigned int new_num, unsigned int new_denom) {
 num = new_num;
 denom = new_denom;
}

Fraction::Fraction(const Fraction & z) {
 num = z.num;
 denom = z.denom;
}

template
class MyArray {
 int mysize;
    Type * content;
    public:
        MyArray(int);
  MyArray(const MyArray & z) {
   mysize = z.mysize;
   content = z.content;
  }
  Type & operator[](int i)  {
   if (i < 0) throw string("index out of bounds");
   else if (i > mysize) throw string("index out of bounds");
   return content[i];
  }
  const Type & operator[](int i) const {
   if (i < 0) throw string("index out of bounds");
   else if (i > mysize) throw string("index out of bounds");
   return content[i];
  }
  bool contains(Type elem) const {
   int i;
   for (i=0; i < mysize; i++) {
    if(content[i] == elem) return true;
   }
   return false;
  }
  const Type & operator!() const {
   int i=0;
   for (int j=1 ; j <= mysize; j++)
    if (content[i] < content[j]) i=j;
   return content[i];
  }
};

template
MyArray::MyArray(int size) {
 mysize = size;
    content = new Type[size];
}

ostream& operator <<(ostream& out, const Fraction& z)  // Overloading <<
{
 out << "( " << z.getNum() << "/" << z.getDenom() << " )";
 return out;
};
int main(int argc, int ** argv) {
    int i;
 MyArray m1(5); // creates an empty 5-element-integer array inside the object m1;
 MyArray m2(3); // creates an empty 3-element-integer array inside the object m2;
 for (int i = 0; i <= 5; i++ ){
  try{
   m1[i] = i;
  }
  catch(const string & err_msg){ // exception handler
   cout << err_msg << endl; //writes "index out of bounds"
  }
 }

 MyArray m3 = m2 = m1;

 for (i = 0; i <= 5; i++ ){
  try{
   cout << m3[i] << " ";
  }
  catch(const string & err_msg){ // exception handler
   cout << err_msg << endl; //writes "index out of bounds"
  }
 }

 if (m1.contains(3))
  cout << "Element 3 is contained in the array" << endl;
 else
  cout << "Element 3 is not contained in the array" << endl;

 cout << "The largest element in the array: " << !m1 << endl ;

 MyArray m4(3); // An array with two empty spaces

 Fraction cObj1(3, 5); // A Fraction object with an unsigned numerator and unsigned denominator
 Fraction cObj2 = cObj1;
 Fraction cObj3 (3,4);
 cObj2.setDenom(7); // sets the denomenator of the Fraction object as 7

 try {
  m4[0] = cObj1;
  m4[1] = cObj2;
  m4[2] = cObj3;
 }
 catch(const string & err_msg){ // exception handler
  cout << err_msg << endl; //writes "index out of bounds"
 }

 for (i = 0; i < 3; i++ ){ // NOTE: burada yanlislikla i<=3 yazildigini sanip degistirdim
  try{
   cout << m4[i] << " ";
  }
  catch(const string & err_msg){ // exception handler
   cout << err_msg << endl; //writes "index out of bounds"
  }
 }
 if (m4.contains(Fraction(3,7)))
  cout << "The element is contained in the array" << endl;
 else
  cout << "The element is not contained in the array" << endl;

 cout << "The largest element in the array: " << !m4 << endl ;

    return 0;
}

1 comment:

  1. Дагвадоржоо чи Embedded Firebird database ийн талаар мэдэх үү. Би 2 application ээс 1 local database тай ажиллах гэсэн чадахгүй байна. Санаа байвал chimuud_kh@yahoo.com оор майл бичнэ үү? Thanks.

    Jaki

    ReplyDelete