Created
March 10, 2022 19:18
-
-
Save murtaza-u/fea3611b7ddb8a378223364f9cf5c726 to your computer and use it in GitHub Desktop.
using arrays in a special way
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void add(int a, int b) { | |
printf("%d\n", a + b); | |
} | |
void sub(int a, int b) { | |
printf("%d\n", a - b); | |
} | |
void mul(int a, int b) { | |
printf("%d\n", a * b); | |
} | |
void div(int a, int b) { | |
printf("%d\n", a / b); | |
} | |
enum Action { | |
Add, | |
Sub, | |
Mul, | |
Div, | |
LastAction | |
}; | |
static void (*perform[LastAction])(int a, int b) = { | |
[Add] = add, | |
[Sub] = sub, | |
[Mul] = mul, | |
[Div] = div, | |
}; | |
int main() { | |
int n, a, b; | |
printf("0 for add\n1 for sub\n2 for mul\n3 for div\n"); | |
while (1) { | |
printf("Action: "); | |
scanf("%d", &n); | |
printf("Enter 1st num: "); | |
scanf("%d", &a); | |
printf("Enter 2nd num: "); | |
scanf("%d", &b); | |
if (perform[n]) perform[n](a, b); | |
else printf("Invalid action\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment