利用函数的两点斜率

时间:2015-11-01 03:41:26

标签: c function return-value

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

float slopecalc();


float main()
{
int x1;
int x2;
int y1;
int y2;
float slope;


printf("Please enter values for the first X and Y set\t");
scanf_s(" %d %d", &x1, &y1);

printf("Please enter values for the second X and Y set\t");
scanf_s(" %d %d", &x2, &y2);

printf("The values you have entered are \t Point 1 (%d, %d) \t Point 2 (%d, %d)\n", x1, y1, x2, y2);

slope = slopecalc();

printf("The intersection of the two inputed values is\t %f", slope);

return 0;
}


float slopecalc(int x1,int x2,int y1,int y2){

float dx;
float dy;
float slope;

 dx = x2 - x1;  
 dy = y2 - y1;  
 slope = dy / dx;   

return slope;
}
嘿,所以我试图获得用户输入的两个点的斜率。我似乎无法理解如何正确使用函数进行计算,然后将该函数调用main并将其打印给用户。任何帮助都会很棒!

1 个答案:

答案 0 :(得分:1)

函数slopecalc调用参数,因此必须将一些参数传递给函数。

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

#ifndef _MSC_VER
#define scanf_s scanf
#endif

float slopecalc(int x1,int x2,int y1,int y2);


int main(void)
{
    int x1;
    int x2;
    int y1;
    int y2;
    float slope;


    printf("Please enter values for the first X and Y set\t");
    scanf_s(" %d %d", &x1, &y1);

    printf("Please enter values for the second X and Y set\t");
    scanf_s(" %d %d", &x2, &y2);

    printf("The values you have entered are \t Point 1 (%d, %d) \t Point 2 (%d, %d)\n", x1, y1, x2, y2);

    slope = slopecalc(x1, x2, y1, y2);

    printf("The intersection of the two inputed values is\t %f", slope);

    return 0;
}


float slopecalc(int x1,int x2,int y1,int y2){

    float dx;
    float dy;
    float slope;

    dx = x2 - x1;
    dy = y2 - y1;
    slope = dy / dx;

    return slope;
}

另请注意:

  • 使用没有参数列表float slopecalc()的函数声明并不好,因为编译器不会检查你是否有适当的参数来调用函数。
  • 使用float main()并不好,因为它不是标准的,我也不知道你使用main函数的奇怪定义的原因。