[英]Finding Distance Between Euclidean Distance Between Two Points C
我正在嘗試編寫一個 function 來計算兩個浮點之間的歐氏距離。 我編碼了 function 並調用了它,但出現了一些錯誤。 我該如何解決這些問題?
我的代碼:
#include <stdio.h>
#include <math.h>
//Function Declarations
float CalculateDistance(float, float, float, float);
int main ()
{
/* Function Call */
float distance1 = CalculateDistance(float x1,float y1,float x2,float y2);
printf(" Point1 Point 2 Distance\n");
printf("(%4.1f, %4.1f)", x1, y1);
printf("(%4.1f, %4.1f)", x2, y2);
printf("%4.1f\n", distance1);
return 0;
}
//Distance Function
double CalculateDistance(float x1, float y1, float x2, float y2)
{ printf("Enter X1 : ");
scanf("%f",&x1);
printf("Enter Y1 : ");
scanf("%f",&y1);
/////////////////////////////////////
printf("Enter X2 : ");
scanf("%f",&x2);
printf(" Enter Y2 : ");
scanf("%f",&y2);
//////////////////////////////////////
double diffx = x1 - x2;
double diffy = y1 - y2;
double diffx_sqr = pow(diffx,2);
double diffy_sqr = pow(diffy,2);
double distance = sqrt(diffx_sqr + diffy_sqr);
return distance;
}
我得到的錯誤:
main.c:18:37: error: expected expression before ‘float’
18 | float distance1 = CalculateDistance(float x1,float y1,float x2,float y2);
| ^~~~~
main.c:18:19: error: too few arguments to function ‘CalculateDistance’
18 | float distance1 = CalculateDistance(float x1,float y1,float x2,float y2);
| ^~~~~~~~~~~~~~~~~
main.c:13:7: note: declared here
13 | float CalculateDistance(float, float, float, float);
| ^~~~~~~~~~~~~~~~~
main.c:22:26: error: ‘x1’ undeclared (first use in this function); did you mean ‘y1’?
22 | printf("(%4.1f, %4.1f)", x1, y1);
| ^~
| y1
main.c:22:26: note: each undeclared identifier is reported only once for each function it appears in
main.c:23:26: error: ‘x2’ undeclared (first use in this function)
我是 C 的新人。非常感謝您的幫助。
首先,我要說明您的第一個問題對代碼的外觀不夠關心。 遠非虛榮心的問題,代碼的清晰度始於足夠的縮進,這對於您更清楚地了解正在發生的事情以及最終會接觸到您的代碼的其他人來說都是非常相關的(這種情況是一個很好的例子: ))。 web 上有很多關於為什么縮進很重要的資源。 我在快速搜索中發現的一些是這個和這個。
對於您的代碼:其中一些錯誤消息確實不是很直觀,並且可能會使沒有該語言經驗的人感到困惑。 但其他人很清楚發生了什么。 即:
main.c:22:26: error: ‘x1’ undeclared (first use in this function); did you mean ‘y1’?
和
main.c:23:26: error: ‘x2’ undeclared (first use in this function)
關於這兩個錯誤,事實是編譯器無法識別變量x1
和x2
在哪里聲明。 您嘗試在printf
調用中使用它們,但在當前 scope 中沒有定義(即當前邏輯塊,在這種特定情況下為main
函數)。
在您的代碼中沒有任何地方聲明x1
、 x2
、 y1
和y2
變量。 修復代碼的第一步就是這樣做。
另一件可能不是您想要做的事情(假定 function 期望數據作為其參數)是將用戶輸入集合添加到CalculateDistance
function。鑒於其名稱,人們會期望其目的是計算距離,僅此而已。 將代碼拆分為小的、特定的函數是一種很好的做法,並且職責很少。
另一個問題是您在第 6 行用float
類型聲明了CalculateDistance
function,並在第 25 行用double
類型定義了它。
最后,最后一個(不會阻止您編譯代碼的東西)是您正在使用float
類型定義x
和y
變量,並且在CalculateDistance
function 中您正在使用double
s。 這稱為隱式轉換,可能是有時難以調試的錯誤的來源。 在這種特定情況下,它可能不會,因為您將float
數據移動到double
類型,它以更高或相同的精度保存值,但重要的是您要意識到這一點並盡量避免它。 而且,在您知道發生了什么並且想要/需要在不同類型之間進行轉換的情況下,一個好習慣是明確地進行轉換,這樣您和看到您的代碼的任何人都清楚這是有意的。
這是您的代碼的潛在版本,其中應用了我上面提到的更改:
#include <stdio.h>
#include <math.h>
// Fixed declaration type
double CalculateDistance(float, float, float, float);
int main ()
{
// Variable declarations
float x1;
float y1;
float x2;
float y2;
// Moved input-handling to `main`
printf("Enter X1 : ");
scanf("%f",&x1);
printf("Enter Y1 : ");
scanf("%f",&y1);
/////////////////////////////////////
printf("Enter X2 : ");
scanf("%f",&x2);
printf(" Enter Y2 : ");
scanf("%f",&y2);
//////////////////////////////////////
/* Function Call */
double distance1 = CalculateDistance(x1, y1, x2, y2);
printf(" Point1 Point 2 Distance\n");
printf("(%4.1f, %4.1f)", x1, y1);
printf("(%4.1f, %4.1f)", x2, y2);
// Used `%lf` instead of `%f` to represent `double`s
printf("%4.1lf\n", distance1);
return 0;
}
//Distance Function
double CalculateDistance(float x1, float y1, float x2, float y2)
{
double diffx = x1 - x2;
double diffy = y1 - y2;
double diffx_sqr = pow(diffx,2);
double diffy_sqr = pow(diffy,2);
double distance = sqrt(diffx_sqr + diffy_sqr);
return distance;
}
最后的一些考慮:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.