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());
   }
}