diff --git a/shazaAly/README.md b/shazaAly/README.md new file mode 100644 index 0000000..54b8735 --- /dev/null +++ b/shazaAly/README.md @@ -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 +#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 +``` \ No newline at end of file diff --git a/shazaAly/calc.c b/shazaAly/calc.c new file mode 100644 index 0000000..a679dc0 --- /dev/null +++ b/shazaAly/calc.c @@ -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); +} \ No newline at end of file diff --git a/shazaAly/calc.h b/shazaAly/calc.h new file mode 100644 index 0000000..32501f5 --- /dev/null +++ b/shazaAly/calc.h @@ -0,0 +1,16 @@ +#ifndef CALC_H +#define CALC_H + +#include +#include +#include + + +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 \ No newline at end of file diff --git a/shazaAly/main.c b/shazaAly/main.c new file mode 100644 index 0000000..aacbb34 --- /dev/null +++ b/shazaAly/main.c @@ -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); + + + +} \ No newline at end of file diff --git a/shazaAly/shaza b/shazaAly/shaza new file mode 100644 index 0000000..923175d Binary files /dev/null and b/shazaAly/shaza differ