这道C++编程题怎么做?怎么进行整数集合运算?高悬赏求大神解答~
2个回答
展开全部
//
// main.cpp
// 重载运算符
//
// Created by Jackie Han on 26/03/2017.
// Copyright © 2017 jackie. All rights reserved.
//
#include <iostream>
#include <vector>
using namespace std;
class intUnion {
public:
vector<int> vt;
int intNum;
intUnion(vector<int> & v,int iN) {
vt = v;
intNum = iN;
}
intUnion(int iN):intNum(iN){};
intUnion(intUnion & iu){
vt = iu.vt;
intNum = iu.intNum;
}
intUnion & operator + (intUnion & iu){
vector<int>::iterator iti;
vector<int>::iterator itj;
for (iti = iu.vt.begin();iti != iu.vt.end();iti++){
int flag = 0;
for (itj = vt.begin();itj != vt.end();itj++){
if(*iti == *itj){
flag = 1;
}
}
if(flag == 0){
vt.push_back(*iti);
intNum++;
}
}
return *this;
}
intUnion & operator - (intUnion & iu){
vector<int>::iterator iti;
vector<int>::iterator itj;
for (iti = iu.vt.begin();iti != iu.vt.end();iti++){
for (itj = vt.begin();itj != vt.end();itj++){
if(*iti == *itj){
vt.erase(itj);
intNum--;
break;
}
}
}
return *this;
}
friend ostream & operator << (ostream & o, intUnion & t){
for (int i = 0;i < t.intNum;i++){
o << t.vt[i] << ' ';
}
return o;
}
};
int main(int argc, const char * argv[]) {
int in1,in2;
cin >> in1;
intUnion int1(in1);
int tmp;
for(int i = 0;i < in1;++i) {
cin >> tmp;
int1.vt.push_back(tmp);
}
cin >> in2;
intUnion int2(in2);
for (int i = 0;i < in2;++i){
cin >> tmp;
int2.vt.push_back(tmp);
}
intUnion int3(int1);
cout << "int1+int2=" << int3 + int2 << endl;
cout << "int1-int2=" << int1 - int2 << endl;
return 0;
}
偷了个小懒,过题应该没问题
追答
friend ostream & operator << (ostream & o, intUnion & t){
o << t.vt[0];
for (int i = 1;i < t.intNum;i++){
o << ' ' << t.vt[i];
}
return o;
}
把里面对应函数名替换成这个就行了
请采纳谢谢
展开全部
//#include "stdafx.h"//If the vc++6.0, with this line.
#include <iostream>
#include "math.h"
using namespace std;
class CPoint{
public:
double x,y;
friend double GetD(CPoint a,CPoint b);
};
double GetD(CPoint a,CPoint b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main(int argc,char *argv[]){
CPoint t1,t2;
cout << "Please enter the coordinates of point 1:";
cin >> t1.x >> t1.y;
cout << "Please enter the coordinates of point 2:";
cin >> t2.x >> t2.y;
cout << "The distance=" << GetD(t1,t2) << endl;
return 0;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询