C中的除法误差
我有一个图形程序,里面有一些计算。 它没有给出任何错误,但在程序运行后,它给出了6/4除法错误。 有人能帮我吗C中的除法误差,c,graphics,C,Graphics,我有一个图形程序,里面有一些计算。 它没有给出任何错误,但在程序运行后,它给出了6/4除法错误。 有人能帮我吗 #include<stdio.h> #include<conio.h> #include<graphics.h> #include<stdlib.h> struct face { int **vertices; }; void main() { int gd = DETECT,gm; int x, y, z,
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
struct face
{
int **vertices;
};
void main()
{
int gd = DETECT,gm;
int x, y, z, i, j, k, m, n;
struct face *f_3d, *f;
FILE *fp;
char c;
initgraph(&gd, &gm, "d:/tc/bgi");
fp=fopen("cube.txt", "r");
clrscr();
if(fp == NULL)
{
printf("\nFile doesn't exist");
exit(0);
}
fscanf(fp, "%d", &m);
fseek(fp, 1, SEEK_CUR); //no of faces
fscanf(fp, "%d", &n); //no of vertices for each face
printf("\n%d %d", m, n);
f = (struct face *) malloc(sizeof(m*sizeof(struct face)));
f_3d = (struct face *) malloc(sizeof(m*sizeof(struct face)));
for(i = 0; i < m; i++)
{
f[i].vertices = (int **) malloc(n*sizeof(int));
f_3d[i].vertices = (int **) malloc(n*sizeof(int));
}
for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
{
f[i].vertices[j] = (int *) malloc(2*sizeof(int));
f_3d[i].vertices[j] = (int *) malloc(3*sizeof(int));
}
for(i = 0; i < m; i++) //for every face
for(j = 0;j < n; j++) // for every vertex in the face
{
fscanf(fp, "%d", &f_3d[i].vertices[j][0]); //read x coord
fseek(fp, 1, SEEK_CUR); //to skip comma
fscanf(fp, "%d", &f_3d[i].vertices[j][1]); //read y coord
fseek(fp, 1, SEEK_CUR);
fscanf(fp, "%d" ,&f_3d[i].vertices[j][2]); //read y coord
fseek(fp, 1, SEEK_CUR);
}
for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
{
x=f_3d[i].vertices[j][0];
y=f_3d[i].vertices[j][1];
z=f_3d[i].vertices[j][2];
f[i].vertices[j][0]=(int)x/z;
f[i].vertices[j][1]=(int)y/z;
}
for(i = 0; i < 6; i++)
{
for(j = 0; j < 3; j++)
line(f[i].vertices[j][0], f[i].vertices[j][1], f[i].vertices[j+1][0], f[i].vertices[j+1][1]);
line(f[i].vertices[j][0], f[i].vertices[j][1], f[i].vertices[0][0], f[i].vertices[0][1]);
}
getch();
}
编辑:从评论中添加其他信息:
分配给x和y的数组的所有值都是整数值。因此,分配的最终值也应仅为整数。所以,我想知道除法错误的原因
cube.txt包含形式为400200,2、300400,2等的顶点。我正在Turbo C中运行此程序。我正在执行400/2和200/2,其中x=400、y=200和z=2,并将其分配给f[I]。顶点[j][0]=x/z和f[I]。顶点[j][1]=y/z?。没有这样的错误。但当程序运行时,不会看到输出。我希望看到f[I].顶点数组的输出。我猜问题出在这里:
for(i=0; i<m; i++)
for(j=0; j<n; j++)
{
x=f_3d[i].vertices[j][0];
y=f_3d[i].vertices[j][1];
z=f_3d[i].vertices[j][2];
f[i].vertices[j][0]=(int)x/z;
f[i].vertices[j][1]=(int)y/z;
}
可能在某个点上z=0。尝试添加一些调试代码:
for(i=0; i<m; i++)
for(j=0; j<n; j++)
{
x=f_3d[i].vertices[j][0];
y=f_3d[i].vertices[j][1];
z=f_3d[i].vertices[j][2];
if (z == 0)
{
printf("ERROR: i = %d, j = %d, x = %d, y = %d, z = %d\n", i, j, x, y, z);
exit(1);
}
f[i].vertices[j][0]=(int)x/z;
f[i].vertices[j][1]=(int)y/z;
}
当某些除法被0“零”除法时,会发生错误除法错误。试着找出这种划分发生的地方并修改这些部分。好吧,请帮助我们帮助您-错误在哪里???我想这是为了示例,但请记住释放您已删除的内存。@hamsa:您得到的确切错误是什么?错误输出是什么?发生了什么意想不到的事情?你期望会发生什么呢?为什么不告诉我们cube.txt的内容是什么样的,这样人们就有机会运行代码了?你总是用这样糟糕的方式编写代码吗?从未听说过“缩进”??谢谢你的建议。我的cube.txt看起来像这样。它没有任何z值设置为0。其内容如下-6,4 400200,2 600200,2 750450,3 600600,4 400200,2 600200,2 600400,2 600600,3 750450,3 600200,2 600400,2 500500,2 600600,3 600400,2 500500,2 450750,3 600600,4 400200,2 600600,3 450750,3 600600,4 750450,3因此,我认为不能被零除错误。但问题可能在于您的文件读取代码-请尝试printf调试建议,如果它捕获到0,那么您可以从那里返回。