Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking a variable meanwhile calling the functions needed

Tags:

c

So im fairly new to programming and I ran into something which I cannot figure out.

I'll demonstrate my problem.

int main(void){

    int number;

    if(number == 1){
        number1();
    }else if(number == 2){
        number2();
    }else if(number == 3){
        number3();
    }else if(number == 4){
        number4();
    }else if(number == 5){
        number5();
    }else if(number == 6){
        number6();
    }else if(number == 7){
        number7();
    }else if(number == 8){
        number8();
    }else if(number == 9){
        number9();
    }else if(number == 0){
        number0();
    } else if (number >= 10 && <= 19){
        number1();
        number2(25);
    }

}

void number1(int Q){
    VGA_box(X + 5 + Q, 20, X+7 + Q, 60, white);
}

void number2(int Q){
    VGA_box(X + Q, 20, X+ 20 + Q, 22, white);           
    VGA_box(X + 18 + Q, 22, X+ 20 + Q, 38, white);      
    VGA_box(X + Q, 38, X+ 20 + Q, 40, white);           
    VGA_box(X + Q, 40, X+ 2 + Q, 58, white);            
    VGA_box(X + Q, 58, X+ 20 + Q, 60, white);                   
}

Please ignore the functions VGA_box(), since this is just a function to write a line/box.

I'm making a little game and I would like to add a scoreboard, but here is my problem:

Since I'm drawing the numbers (so they look better IMO), I have to call functions, functions which represent a number. So if number = 1, function number1(); is called. Is there an easy way to call the functions number2(), number5() and number9() if the number is 259? Or lets say; 632. The only possible way I can figure out is to use a lot of if-statements but that will take me a while to do.

Is it possible to use a for-loop which keeps track of what the number is and calls the functions which needed?

Thank you in advance.

like image 806
Schottovich Avatar asked Dec 06 '22 18:12

Schottovich


2 Answers

You can use an array of pointers to function and a recursive function to reverse the value:

#include <stdio.h>

static void func0(void) { printf("0\n"); }
static void func1(void) { printf("1\n"); }
static void func2(void) { printf("2\n"); }
static void func3(void) { printf("3\n"); }

static void (*func[])(void) = {func0, func1, func2, func3};

static void exec(int value)
{
    if (value) {
        exec(value / 10);
        func[value % 10]();
    }
}

int main(void)
{
    exec(123);
    return 0;
}

Output:

1
2
3

As pointed out by @user3078414 in comments this fails when exec(012) is passed (the first 0 is not evaluated in the recursive function), in addition, 012 is interpreted as an octal (not as a decimal).

An alternative using strings:

#include <stdio.h>

static void func0(void) { printf("0\n"); }
static void func1(void) { printf("1\n"); }
static void func2(void) { printf("2\n"); }
static void func3(void) { printf("3\n"); }

static void (*func[])(void) = {func0, func1, func2, func3};

int main(void)
{
    char *ptr = "012";

    while (*ptr) {
        func[*ptr - '0']();
        ptr++;
    }
    return 0;
}
like image 131
David Ranieri Avatar answered May 25 '23 07:05

David Ranieri


It seems like functions number1(), number2(), etc are quite similar. I'd be against creating different functions; I'd rather create one single function (let's say number()) which receives the whole number "632" from your example, and loops digit by digit.

Creating one single function is easier to handle by you. Just check that the resulting function is not too big, but it's quite likely that functions number1(), number2(), etc have a lot of code in common. If that's the case, reducing redundancy and repetition in code is highly recommended!

Let me know if I'm right with this.

Cheers!

like image 36
Ezequiel Avatar answered May 25 '23 07:05

Ezequiel