将 if 语句转换为 for 循环?
我在 mikroC 中编写了一个函数,用于扫描 4x4 键盘中按下的按键
void scan_key()
{
PORTB = 0B11111110;
if ( PORTB == 0b11101110){
Row = 1;
Column = 1;
return;
}
if ( PORTB == 0b11011110){
Row = 2;
Column = 1;
return;
}
if ( PORTB == 0b10111110){
Row = 3;
Column = 1;
return;
}
if ( PORTB == 0b01111110){
Row = 4;
Column = 1;
return;
}
PORTB = 0B11111101;
if ( PORTB == 0b11101101){
Row = 1;
Column = 2;
return;
}
if ( PORTB == 0b11011101){
Row = 2;
Column = 2;
return;
}
if ( PORTB == 0b10111101){
Row = 3;
Column = 2;
return;
}
if ( PORTB == 0b01111101){
Row = 4;
Column = 2;
return;
}
PORTB = 0B11111011;
if ( PORTB == 0b11101011){
Row = 1;
Column = 3;
return;
}
if ( PORTB == 0b11011011){
Row = 2;
Column = 3;
return;
}
if ( PORTB == 0b10111011){
Row = 3;
Column = 3;
return;
}
if ( PORTB == 0b01111011){
Row = 4;
Column = 3;
return;
}
PORTB = 0B11110111;
if ( PORTB == 0b11100111){
Row = 1;
Column = 4;
return;
}
if ( PORTB == 0b11010111){
Row = 2;
Column = 4;
return;
}
if ( PORTB == 0b10110111){
Row = 3;
Column = 4;
return;
}
if ( PORTB == 0b01110111){
Row = 4;
Column = 4;
return;
}
PORTB = 0B11110000;
}
有没有办法将此算法转换为循环?
I've written a function in mikroC that scan the pressed key in a 4x4 keypad
void scan_key()
{
PORTB = 0B11111110;
if ( PORTB == 0b11101110){
Row = 1;
Column = 1;
return;
}
if ( PORTB == 0b11011110){
Row = 2;
Column = 1;
return;
}
if ( PORTB == 0b10111110){
Row = 3;
Column = 1;
return;
}
if ( PORTB == 0b01111110){
Row = 4;
Column = 1;
return;
}
PORTB = 0B11111101;
if ( PORTB == 0b11101101){
Row = 1;
Column = 2;
return;
}
if ( PORTB == 0b11011101){
Row = 2;
Column = 2;
return;
}
if ( PORTB == 0b10111101){
Row = 3;
Column = 2;
return;
}
if ( PORTB == 0b01111101){
Row = 4;
Column = 2;
return;
}
PORTB = 0B11111011;
if ( PORTB == 0b11101011){
Row = 1;
Column = 3;
return;
}
if ( PORTB == 0b11011011){
Row = 2;
Column = 3;
return;
}
if ( PORTB == 0b10111011){
Row = 3;
Column = 3;
return;
}
if ( PORTB == 0b01111011){
Row = 4;
Column = 3;
return;
}
PORTB = 0B11110111;
if ( PORTB == 0b11100111){
Row = 1;
Column = 4;
return;
}
if ( PORTB == 0b11010111){
Row = 2;
Column = 4;
return;
}
if ( PORTB == 0b10110111){
Row = 3;
Column = 4;
return;
}
if ( PORTB == 0b01110111){
Row = 4;
Column = 4;
return;
}
PORTB = 0B11110000;
}
Is there a way to convert this algorithm into a loop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的。 (至少假设您实际上并不打算在每个
if
块之前进行这些分配。我假设它们是为了测试?否则您的代码就没有意义。)至少,您可以将每个二进制值放入一个数组中,并构造一个如下所示的循环:
Yes. (At least assuming you don't actually mean to have those assignments before each block of
if
s. I assume they were there for testing? otherwise your code doesn't make sense.)At the very least, you can put each of the binary values into an array, and construct a loop like this:
是的当然。
首先用你的值做一些数组。
我们称之为 t[]
第二个是 d[][] // 二维数组
,让我们做双循环
希望有帮助。
不知道一维数组是否只需要 d 值,没有检查每个数字
Yes of course.
First do some array with your values.
we'll call it t[]
and second is d[][] // two dimensional array
and lets do double loop
Hope that helps.
Don't know if 1 dimensional array is only needed for d values, didnt check every number