Showing posts sorted by relevance for query C++. Sort by date Show all posts
Showing posts sorted by relevance for query C++. Sort by date Show all posts

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

}

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;

}

Friday, May 13, 2016

Bài tập chồng toán tử +,> trong xâu bằng C++


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

----------------------------------------------------------------------------------------------------------------------------------

#include<iostream>
#include<string.h>
#include<stdio.h>

using namespace std;

//Khai bao lop
class xau
{
private:
 enum {size=256};
         char str[size];
public:
 xau();
 xau(const char *s);
 xau operator+(xau &s2); //hàm cộng hai xâu
 int operator>(xau &s2);//hàm so sánh > hai xâu
          friend istream& operator>>(istream& cin,xau &s2); //Nhập xâu
 friend ostream& operator<<(ostream& cout,xau &s2);//In xâu
};
//==Chuong trinh chinh==
int main()
{
xau s1("Day la xau 1 ");
xau s2,s3("Day la xau 2 ");

s2=s1+s3;

cout<<s1<<endl;
cout<<s2<<endl;
cout<<s3<<endl;

s1>s3?cout<<s1:cout<<s3;

return 0;
}
//===Dinh nghia ham===
xau::xau()
{
           strcpy(str,"");
}
//-------------
xau::xau(const char *s)
{
strcpy(str,s);
}
//-------------
xau xau::operator+(xau &s2)
{
xau tong;
strcpy(tong.str,str);
if(strlen(str)+strlen(s2.str)<size)
strcat(tong.str,s2.str);
else
cout<<"Xau qua dai khong the noi duoc.!";
return tong;
}
//-------------
int xau::operator>(xau &s2)
{
return (strcasecmp(str,s2.str)>0);
}
//-------------
istream& operator>>(istream& cin, xau &s2)
{
          cout<<"Nhap xau:";
 scanf(" ");//Khu dau enter
 cin.get(s2.str,sizeof(s2.str));
 return cin;
}
//-------------
ostream& operator<<(ostream& cout,xau &s2)
{
cout<<s2.str;
return cout;
}
//-------------

Chương trình về Phân Số với C++ (Chồng toán tử )

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

--------------------------------------------------------------------------------------------------------------------------
phanso.cpp
#include<iostream>

using namespace std;

class phanso
{
private:
int mau;
int tu;
public:
phanso();
phanso(float x,float y);
friend istream& operator>>(istream &cin,phanso &ps);
friend ostream& operator<<(ostream &cout,phanso &ps);
phanso operator+(phanso &ps2);
int operator>(phanso &ps2);
};
//===Chuong trinh chinh===
int main()
{
phanso ps[50],tong;
int i,n,j;
cout<<"Nhap so luong phan so:";
cin>>n;

for(i=0;i<n;i++)
{
cout<<"Nhap phan so thu "<<i+1<<":"<<endl;
cin>>ps[i];
}

cout<<"\nCac phan so da nhap la:"<<endl;
for(i=0;i<n;i++)
cout<<ps[i]<<"\t";
//sap xep
for(i=0;i<n;i++)
for(j=n-1;j>i;j--)
if(ps[j-1]>ps[j])
{
phanso temp=ps[j];
ps[j]=ps[j-1];
ps[j-1]=temp;
}
cout<<"\nCac phan so da nhap sau khi sap xep la:"<<endl;
for(i=0;i<n;i++)
cout<<ps[i]<<"\t";

tong=ps[0];
for(i=1;i<n;i++)
tong=tong+ps[i];
cout<<"\nTong cua cac phan so la:";
cout<<tong;
return 0;
}
//===Dinh nghia ham===
//---hàm tạo----
phanso::phanso():tu(0),mau(0)
{

}
//----------
phanso::phanso(float x,float y):tu(x),mau(y)
{

}
//------hàm nhập phân số--------
istream& operator>>(istream &cin,phanso &ps)
{
do
{
cout<<"Nhap tu so:";
cin>>ps.tu;
cout<<"Nhap mau so:";
cin>>ps.mau;
if(ps.mau==0)
cout<<"Mau phai khac 0,nhap lai:"<<endl;
}while(ps.mau==0);
return cin;
}
//-----hàm hiện phân số--------
ostream& operator<<(ostream &cout,phanso &ps)
{
int uc;
int a=ps.tu;
int b=ps.mau;

while(a!=0&&b!=0)
if(a>b)
a=a-b;
else
b=b-a;
if(a==0)
uc=b;
else
uc=a;
cout<<ps.tu/uc<<"/"<<ps.mau/uc<<endl;

return cout;
}
//-----hàm cộng hai phân số
phanso phanso::operator+(phanso &ps2)
{
phanso tong;
tong.tu=tu*ps2.mau+ps2.tu*mau;
tong.mau=mau*ps2.mau;
return tong;
}
//----hàm so sánh hai phân số----
int phanso::operator>(phanso &ps2)
{
return (tu*ps2.mau>ps2.tu*mau);
}
//----------

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

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