问题来源
http://topic.csdn.net/u/20080421/19/2767192b-376d-4709-9441-11a2bcda2a88.html
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*
* 判断自反
*/
bool Reflexive(int a[100][100], int n)
{
int i;
for(i = 0; i < n; i++)
if(a[i][i] == 0)
return false;
return true;
}
/*
* 判断反自反
*/
bool Reflexiveness(int a[100][100], int n)
{
int i;
for(i = 0; i < n; i++)
if(a[i][i] == 1)
return false;
return true;
}
/*
* 判断是否对称
*/
bool Symmetry(int a[100][100], int n)
{
int i, j;
for(i = 0; i < n - 1; i++){
for(j = i + 1; j < n; j++){
if(a[i][j] != a[j][i])
return false;
}
}
return true;
}
/*
* 判断是否反对称
*/
bool Antisymmetry(int a[100][100], int n)
{
int i, j;
for(i = 0; i < n - 1; i++){
for(j = i + 1; j < n; j++){
if((a[i][j] == 1 && a[j][i] == 1) && (i != j))
return false;
}
}
return true;
}
/*
* 判断是否传递
*/
bool Transitive(int a[100][100], int n)
{
int i, j, k;
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
if(a[i][j] == 1){ /* 若ij存在关系,则对所有与 j 存在关系的必与 i 存在关系,是为传递 */
for(k = 0; k < n;k++){
if(a[j][k] == 1 && a[i][k] != 1)
return false;
}
}
}
}
return true;
}
/*
* 判断是否为空
*/
bool isEmptySet(int a[100][100], int n){
int i,j;
for(i = 0;i < n;i++){
for(j = 0;j < n;j++)
if(a[i][j])
return false;
}
return true;
}
int main(int argc,char *argv[])
{
FILE *fp;
int i = 0, j, n;
int b[10000], a[100][100];
if(argc < 2){
printf("Usage :/n<程序名> <input-file-name>/n/n");
exit(0);
}
fp = fopen(argv[1], "r");
if(!fp){
printf("Can not open !");
exit(0);
}
while(!feof(fp)){
fscanf(fp, "%d", b + i++);
}
fclose(fp);
n = (int)sqrt(i);
/* 直接用坐标映射,不需要再次读取文件 */
for(j = 0;j < i;j++){
a[j/n][j % n] = b[j];
}
printf("The relation is:/n");
for(i = 0; i < n; i++){
for(j = 0; j < n; j++)
printf("%-3d", a[i][j]);
printf("/n");
}
if(isEmptySet(a,n))
printf("<Empty Set>/n");
else{
if(!Reflexive(a, n)) printf("not "); printf("reflexive, ");
if(!Reflexiveness(a, n))printf("not "); printf("reflexiveneww, ");
if(!Symmetry(a, n)) printf("not "); printf("symmetry, ");
if(!Antisymmetry(a, n)) printf("not "); printf("antisymmetry, ");
if(!Transitive(a, n)) printf("not "); printf("transitive./n");
}
return 1;
}
测试例 1: 1 0 1 1 0 1 0 1 0 0 0 0 0 0 0 1 输出: not reflexive, not reflexiveneww, not symmetry, antisymmetry, transitive. 测试例 2: 1 0 1 0 0 1 0 1 1 0 1 0 0 0 0 1 输出: reflexive, not reflexiveneww, not symmetry, not antisymmetry, transitive. 测试例 3: 0 0 1 0 0 0 0 1 1 0 0 0 0 1 0 0 输出: not reflexive, reflexiveneww, symmetry, not antisymmetry, not transitive.