Monday, June 20, 2016

Lập trình hướng đối tượng với C++ (Giải đề thi ngày 21/06/2016)

//Họ và tên: Trần Văn Linh

//Msv: 581597

//Lớp: K58QLTT

//Đề 29 Bài 2:Nhập vào 1 dãy n số nguyên, lưu trữ trong một hàng đợi. Đưa ra dãy số nguyên ra màn hình theo đúng thứ tự đã nhập. Tính tổng các số nguyên dương. Yêu cầu trong chương trình có sử dụng hàng đợi lưu trữ bằng danh sách liên kết đơn


#include<iostream>
using namespace std;
//khai bao lop
class DSLKD
{
          private:
                   struct node
                   {
                             int info;
                             node *link;
                   } *F,*R;
          public:
                   DSLKD();
                   ~DSLKD();
                   void cqinsert(int x);
                   int cqdelete();
                   int empty();
};
//==chuong trinh chinh
int main()
{
          DSLKD ds;
          int n;
          cout<<"Nhap so luong so nguyen: ";
          cin>>n;
          int x;
          for(int i=0;i<n;i++)
          {
                   cout<<"Nhap so nguyen: ";
                   cin>>x;
                   if(x>0)
                             ds.cqinsert(x);
          }
          int tong=0;
          cout<<"Cac so nguyen duong da nhap: ";
          while(!ds.empty())
          {
                   int tg=ds.cqdelete();
                   tong=tong+tg;
                   cout<<tg<<"  ";
          }
          cout<<"\nTong cac so nguyen duong da nhap: "<<tong<<endl;
          return 0;
}
//==dinh nghia ham
DSLKD::DSLKD():F(NULL),R(NULL)
{
                  
}
DSLKD::~DSLKD()
{
          node *P;
          if(F==R)
          {
                   delete F;
                   delete R;
          }
          while(F)
          {
                   P=F;
                   F=F->link;
                   delete P;
          }                 
}
//--------------
void DSLKD::cqinsert(int x)
{
          node *N;
          N=new node;
          N->info=x;
          N->link=NULL;
          if(R==NULL)
                   R=F=N;
          else
          {
                   R->link=N;
                   R=N;
          }
}
//--------------
int DSLKD::cqdelete()
{
          if(F==NULL)
          {
                   return 0;
          }
          int y=F->info;
          node *P;
          P=F;
          F=F->link;
          delete P;
          return y;
}
//--------------
int DSLKD::empty()
{
          if(F==NULL)
                   return 1;
          return 0;
}

//--------------
Kết quả khi chạy thử :



Lập trình hướng đỗi tượng với C++ (Giải đề thi ngày 21/06/2016)

//Họ và Tên: Trần Văn Linh
//Msv:581597
//Lớp: K58QLTT
//Đề 29 Bài 1 : Nhập vào n nhân sự trong đó có cả giảng viên và sinh viên, giảng viên có tên (không có họ đệm) và số bài báo ,sinh viên có điểm TBC . Đưa ra thông tin của các nhân sự kèm theo đánh giá: giảng viên  là giỏi nếu có số bài báo >=10, sinh viên đánh giá là giỏi nếu có điểm TBC>=8.0 . Yêu cầu trong chương trình cài đặt đa hình động cho hàm dữ liệu và hàm đưa ra thông tin.


#include<iostream>
#include<stdio.h>
using namespace std;
//khai bao lop
class nhansu
{
          private:
                   char ten[10];
          public:
                   virtual void  nhap();
                   virtual void hien();
};
class giangvien: public nhansu
{
          private:
                   int sbb;
          public:
                   void nhap();
                   void hien();
};
class sinhvien: public nhansu
{
          private:
                   float diemtb;
          public:
                   void nhap();
                   void hien();
};
//khai bao ham
nhansu* nhapDL();
void hienDL(nhansu *P);
//chuong trinh chinh
int main()
{
          nhansu *ds[100];
          int n=0,i;
          char tl1,tl2;
          do
          {
                   ds[n++]=nhapDL();
                   cout<<"Co muon nhap nua khong (c/k):";
                   cin>>tl2;
          }while(tl2=='c'||tl2=='C');
          for(i=0;i<n;i++)
          {
                   hienDL(ds[i]);
                   cout<<endl;
          }
          return 0;
}
//==Dinh nghia ham
//ham thanh vien lop nhan su
void nhansu::nhap()
{
           cout<<"Nhap ten:"<<endl;
           cin>>ten;
}
//------------
void nhansu::hien()
{
          cout<<"\nTen: "<<ten<<endl;
}
//------------
//ham thanh vien lop giangvien
void giangvien::nhap()
{
          nhansu::nhap();
          cout<<"Nhap so bai bao:"<<endl;
          cin>>sbb;
}
//------------
void giangvien::hien()
{
          nhansu::hien();
          cout<<"So bai bao: "<<sbb<<endl;
          if(sbb>=10)
                   cout<<"Day la giang vien  gioi.!"<<endl;
}
//------------
//ham thanh vien lop sinhvien
void sinhvien::nhap()
{
          nhansu::nhap();
          cout<<"Nhap diem trung binh:";
          cin>>diemtb;
}
//------------
void sinhvien::hien()
{
          nhansu::hien();
          cout<<"Diem trung binh: "<<diemtb<<endl;
          if(diemtb>=8.0)
                   cout<<"Day la sinh vien gioi.!"<<endl;
}
//------------
nhansu* nhapDL()
{
          nhansu *P;
          char tl;
          cout<<"Chon de nhap nhan su:1-giangvien,2sinhvien:";
          cin>>tl;
          if(tl=='1')
                   P=new giangvien;
          else
                   P=new sinhvien;
          P->nhap();
          return P;
}
void hienDL(nhansu *P)
{
          P->hien();

}

