我有这个任务,要求使用函数来计算两点的距离和斜率。问题是我真的不明白如何将单个函数用于两个单独的变量(必须以这种方式进行赋值)。在这种情况下,公式要求x1和x2,但我必须使用单个函数。所以我的问题是如何使用我的getX和getY函数提示x和y两次?基本上对第一个和第二个值使用单个函数。希望更有意义。
/*Include statements*/
#include <stdio.h>
#include <math.h>
/*Given Prototypes*/
double getX();
double getY();
double distance(double, double, double, double);
double slope(double, double, double, double);
// begin main
int main()
{
// declare variables
double x1;
double x2;
double y1;
double y2;
// read in variables for points on a graph
x1 = getX();
y1 = getY();
x2 = getX();
y2 = getY();
// print out the distance between the points and the slope of the line
printf("\n");
printf("Distance between the points is %.2f \n", distance(x1, x2, y1, y2));
printf("Slope of the line is %.2f \n\n", slope(x1, x2, y1, y2));
}
/* begin getX function */
double getX()
{
double x;
printf( "Please enter the value of x:");
scanf("%lf", &x);
return x;
}
/* begin getY function */
double getY()
{
double y;
printf("Please enter the value of y:");
scanf("%lf", &y);
return y;
}
/* header for distance function */
double distance(double x1, double y1, double x2, double y2)
{
return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}
/* header for slope function */
double slope(double x1, double y1, double x2, double y2)
{
return (y2 - y1) / (x2 - x1);
}
答案 0 :(得分:0)
你可以使用&#34; Struct&#34;在C中,这将定义新的数据类型,可以将两个或多个单独的值放在下面的代码中 - 如果您有任何问题,请 代码在这里:
/ 包含声明 /
#include <stdio.h>
#include <math.h>
/*Given Prototypes*/
double getX();
double getY();
double distance(double, double, double, double);
double slope(double, double, double, double);
// begin main
int main()
{
// declare variables
double x1;
double x2;
double y1;
double y2;
// read in variables for points on a graph
x1 = getX();
y1 = getY();
x2 = getX();
y2 = getY();
// print out the distance between the points and the slope of the line
struct SlopDistance FinalValues;
FinalValues = CalcuateValues(x1, y1, x2, y2);
printf("\n");
printf("Distance between the points is %.2f \n", FinalValues.Distance);
printf("Slope of the line is %.2f \n\n", FinalValues.Distance);
}
/* begin getX function */
double getX()
{
double x;
printf("Please enter the value of x:");
scanf_s("%lf", &x);
return x;
}
/* begin getY function */
double getY()
{
double y;
printf("Please enter the value of y:");
scanf_s("%lf", &y);
return y;
}
/* header for distance function */
double distance(double x1, double y1, double x2, double y2)
{
return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}
/* header for slope function */
double slope(double x1, double y1, double x2, double y2)
{
return (y2 - y1) / (x2 - x1);
}
struct SlopDistance
{
double Slope;
double Distance
};
struct SlopDistance CalcuateValues(double x1, double y1, double x2, double y2)
{
struct SlopDistance FinalValue;
FinalValue.Distance = distance(x1, x2, y1, y2);
FinalValue.Slope = slope(x1, x2, y1, y2);
return FinalValue;
}