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ử :



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

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

Friday, May 13, 2016

Chương trình cài đặt hằng đợi theo danh sách liên kết đơn bằng C++

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

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

using namespace std;

//Khai bao lop
class hangdoi
{
private:
struct node
{
int info;
node *link;
}*F,*R;
public:
hangdoi();
~hangdoi();
void cqinsert(int &x);
int cqdelete();
};
//===Chuong trinh chinh===
int main()
{
hangdoi ds;
int i,n;
int a[50];

cout<<"Nhap so luong so nguyen:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Nhap phan tu thu "<<i<<" :"<<endl;
cin>>a[i];
ds.cqinsert(a[i]);
}

cout<<"Danh sach so da nhap la:"<<endl;
for(i=0;i<n;i++)
cout<<ds.cqdelete()<<" ";

return 0;
}
//Dinh nghia ham
hangdoi::hangdoi():F(NULL),R(NULL)
{

}
//--------------
hangdoi::~hangdoi()
{
node *P;
while(F)
{
P=F;
F=F->link;
delete P;
}
}
//--------------
void hangdoi::cqinsert(int &x)
{
node *N;
N= new node;
N->info=x;
N->link=NULL;

if(F==NULL)
{
F=N;
R=N;
}
else
{
R->link=N;
R=N;
}
}
//--------------
int hangdoi::cqdelete()
{
node *P;
int y;

P=F;
if(F==NULL)
{
cout<<"hang doi rong.!";
return 0;
}

y=F->info;
F=F->link;
delete P;
return y;
}
//--------------

Chương trình cài đặt ngăn xếp bằng danh sách liên kết đơn cho phép chèn , xóa một số phần tử bằng C++

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

DSLK.cpp
#include<iostream>

using namespace std;

//Khai bao lop
class dslkd
{
private:
struct node{
int info;
node *next;
}*F;
public:
dslkd();
~dslkd();
void chencuoi(int x);
void xoanode(int x);
void hien();

};

//==Chuong trinh chinh====
int main()
{

dslkd ds;
int x;
char kt;
do
{
cout<<"Nhap phan tu muon them vao danh sach:";
cin>>x;
ds.chencuoi(x);
cout<<"\nCo muon nhap them nua khong (C-K):";
cin>>kt;
}
while(kt=='c'||kt=='C');
cout<<"\nDanh sach da nhap la:";
ds.hien();
cout<<"\nNhap phan tu muon xoa:";
cin>>x;
ds.xoanode(x);
cout<<"Danh sach sau khi xoa la:";
ds.hien();
cout<<endl;
return 0;
return 0;
}
//Dinh nghia ham
dslkd::dslkd():F(NULL)
{

}
//------------
dslkd::~dslkd()
{
node *P;
while(F)
{
P=F;
F=F->next;
delete P;
}
}
//------------
void dslkd::chencuoi(int x)
{
node* N,*P;
N=new node;
N->info=x;
N->next=NULL;

P=F;
if(F==NULL)
F=N;
else
{
while(P->next)
P=P->next;
P->next=N;
}
}
//------------
void dslkd::xoanode(int x)
{
node *P;
node *Q;
P=F;
while(P)
{
if(P->info==x)
break;
Q=P;
P=P->next;
}
if(P)
{
if(P==F)
F=F->next;
else
{
Q->next=P->next;
}
delete P;
}

}
//------------
void dslkd::hien()
{
node*P;
P=F;
while(P)
{
cout<<P->info;
P=P->next;
}
}
//------------