黑马程序员技术交流社区

标题: 视频里类方法作业求解答~~~ [打印本页]

作者: ydy96315    时间: 2015-4-29 17:17
标题: 视频里类方法作业求解答~~~
看到ios点和点距离作业点评那里 前面的代码就不赘述了
double squarex =  pow(self->_x - other->_x,2);  //求(x1-x2)的平方 和 (y1-y2)的平方
double squarey =  pow(self->_y - other->_y,2);
    double distance = sqrt(squarex+squarey); //求开根号
    return distance; //这样返回 5 ;
double squarex =  pow([self X] - [other X],2);
    double squarey =  pow([self X] - [other X],2);
    double distance = sqrt(squarex+squarey);
    return distance;//这样返回也是5 ;
那么问题来了
   double squarex =  pow(self->_x - other->_x,2);
    double squarey =  pow([self X] - [other X],2);
    double distance = sqrt(squarex+squarey);
    return distance;//这样为什么就返回8.544004呢
作者: darewolf    时间: 2015-4-29 21:20
本帖最后由 darewolf 于 2015-4-29 21:25 编辑

你确定你的这两句没有错误
    double squarex =  pow([self X] - [other X],2);
    double squarey =  pow([self X] - [other X],2);
1>你计算来计算来,计算去的,不就是计算(x1 - x2)^2 + (x1-x2)^2开根吗
2>还有就是你的get方法是不规范的,在pow,sqrt 中,
3>为了规范两数相减可以用()括住,pow(([slef x]- [other x]),2)
你输入的(24,56) (21,60),距离肯定是5

作者: ydy96315    时间: 2015-4-29 21:50
本帖最后由 ydy96315 于 2015-4-29 21:55 编辑
darewolf 发表于 2015-4-29 21:20
你确定你的这两句没有错误
    double squarex =  pow([self X] - [other X],2);
    double squarey =  po ...

又试来一下  。。。。好像是我改的时候改错了。。。谢谢啊兄弟
作者: ydy96315    时间: 2015-4-29 21:54
本帖最后由 ydy96315 于 2015-4-29 21:56 编辑
darewolf 发表于 2015-4-29 21:20
你确定你的这两句没有错误
    double squarex =  pow([self X] - [other X],2);
    double squarey =  po ...

又试来一下  。。。。好像是我改的时候改错了。。。谢谢啊兄弟




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2