forked from ALX-SE-Algorithmia/Demo-Project
-
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.
- Loading branch information
Showing
5 changed files
with
188 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,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 | ||
``` |
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,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); | ||
} |
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,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 |
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,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 not shown.