Skip to content

Commit

Permalink
function pointer in calc app
Browse files Browse the repository at this point in the history
  • Loading branch information
shazaaly committed Jul 25, 2023
1 parent 261ece5 commit 1cabbb8
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 0 deletions.
52 changes: 52 additions & 0 deletions shazaAly/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# calc.h

**Description:**
`calc.h` is a header file that provides function prototypes for basic mathematical operations. The file contains declarations for functions to perform addition, subtraction, multiplication, division, and exponentiation.

**Usage:**
To use the functions defined in this header file, include `calc.h` in your C/C++ source code and link with the appropriate implementation.

**Note:**
Ensure that you have the necessary math library linked when using `expo` function to utilize the `pow()` function.

## Functions:

### `double add(double a, double b)`
This function takes two double precision floating-point numbers `a` and `b` as input and returns their sum.

### `double sub(double a, double b)`
This function takes two double precision floating-point numbers `a` and `b` as input and returns the result of subtracting `b` from `a`.

### `double mul(double a, double b)`
This function takes two double precision floating-point numbers `a` and `b` as input and returns their product.

### `double div(double a, double b)`
This function takes two double precision floating-point numbers `a` and `b` as input and returns the result of dividing `a` by `b`. If `b` is equal to zero, it will print an error message and return 0.00 to avoid division by zero.

### `double expo(double a, double b)`
This function takes two double precision floating-point numbers `a` and `b` as input and returns `a` raised to the power of `b`. Make sure to link the necessary math library to use the `pow()` function.

**Example:**
```c
#include <stdio.h>
#include "calc.h"

int main() {
double num1 = 10.0;
double num2 = 5.0;

printf("Addition: %.2f\n", add(num1, num2));
printf("Subtraction: %.2f\n", sub(num1, num2));
printf("Multiplication: %.2f\n", mul(num1, num2));
printf("Division: %.2f\n", div(num1, num2));
printf("Exponentiation: %.2f\n", expo(num1, num2));

return 0;
}
```

**Note:** Don't forget to link with the math library when compiling the code that uses the `expo` function:

```
gcc -o my_program my_program.c -lm
```
68 changes: 68 additions & 0 deletions shazaAly/calc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "calc.h"
/**
* add - Adds two numbers.
* @a: The first number.
* @b: The second number.
*
* Return: The sum of @a and @b.
*/
double add(double a, double b)
{
return a + b;
}
/**
* sub - Subtracts two numbers.
* @a: The first number.
* @b: The second number.
*
* Return: The result of @a minus @b.
*/

double sub(double a, double b)
{
return a - b;
}
/**
* mul - Multiplies two numbers.
* @a: The first number.
* @b: The second number.
*
* Return: The product of @a and @b.
*/
double mul(double a, double b)
{
return a * b;
}

/**
* div - Divides two numbers.
* @a: The first number.
* @b: The second number.
*
* Return: The result of @a divided by @b.
* If @b is zero, returns 0.00 and prints an error message.
*/
double div(double a, double b)
{
if (b != 0)
{
return (a / b);
}
else
{
printf("Error: Division by zero\n");
return 0.00;
}

}
/**
* expo - Computes the exponentiation of a number.
* @a: The base number.
* @b: The exponent.
*
* Return: The result of @a raised to the power of @b.
*/
double expo(double a, double b)
{
return pow(a, b);
}
16 changes: 16 additions & 0 deletions shazaAly/calc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef CALC_H
#define CALC_H

#include <math.h>
#include <stdio.h>
#include <unistd.h>


double add(double a, double b);
double sub(double a, double b);
double mul(double a, double b);
double div(double a, double b);
double expo(double a, double b);


#endif
52 changes: 52 additions & 0 deletions shazaAly/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "calc.h"

int main()
{
double a, b, result;
char op;

printf("Enter the first number: ");
scanf("%lf", &a);
sleep(2);
printf("Enter the second number: ");
scanf("%lf", &b);
printf("Enter an arithmetic operator (+, -, *, /, ^): ");
scanf(" %c", &op);

/*declare function pointer*/
double (*operation)(double, double);

switch (op)
{
case '+':
operation = add;
break;

case '-':
operation = sub;
break;

case '*':
operation = mul;
break;

case '/':
operation = div;
break;

case '^':
operation = expo;
break;

default:
printf("Invalid operator \n");
return (1);
}

result = operation(a, b);
printf("Result: %lf\n",result);
return (0);



}
Binary file added shazaAly/shaza
Binary file not shown.

0 comments on commit 1cabbb8

Please sign in to comment.