如何确定使用的功能顺序?

时间:2018-09-28 03:59:50

标签: c++ oop

我目前正在为3号健身房拨号锁编写程序。现实拨号盘的工作方式是,您必须向右转到第一个数字,向左转到第二个数字,然后再次向右转到第三个数字。我有看起来像turnRight和turnLeft的函数。 目前,我正在为我的组合锁编写测试,其中一个测试涉及转向正确的数字,但方向错误(正确,正确,正确,而不是正确,正确,错误)。有没有办法确定使用函数的顺序(例如,使用turnRight,turnLeft,turnRight,所以返回1)?

谢谢

1 个答案:

答案 0 :(得分:0)

一种方法是

     enum {RIGHT, LEFT};

     int my_combo[]=
     {
       RIGHT, LEFT, RIGHT, -1 ///-1 is a delimiter in order to test if the end of the array of combo keys.
     };

     TypeReturn turnLeft(){...}
     TypeReturn turnRight(){...}

     ...
     ///the function where you are going to test it, for this example return Boolean
     /// you change for your need

     int entry_combo[]={RIGHT, RIGHT, RIGHT, -1};
     /// this could be a while loop too, some people preferred that
      bool ret = true;
     int i(0);
     for(; ;i++)
     {
        if(entry_combo[i]!= -1 && my_combo[i] !=-1)
        {
            if(entry_combo[i] != my_combo[i])
               { 
                  ret = false;
                  break;
                }
             else
               {
                     ////here you could put turnLeft or turnRight, depending of what value are in my_combo.
               }
        }
        else
        {
            ret = (i==0)?true:(entry_combo[i] != my_combo[i])?false:ret))      ; /// you must decide if what to return if the combo key  
                                    /// are empty. for me just return true. there are other criteria to choose,
        }
     } 
 return ret;