这是我用于查找两点之间距离的代码。我的任务是强制使用函数,因此是一个单独的用于查找距离的函数。当我执行代码时,我没有收到任何语法错误,只有一个闪烁的光标。我是 C 的初学者。
我的代码:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
float distance(float *x1, float *y1, float *x2, float *y2);
int main()
{ printf("Input coordinates as x1,y1,x2,y2:\n");
float x1,y1,x2,y2;
scanf("%f %f %f %f ",&x1,&y1,&x2,&y2);
printf("Coordinates are : (%f,%f) and (%f,%f)\n",x1,y1,x2,y2);
printf("%f",distance (&x1,&y1,&x2,&y2));
return 0;
}
float distance(float *x1, float *y1, float *x2, float *y2)
{
float d= sqrt(pow((*x1-*x2),2)+pow((*y1-*y2),2));
printf("%f", d);
return d;
}
Run Code Online (Sandbox Code Playgroud)
编译时: gcc -o 1 1.c -lm 输出:
Input coordinates as x1,y1,x2,y2:
1 2 3 4
Run Code Online (Sandbox Code Playgroud)
就是这样。光标一直闪烁而不提供任何输出。
此外,我尝试在不同的点放入打印语句。之后的任何打印scanf …