实验要求
关系矩阵的初始化和打印
我们将关系矩阵存入一个二维数组中,因为集合元素个数不会超过5个所以就用一个5行5列二维数组来表示。
在我们得到了集合元素个数之后我们就可以对数组进行0,1随机赋值
//初始关系矩阵
void init_matrix(int array[][5], int n)
{for (int i = 0; i < n; i++){for (int j = 0; j < n; j++){array[i][j] = rand() % 2;}}
}//打印矩阵
void print_matrix(int array[][5], int n)
{for (int i = 0; i < n; i++){for (int j = 0; j < n; j++){printf("%d ", array[i][j]);}printf("\n");}
}
验证自反性
自反性的验证就是主对角线上元素是不是全为1。
//验证自反性
void judge_introspect(int array[][5], int n)
{n--;while (n >= 0){if (array[n][n] != 1){printf("不满足自反性,因为第%d行第%d列应该为1\n", n+1, n+1);return;} n--;}printf("满足自反性\n");
}
验证反自反性
反自反性的验证就是主对角线上元素只要有一个不是1那就满足反自反性。
//验证反自反性
void judge_irreflexive(int array[][5], int n)
{n--;while (n >= 0){if (array[n][n] != 0){printf("不满足反自反性,因为第%d行第%d列应该为0\n", n+1, n+1);return;}n--;}printf("满足反自反性\n");
}
验证对称性
对称性就是关于主对角线对称相等。
//验证对称性
void judge_symmetry(int array[][5], int n)
{for (int i = 0; i < n; i++){for (int j = 0; j < n; j++){if (array[i][j] != array[j][i]){printf("不满足对称性,因为第%d行第%d列和第%d行第%d列不同时为%d\n", i+1, j+1, j+1, i+1, array[i][j]);return;}}}printf("满足对称性\n");
}
验证反对称性
反对称性就是如果两个元素相等,那么它一定是行列相等的元素。
//验证反对称性
void judge_antisymmetry(int array[][5], int n)
{for (int i = 0; i < n; i++){for (int j = 0; j < n; j++){if (array[i][j] == array[j][i] && i != j){printf("不满足反对称性,因为第%d行第%d列和第%d行第%d列同时为%d\n", i+1, j+1, j+1, i+1, array[i][j]);return;}}}printf("满足反对称性\n");
}
验证传递性
就是arr[a][b] == 1并且arr[a][c] == 1并且arr[c][b] == 1。我在这用了三个循环来判断,是最笨的方法,如果有好办法请给在评论区分享一下。
//验证传递性
void judge_transfer(int array[][5], int n)
{int flag = 0;for (int i = 0; i < n; i++){for (int j = 0; j < n; j++){if (array[i][j] == 1 && i !=j){for(int m = 0; m < n; m++){if (array[i][m] == 1 && array[m][j] == 1 && m != i && m != j){printf("满足传递性因为第%d行第%d列为1,第%d行第%d列为1,第%d行第%d列也为1\n", i + 1, m + 1, m + 1, j + 1, i + 1, j + 1);flag++;break;}}}}}if (flag == 0){for (int i = 0; i < n; i++){for (int j = 0; j < n; j++){if (array[i][j] == 1 && i != j){int m = 0;while (1){m = rand() % n;if (m != i && m != j){break;}}printf("不满足传递性因为第%d行第%d列为%d,第%d行第%d列为%d,第%d行第%d列也为%d\n",i + 1, m + 1,array[i][m], m + 1, j + 1,array[m][j], i + 1, j + 1,array[i][j]);}}}}
}
源码
# define _CRT_SECURE_NO_WARNINGS 1;
#include<stdio.h>
#include<time.h>
#include<stdlib.h>//初始关系矩阵
void init_matrix(int array[][5], int n)
{for (int i = 0; i < n; i++){for (int j = 0; j < n; j++){array[i][j] = rand() % 2;}}
}//打印矩阵
void print_matrix(int array[][5], int n)
{for (int i = 0; i < n; i++){for (int j = 0; j < n; j++){printf("%d ", array[i][j]);}printf("\n");}
}
//验证自反性
void judge_introspect(int array[][5], int n)
{n--;while (n >= 0){if (array[n][n] != 1){printf("不满足自反性,因为第%d行第%d列应该为1\n", n+1, n+1);return;} n--;}printf("满足自反性\n");
}
//验证反自反性
void judge_irreflexive(int array[][5], int n)
{n--;while (n >= 0){if (array[n][n] != 0){printf("不满足反自反性,因为第%d行第%d列应该为0\n", n+1, n+1);return;}n--;}printf("满足反自反性\n");
}
//验证对称性
void judge_symmetry(int array[][5], int n)
{for (int i = 0; i < n; i++){for (int j = 0; j < n; j++){if (array[i][j] != array[j][i]){printf("不满足对称性,因为第%d行第%d列和第%d行第%d列不同时为%d\n", i+1, j+1, j+1, i+1, array[i][j]);return;}}}printf("满足对称性\n");
}
//验证反对称性
void judge_antisymmetry(int array[][5], int n)
{for (int i = 0; i < n; i++){for (int j = 0; j < n; j++){if (array[i][j] == array[j][i] && i != j){printf("不满足反对称性,因为第%d行第%d列和第%d行第%d列同时为%d\n", i+1, j+1, j+1, i+1, array[i][j]);return;}}}printf("满足反对称性\n");
}
//验证传递性
void judge_transfer(int array[][5], int n)
{int flag = 0;for (int i = 0; i < n; i++){for (int j = 0; j < n; j++){if (array[i][j] == 1 && i !=j){for(int m = 0; m < n; m++){if (array[i][m] == 1 && array[m][j] == 1 && m != i && m != j){printf("满足传递性因为第%d行第%d列为1,第%d行第%d列为1,第%d行第%d列也为1\n", i + 1, m + 1, m + 1, j + 1, i + 1, j + 1);flag++;break;}}}}}if (flag == 0){for (int i = 0; i < n; i++){for (int j = 0; j < n; j++){if (array[i][j] == 1 && i != j){int m = 0;while (1){m = rand() % n;if (m != i && m != j){break;}}printf("不满足传递性因为第%d行第%d列为%d,第%d行第%d列为%d,第%d行第%d列也为%d\n",i + 1, m + 1,array[i][m], m + 1, j + 1,array[m][j], i + 1, j + 1,array[i][j]);}}}}
}
int main()
{srand((unsigned int)time(NULL));//用随机数生成集合数int num = rand() % 5 + 1;int relationMatrix[5][5] = { 0 };//关系矩阵init_matrix(relationMatrix, num);//初始关系矩阵print_matrix(relationMatrix, num);//打印矩阵judge_introspect(relationMatrix, num);//验证自反性judge_irreflexive(relationMatrix, num);//验证反自反性judge_symmetry(relationMatrix, num);//验证对称性judge_antisymmetry(relationMatrix, num);//验证反对称性judge_transfer(relationMatrix, num);//验证传递性return 0;
}