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

Tìm kiếm, xóa mặt hàng trên danh sách mặt hàng lưu trong danh sách liên kết đơn bằng C


Lưu ý: Bài này mình làm với mặt hàng đơn giản chỉ có hai thuộc tính là mã và tên, mọi người cần thêm đầy đủ các thuộc tính của một mặt hàng cần có như giá , số lượng, .....! 
-------------------------------------------------------------------------------------------------------------------------
Code quảng cáo của bạn ở đây

mathang.c

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

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

//==Khai bao cau truc
typedef struct mathang mathang;
struct mathang
{
int maMH;
char tenMH[256];
};
typedef struct node node;
struct node
{
mathang infor;
node *link;
}*F=NULL,*R=NULL;
//Khai bao ham
void inSert(mathang infor);// hàm chèn một nút
void  Search(int maMH);//Hàm tìm kiếm một mặt hàng với mã mặt hàng
void Delete(int maMH); //Hàm xóa một một mặt hàng với mã mặt hàng
void in();//Ham in ra danh sach các mặt hàng
//Chuong trình chính
int  main()
{
mathang mh;
int maMH;
char tl;
do
{
printf("Nhap ma mat hang: ");
scanf("%d",&mh.maMH);
fflush(stdin);//Xóa bộ đệm

printf("Nhap ten mat hang: ");
gets(mh.tenMH);
inSert(mh);//Bổ sung

printf("Co nhap nua khong (c/k):");
scanf("%c",&tl);
}while(tl=='c'||tl=='C');
printf("\n\nDanh sach da nhap la: \n");
in();
printf("\n\nNhap ma mat hang ban can xoa: ");
scanf("%d",&maMH);
Delete(maMH);
printf("\n\nDanh sach sau khi xoa: ");
in();
printf("\n\nNhap ma mat hang ban can tim kiem: ");
scanf("%d",&maMH);
Search(maMH);
return 0;
}
//--Dinh nghia ham
void inSert(mathang infor)
{
node *N;
N=(node*)malloc(sizeof(node));
N->infor=infor;
N->link=NULL;
//Bổ sung
N->link=F;
F=N;
}
//---------------------
void  Search(int maMH)
{
node *P;
P=F;
mathang tim;
      tim.maMH=0;
      strcpy(tim.tenMH,"");

while(P)
{
if(P->infor.maMH==maMH)
{
tim=P->infor;
break;
}
P=P->link;
}
    if(strcmp(tim.tenMH,"")==0)
    {
   printf("Mat hang khong ton tai");
   return;
    }
printf("Ma mat hang: %d\n",tim.maMH);
printf("Ten mat hang: %s",tim.tenMH);
}
//--------------------------
void Delete(int maMH)
{
node *P;
node *Q;
P=F;

while(P)
{
if(P->infor.maMH==maMH)
break;
Q=P;
P=P->link;
}

if(P)
{
if(P==F)//Trường hợp mặt hàng  cần xóa ở nút đầu tiên
F=F->link;
else
{
Q->link=P->link;
}
free(P);
}
}
//---------------------------------------------------------------------
void in()
{
node *P;
P=F;
while(P)
{
printf("Ma mat hang: %d\n",P->infor.maMH);
printf("Ten mat hang: %s\n\n",P->infor.tenMH);
P=P->link;
}
}

Lưu ý: Bài này mình làm với mặt hàng đơn giản chỉ có hai thuộc tính, mọi người cần thêm đầy đủ các thuộc tính của một mặt hàng cần có như giá , số lượng, .....! 

Nhập một danh sách thời gian rồi sắp xếp danh sách thời gian đã nhập theo chiều tăng dần

//Họ Tên: Trần Văn Linh
//MaSV:581597
//Lớp:K58QLTT
--------------------------------------------------------------------------------------------------------------------------
//thoigian.cpp
#include<iostream>

using namespace std;

//Khai bao lop
class thoigian
{
private:
int gio;
int phut;
int giay;
public:
void nhap();
void hien();
int operator>(thoigian &t);
};
//Chuong trinh chinh
int main()
{
  thoigian t[10];
int i,j;
int n;

cout<<"Nhap so luong thoi gian.";
cin>>n;
for(i=0;i<n;i++)
t[i].nhap();
       
        //Sắp xếp
for(i=0;i<n;i++)
for(j=n-1;j>i;j--)
if(t[j-1]>t[j])
{
thoigian temp=t[j];
t[j]=t[j-1];
t[j-1]=temp;
}

cout<<"Danh sach thoi gian da nhap:";
for(i=0;i<n;i++)
t[i].hien();

return 0;
}
//Dinh nghia ham
void thoigian::nhap()
{
cout<<"Nhap gio:";
cin>>gio;
cout<<"Nhap phut:";
cin>>phut;
  cout<<"Nhap giay:";
cin>>giay;
}
//-------------
void thoigian::hien()
{
cout<<gio<<":"<<phut<<":"<<giay<<endl;
}
//----------
int thoigian::operator>(thoigian &t)
{
if(gio>t.gio)
return 1;
else if(gio==t.gio&&phut>t.phut)
return 1;
else if(gio==t.gio&&phut==t.phut&&giay>t.giay)
return 1;
return 0;
}