请高手补全!

风尘雨路 2003-12-13 09:46:53
#include <iostream.h>

class Relation //二元关系类
{
int* one; //序偶一
int* two; //序偶二
int c; //二元关系元素个数
public:
Relation(){}
relation();
~Relation(){delete[] one;delete[] two;}
void show();
void contrary();
void tie();//求逆
};
Relation::relation()
{
cout<<"please input number:";
cin>>c;
one = new int[c];
two = new int[c];
int value;
for(int i=0;i<c;++i){
cout << "Input element "<< i+1 <<" part one:";
cin>>value;
cout << "Input element "<< i+1 <<" part two:";
one[i] = value;
cin>>value;
two[i] = value;
}
cout << endl;
}
void Relation::show()
{
cout << "{";
for(int i=0;i<c-1;++i){
cout << "<" << one[i] << ",";
cout << two[i] << ">,";
}
cout<<"<"<<one[c-1] << ","<< two[c-1] << ">";
cout << "}" << endl;
}
void Relation::contrary()
{
for(int i=0;i<c;++i){
int temp = one[i];
one[i] = two[i];
two[i] = temp;
}
}
void compound(Relation * ps1,Relation * ps2)
{

ps1->relation();ps2->relation();
ps1->show();ps2->show();


...
}
void main(void)
{
Relation r1;
r1.relation();
r1.show(); //显示关系
r1.contrary(); //求逆
r1.show(); //显示逆关系
Relation r2,r3;
compound(& r2,& r3);


}
要把两个二元关系复合!!在省类号那里补全.谢谢了
...全文
80 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
风尘雨路 2003-12-17
  • 打赏
  • 举报
回复
hao
inline 2003-12-16
  • 打赏
  • 举报
回复
将类定义中这两句注释掉:留着也不错(我改了思路,忘删了!)
Relation(Relation &);
Relation(int c);
inline 2003-12-16
  • 打赏
  • 举报
回复
// 可在运行中输入元素个数:

#include <iostream>
using namespace std;

class Relation //二元关系类
{
public:
Relation(){}
Relation(Relation &);
Relation(int c);
~Relation(){delete[] one;delete[] two;}
void show(); //显示二元关系
void contrary(); //求逆
void init_R(); //初始化
private:
int* one; //序偶一
int* two; //序偶二
int count; //二元关系元素个数
friend Relation* compound(const Relation& f,
const Relation& g); //求复合
};

void Relation::init_R()
{
int len; cout << "Input count of element: ";
cin >> len; count = len; cout << endl;
one = new int[len];
two = new int[len];
for(int i = 0; i < len; ++ i) {
cout << "Element " << i+1 << " Part One: ";
cin >> one[i];
cout << "Element " << i+1 << " Part Two: ";
cin >> two[i]; cout << endl;
}
cout << endl;
}

void Relation::contrary()
{
for(int i=0;i<count;++i){
int temp = one[i];
one[i] = two[i];
two[i] = temp;
}
}

// 检查复合关系中是否有重复元素
bool isexist(int ONE[], int TWO[], int one, int two, int count)
{
for(int i = 0; i < count; ++ i)
if(one == ONE[i] && two == TWO[i])
return false;
return true;
}

Relation* compound(const Relation& f, const Relation& g)
{
int* elemone = new int[f.count * g.count];
int* elemtwo = new int[f.count * g.count];
int compcount = 0;
for(int i = 0; i < f.count; ++ i)
for(int j = 0; j<g.count; ++ j)
if(f.two[i] == g.one[j] && isexist(elemone,
elemtwo,f.one[i],g.two[j],compcount))
{
elemone[compcount] = f.one[i];
elemtwo[compcount] = g.two[j];
++ compcount;
}
Relation* result = new Relation();
result->one = new int[compcount];
result->two = new int[compcount];
result->count = compcount;
for(int k = 0; k< result->count; ++ k) {
result->one[k] = elemone[k];
result->two[k] = elemtwo[k];
}
delete[] elemone; delete[] elemtwo;
return result;
}

