寻找中点ADT

时间:2018-03-17 04:27:36

标签: c

点表示x-y平面中的坐标。它由以下功能支持:

Point * make_point(double x, double y)
double x_of(Point *p)
double y_of(Point *p)
void print_point(Point *p)

一个函数Point * mid_point,它接受​​两个点作为参数,并返回一个作为这两个输入坐标中点的点。

尝试在mid_point(make_point(1.0, 1.0), make_point(3.0, 3.0))的在线模拟器上运行时,显示错误

Point * mid_point(Point *x, Point *y) {

    int mid = make_point((x_of(x)+x_of(x))/2,(y_of(y)+y_of(y))/2);

    print_point(mid);

}

2 个答案:

答案 0 :(得分:1)

行中还有一个额外的括号:

return make_point( |(| (x_of (x) + x_of(x) ) / 2,  (y_of(y) + y_of(y) ) / 2);
    //Extra bracket ^  ^open               ^close  ^open              ^close

我也很确定你想说:

return make_point((x_of (x) + x_of(|y|) ) / 2,  (y_of(|x|) + y_of(y) ) / 2);
                                   ^^^                ^^^            

另外我会从make_point(double x,double y)返回Point副本而不是Point *指针,否则你将不得不返回一个静态变量或全局。

我认为这更简单:

#include <stdio.h>

typedef struct Point
{
    double x, y;

}Point;

Point getMidPointOf(Point p1, Point p2)
{
    Point newP = { (p1.x + p2.x) / 2.0, (p1.y + p2.y) / 2.0 };
    return newP;
}

int main()
{
    Point p1 = { 5.0, 15.0 };
    Point p2 = { 15.0, 30.0 };
    Point p3 = getMidPointOf(p1, p2);
    printf("Point value = (%f, %f)", p3.x, p3.y);

    return 0;
}

答案 1 :(得分:0)

\begin{landscape}
\begin{table} 
\begin{center}
\begin{tabular}{lll}
 \multicolumn{3}{l}{Table 1: Dataset, Sources and Construction}         \\ \hline \hline                                          
 No & Abbrev. & Construction                                                 \\ \hline

1 & EquityM & Logarithmic returns of 6 equity markets (FTSE100, NIKKEI225, SPI, DAX30, SPTSX, AllOrds for UK, Japan, Switzerland, Eurozone, Canada, Australia, respectively) \\


 \hline 
\end{tabular}
\end{center}
\end{table}
\end{landscape}