0x0F. C Function pointers Task 3 - humtej1204/holbertonschool-low_level_programming GitHub Wiki
3. A goal is not always meant to be reached, it often serves simply as something to aim at
Write a program that performs simple operations.
- You are allowed to use the standard library
- Usage:
calc num1 operator num2
- You can assume
num1
andnum2
are integers, so use theatoi
function to convert them from the string input toint
operator
is one of the following:+
: addition-
: subtraction*
: multiplication/
: division%
: modulo
- The program prints the result of the operation, followed by a new line
- You can assume that the result of all operations can be stored in an
int
- if the number of arguments is wrong, print
Error
, followed by a new line, and exit with the status98
- if the
operator
is none of the above, printError
, followed by a new line, and exit with the status99
- if the user tries to divide (
/
or%
) by0
, printError
, followed by a new line, and exit with the status100
This task requires that you create four different files.
3-calc.h
This file should contain all the function prototypes and data structures used by the program. You can use this structure:
/**
* struct op - Struct op
*
* @op: The operator
* @f: The function associated
*/
typedef struct op
{
char *op;
int (*f)(int a, int b);
} op_t;
3-op_functions.c
This file should contain the 5 following functions (not more):
op_add
: returns the sum ofa
andb
. Prototype:int op_add(int a, int b);
op_sub
: returns the difference ofa
andb
. Prototype:int op_sub(int a, int b);
op_mul
: returns the product ofa
andb
. Prototype:int op_mul(int a, int b);
op_div
: returns the result of the division ofa
byb
. Prototype:int op_div(int a, int b);
op_mod
: returns the remainder of the division ofa
byb
. Prototype:int op_mod(int a, int b);
3-get_op_func.c
This file should contain the function that selects the correct function to perform the operation asked by the user. You’re not allowed to declare any other function.
- Prototype:
int (*get_op_func(char *s))(int, int);
- where
s
is the operator passed as argument to the program - This function returns a pointer to the function that corresponds to the operator given as a parameter. Example:
get_op_func("+")
should return a pointer to the functionop_add
- You are not allowed to use
switch
statements - You are not allowed to use
for
ordo ... while
loops - You are not allowed to use
goto
- You are not allowed to use
else
- You are not allowed to use more than one
if
statement in your code - You are not allowed to use more than one
while
loop in your code - If s does not match any of the 5 expected operators
(+, -, *, /, %)
, returnNULL
- You are only allowed to declare these two variables in this function:
op_t ops[] = {
{"+", op_add},
{"-", op_sub},
{"*", op_mul},
{"/", op_div},
{"%", op_mod},
{NULL, NULL}
};
int I;
3-main.c
This file should contain your main
function only.
- You are not allowed to code any other function than
main
in this file - You are not allowed to directly call
op_add
,op_sub
,op_mul
,op_div
orop_mod
from themain
function - You have to use
atoi
to convert arguments toint
- You are not allowed to use any kind of loop
- You are allowed to use a maximum of 3
if
statements
Compilation and examples
julien@ubuntu:~/0x0e. Function pointers$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 3-main.c 3-op_functions.c 3-get_op_func.c -o calc
julien@ubuntu:~/0x0e. Function pointers$ ./calc 1 + 1
2
julien@ubuntu:~/0x0e. Function pointers$ ./calc 97 + 1
98
julien@ubuntu:~/0x0e. Function pointers$ ./calc 1024 / 10
102
julien@ubuntu:~/0x0e. Function pointers$ ./calc 1024 '*' 98
100352
julien@ubuntu:~/0x0e. Function pointers$ ./calc 1024 '\*' 98
Error
julien@ubuntu:~/0x0e. Function pointers$ ./calc 1024 - 98
926
julien@ubuntu:~/0x0e. Function pointers$ ./calc 1024 '%' 98
44
julien@ubuntu:~/0x0e. Function pointers$
Repo:
- GitHub repository: holbertonschool-low_level_programming
- Directory: 0x0F-function_pointers
- File: 3-main.c, 3-op_functions.c, 3-get_op_func.c, 3-calc.h