void Relation::show()
{
cout << "{";
for(int i=0;i<count;++i) {
cout << "<" << one[i] << ",";
cout << two[i] << ">";
}
cout << "}" << endl;
}

void main(void)
{
Relation r1, r2, *r3;
r1.init_R(); r2.init_R();
r1.show();
r2.show();
r3 = compound(r1,r2); // 在 compound 中将分配一定内存给 r3
r3->show();
r3->contrary();
r3->show();
delete r3;
}
xiaonian_3654 2003-12-16
  • 打赏
  • 举报
回复
stl的pair不好用?
风尘雨路 2003-12-16
  • 打赏
  • 举报
回复
???
风尘雨路 2003-12-15
  • 打赏
  • 举报
回复
还是不行呀
大哥你能不能写个?
lyr311 2003-12-14
  • 打赏
  • 举报
回复
Mark!Study!
风尘雨路 2003-12-14
  • 打赏
  • 举报
回复
那我试试!
inline 2003-12-13
  • 打赏
  • 举报
回复
你可以在构造函数中规定输入一个符号结束!
每输入一个元素给 one,two 分配内存!
遇到结束符后再给 count 赋值!
如果结果不正确则把 Relation* rp = compound(r1,r2);这句
换成 Relation* rp = compound(r2,r1);
注意对象在定义时元素个数一定不要搞错!
风尘雨路 2003-12-13
  • 打赏
  • 举报
回复
二元关系元素的个数能不能运行时自己输入吗?
inline 2003-12-13
  • 打赏
  • 举报
回复
//楼上的兄弟我回复过你一次定义二元关系类
//试试下面的代码:不好用吗?

#include <iostream.h>

class Relation //二元关系类
{
int* one; //序偶一
int* two; //序偶二
int count; //二元关系元素个数
friend Relation* compound(Relation f,Relation g); //求复合
public:
Relation(){}
Relation(Relation &);
Relation(int c);
~Relation(){delete[] one;delete[] two;}
void show(); //显示二元关系
void contrary(); //求逆
};

Relation::Relation(int c)
{
count = c;
one = new int[c];
two = new int[c];
int value;
for(int i=0;i<c;++i){
cout << "Input element "<< i+1 <<" part one:";
cin>>value;
cout << "Input element "<< i+1 <<" part two:";
one[i] = value;
cin>>value;
two[i] = value;
}
cout << endl;
}

Relation::Relation(Relation &x)
{
one = new int[x.count];
two = new int[x.count];
for(int i=0;i<x.count;++i){
one[i] = x.one[i];
two[i] = x.two[i];
}
count = x.count;
}

void Relation::show()
{
cout << "{";
for(int i=0;i<count;++i){
cout << "<" << one[i] << ",";
cout << two[i] << ">";
}
cout << "}" << endl;
}

void Relation::contrary()
{
for(int i=0;i<count;++i){
int temp = one[i];
one[i] = two[i];
two[i] = temp;
}
}

Relation* compound(Relation f,Relation g)
{
int* elemone = new int[f.count * g.count];
int* elemtwo = new int[f.count * g.count];
Relation* result; int compcount = 0;
for(int i=0;i<f.count;++i)
for(int j=0;j<g.count;++j)
if(f.two[i] == g.one[j]){
elemone[compcount] = f.one[i];
elemtwo[compcount] = g.two[j];
++ compcount;
}
result = new Relation();
result->one = new int[compcount];
result->two = new int[compcount];
result->count = compcount;
for(int k=0;k<result->count;++k){
result->one[k] = elemone[k];
result->two[k] = elemtwo[k];
}
delete[] elemone;
delete[] elemtwo;
return result;
}

void main(void)
{
Relation r1(3),r2(3); //定义两个二元关系各有 3 个元素
r1.show(); //显示关系
r2.show();
r1.contrary(); //求逆
r2.contrary();
r1.show(); //显示逆关系
r2.show();
Relation* rp = compound(r1,r2); //求复合
rp->show();
rp->contrary(); //求复合的逆
rp->show();
delete rp;
}

64,651

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