Mar 9, 2012

Xây dựng lớp date trong C++


Xây dựng lớp date1 có các thành phần sau:


- Các thuộc tính : day, month, year
- Hàm thiết lập có tham số mặc định
- Hàm kiểm tra năm nhuận
- Hàm kiểm tra ngày cuối tháng
- Nạp chồng toán tử tăng (++)
- Nạp chồng toán tử (+ =)
- Nạp chồng toán tử xuất (<<)


Viết chương trình kiểm tra


#include <iostream.h>
#include <conio.h>
class date1
{
  private:
    int day;
    int month;
    int year;
    static const int days[];
    void helpIncrement();
  public:
    date1(int d=1,int m=1,int y=1900);
    void setdate(int d,int m,int y);
    date1 &operator++();
    date1 &operator+=(int addday);
    bool leapyear(int y);//kiem tra nam nhuan
    bool endofmonth(int d);//kiem tra ngay cuoi thang
    friend ostream & operator<<(ostream & out,date1 &date);

};

const int date1::days[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

date1::date1(int d,int m,int y)
{
   setdate(d,m,y);
}

void date1::setdate(int d,int m,int y)
{
    month=(m>=1 && m<=12)? m:1;
    year=(y>=1900 && y<=2100)?y:1900;
      if( month==2 && leapyear(y))
        day=(d>=1 && d<=29)? d:1;
      else
        day=(d>=1 && d<=days[month])? d:1;
}

bool date1::leapyear(int y)
{
       if ((y%400 ==0)||(y%100 !=0 && y%4==0))
           return true;
       else
           return false;
}

bool date1::endofmonth(int d)
{
     if(month==2 && leapyear(year))
       return d==29;
     else
       return d==days[month];
}

void date1::helpIncrement()
{
    if(month==2 && leapyear(year))//ngay cuoi nam
       {
          day=1;
          month=1;
          ++year;
       }
   else
     if(endofmonth(day))//ngay cuoi thang
       {
        day=1;
        ++month;
       }
     else
       ++day;
}

date1 &date1::operator++()
{
   helpIncrement();
   return * this;
}

date1 & date1::operator+=(int addday)
{
  for(int i=0;i<addday;i++)
    {
       helpIncrement();
    }
  return *this;
}

ostream & operator<<(ostream & out,date1 & date)
{
   char * monthname[13]={" " , "January","February","Match","April","May",
   "June","July","August","September","October","November","December"};

   out<< date.day <<"/"<<monthname[date.month]<<" / " <<date.year<<endl;
   return out;
}
void main()
 {
    date1 d1,d(23,5,2006);
    cout<<d1;
    cout<<d;
    cout<<d++;
    cout<<d;
    d+=2;
    cout<<d;
    getch();
 }

Xây dựng lớp complex có dạng real+ image*i trong C++

Tạo lớp complex (real + image *i ) có các thành phần sau:
- Các thuộc tính : real , image;
- Hàm tạo có sử dụng tham số mặc định
- Nạp chồng các toán tử sau:
+ Toán tử cộng (+) 
+ Toán tử cộng thành phần real của số phức b lên x (b.real + x)
+ Toán tử trừ (-)
+ Toán tử nhập (>>)
+ Toán tử xuất (<<)

#include <iostream.h>
#include <conio.h>
class complex
{
  private:
    float real,image;
  public:
    complex(float r=0,float i=0);
    complex operator+(complex c);
    complex operator-(complex c);
    friend complex operator+(float x,complex c);
    friend ostream& operator<<(ostream &out,complex c);
    friend istream& operator>>(istream &inp,complex& c);
};
 
complex::complex(float r,float i)
{
   real=r;
   image=i;
}
 
complex complex::operator+(complex c)
{
    complex temp;
    temp.real=real+c.real;
    temp.image=image+c.image;
    return temp;
}
 
complex complex::operator-(complex c)
{
   complex temp;
   temp.real=real-c.real;
   temp.image=image-c.image;
   return temp;
}
 
complex operator+(float x, complex c)
{
   complex temp;
   temp.real=c.real+x;
   temp.image=c.image;
   return temp;
}
 
ostream& operator<<(ostream &out,complex c)
{
   cout<<c.real<<"+"<<c.image<<"i"<<endl;
   return out;
}
 
istream& operator>>(istream &inp,complex &c)
{
  cout<<" Nhap vao phan thuc:";
  cin>>c.real;
  cout<<" Nhap vao phan ao:";
  cin>>c.image;
  return inp;
}
 
void main()
{
    complex c,c1,c2,c3;
    float x;
    cin>>c;
    cin>>c1;
    c2=c+c1;
    cout<<" Cong hai so phuc :"<<c2;
    c3=c1-c;
    cout<<" Tru hai so phuc :"<<c3;
    cout<<" Nhap vao x :";
    cin>>x;
    c3=c3+x;
    cout<< c3;
    getch();
}

Xây dựng lớp Phânsố trong C++


Tạo lớp Phân số có các thành phần sau:
- Các thuộc tính: ts,ms;
- Hàm tạo có sử dụng tham số mặc định
- Nạp chồng các toán tử sau:
+ Toán tử cộng (+)
+ Toán tử trừ (-)
+ Toán tử nhân ( * )
+ Toán tử chia (/)
+ Toán tử nhập (>>)
+ Toán tử xuất (<<)


#include "iostream.h"
#include "conio.h"
#include "math.h"
class PS
{
     int t,m;
     public:
     friend ostream& operator<<(ostream& os, PS p);
     friend istream& operator>>(istream& is, PS &p);
     friend PS rutgon(PS p);
     PS operator+(PS p);
     PS operator-(PS p);
     PS operator*(PS p);
     PS operator/(PS p);
};

int USCLN(int x,int y)
{
   if(x==0)
     return y;
   if (y==0)
       return x;
   while (x!=y)
      {
        if(x>y)
         x-=y;
        else
         y-=x;
      }
}

ostream& operator<<(ostream& os, PS p)
{
           os<<p.t<</<<p.m<<endl;
           return os;
}

istream& operator>>(istream& is, PS &p)
{
      is>>p.t>>p.m;
      return is;
}

PS rutgon(PS p)
{
      PS q;
      int x;
      x=uscln(p.t,p.m);
      q.t=p.t/x;
      q.m=p.m/x;
       return q;
}

PS PS::operator+(PS p)
{
      PS q;
      q.t=t*p.m+p.t*m;
      q.m=m*p.m;
      return rutgon(q);
}

PS PS::operator-(PS p)
{
    PS q;
   q.t=t*p.m-p.t*m;
   q.m=m*p.m;
   return rutgon(q);
}

PS PS::operator*(PS p)
{
    PS q;
    q.t=t*p.t;
    q.m=m*p.m;
    return rutgon(q);
}

PS PS::operator/(PS p)
{
    PS q;
    q.t=t*p.m;
    q.m=m*p.t;
    return rutgon(q);
}

void main()
{
     PS p1,p2,p;
    cout<<"Nhap p1=";cin>>p1;
    cout<<"Nhap p2=";cin>>p2;
    p=p1+p2;
   cout<<"p="<<p<<endl;
   getch();
}

Xây dựng lớp Nhanvien trong C+

Xây dựng lớp nhanvien có các thành phần sau:
- Các thuộc tính: tendem, ho, ngaysinh, ngayvaolam. Chú ý : sử dụng con trỏ char cho tendem, ho và ngaysinh, ngayvaolam có kiểu lớp date1 vừa xây dựng ở bài tập trước.
- Hàm tạo
- Hàm trả về tendem
- Hàm trả về ho
- Hàm huỷ
- Hàm hiển thị

File employee.h

#ifndef employee1_h
#define employee1_h
#include "date1.h"
class employee1
{
  private :
    char * firstname;
    char * lastname;
    date1 * birthdate;
    date1 * hiredate;
  public:
    employee1(char *,char *,int,int,int,int,int,int);
    void print();
    char *get_firstname();
    char *get_lastname();
    ~employee1();
};
#endif

File employee.cpp

#include <iostream.h>
#include <conio.h>
#include "employee1.h"
#include <string.h>
#include "date1.h"
 
employee1::employee1(char * fn,char *ln,int bd,int bm,int by,int hd,int hm,int hy)
{
  firstname=new char[strlen(fn)+1];
  strcpy(firstname,fn);
 
  lastname=new char[strlen(fn)+1];
  strcpy(lastname,ln);
  birthdate=new date1(bd,bm,by);
  hiredate=new date1(hd,hm,hy);
 
}
char *employee1::get_firstname()
{
    return firstname;
}
 
char *employee1::get_lastname()
{
    return lastname;
}
 
employee1::~employee1()
{
  cout<<"ham huy duoc goi!"<<endl;
  delete firstname;
  delete lastname;
  delete birthdate;
  delete hiredate;
  getch();
}
 
void employee1::print()
{
  cout << " Ho va ten la:"<<lastname<<"  " << firstname <<endl;
  cout<<" Ngay sinh la:";
   birthdate->print();
  cout<<" Ngay dau di lam la:";
  hiredate->print();
}
 
void main()
{
  employee1 e(" Van Dong "," Pham ",3,4,1983,4,3,2004);
  e.print();
  employee1 * e1=new employee1(" Van A ", " Nguyen ",20,4,1988,7,9,2006);
  cout<<"Ten la:"<<e1->get_firstname()<<endl;
  delete e1;
  getch();
}

Xây dựng lớp point trong C++


Tạo lớp điểm (point) có thuộc tính là các toạ độ x, y.
Xây dựng các hàm cần thiết như nhập và hiển thị các toạ độ, tính khoảng cách giữa hai điểm.
Viết chương trình nhập vào n điểm từ bàn phím.Tìm và hiển thị khoảng cách lớn nhất giữa hai điểm trong n điểm đó.

Xem thêm: http://kenhdaihoc.com/forum/showthread.php?t=2813


Tạo lớp điểm (point) có thuộc tính là các toạ độ x, y.
Xây dựng các hàm cần thiết như nhập và hiển thị các toạ độ, tính khoảng cách giữa hai điểm.
Viết chương trình nhập vào n điểm từ bàn phím.Tìm và hiển thị khoảng cách lớn nhất giữa hai điểm trong n điểm đó.


#include "conio.h"
#include "math.h"
class point
{
    int x,y;
    public:
    void nhap(){
    cout<<"x=";cin>>x;
    cout<<"y=";cin>>y;
}
void in()
{
   cout<<" x= "<<x<<" y= "<<y;
}
double khoangcach(point d)
{
    return sqrt(pow((x-d.x),2)+pow((y-d.y),2));
}
};
void main()
{
      point a[10];
      int i,j,n,imax, jmax;
      double max;
      cout<<" Nhap so diem ";cin>>n;
      for(i=0;i<n;++i) a[i].nhap();
      max=0;
      for(i=0;i<n-1;++i)
     for(j=i+1;j<n;++j)
          if(a[i].khoangcach(a[j])>max)
         {
            max=a[i].khoangcach(a[j]);
            imax=i; jmax=j;
         }
    cout<<"Khoang cach max:"<<max<<endl;
    cout<<"imax="<<(imax+1)<<" jmax="<<(jmax+1);
 getch();
}

Xây dựng lớp vector sử dụng hàm tạo và hàm huỷ trong C++


Tạo một lớp vector gồm có các thành phần sau:
- Các thuộc tính : float * v; int n
- Hàm thiết lập không tham số
- Hàm thiết lập một tham số
- Hàm thiết lập hai tham số
- Hàm hiển thị
- Hàm huỷ
Viết một chương trình kiểm tra.

#include <iostream.h>
#include <conio.h>
class vector
{
   private:
     int n;
     float *v;
   public:
     vector();//ham thiet lap khong tham so
     vector(int size);//ham thiet lap mot tham so
     vector(int size,float * a);//ham thiet lap hai tham so
     void display();
     ~vector();//ham huy
};

vector::vector()
{
   cout<<" Su dung ham thiet lap khong tham so :"<<endl;
   cout<<" Tao mot doi tuong lop vecto co dia chi tai:"<<this<<endl;
   cout<<" Nhap vao so toa do :";
   cin>>n;
   v=new float[n];
   cout<<" Xin cap phat mot vung bo nho cho "<<n<<" so thuc "<<" tai dia chi la: "<<v<<endl;
   for(int i=0;i<n;i++)
    {
      cout<<" Toa do thu "<<(i+1)<<":";
      cin>>v[i];
    }
}
 vector::vector(int size)
{  cout<<" Su dung ham thiet lap mot tham so :"<<endl;
   cout<<" Tao mot doi tuong lop vecto co dia chi tai:"<<this<<endl;
   n=size;
   v=new float[n];
   cout<<" Xin cap phat mot vung bo nho cho "<<n<<"so thuc"<<" tai dia chi la:"<<v<<endl;
   for(int i=0;i<n;i++)
    {
      cout<<" Toa do thu "<<(i+1)<<":";
      cin>>v[i];
    }
}

vector::vector(int size,float * a)
{
   cout<<" Su dung ham thiet lap hai tham so :"<<endl;
   cout<<" Tao mot doi tuong lop vecto co dia chi tai:"<<this<<endl;
   n=size;
   v=new float[n];
   cout<<" Xin cap phat mot vung bo nho cho "<<n<<" so thuc"<<" tai dia chi la:"<<v<<endl;
   for(int i=0;i<n;i++)
      v[i]=a[i];
}
vector::~vector()
{
   cout<<" Giai phong "<<v<<" cua doi tuong co dia chi tai "<<this<<endl;
   delete v;
   getch();
}
void vector::display()
{
  cout<<" Hien thi doi tuong co dia chi tai:"<<this<<endl;
  for(int i=0;i<n;i++)
     cout<<"  " <<v[i]<<"  ";
     cout<<endl;
}
 void main()
{
     vector s;
     s.display();
     vector s1(5);
     s1.display();
     float a[5]={1,2,3,4,5};
     vector s2(5,a);
     s2.display();
     getch();
}

Xây dựng lớp tamgiác trong C++

Xây dựng một lớp tamgiac có các thành phần sau:
- Các thuộc tính là các cạnh a, b, c
- Các hàm thành phần bao gồm:
+ Hàm nhập giá trị cho các cạnh (Kiểm tra tính hợp lệ đảm bảo là 3 cạnh của một tam giác)
+ Hàm tính diện tích tam giác
+ Hàm kiểm tra tam giác(đều, vuông cân, cân, vuông, thường)
+ Hàm hiển thị thông tin( diện tích, tính chất tam giác)
Viết một chương trình kiểm tra.
Trích từ: http://kenhdaihoc.com/forum/showthread.php?t=2811

#include <iostream.h>
#include <conio.h>
#include <math.h>
class tamgiac
{
   private:
     int a,b,c;
      float dientich();
     int kttamgiac();
   public:
     void nhap();
     void in();
};
void tamgiac::nhap()
{
   do
    {
       cout<<"Nhap canh a:";cin>>a;
       cout<<"Nhap canh b:";cin>>b;
         cout<<"Nhap canh c:";cin>>c;
    }while(a+b<c||a+c<b||b+c<a);
}
float tamgiac::dientich()
{
         float p;
            p=(a+b+c)/2;
         return (sqrt(p*(p-a)*(p-b)*(p-c)));
}
 int tamgiac::kttamgiac()
 {
   if(a==b||b==c||c==a)
         if(a==b&&b==c)
          return 1;
       else
            if(a*a==b*b+c*c||b*b==a*a+c*c||c*c==a*a+b*b)
           return 2;
         else
              return 3;
  else
     if(a*a==b*b+c*c||b*b==a*a+c*c||c*c==a*a+b*b)
              return 4;
     else
        return 5;
 }
 void tamgiac::in()
 {
    cout<<"Dien tich tam giac la:"<<dientich()<<endl;
   int kt=kttamgiac();
   switch(kt)
     {
        case 1:
              cout<<" Tam giac deu"<<endl;
              break;


        case 2:
               cout<<" Tam giac vuong can"<<endl;
               break;
        case 3:
                    cout<<" Tam giac can "<<endl;
               break;
        case 4:
               cout<<" Tam giac vuong"<<endl;
               break;
         case 5:
                    cout<<" Tam giac thuong"<<endl;
               break;
    }
 
}
 void main()
 {
    tamgiac a;
    a.nhap();
     a.in();
    getch();
 }