Sunday, June 19, 2016

Lập trình hướng đối tượng với C++ cài đặt danh sách liên kết kép chứa các phần tử là số nguyên

//Họ và tên: Trần Văn Linh

//Msv: 581597

//Lớp: K58QLTT

//Đề: Cài đặt danh sách liên kép

#include<iostream>

using namespace std;

//Khai bao lop
class dslkkep
{
          private:
                   struct node
                   {
                             int infor;
                             node *left;
                             node *right;
                   }*L,*R;
          public:
                   dslkkep();
                   ~dslkkep();
                   void insertFirst(int x);//chen vào nút đầu 
                   int deleteFirst();// xóa nút cuối
                   int empty();//Kiểm tra danh sách rỗng
};
//===Chuong trinh chinh===
int main()
{
          dslkkep ds;
          ds.insertFirst(1);
          ds.insertFirst(2);
          ds.insertFirst(3);
          ds.insertFirst(4);
          while(!ds.empty())
                   cout<<ds.deleteFirst()<<"   ";
}
//===Dinh nghia ham===
dslkkep::dslkkep():L(NULL),R(NULL)
{
         
}
//-----------------
dslkkep::~dslkkep()
{
          node *P;
          if(L==R)
          {
                   delete L;
                   delete R;
          }
          else while(L)
          {
                   P=L;
                   L=L->right;
                   delete P;
          }
}
//-----------------
void dslkkep::insertFirst(int x)
{
          node *N;
          N=new node();
          N->infor=x;
          N->left=NULL;
          N->right=NULL;
         
          if(L==NULL)
          {
                   L=N;
                   R=N;
          }
          else
          {
                   N->right=L;
                   L->left=N;
                   L=N;
          }
}
//-----------------
int dslkkep::deleteFirst()
{
          if(L==NULL)
                   return 0;
          int tg=L->infor;
          node *P=L;
          L=L->right;
          delete P;
          return tg;
}
//-----------------
int dslkkep::empty()
{
          if(L==NULL)
                   return 1;
          return 0;
}

//-----------------

Friday, June 17, 2016

Hàng đợi cài đặt bằng danh sách liên kết đơn tính tổng các chữ số của một số nguyên dương Lập trình hướng đối tượng Với C++

//Họ và tên: Trần Văn Linh
//Msv:581597
//Lớp :K58QLTT

#include<iostream>

using namespace std;
//Khai bao lop
class DSLKD
{
          private:
                   struct node
                   {
                             int infor;
                             node *link;
                   }*F,*R;
          public:
                   ~DSLKD();
                   DSLKD();
                   void Insert(int x);//Chèn vào lối sau
                   int Delete();//Xóa ở lối sau
                   int empty();//Kiểm tra hằng đợi rỗng
};
//==Chuong trinh chinh==
int main()
{
          DSLKD ds;
          int n;
          do
          {
                   cout<<"Nhap so nguyen :";
                   cin>>n;
                   if(n<0)
                             cout<<"So nguyen nhap <0 vui long nhap lai: "<<endl;
          }while(n<0);
         
          int tg;
          while(n)
          {
                   ds.Insert(n%10);
                   n/=10;
          }
          int tong=0;
          while(!ds.empty())
                   tong+=ds.Delete();
          cout<<"Tong cua so nguyen da nhap la: "<<tong;
          return 0;
}
DSLKD::~DSLKD()
{
          node *P;
          while(F)
          if(F==R)
                   delete F;
          else
          {
                   P=F;
                   delete P;
                   F=F->link;
          }
}
//----------------
DSLKD::DSLKD():F(NULL),R(NULL)
{
         
}
//----------------
void DSLKD::Insert(int x)
{
          node *N;
          N=new node();
          N->infor=x;
          N->link=NULL;
          if(F==NULL)
                   F=R=N;
          else
          {
                   R->link=N;
                   R=N;
          }
}
//----------------
int DSLKD::Delete()
{
          if(F==NULL)
                   return 0;
          node *P=F;
          int y=F->infor;
          F=F->link;
          delete P;
          return y;
}
//----------------
int DSLKD::empty()
{
          if(F==NULL)
                   return 1;
          return 0;

}

