要求是多載運算子==,判斷
#include <iostream>
using namespace std;
class SET兩集合是否相等
{public:
int a[5];
int len;
SET(int *p,int n);
int operator==(int );
friend int operator==(SET &s1,SET &s2);
void print();
~SET()
{}
};
SET::SET(int p[5],int n)
{
int i;
for(i=0;i<len;i++)
{
a[i]=p[i];
}
len=n;
}
int SET::operator==(int b)
{ int i;
for(i=0;i<len;i++)
{
if(a[i]==b) return 1;
}
}
SET operator==(SET &s1,SET &s2)
{ int i,j,k=0;
for(i=0;i<5;i++)
{ for(j=0;j<5;j++)
if(s1.a[i]==s2.a[j])
k++;
}
}
void SET::print()
{
int i;
for(i=0;i<len;i++)
{
cout<<a[i]<<" ";
}
}
int main()
{
int a[5],b[5],c[5],d[5];
int i;
for(i=0;i<5;i++)
{ cin>>a[i];
}
for(i=0;i<5;i++)
{ cin>>b[i];
}
for(i=0;i<5;i++)
{ cin>>c[i];
}
for(i=0;i<5;i++)
{ cin>>d[i];
}
SET a1(a,5);
SET b1(b,5);
SET c1(c,5);
SET d1(d,5);
cout<<"a={";a1.print();cout<<"b={";b1.print();
if(a==b)
{ cout<<" 相同"<<endl;
}
else
{ cout<<"不相同";
}
cout<<"a={";a1.print();cout<<"b={";c1.print();
if(a==c)
{
cout<<"相同";
}
else
{ cout<<" 不相同";
}
cout<<"a={";a1.print();cout<<"d={";d1.print();
if(a==d)
{ cout<<"相同";
}
else
{
cout<<"不相同";
}
return 0;
}
uj5u.com熱心網友回復:
SET operator==(SET &s1,SET &s2){ int i,j,k=0;
for(i=0;i<5;i++)
{ for(j=0;j<5;j++)
if(s1.a[i]==s2.a[j])
k++;
}
這個實作太復雜了,
可以簡單一些
for(int i=0;i<5;i++)
{
if(a[i] != b[i])
return false;
}
return true;
uj5u.com熱心網友回復:
但是編譯器顯示不能回傳cannot convert parameter 1 from 'const bool' to 'const class SET &'
uj5u.com熱心網友回復:
你不是要判斷是否相等嗎?應該用bool值啊uj5u.com熱心網友回復:
但是是SET ,回傳的應該是物件
uj5u.com熱心網友回復:
不是這樣的,那是==,又不是=賦值
uj5u.com熱心網友回復:
基本上什么演算法設計技巧例外處理都沒用(懶)lz寫的啊簡直了。。。
我猜應該是這個意思。。。
如下:
#include <iostream>
#include <cstddef>
using namespace std;
//注:僅提供思路使用請重構
//注:請自行完整,以下僅實作了需求
class set_my //注:請對const物件適配
{
public:
friend ostream& operator<<(ostream&, set_my );
set_my(int* data, int size_s) {
data_point = new int[size_s];
copy(data, data + size_s, data_point);
size = size_s;
havedone = true;
};
~set_my() = default;
int operator[](size_t point_where) {
cando();
return data_point[point_where];
};
int at(size_t point_where) {
cando();
if(point_where>size)throw out_of_range("下標越界");
return data_point[point_where];
};
bool operator==(set_my Rvaluey) {
cando();
for (size_t i = 0; i < Rvaluey.size; i++)
{
if (at(i)!=Rvaluey.at(i)) return 0;
}
return 1;
};
set_my& operator=(set_my Rvaluey) {
cando();
Rvaluey.~set_my();
Rvaluey.build(data_point, size);
return Rvaluey;
}
void print() {
cando();
for (size_t i = 0; i < size; i++)
{
cout << data_point[i] << " ";
}
};
private:
int *data_point; //注意請使用智能指標(怕你看不懂沒用)
int size;
bool havedone = 0;
void cando() {
if (!havedone)throw runtime_error("未初始化");
};
void build(int* data, int size_s) {
data_point = new int[size_s];
copy(data, data + size_s, data_point);
size = size_s;
havedone = true;
};
};
ostream& operator<<(ostream& stream, set_my data) {//注:請完善例外機制
for (size_t i = 0; i < data.size; i++)
{
stream << data.at(i);
}
return stream;
}
void input_my(int* point, size_t size = 5) {
for (size_t i = 0; i < size; i++)
{
std::cin >> point[i];
}
}
int main()
{
int a[5], b[5], c[5], d[5];
input_my(a);
input_my(b);
input_my(c);
input_my(d);
set_my as(a,5);
set_my bs(b,5);
set_my cs(c,5);
set_my da(d,5);
cout << "a1為:" << as << " b1為:" << bs << ((as == bs) ? "a1與b1相同" : "a1與b1不相同")<<endl;
cout <<"c1為:"<<cs <<" d1為:"<<da << ((cs == da)?"c1與d1相同":"c1與d1不相同");
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/22523.html
標籤:C++ 語言
下一篇:Qt 加速鍵(&)翻譯無效?
