-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
What's my name If you spend too much time thinking about a thing, yo…
…u'll never get it done To hell with circumstances; I create opportunities A goal is not always meant to be reached, it often serves simply as something to aim at Most hackers are young because young people tend to be adaptable. As long as you remain adaptable, you can always be a good hacker
- Loading branch information
Showing
10 changed files
with
275 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include "function_pointers.h" | ||
|
||
/** | ||
* print_name - prints a name depends of the function. | ||
* | ||
* @name: pointer to Name's string. | ||
* @f: pointer to function. | ||
*/ | ||
|
||
void print_name(char *name, void (*f)(char *)) | ||
{ | ||
if (name != NULL && f != NULL) | ||
f(name); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include "function_pointers.h" | ||
|
||
/** | ||
* array_iterator - executes a function given as a parameter on each element of | ||
* an array. | ||
* @array: pointer to the array. | ||
* @size: size of array. | ||
* @action: function pointer | ||
*/ | ||
|
||
void array_iterator(int *array, size_t size, void (*action)(int)) | ||
{ | ||
size_t i; | ||
|
||
if (array != NULL && action != NULL) | ||
for (i = 0; i < size; i++) | ||
action(array[i]); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include "function_pointers.h" | ||
|
||
/** | ||
* main - generates opcodes. | ||
* @argc: argument counter. | ||
* @argv: argument vector. | ||
* Return: the opcodes | ||
*/ | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
int i, number; | ||
|
||
if (argc != 2) | ||
{ | ||
printf("Error\n"); | ||
return (1); | ||
} | ||
number = atoi(argv[1]); | ||
if (number < 0) | ||
{ | ||
printf("Error\n"); | ||
exit(2); | ||
} | ||
|
||
for (i = 0; i < number; i++) | ||
{ | ||
printf("%02hhx", ((char *)main)[i]); | ||
if (i == (number - 1)) | ||
printf("\n"); | ||
else | ||
printf(" "); | ||
} | ||
return (0); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#include "function_pointers.h" | ||
|
||
/** | ||
* int_index - searches for an integer in @array | ||
* @array: pointer to the array of numbers. | ||
* @size: size of array. | ||
* @cmp: pointer to comparation function | ||
* Return: the index of array. | ||
*/ | ||
|
||
int int_index(int *array, int size, int (*cmp)(int)) | ||
{ | ||
int i; | ||
|
||
if (size <= 0) | ||
return (-1); | ||
if (!array || !cmp) | ||
return (-1); | ||
for (i = 0; i < size; i++) | ||
{ | ||
if (cmp(array[i]) != 0) | ||
return (i); | ||
} | ||
return (-1); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#ifndef _CALC_H_ | ||
#define _CALC_H_ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
/** | ||
* struct op - Struct op | ||
* | ||
* @op: The operator | ||
* @f: The function associated | ||
*/ | ||
|
||
typedef struct op | ||
{ | ||
char *op; | ||
int (*f)(int a, int b); | ||
} op_t; | ||
|
||
int op_add(int a, int b); | ||
int op_sub(int a, int b); | ||
int op_mul(int a, int b); | ||
int op_div(int a, int b); | ||
int op_mod(int a, int b); | ||
int (*get_op_func(char *s))(int, int); | ||
|
||
#endif |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include "3-calc.h" | ||
|
||
/** | ||
* get_op_func - compares the struct and proceed to operates. | ||
* @s: operator string. | ||
* Return: result. | ||
*/ | ||
|
||
int (*get_op_func(char *s))(int, int) | ||
{ | ||
op_t ops[] = { | ||
{"+", op_add}, | ||
{"-", op_sub}, | ||
{"*", op_mul}, | ||
{"/", op_div}, | ||
{"%", op_mod}, | ||
{NULL, NULL} | ||
}; | ||
int i; | ||
|
||
i = 0; | ||
|
||
while (i < 5) | ||
{ | ||
if (*s == *ops[i].op) | ||
return (ops[i].f); | ||
i++; | ||
} | ||
return (NULL); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include "3-calc.h" | ||
|
||
/** | ||
* main - operates two numbers. | ||
* @argc: argument counter. | ||
* @argv: argument vector. | ||
* Return: result, otherwise 98 for arg error, 99 for op, error 100 for divide | ||
* or multiply by 0 | ||
*/ | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
int num1, num2, answer; | ||
int (*function)(int, int); | ||
|
||
if (argc != 4) | ||
{ | ||
puts("Error"); | ||
exit(98); | ||
} | ||
|
||
if (argv[2][1] != '\0') | ||
{ | ||
puts("Error"); | ||
exit(99); | ||
} | ||
|
||
function = get_op_func(*(argv + 2)); | ||
if (!function) | ||
{ | ||
puts("Error"); | ||
exit(99); | ||
} | ||
|
||
num1 = atoi(argv[1]); | ||
num2 = atoi(argv[3]); | ||
answer = function(num1, num2); | ||
printf("%d\n", answer); | ||
return (0); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include "3-calc.h" | ||
|
||
/** | ||
* op_add - adds two number. | ||
* @a: first number. | ||
* @b: second number. | ||
* Return: result. | ||
*/ | ||
|
||
int op_add(int a, int b) | ||
{ | ||
return (a + b); | ||
} | ||
|
||
/** | ||
* op_sub - substracts two number. | ||
* @a: first number. | ||
* @b: second number. | ||
* Return: result. | ||
*/ | ||
|
||
int op_sub(int a, int b) | ||
{ | ||
return (a - b); | ||
} | ||
|
||
/** | ||
* op_mul - multiplicates two number. | ||
* @a: first number. | ||
* @b: second number. | ||
* Return: result. | ||
*/ | ||
|
||
int op_mul(int a, int b) | ||
{ | ||
return (a * b); | ||
} | ||
|
||
/** | ||
* op_div - divides two number. | ||
* @a: first number. | ||
* @b: second number. | ||
* Return: result. | ||
*/ | ||
|
||
int op_div(int a, int b) | ||
{ | ||
if (b == 0) | ||
{ | ||
puts("Error"); | ||
exit(100); | ||
} | ||
else | ||
return (a / b); | ||
} | ||
|
||
/** | ||
* op_mod - obteins the module between two number. | ||
* @a: first number. | ||
* @b: second number. | ||
* Return: result. | ||
*/ | ||
|
||
int op_mod(int a, int b) | ||
{ | ||
if (b == 0) | ||
{ | ||
puts("Error"); | ||
exit(100); | ||
} | ||
return (a % b); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
0x0F-function_pointers | ||
This directory contains some certain files on C programming language about Function pointers, that will be compiled on Ubuntu 20.04 LTS using gcc using the options -Wall -Werror -Wextra -pedantic -std=gnu89 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#ifndef _FUNC_PTR_H_ | ||
#define _FUNC_PTR_H_ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
void print_name(char *name, void (*f)(char *)); | ||
void array_iterator(int *array, size_t size, void (*action)(int)); | ||
int int_index(int *array, int size, int (*cmp)(int)); | ||
int (*get_op_func(char *s))(int, int); | ||
|
||
#endif |