关系代数运算基本实现
传统的集合运算:
(1):并(Union)
1 //并
2 relate union_relate(relate u_R, relate u_S)
3 {
4 relate P;
5 int i = 0, j = 0, k = 0;
6 for (i = 0; i < 25; i++)
7 {
8 P.num_tuple = u_S.num_tuple;
9 P.num_row = u_S.num_row;
10 P.i_A[i] = 0;
11 P.i_B[i] = 0;
12 P.i_C[i] = 0;
13 P.i_D[i] = 0;
14 P.i_E[i] = 0;
15 P.i_F[i] = 0;
16 }
17
18 if (u_R.num_row != u_S.num_row)
19 {
20 cout << "R、S不是同目关系" << endl;
21 return P;
22 }
23
24 for (i = 0; i < u_R.num_tuple; i++)
25 {
26 P.i_A[i] = u_S.i_A[i];
27 P.i_B[i] = u_S.i_B[i];
28 P.i_C[i] = u_S.i_C[i];
29 }
30
31 for (i = 0, k = u_R.num_tuple; i < u_R.num_tuple; i++)
32 {
33 for (j = 0; j < u_S.num_tuple; j ++)
34 {
35 if (u_R.i_A[i] == u_S.i_A[j]
36 && u_R.i_B[i] == u_S.i_B[j]
37 && u_R.i_C[i] == u_S.i_C[j])
38 {
39 break;
40 }
41 else
42 {
43 if (j == u_S.num_tuple -1)
44 {
45 P.i_A[k] = u_R.i_A[i];
46 P.i_B[k] = u_R.i_B[i];
47 P.i_C[k] = u_R.i_C[i];
48 (P.num_tuple)++;
49 k++;
50 }
51 }
52 }
53 }
54
55 return P;
56 }
(2)差(Difference):
//差
relate except_relate(relate e_R, relate e_S) //except
{
relate P;
int i = 0, j = 0, k = 0;
for (i = 0; i < 25; i++)
{
P.num_tuple = 0;
P.num_row = e_R.num_row;
P.i_A[i] = 0;
P.i_B[i] = 0;
P.i_C[i] = 0;
P.i_D[i] = 0;
P.i_E[i] = 0;
P.i_F[i] = 0;
}
if (e_R.num_row != e_S.num_row)
{
cout << "R、S不是同目关系" << endl;
return P;
}
for (i = 0, k = 0; i < e_R.num_tuple; i++)
{
for (j = 0; j < e_S.num_tuple; j ++)
{
if (e_R.i_A[i] == e_S.i_A[j]
&& e_R.i_B[i] == e_S.i_B[j]
&& e_R.i_C[i] == e_S.i_C[j])
{
break;
}
else
{
if (j == e_S.num_tuple -1)
{
P.i_A[k] = e_R.i_A[i];
P.i_B[k] = e_R.i_B[i];
P.i_C[k] = e_R.i_C[i];
(P.num_tuple)++;
k++;
}
}
}
}
return P;
}
(3)交(Interstation):
/****************************************************************
方法一:
*****************************************************************/
//交
relate intersect_relate(relate i_R, relate i_S) //intersection
{
relate P;
int i = 0, j = 0, k = 0;
for (i = 0; i < 25; i++)
{
P.num_tuple = 0;
P.num_row = i_R.num_row;
P.i_A[i] = 0;
P.i_B[i] = 0;
P.i_C[i] = 0;
P.i_D[i] = 0;
P.i_E[i] = 0;
P.i_F[i] = 0;
}
if ( i_R.num_row != i_S.num_row)
{
cout << "R、S不是同目关系" << endl;
return P;
}
for (i = 0, k =0; i < i_R.num_tuple; i++)
{
for (j = 0; j < i_S.num_tuple; j++)
{
if (i_R.i_A[i] == i_S.i_A[j]
&& i_R.i_B[i] == i_S.i_B[j]
&& i_R.i_C[i] == i_S.i_C[j])
{
P.i_A[k] = i_R.i_A[i];
P.i_B[k] = i_R.i_B[i];
P.i_C[k] = i_R.i_C[i];
(P.num_tuple)++;
k++;
}
}
}
return P;
}
对于交还有另一种算法:R n S = R - (R - S)
代码如下:
//***************************************************************
//
//方法二:
//
//***************************************************************
relate intersect_relate(relate i_R, relate i_S) //intersection
{
relate P;
int i = 0;
for (i = 0; i < 25; i++)
{
P.num_tuple = 0;
P.num_row = i_R.num_row;
P.i_A[i] = 0;
P.i_B[i] = 0;
P.i_C[i] = 0;
P.i_D[i] = 0;
P.i_E[i] = 0;
P.i_F[i] = 0;
}
if (i_R.num_row != i_S.num_row)
{
cout << "R、S不是同目关系" << endl;
return P;
}
P = except_relate(i_R,except_relate(i_R,i_S));
return P;
}
(4)笛卡尔积(Cartesian Product):
//笛卡尔积
relate car_pro_relate(relate c_R, relate c_S) //cartesian product
{
relate P;
int i = 0, j = 0, k = 0, n = 0;
for (i = 0; i < 25; i++)
{
P.num_tuple = c_R.num_tuple * c_S.num_tuple;
P.num_row = c_R.num_row + c_S.num_row;
P.i_A[i] = 0;
P.i_B[i] = 0;
P.i_C[i] = 0;
P.i_D[i] = 0;
P.i_E[i] = 0;
P.i_F[i] = 0;
}
for (i = 0; i < c_R.num_tuple * c_S.num_tuple; i++)
{
if (i < (j+1)*c_S.num_tuple)
{
P.i_A[i] = c_R.i_A[j];
P.i_B[i] = c_R.i_B[j];
P.i_C[i] = c_R.i_C[j];
P.i_D[i] = c_S.i_D[j];
P.i_E[i] = c_S.i_E[j];
P.i_F[i] = c_S.i_F[j];
}
if ((i+1) % c_S.num_tuple == 0)
{
j++;
}
}
for (i = 0; i < c_R.num_tuple * c_S.num_tuple; i++)
{
for (j = 0; j < c_S.num_tuple; j++)
{
if (i % c_S.num_tuple == j)
{
if (c_R.num_row == 1)
{
P.i_B[i] = c_S.i_A[j];
P.i_C[i] = c_S.i_B[j];
P.i_D[i] = c_S.i_C[j];
}
if (c_R.num_row == 2)
{
P.i_C[i] = c_S.i_A[j];
P.i_D[i] = c_S.i_B[j];
P.i_E[i] = c_S.i_C[j];
}
if (c_R.num_row == 3)
{
P.i_D[i] = c_S.i_A[j];
P.i_E[i] = c_S.i_B[j];
P.i_F[i] = c_S.i_C[j];
}
}
}
}
return P;
}
专门的关系运算:
(1)投影(Projection):
//投影,只提供实现在单个属性列上的投影
//R在S上的投影
// R
// A B C C
//-------------------------------
// 1 3 2 2
// 5 7 2 2
// 1 3 4 4
//--------------------------------
// PAI (R)
// (S)
relate project_relate(relate p_R, char ch) //projection
{
relate P;
int i = 0, j = 0, k = 0, n = 0;
for (i = 0; i < 25; i++)
{
P.num_tuple = p_R.num_tuple;
P.num_row = 1;
P.i_A[i] = 0;
P.i_B[i] = 0;
P.i_C[i] = 0;
P.i_D[i] = 0;
P.i_E[i] = 0;
P.i_F[i] = 0;
}
if (ch == 'A')
{
for (i = 0; i < p_R.num_tuple; i++)
{
P.i_A[i] = p_R.i_A[i];
}
}
if (ch == 'B')
{
for (i = 0; i < p_R.num_tuple; i++)
{
P.i_A[i] = p_R.i_B[i];
}
}
if (ch == 'C')
{
for (i = 0; i < p_R.num_tuple; i++)
{
P.i_A[i] = p_R.i_C[i];
}
}
//去除相同元素
for (i = 0; i < p_R.num_tuple; i++)
{
for (j = i+1; j < p_R.num_tuple; j++)
{
if (P.i_A[i] == P.i_A[j])
{
P.i_A[j] = 0;
}
}
}
for (i = 0, k= 0; i < P.num_tuple; i++)
{
if (P.i_A[i] != 0)
{
P.i_A[k] = P.i_A[i];
k++;
}
}
P.num_tuple = k;
return P;
}
(2)自然连接(Natural join):
//自然连接
relate join_relate(relate j_R, relate j_S) //join
{
relate P, Q;
int i = 0, j = 0, k = 0, n = 0;
for (i = 0; i < 25; i++)
{
P.num_tuple = 0;
P.num_row = 4; //R(A,B,C)和S(B,C,D)相同属性名是B、C,自然连接后剩下四个属性组
//R(A,B)和S(B,C,D)相同属性名是B,自然连接后4个属性组
P.i_A[i] = 0;
P.i_B[i] = 0;
P.i_C[i] = 0;
P.i_D[i] = 0;
P.i_E[i] = 0;
P.i_F[i] = 0;
Q.num_tuple = 0;
Q.num_row = 0;
Q.i_A[i] = 0;
Q.i_B[i] = 0;
Q.i_C[i] = 0;
Q.i_D[i] = 0;
Q.i_E[i] = 0;
Q.i_F[i] = 0;
}
for (i = 0; i < j_S.num_tuple; i++)//为了能使用笛卡尔积函数,将S(B,C,D,)转为S(A,B,C)
{
j_S.i_A[i] = j_S.i_B[i];
j_S.i_B[i] = j_S.i_C[i];
j_S.i_C[i] = j_S.i_D[i];
j_S.i_D[i] = j_S.i_E[i];
j_S.i_E[i] = j_S.i_F[i];
}
Q = car_pro_relate(j_R, j_S);
//R有三个属性组,且B,C为相同属性名
if (j_R.num_row == 3)
{
//相同属性名是B、C
// R S
// A B C B C D
//-> A B C A B C
//---------------------------
//Q: A B C D E F
//P: A B C D
for (i = 0,j = 0; i < Q.num_tuple; i++)
{
if (Q.i_B[i] == Q.i_D[i] && Q.i_C[i] == Q.i_E[i])
{
P.i_A[j] = Q.i_A[i];
P.i_B[j] = Q.i_B[i];
P.i_C[j] = Q.i_C[i];
P.i_D[j] = Q.i_D[i];
P.i_E[j] = Q.i_E[i];
P.i_F[j] = Q.i_F[i];
j++;
}
}
P.num_tuple = j;
for (i = 0; i < P.num_tuple; i++)
{
P.i_D[i] = P.i_F[i];
P.i_E[i] = 0;
P.i_F[i] = 0;
}
}
//相同属性名是B且R只有两个属性组
if (j_R.num_row == 2)
{
//相同属性名是B
// R S
//--------------------------
// A B | B C D
//-> A B | A B C
//Q: A B C D E
// \ / \ / \ /
// \ / \/ \/
//P: A B C D
for (i = 0,j = 0; i < Q.num_tuple; i++)
{
if (Q.i_B[i] == Q.i_C[i])
{
P.i_A[j] = Q.i_A[i];
P.i_B[j] = Q.i_B[i];
P.i_C[j] = Q.i_C[i];
P.i_D[j] = Q.i_D[i];
P.i_E[j] = Q.i_E[i];
P.i_F[j] = Q.i_F[i];
j++;
}
}
P.num_tuple = j;
for (i = 0; i < P.num_tuple; i++)
{
P.i_B[i] = P.i_C[i];
P.i_C[i] = P.i_D[i];
P.i_D[i] = P.i_E[i];
P.i_E[i] = 0;
}
}
return P;
}
(3)除法(Division):
// R / S的必要条件是:
//(1) R.num_tuple>S.num_tuple
//(2) S非空
//(3) R中存在S.num_tuple个属性与S的S.num_tuple个属性定义在相同的域中
relate div_relate(relate d_R ,relate d_S) //division
{
relate P;
int i = 0, j = 0, k = 0, n = 0;
for (i = 0; i < 25; i++)
{
P.num_tuple = 0;
P.num_row = d_R.num_row - d_S.num_row;
P.i_A[i] = 0;
P.i_B[i] = 0;
P.i_C[i] = 0;
P.i_D[i] = 0;
P.i_E[i] = 0;
P.i_F[i] = 0;
}
//R(A,B,C) S(A,B)
//X的属性组:C
//Y的属性组:A,B
P = project_relate(d_R, 'C');
P = car_pro_relate(d_S,P);
P = except_relate(P, d_R);
P = project_relate(P,'C');
P = except_relate(project_relate(d_R,'C'),P);
return P;
}
好了,写个主函数测试下:
//main.cpp #include "common.h" #include <iostream> using namespace std; //并、交、差 void UIE(void) { relate R, S, U, I, E; int i = 0; for (i = 0; i < 25; i++) { R.num_tuple = 3; //记录元组个数 R.num_row = 3; //记录列数 S.num_tuple = 3; //记录元组个数 S.num_row = 3; //记录列数 R.i_A[i] = 0; R.i_B[i] = 0; R.i_C[i] = 0; R.i_D[i] = 0; R.i_E[i] = 0; R.i_F[i] = 0; S.i_A[i] = 0; S.i_B[i] = 0; S.i_C[i] = 0; S.i_D[i] = 0; S.i_E[i] = 0; S.i_F[i] = 0; } // R | S //---------------------------------------- // A | B | C | A | B | C // 1 | 1 | 2 | 1 | 2 | 1 // 1 | 2 | 1 | 1 | 1 | 1 // 2 | 2 | 3 | 2 | 2 | 3 //---------------------------------------- // R.i_A[0] = 1; R.i_A[1] = 1; R.i_A[2] = 2; R.i_B[0] = 1; R.i_B[1] = 2; R.i_B[2] = 2; R.i_C[0] = 2; R.i_C[1] = 1; R.i_C[2] = 3; S.i_A[0] = 1; S.i_A[1] = 1; S.i_A[2] = 2; S.i_B[0] = 2; S.i_B[1] = 1; S.i_B[2] = 2; S.i_C[0] = 1; S.i_C[1] = 1; S.i_C[2] = 3; cout << "R:" << endl; for (i = 0; i < R.num_tuple; i++) { cout << R.i_A[i] << "\t" << R.i_B[i] << "\t" << R.i_C[i] <<endl; cout << "--------------------" << endl; } cout << "S:" << endl; for (i = 0; i < S.num_tuple; i++) { cout << S.i_A[i] << "\t" << S.i_B[i] << "\t" << S.i_C[i] <<endl; cout << "--------------------" << endl; } U = union_relate(R, S); cout << "\n" << "并:" << endl; for (i = 0; i < U.num_tuple; i++) { cout << U.i_A[i] << "\t" << U.i_B[i] << "\t" << U.i_C[i] <<endl; cout << "--------------------" << endl; } I = intersect_relate(R,S); cout << "交:" << endl; for (i = 0; i < I.num_tuple; i++) { cout << I.i_A[i] << "\t" << I.i_B[i] << "\t" << I.i_C[i] <<endl; cout << "--------------------" << endl; } E =except_relate(R,S); cout << "差:" << endl; for (i = 0; i < E.num_tuple; i++) { cout << E.i_A[i] << "\t" << E.i_B[i] << "\t" << E.i_C[i] <<endl; cout << "--------------------" << endl; } return; } //笛卡尔积 void Car_pro(void) { relate R, S, C; int i = 0; for (i = 0; i < 25; i++) { R.num_tuple = 2; //记录元组个数 R.num_row = 3; //记录列数 S.num_tuple = 3; //记录元组个数 S.num_row = 3; //记录列数 R.i_A[i] = 0; R.i_B[i] = 0; R.i_C[i] = 0; R.i_D[i] = 0; R.i_E[i] = 0; R.i_F[i] = 0; S.i_A[i] = 0; S.i_B[i] = 0; S.i_C[i] = 0; S.i_D[i] = 0; S.i_E[i] = 0; S.i_F[i] = 0; } // R | S //---------------------------------------- // A | B | C | A | B | C // 1 | 1 | 2 | 1 | 2 | 1 // 1 | 2 | 1 | 1 | 1 | 1 // 2 | 2 | 3 | 2 | 2 | 3 //---------------------------------------- // R.i_A[0] = 1; R.i_A[1] = 1; // R.i_A[2] = 2; R.i_B[0] = 1; R.i_B[1] = 2; // R.i_B[2] = 2; R.i_C[0] = 2; R.i_C[1] = 1; //R.i_C[2] = 3; S.i_A[0] = 1; S.i_A[1] = 1; S.i_A[2] = 2; S.i_B[0] = 2; S.i_B[1] = 1; S.i_B[2] = 2; S.i_C[0] = 1; S.i_C[1] = 1; S.i_C[2] = 3; cout << "\n" <<"R:" << endl; for (i = 0; i < R.num_tuple; i++) { cout << R.i_A[i] << "\t" << R.i_B[i] << "\t" << R.i_C[i] <<endl; cout << "--------------------" << endl; } cout << "S:" << endl; for (i = 0; i < S.num_tuple; i++) { cout << S.i_A[i] << "\t" << S.i_B[i] << "\t" << S.i_C[i] <<endl; cout << "--------------------" << endl; } C =car_pro_relate(R,S); cout << "\n" << "笛卡尔积:" << endl; for (i = 0; i < C.num_tuple; i++) { cout << C.i_A[i] << "\t" << C.i_B[i] << "\t" << C.i_C[i] << "\t" << C.i_D[i] << "\t" << C.i_E[i] << "\t" << C.i_F[i] <<endl; cout << "---------------------------------------------" << endl; } } //自然连接 void natural(void) { relate R, S, N; int i = 0; for (i = 0; i < 25; i++) { R.num_tuple = 4; //记录元组个数 R.num_row = 3; //记录列数 S.num_tuple = 3; //记录元组个数 S.num_row = 3; //记录列数 R.i_A[i] = 0; R.i_B[i] = 0; R.i_C[i] = 0; R.i_D[i] = 0; R.i_E[i] = 0; R.i_F[i] = 0; S.i_A[i] = 0; S.i_B[i] = 0; S.i_C[i] = 0; S.i_D[i] = 0; S.i_E[i] = 0; S.i_F[i] = 0; } // R | S //----------------------------------------------------- // A | B | C | B | C | D //----------------------------------------------------- // 2 | 4 | 6 | 5 | 7 | 3 // 3 | 5 | 7 | 4 | 6 | 2 // 7 | 4 | 6 | 5 | 7 | 9 // 5 | 4 | 7 | //---------------------------------------------------- R.i_A[0] = 2; R.i_A[1] = 3; R.i_A[2] = 7; R.i_A[3] = 5; R.i_B[0] = 4; R.i_B[1] = 5; R.i_B[2] = 4; R.i_B[3] = 4; R.i_C[0] = 6; R.i_C[1] = 7; R.i_C[2] = 6; R.i_C[3] = 7; S.i_B[0] = 5; S.i_B[1] = 4; S.i_B[2] = 5; S.i_C[0] = 7; S.i_C[1] = 6; S.i_C[2] = 7; S.i_D[0] = 3; S.i_D[1] = 2; S.i_D[2] = 9; cout << "\n" << "R:" << endl; cout << "R.A" << "\t" << "R.B" << "\t" << "R.C" <<endl; cout << "--------------------" << endl; for (i = 0; i < R.num_tuple; i++) { cout << R.i_A[i] << "\t" << R.i_B[i] << "\t" << R.i_C[i] <<endl; cout << "--------------------" << endl; } cout << "S:" << endl; cout << "S.B" << "\t" << "S.C" << "\t" << "S.D" <<endl; for (i = 0; i < S.num_tuple; i++) { cout << S.i_B[i] << "\t" << S.i_C[i] << "\t" << S.i_D[i] <<endl; cout << "--------------------" << endl; } N = join_relate(R, S); cout << "\n" << "自然连接:" << endl; cout << "A" << "\t" << "B" << "\t" << "C" << "\t" << "D" <<endl; cout << "-----------------------------" << endl; for (i = 0; i < N.num_tuple; i++) { cout << N.i_A[i] << "\t" << N.i_B[i] << "\t" << N.i_C[i] << "\t" << N.i_D[i] <<endl; cout << "-----------------------------" << endl; } return; } //除法 void div(void) { relate R, S, D; int i = 0; for (i = 0; i < 25; i++) { R.num_tuple = 5; //记录元组个数 R.num_row = 3; //记录列数 S.num_tuple = 2; //记录元组个数 S.num_row = 2; //记录列数 R.i_A[i] = 0; R.i_B[i] = 0; R.i_C[i] = 0; R.i_D[i] = 0; R.i_E[i] = 0; R.i_F[i] = 0; S.i_A[i] = 0; S.i_B[i] = 0; S.i_C[i] = 0; S.i_D[i] = 0; S.i_E[i] = 0; S.i_F[i] = 0; } // R | S //---------------------------------------------- // A | B | C | A | B //---------------------------------------------- // 1 | 3 | 2 | 1 | 3 // 5 | 7 | 2 | 5 | 7 // 1 | 3 | 4 | // 1 | 3 | 6 | // 5 | 7 | 6 | //----------------------------------------------- R.i_A[0] = 1; R.i_A[1] = 5; R.i_A[2] = 1; R.i_A[3] = 1; R.i_A[4] = 5; R.i_B[0] = 3; R.i_B[1] = 7; R.i_B[2] = 3; R.i_B[3] = 3; R.i_B[4] = 7; R.i_C[0] = 2; R.i_C[1] = 2; R.i_C[2] = 4; R.i_C[3] = 6; R.i_C[4] = 6; S.i_A[0] = 1; S.i_A[1] = 5; S.i_B[0] = 3; S.i_B[1] = 7; cout << "\n" << "R:" << endl; cout << "R.A" << "\t" << "R.B" << "\t" << "R.C" <<endl; cout << "--------------------" << endl; for (i = 0; i < R.num_tuple; i++) { cout << R.i_A[i] << "\t" << R.i_B[i] << "\t" << R.i_C[i] <<endl; cout << "--------------------" << endl; } cout << "S:" << endl; cout << "S.A" << "\t" << "S.B" <<endl; for (i = 0; i < S.num_tuple; i++) { cout << S.i_A[i] << "\t" << S.i_B[i] <<endl; cout << "-------------" << endl; } D = div_relate(R, S); cout << "\n" << "除法:" << endl; cout << "\t" << "C" <<endl; cout << "----------------" << endl; for (i = 0; i < D.num_tuple; i++) { cout << "\t" << D.i_A[i] <<endl; cout << "----------------" << endl; } cout << "\t\t\tOK! \n\t功能基本上实现了,但还是有多处不能推广(比如说有些属性组被限制)!\n " << endl; return; } int main() { UIE(); Car_pro(); natural(); div(); return 0; }
好,commom.h里的内容只是声明函数和定义结构体:
//commom.h #ifndef COMMON_H_INCLUDED #define COMMON_H_INCLUDED typedef struct { int num_tuple; //记录元组个数 int num_row; //记录列数,即:目 int i_A[25]; int i_B[25]; int i_C[25]; int i_D[25]; int i_E[25]; int i_F[25]; }relate; relate union_relate(relate u_R, relate u_S); //union relate except_relate(relate e_R, relate e_S); //except relate intersect_relate(relate i_R, relate i_S); //intersection relate car_pro_relate(relate c_R, relate c_S); //cartesian product relate project_relate(relate p_R, char ch); //projection relate join_relate(relate j_R, relate j_S); //join relate div_relate(relate d_R ,relate d_S); //division #endif // COMMON_H_INCLUDED
看看效果:
作者:涵曦(www.hanxi.cc)
出处:hanxi.cnblogs.com
GitHub:github.com/hanxi
Email:im.hanxi@gmail.com
文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
《 Skynet 游戏服务器开发实战》
-
学习地址:
-
优惠推荐码:
2CZ2UA5u
-
可以先免费试学前 2 章内容