Tboy Cá voi
Tổng số bài gửi : 178 Cảm ơn : 413 Danh vọng : 4 Join date : 03/01/2011 Age : 32 Đến từ : TPHCM
| Tiêu đề: Bài CString Thu Aug 11, 2011 10:16 pm | |
| | Thu Aug 11, 2011 10:16 pm Bài CString | | | Của Tâm gửi nhờ mình viết dùm void main() - Code:
-
#include <iostream.h> #include <string.h> #define MAX 1000 class CString { private: char *data; public: //Constructor CString(); CString(char *s); CString(CString &u); ~CString(); // char LayKiTu(int vt); void GanKiTu(char ch,int vt); int LayChieuDai(); bool ChuoiRong(); void XoaRong(); void ChenChuoi(int vt, char *p); void XoaChuoiCon(int vt, int num); void ChuoiHoa(); void ChuoiThuong(); void Nhap(); void Xuat(); };
CString::CString() { data=NULL; } CString::CString(char *s) { int len = strlen(s); data = new char[len+1]; strcpy(data,s); } CString::CString(CString &u) { int len = strlen(u.data); if(!ChuoiRong()) data = new char[len+1];
strcpy(data,u.data); } CString::~CString() { if(!ChuoiRong()) delete []data; } char CString::LayKiTu(int vt) { return data[vt]; } void CString::GanKiTu(char ch,int vt) { data[vt] = ch; } int CString::LayChieuDai() { return strlen(data); } bool CString::ChuoiRong() { return (data == NULL); } void CString::XoaRong() { delete []data; data=NULL; } void CString::ChenChuoi(int vt, char *p) { char *temp; int len1=LayChieuDai(),len2=strlen(p); temp = new char[len1+1]; strcpy(temp,data); data=new char[len1+len2+1]; strncpy(data,temp,vt); data[vt]='\0'; strcat(data,p); strcat(data,temp+vt); }
void CString::XoaChuoiCon(int vt, int num) { int len=strlen(data); if((num+vt)>len) data[vt]='\0'; else strcpy(data+vt,data+vt+num); } void CString::ChuoiHoa() { int len = strlen(data); for(int i = 0;i<len;i++) if(data[i] >= 'a' && data[i] <= 'z') data[i]-=32; } void CString::ChuoiThuong() { int len = strlen(data); for(int i = 0;i<len;i++) if(data[i] >= 'A' && data[i] <= 'Z') data[i]+=32; } void CString::Nhap() { char temp[MAX]; cout<<"Nhap chuoi: "; cin.getline(temp,MAX); int len=strlen(temp); data= new char [len+1]; strcpy(data,temp); } void CString::Xuat() { if(!ChuoiRong()) cout<<data<<endl; }
void main() { CString a("~LoVely_^_^_gIrl_@_@~"); CString b(a); //---Copy int vt,num; char chuoi[MAX]; // k0 hiu tai sa0 cho nay the la MAX thi k0 chay!?! cout<<"Chuoi:\t\t\t"; b.Xuat(); cout<<"Chuoi gom:\t\t"<<b.LayChieuDai()<<" ki tu\n"; //-----In hoa ~ thuong cout<<"In chu hoa:\t\t"; b.ChuoiHoa(); b.Xuat(); cout<<"In chu thuong:\t\t"; b.ChuoiThuong(); b.Xuat(); //---------Xoa //Xoa chuoi con cout<<"Chon vi tri can xoa: "; cin>>vt; if(vt<=a.LayChieuDai()) { cout<<"Ki tu o vi tri "<<vt<<" la: "<<a.LayKiTu(vt)<<endl; cout<<"So ki tu muon xoa: "; cin>>num; b.XoaChuoiCon(vt,num); cout<<"Chuoi truoc khi xoa: "; a.Xuat(); cout<<"Chuoi sau khi xoa: "; b.Xuat(); } else cout<<"Vi tri ban chon vuot ngoai chuoi!!!\n"; //Xoa chuoi cout<<"Thuc hien xoa toan chuoi!!!!!!!"<<endl; b.XoaRong(); cout<<"In ra chuoi vua xoa: "; b.Xuat(); cout<<endl; cin.ignore(); //------Chen b.Nhap(); cout<<"Chon vi tri chen: "; cin>>vt; cout<<"Nhap chuoi can chen: "; cin.ignore(); cin.getline(chuoi,MAX); b.ChenChuoi(vt-1,chuoi); cout<<"Chuoi moi: "; b.Xuat(); //------Gan ki tu char ch; cout<<"Chon vi tri ki tu muon thay doi: "; cin>>vt; if(vt<b.LayChieuDai()) { cout<<"Ki tu o vi tri "<<vt<<" la: "<<b.LayKiTu(vt)<<endl; cout<<"Nhap vao 1 ki tu: "; cin>>ch; b.GanKiTu(ch,vt); cout<<"Chuoi moi: "; b.Xuat(); } else cout<<"Vi tri ban chon vuot ngoai chuoi!!!\n";
} Bản gốc: - Code:
-
#include <iostream.h> #include <string.h>
#define MAX 1000000
class CString { private: char *data; public: //Constructor CString(); CString(char *s); CString(CString &u); ~CString();
// char LayKiTu(int vt); void GanKiTu(char *ch,int vt); int LayChieuDai(); bool ChuoiRong(); void XoaRong(); void ChenChuoi(int vt, char *p); void XoaChuoiCon(int vt, int num); void ChuoiHoa(); void ChuoiThuong();
void Nhap(); void Xuat(); };
CString::CString() { data = NULL; } CString::CString(char *s) { int len = strlen(s); data = new char[len+1];
strcpy(data,s); } CString::CString(CString &u) { int len = strlen(u.data);
if(data != NULL) data = new char[len+1];
strcpy(data,u.data); } CString::~CString() { } char CString::LayKiTu(int vt) { return data[vt]; } void CString::GanKiTu(char *ch,int vt) { data[vt] = *ch; } int CString::LayChieuDai() { return strlen(data); } bool CString::ChuoiRong() { return (data == NULL); } void CString::XoaRong() { delete []data; data = NULL; } void CString::ChenChuoi(int vt, char *p) { char *tam; tam = new char[strlen(data)+strlen(p)+1];
strncpy(tam,data,vt); tam[vt] = '\0'; //chu y: ki tu /0: Neu khong co ket qua se la sai. chay debug de thay ro dieu nay. strcat(tam,p); strcat(tam,data+vt);
delete []data; data = new char[strlen(tam)+1]; strcpy(data,tam); }
void CString::XoaChuoiCon(int vt, int num) { strcpy(data+vt,data+vt+num); } void CString::ChuoiHoa() { int len = strlen(data); for(int i = 0;i<len;i++) if(data[i] >= 'a' && data[i] <= 'z') { data[i] = data[i] - 32; }
} void CString::ChuoiThuong() { int len = strlen(data); for(int i = 0;i<len;i++) if(data[i] >= 'A' && data[i] <= 'Z') { data[i] = data[i] + 32; }
} void CString::Nhap() { char s[MAX]; cout<<"Nhap chuoi: "; cin.getline(s,MAX);
if(data != NULL) delete []data;
data = new char[strlen(s)+1]; strcpy(data,s); } void CString::Xuat() { cout<<data<<endl; }
void main() { //Vi de bai khong yeu cau cu the nen ham main cac ban tu viet. //Chu y: Viet sao cho the hien duoc het tat ca cac ham trong class CString nhu vay la dat. //Bai nay yeu cau cac ban phai nam vung kien thuc ve chuoi(da hoc o phan co so lap trinh 1) //neu khong thi giai bai nay rat kho. //Cac ban co the tu debug de thay ro hon ve co che hoat dong cua cac ham. } Tboy
| | | | | |
|