English 中文(简体)
Convert if statements to for loop? [closed]
原标题:

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?

最佳回答

Yes. (At least assuming you don t actually mean to have those assignments before each block of ifs. 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:

int portValue[4][4] = {{0b11101110, 0b11011110, 0b10111110, 0b01111110}, {//...Rest of values here }}

int loop_col = 0;
int loop_row = 0;

for (loop_col = 0; loop_col < 4; loop_col++){
    for (loop_row = 0; loop_row < 4; loop_row++){
         if ( PORTB == portValue[loop_col][loop_row]){
             Row = loop_row + 1;
             Column = loop_col + 1;
             return;
         }
    }
}
问题回答

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

for(i=1;i<=4;i++)
{
    cur_value=t[i]; // our value

    for(j=1;j<=4;j++)
    {
        right_side_value=d[i][j];
        if(cur_value==right_side_value)
        {
            rows=i;
            columns=j;
            return;
        }
    }
}

Hope that helps.

Don t know if 1 dimensional array is only needed for d values, didnt check every number





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签