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

How to determine the sequence of functions used?

本文关键字:功能 顺序 何确定      更新时间:2023-10-16

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

谢谢

一种方式是

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;