Tính tổng các chữ số của một số nguyên dương sử dụng danh sách liên kết bằng C++

//Họ và Tên: Trần Văn Linh
//Msv:581597
//Lớp: K58QLTT

#include<iostream>

using namespace std;
//Khai bao lop
class DSLKD
{
          private:
                   struct node
                   {
                             int infor;
                             node *link;
                   }*F;
          public:
                   ~DSLKD();
                   DSLKD();
                   void insertFirst(int x);//chen vào vị trí đầu của ds
                   int deleteFirst();//xóa nút đầu của ds
                   int empty();//kiểm tra ds rỗng
};
//==Chuong trinh chinh==
int main()
{
          DSLKD ds;
          int n;
          do
          {
                   cout<<"Nhap so nguyen :";
                   cin>>n;
                   if(n<0)
                             cout<<"So nguyen nhap <0 vui long nhap lai: "<<endl;
          }while(n<0);
         
         
          while(n)
          {
                   ds.insertFirst(n%10);
                   n/=10;
          }
          int tong=0;
          while(!ds.empty())
                   tong+=ds.deleteFirst();
          cout<<"Tong cua so nguyen da nhap la: "<<tong;
          return 0;
}
//===Dinh nghia ham==========
DSLKD::~DSLKD()
{
          node *P;
          while(F)
          {
                   P=F;
                   F=F->link;
                   delete P;
          }
}
//----------------
DSLKD::DSLKD():F(NULL)
{
         
}
//----------------
void DSLKD::insertFirst(int x)
{
          node *N;
          N=new node();
          N->infor=x;
          N->link=NULL;
          N->link=F;
          F=N;
}
//----------------
int DSLKD::deleteFirst()
{
          if(F==NULL)
                   return 0;
          node *P=F;
          int y=F->infor;
          F=F->link;
          delete P;
          return y;
}
//----------------
int DSLKD::empty()
{
          if(F==NULL)
                   return 1;
          return 0;

}

Monday, June 13, 2016

Bài tập lập trình hướng đối tượng java Số 9 (Chữa bài kiểm tra học kỳ )

//Họ tên: Trần Văn Linh
//Msv:581597
//Lớp:K58QLTT

Đề 03;



public class Circle
{
    private double radius;
    private String color;
   
    public Circle()
    {
        this.radius=1.0;
    }
    public Circle(double radius)
    {
        this.radius=radius;
    }
    public double getRadius()
    {
        return this.radius;
    }
     public double getArea()
    {
        return Math.PI*this.radius*this.radius;
    }
}
 //----------------------------------------------------
public class Cylinder extends Circle
{
    private double height;
    //contructor
    public Cylinder()
    {
        this.height=1.0;
    }
    public Cylinder(double radius)
    {
         super(radius);
         this.height=1.0;
    }
    public Cylinder(double radius,double height)
    {
         super(radius);
         this.height=height;
    }
   
    public double getHeight()
    {
        return this.height;
    }
    //ham tinh the tich
    public double getVolume()
    {
        return super.getArea()*this.height;
    }
    //overide equals
    public boolean equals(Object obj)
    {
        Cylinder tg=(Cylinder)obj;
        if(this.getRadius()==tg.getRadius()&&this.height==tg.getHeight())
        {
            return true;
        }
        return false;
    }
}
 //---------------------------------------------------------
public class Test
{
   public static void main(String[] args)
   {
      //su dung ham tao Cylinder();
       Cylinder ht1=new Cylinder();
       System.out.println("Ban kinh :"+ht1.getRadius());
       System.out.println("Chieu cao: "+ht1.getHeight());
       System.out.println("Dien tich mot mat: "+ht1.getArea());
       System.out.println("The tich: "+ht1.getVolume());
      
       //su dung ham tao Cylinder(5.0,2.0);
       Cylinder ht2=new Cylinder(5.0,2.0);
       System.out.println("Ban kinh :"+ht2.getRadius());
       System.out.println("Chieu cao: "+ht2.getHeight());
       System.out.println("Dien tich mot mat: "+ht2.getArea());
       System.out.println("The tich: "+ht2.getVolume());
   }
}