Skip to content

Commit

Permalink
What's my name If you spend too much time thinking about a thing, yo…
Browse files Browse the repository at this point in the history
…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
Miles2023 committed Jan 18, 2023
1 parent f2dc4b1 commit 66ac4fa
Show file tree
Hide file tree
Showing 10 changed files with 275 additions and 0 deletions.
14 changes: 14 additions & 0 deletions 0x0F-function_pointers/0-print_name.c
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);
}
18 changes: 18 additions & 0 deletions 0x0F-function_pointers/1-array_iterator.c
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]);
}
35 changes: 35 additions & 0 deletions 0x0F-function_pointers/100-main_opcodes.c
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);
}
25 changes: 25 additions & 0 deletions 0x0F-function_pointers/2-int_index.c
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);
}
27 changes: 27 additions & 0 deletions 0x0F-function_pointers/3-calc.h
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
30 changes: 30 additions & 0 deletions 0x0F-function_pointers/3-get_op_func.c
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);
}
40 changes: 40 additions & 0 deletions 0x0F-function_pointers/3-main.c
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);
}
72 changes: 72 additions & 0 deletions 0x0F-function_pointers/3-op_functions.c
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);
}
2 changes: 2 additions & 0 deletions 0x0F-function_pointers/README.md
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
12 changes: 12 additions & 0 deletions 0x0F-function_pointers/function_pointers.h
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

0 comments on commit 66ac4fa

Please sign in to comment.