Last active
November 16, 2017 20:53
-
-
Save mfurquimdev/78f21caa4be3cf395cda49c6760541d7 to your computer and use it in GitHub Desktop.
A program in C Language which uses two parameters as input and returns the result of any one of the four basic math operations. This is decided with a char and a function pointer is returned to be used in the main function.
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> | |
| typedef float (*pt2Func)(float, float); | |
| float Plus (float a, float b) { return a+b; } | |
| float Minus (float a, float b) { return a-b; } | |
| float Multiply (float a, float b) { return a*b; } | |
| float Divide (float a, float b) { return a/b; } | |
| pt2Func GetPtr2 (const char opCode) | |
| { | |
| if (opCode == '+') return &Plus; | |
| if (opCode == '-') return &Minus; | |
| if (opCode == '*') return &Multiply; | |
| if (opCode == '/') return &Divide; | |
| } | |
| float (*GetPtr1 (const char opCode))(float, float) | |
| { | |
| if (opCode == '+') return &Plus; | |
| if (opCode == '-') return &Minus; | |
| if (opCode == '*') return &Multiply; | |
| if (opCode == '/') return &Divide; | |
| } | |
| void Return_A_Function_Pointer (void) | |
| { | |
| /* define a function pointer */ | |
| float (*pt2Function) (float, float); | |
| pt2Function = GetPtr1 ('+'); | |
| printf ("%03.1f\n", pt2Function (1.2, 3.4)); | |
| pt2Function = GetPtr2 ('-'); | |
| printf ("%03.1f\n", pt2Function (1.2, 3.4)); | |
| } | |
| int main (void) | |
| { | |
| Return_A_Function_Pointer(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment