程序中途停止,除非我按 ctrl+c,否则它不会终止
Program stopped midway and it is not terminating unless I press ctrl+c
这是我的代码,用于通过使用邻接矩阵查找关系是否等价并计算等价性 class
#include <stdio.h>
#include <string.h>
int mat[10][10], setln, relln, n, i, j;
char set[10], rel[50];
int checkrel(char);
int reflexive();
int symmetric();
int transitive();
int display();
int adjacency();
void main()
{
int check = 1, r, s, t;
printf("Enter the no. of elements in the set: ");
scanf("%d", &n);
gets(set);
printf("Enter the elements of the set: ");
fgets(set, 10, stdin);
setln = strlen(set);
while (check == 1)
{
printf("\nEnter the elements of relation in the manner '(a,b)(b,d)...':\n");
fgets(rel, 50, stdin);
relln = strlen(rel);
if (relln < 5)
continue;
for (i = 0; i < relln - 5; i + 5)
{
if (rel[0] == '(' && (checkrel(rel[1])) && rel[2] == ',' && (checkrel(rel[3])) && rel[4] == ')')
check = 0;
else
{
printf("Enter a valid relation");
break;
}
}
}
adjacency();
r = reflexive();
s = symmetric();
t = transitive();
if (r == 1)
printf("\nThe relation is reflexive");
else
printf("\nThe relation is not reflexive");
if (s == 1)
printf("\nThe relation is symmetric");
else
printf("\nThe relation is not symmetric");
if (t == 1)
printf("\nThe relation is transitive");
else
printf("\nThe relation is not transitive");
if (r == 1 && s == 1 && t == 1)
printf("\nTherefore the relation is equivalent");
else
printf("\nTherefore the relation is not equivalent");
int display();
getchar();
}
int checkrel(char a)
{
int m;
for (int i = 0; i < setln; i++)
{
if (a == set[i])
m = 1;
}
if (m == 1)
return 1;
else
return 0;
}
int adjacency()
{
int a, b;
for (i = 1, j = 3; i < relln - 3; i + 5, j + 5)
{
for (int k = 0; k < setln; k++)
{
if (rel[i] == set[k])
a = k;
}
for (int l = 0; l < setln; l++)
{
if (rel[j] == set[l])
b = l;
}
mat[a][b] = 1;
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (mat[i][j] != 1)
mat[i][j] = 0;
}
}
}
int reflexive()
{
for (i = 0; i < n; i++)
{
if (mat[i][i] != 1)
{
return 0;
}
}
return 1;
}
int symmetric()
{
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (mat[i][j] != mat[j][i])
{
printf("\nThe relation is not equivalent as it is not symmetric");
return 0;
}
}
}
return 1;
}
int transitive()
{
int k;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (mat[i][j] == 1)
{
for (k = 0; k < n; k++)
{
if (mat[j][k] == 1 && mat[i][k] != 1)
{
printf("\nThe relation is not equivalent as it is not transitive");
return 0;
}
}
}
}
}
return 1;
}
int display()
{
printf("\nThe equivalence class is:\n{");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (mat[i][j] == 1)
printf("(%c,%c)", set[i], set[j]);
}
}
printf("}\n");
}
并且 vscode 终端中的输出在此之后停止并且不会终止
Enter the no. of elements in the set: 3
Enter the elements of the set: 123
Enter the elements of relation in the manner '(a,b)(b,d)...':
(1,1)(2,2)
^C
D:\c>
光标固定在行(1,1)(2,2)之后的换行符,直到我按ctrl+c
我最后使用了 getchar() 但它不起作用
如果您在启用警告的情况下进行编译(例如 -Wall
),编译器会标记您的问题:
orig.c:11:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
void main()
^~~~
orig.c: In function ‘main’:
orig.c:16:2: warning: implicit declaration of function ‘gets’; did you mean ‘fgets’? [-Wimplicit-function-declaration]
gets(set);
^~~~
fgets
orig.c:27:34: warning: statement with no effect [-Wunused-value]
for (i = 0; i < relln - 5; i + 5)
~~^~~
orig.c: In function ‘adjacency’:
orig.c:78:43: warning: left-hand operand of comma expression has no effect [-Wunused-value]
for (i = 1, j = 3; i < relln - 3; i + 5, j + 5)
^
orig.c:78:43: warning: statement with no effect [-Wunused-value]
orig.c:100:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
orig.c: In function ‘display’:
orig.c:161:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
/tmp/ccIGyhw4.o: In function `main':
orig.c:(.text+0x45): warning: the `gets' function is dangerous and should not be used.
注意第 27 行和第 78 行的警告。
变化:
for (i = 0; i < relln - 5; i + 5)
进入:
for (i = 0; i < relln - 5; i += 5)
同样,更改:
for (i = 1, j = 3; i < relln - 3; i + 5, j + 5)
进入:
for (i = 1, j = 3; i < relln - 3; i += 5, j += 5)
在这两种情况下,您都有什么都不做的增量子句。所以,你有无限循环。
同时修复其他警告。而且,从不使用gets
这是我的代码,用于通过使用邻接矩阵查找关系是否等价并计算等价性 class
#include <stdio.h>
#include <string.h>
int mat[10][10], setln, relln, n, i, j;
char set[10], rel[50];
int checkrel(char);
int reflexive();
int symmetric();
int transitive();
int display();
int adjacency();
void main()
{
int check = 1, r, s, t;
printf("Enter the no. of elements in the set: ");
scanf("%d", &n);
gets(set);
printf("Enter the elements of the set: ");
fgets(set, 10, stdin);
setln = strlen(set);
while (check == 1)
{
printf("\nEnter the elements of relation in the manner '(a,b)(b,d)...':\n");
fgets(rel, 50, stdin);
relln = strlen(rel);
if (relln < 5)
continue;
for (i = 0; i < relln - 5; i + 5)
{
if (rel[0] == '(' && (checkrel(rel[1])) && rel[2] == ',' && (checkrel(rel[3])) && rel[4] == ')')
check = 0;
else
{
printf("Enter a valid relation");
break;
}
}
}
adjacency();
r = reflexive();
s = symmetric();
t = transitive();
if (r == 1)
printf("\nThe relation is reflexive");
else
printf("\nThe relation is not reflexive");
if (s == 1)
printf("\nThe relation is symmetric");
else
printf("\nThe relation is not symmetric");
if (t == 1)
printf("\nThe relation is transitive");
else
printf("\nThe relation is not transitive");
if (r == 1 && s == 1 && t == 1)
printf("\nTherefore the relation is equivalent");
else
printf("\nTherefore the relation is not equivalent");
int display();
getchar();
}
int checkrel(char a)
{
int m;
for (int i = 0; i < setln; i++)
{
if (a == set[i])
m = 1;
}
if (m == 1)
return 1;
else
return 0;
}
int adjacency()
{
int a, b;
for (i = 1, j = 3; i < relln - 3; i + 5, j + 5)
{
for (int k = 0; k < setln; k++)
{
if (rel[i] == set[k])
a = k;
}
for (int l = 0; l < setln; l++)
{
if (rel[j] == set[l])
b = l;
}
mat[a][b] = 1;
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (mat[i][j] != 1)
mat[i][j] = 0;
}
}
}
int reflexive()
{
for (i = 0; i < n; i++)
{
if (mat[i][i] != 1)
{
return 0;
}
}
return 1;
}
int symmetric()
{
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (mat[i][j] != mat[j][i])
{
printf("\nThe relation is not equivalent as it is not symmetric");
return 0;
}
}
}
return 1;
}
int transitive()
{
int k;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (mat[i][j] == 1)
{
for (k = 0; k < n; k++)
{
if (mat[j][k] == 1 && mat[i][k] != 1)
{
printf("\nThe relation is not equivalent as it is not transitive");
return 0;
}
}
}
}
}
return 1;
}
int display()
{
printf("\nThe equivalence class is:\n{");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (mat[i][j] == 1)
printf("(%c,%c)", set[i], set[j]);
}
}
printf("}\n");
}
并且 vscode 终端中的输出在此之后停止并且不会终止
Enter the no. of elements in the set: 3
Enter the elements of the set: 123
Enter the elements of relation in the manner '(a,b)(b,d)...':
(1,1)(2,2)
^C
D:\c>
光标固定在行(1,1)(2,2)之后的换行符,直到我按ctrl+c 我最后使用了 getchar() 但它不起作用
如果您在启用警告的情况下进行编译(例如 -Wall
),编译器会标记您的问题:
orig.c:11:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
void main()
^~~~
orig.c: In function ‘main’:
orig.c:16:2: warning: implicit declaration of function ‘gets’; did you mean ‘fgets’? [-Wimplicit-function-declaration]
gets(set);
^~~~
fgets
orig.c:27:34: warning: statement with no effect [-Wunused-value]
for (i = 0; i < relln - 5; i + 5)
~~^~~
orig.c: In function ‘adjacency’:
orig.c:78:43: warning: left-hand operand of comma expression has no effect [-Wunused-value]
for (i = 1, j = 3; i < relln - 3; i + 5, j + 5)
^
orig.c:78:43: warning: statement with no effect [-Wunused-value]
orig.c:100:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
orig.c: In function ‘display’:
orig.c:161:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
/tmp/ccIGyhw4.o: In function `main':
orig.c:(.text+0x45): warning: the `gets' function is dangerous and should not be used.
注意第 27 行和第 78 行的警告。
变化:
for (i = 0; i < relln - 5; i + 5)
进入:
for (i = 0; i < relln - 5; i += 5)
同样,更改:
for (i = 1, j = 3; i < relln - 3; i + 5, j + 5)
进入:
for (i = 1, j = 3; i < relln - 3; i += 5, j += 5)
在这两种情况下,您都有什么都不做的增量子句。所以,你有无限循环。
同时修复其他警告。而且,从不使用gets