code của e như sau ạ.
#include <cstring>
#include <iostream>
using namespace std;
class boMa{
public:
boMa();
~boMa();
void setCount(int n);
int getCount();
void setTuMa(string str);
string getTuMa(int i);
bool giao(boMa S);
private:
string* tuMa;
int n;
};
boMa::boMa(){
tuMa=new string[100];
n=0;
}
boMa::~boMa(){
delete[] tuMa;
n=0;
}
void boMa::setCount(int n){
this->n=n;
}
int boMa::getCount(){
return n;
}
void boMa::setTuMa(string str){
bool c=true;
for(int i=0;i<n;i++)
if(tuMa[i]==str){
c=false;
break;
}
if(c==true){
tuMa[n]=str;
n++;
}
}
string boMa::getTuMa(int i){
return tuMa[i];
}
bool boMa::giao(boMa S){
for(int i=0;i<n;i++)
for(int j=0;i<S.getCount();j++)
if(tuMa[i]==S.getTuMa(j)) return true;
return false;
}
int main(){
boMa W,S;
int n=0;
cout<<"Nhap so tu ma cua bo ma W: ";
cin>>n;
cout<<"Nhap cac tu ma cua bo ma W: (an phim Enter sau moi tu ma nhap vao)"<<endl;
for(int i=0;i<n;i++){
fflush(stdin);
string str;
cin>>str;
W.setTuMa(str);
}
cout<<"Bo ma ban vua nhap: \n{";
for(int i=0;i<W.getCount()-1;i++) cout<<W.getTuMa(i)<<","; cout<<W.getTuMa(n-1)<<"}\n";
cout<<"Nhap so tu ma cua bo ma S: ";
cin>>n;
cout<<"Nhap cac tu ma cua bo ma S: (an phim Enter sau moi tu ma nhap vao)"<<endl;
for(int i=0;i<n;i++){
fflush(stdin);
string str;
cin>>str;
S.setTuMa(str);
}
cout<<"Bo ma ban vua nhap: \n{";
for(int i=0;i<S.getCount()-1;i++) cout<<S.getTuMa(i)<<","; cout<<S.getTuMa(n-1)<<"}\n";
if(S.giao(W)) cout<<"Giao\n";
else cout<<"Khong giao\n";
system("pause");
return 0;
}
mấy a test thì cho 2 trường hợp có giao và không giao luôn nha. e không biết làm sao để fixed nữa. TT.TT