Friday, June 17, 2016

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

Saturday, May 21, 2016

Chương trình chuyển đổi 1 số nguyên dương sang dạng nhị phân sử dụng ngăn xếp cài đặt bằng danh sách liên kết kép bằng ngôn ngữ C

//Ho va ten: Tran Van Linh
//Msv:581597
//Lop:K58QLTT

#include<stdio.h>
#include<stdlib.h>

//Khai bao cau truc
struct node
{
          int infor;
          struct node *left;
          struct node *right;
} *T=NULL;

//khai bao ham
void push(int x);
int pop();
int empty();

//==chuong trinh chinh==
int main()
{
          int n,thuong;
         
          printf("Nhap so nguyen can chuyen sang dang nhi phan:");
          do
          {
                   scanf("%d",&n);
                   if(n<0)
                             printf("\nSo nguyen nhap phai duong,nhap lai: ");
          }while(n<0);
         
          thuong=n;
         
          while(thuong)
          {
                   push(thuong%2);
                   thuong/=2;
          }
         
          printf("\nDang nhi phan cua %d la:",n);
          while(!empty())
                   printf("%d",pop());
          return 0;
}
//==Dinh nghia ham==
void push(int x)
{
          struct node *N;
          N=(struct node*)malloc(sizeof(struct node));
          N->infor=x;
          N->left=NULL;
          N->right=NULL;
         
          //bo sung vao dinh ngan xep
          if(T==NULL)
                   T=N;
          else
          {
                   N->right=T;
                   T->left=N;
                   T=N;
          }
}
//-------------
int pop()
{
          int tg;
          struct node *P;
         
          if(T==NULL)
          {
                   printf("Danh sach rong.!");
                   return 0;
          }
          tg=T->infor;
          P=T;
          if(T->right==NULL) //Trường hợp ds chỉ có 1 nút
          {
                   T=NULL;
          }
          else
          {
                   T=T->right;
                   T->left=NULL;
          }
          free(P);
          return tg;
}
//-------------
int empty()
{
          if(T==NULL)
                   return 1;
          return 0;
}

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