加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 此代码是否遵循递归的定义?

发布时间:2020-12-16 10:27:43 所属栏目:百科 来源:网络整理
导读:我有一段代码,我怀疑它作为定义的递归实现.我的理解是代码必须调用自身,完全相同的功能.我还质疑是否以这种方式编写代码会增加额外的开销,这可以通过使用递归来看到.你的想法是什么? class dhObject{public: dhObject** children; int numChildren; GLdoubl
我有一段代码,我怀疑它作为定义的递归实现.我的理解是代码必须调用自身,完全相同的功能.我还质疑是否以这种方式编写代码会增加额外的开销,这可以通过使用递归来看到.你的想法是什么?

class dhObject
{
public:
   dhObject** children;
   int numChildren;
   GLdouble linkLength; //ai 
   GLdouble theta; //angle of rot about the z axis
   GLdouble twist; //about the x axis
   GLdouble displacement; // displacement from the end point of prev along z
   GLdouble thetaMax;
   GLdouble thetaMin;
   GLdouble thetaInc;
   GLdouble direction;

   dhObject(ifstream &fin)
   {
      fin >> numChildren >> linkLength >> theta >> twist >> displacement >> thetaMax >> thetaMin;
      //std::cout << numChildren << std::endl;
      direction = 1;
      thetaInc = 1.0;
      if (numChildren > 0)
      {
         children = new dhObject*[numChildren];
         for(int i = 0; i < numChildren; ++i)
         {
            children[i] = new dhObject(fin);
         }
      }
   }

   void traverse(void)
   {
      glPushMatrix();
      //draw move initial and draw
      transform();
      draw();
      //draw children
      for(int i = 0; i < numChildren; ++i)
      {
         children[i]->traverse();
      }
      glPopMatrix();
   }

   void update(void)
   {
      //Update the animation,if it has finished all animation go backwards
      if (theta <= thetaMin)
      {
         thetaInc = 1.0;
      } else if (theta >= thetaMax)
      {
         thetaInc = -1.0;
      }
      theta += thetaInc;
      //std::cout << thetaMin << " " << theta << " " << thetaMax << std::endl;
      for(int i = 0; i < numChildren; ++i)
      {
         children[i]->update();
      }
   }

   void draw(void)
   {
      glPushMatrix();
      glColor3f (0.0f,0.0f,1.0f);
      glutSolidCube(0.1);
      glPopMatrix();
   }

   void transform(void)
   {
      //Move in the correct way,R,T,R
      glRotatef(theta,1.0);
      glTranslatef(0,displacement);
      glTranslatef(linkLength,0);
      glRotatef(twist,1.0,0.0,0.0);
   }
};

解决方法

是的,因为你有自己的某些功能.根据定义,这是直接递归.如果你有函数A()调用函数B(),函数B()依次(直接或间接)再次调用函数A(),你也可以进行间接递归.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读