0

距离计算器,一切正常,直到程序达到功能,而不是程序停止运行。我尝试从我在主函数中调用函数的行中删除voidand s,但仍然没有。int

#include <stdio.h>
#include <math.h>


void distanceFunc(int x2, int x1, int y2, int y1, int distance);

int main ()
{
    int action = 0;
    int x1 = 0;
    int y1 = 0;
    int x2 = 0;
    int y2 = 0;
    int distance = 0;
    printf("Welcome to my calculator!\n");
    printf("1 - Calc distance between 2 points\n");
    printf("2 - Calc hypotenuse of triangle\n");
    printf("3 - Calc area and perimeter of circle\n");
    printf("4 - Calc area of rectangle\n");
    printf("5 - Calc are of square\n");
    printf("6 - Exit\n");
    scanf("%d", &action);
    if (action == 1)
    {
        printf("Enter point1 coordinates: ");
        scanf("%d %d", &x1, &y1);
        printf("Enter point2 coordinates: ");
        scanf("%d %d", &x2, &y2);
        void distanceFunc(int x2, int x1, int y2, int y1, int distance);
    }
    return 0;
}


void distanceFunc(int x2, int x1, int y2, int y1, int distance)
{
    distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
    printf("Distance is %f\n", distance);
}
4

2 回答 2

3

main你有

void distanceFunc(int x2, int x1, int y2, int y1, int distance);

这不会调用该函数,它只是声明它(但您已经在顶部这样做了)。你需要

distanceFunc(x2, x1, y2, y1, distance);

你也有格式不匹配。您应该更改distance.

double distance;

在函数内,并删除int distance函数参数,因为它没有任何作用。

于 2018-12-09T18:39:52.800 回答
2
    void distanceFunc(int x2, int x1, int y2, int y1, int distance);

是函数声明,而不是函数调用。

要调用该函数,就像对您使用的其他函数一样:

    distanceFunc(x2, x1, y2, y1, distance);

还可以通过指定选项来提高编译器的警告级别:

-Wall -Wextra -pedantic

看到这样的东西:

main.c: In function ‘main’:
main.c:14:9: warning: unused variable ‘distance’ [-Wunused-variable]
     int distance = 0;
     ^
main.c: In function ‘distanceFunc’:
main.c:45:5: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat=]
     printf("Distance is %f\n", distance);
     ^
main.c:45:5: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat=]

并了解这f不是打印int.

于 2018-12-09T18:42:15.380 回答