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.
Merge pull request ALX-SE-Algorithmia#42 from rodgersxy/basic-calc
simple calculator in C programming
- Loading branch information
Showing
4 changed files
with
168 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,10 @@ | ||
# Basic Calculator | ||
-------------------------------------------------------- | ||
Enter number one: 6 | ||
Enter number two: 7 | ||
Enter an operator (+, -, *, /, ^, r, s, c): + | ||
Result: 13.00 | ||
|
||
# To run the calculator | ||
* Navigate to the codescience and run: | ||
* gcc calcul.c main.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,92 @@ | ||
#include <stdio.h> | ||
#include <math.h> | ||
#include "calcul.h" | ||
|
||
/** | ||
* Function that adds two numbers | ||
* @num1: the first number | ||
* @num2: the second number | ||
* | ||
* Return: the sum of num1 and num2 | ||
*/ | ||
float add(float num1, float num2) { | ||
return num1 + num2; | ||
} | ||
|
||
/** | ||
* Function that subtracts two numbers | ||
* @num1: the first number | ||
* @num2: the second number | ||
* | ||
* Return: the difference of num1 and num2 | ||
*/ | ||
float subtract(float num1, float num2) { | ||
return num1 - num2; | ||
} | ||
|
||
/** | ||
* Function that multiplies two numbers | ||
* @num1: the first number | ||
* @num2: the second number | ||
* | ||
* Return: the product of num1 and num2 | ||
*/ | ||
float multiply(float num1, float num2) { | ||
return num1 * num2; | ||
} | ||
|
||
/** | ||
* Function that divides two numbers | ||
* @num1: the first number | ||
* @num2: the second number | ||
* | ||
* Return: the quotient of num1 and num2, or 0 if num2 is 0 | ||
*/ | ||
float divide(float num1, float num2) { | ||
if (num2 != 0) { | ||
return num1 / num2; | ||
} else { | ||
return 0; | ||
} | ||
} | ||
|
||
/** | ||
* Function that calculates the power of a base raised to an exponent | ||
* @base: the base number | ||
* @exponent: the exponent | ||
* | ||
* Return: the result of base raised to the exponent | ||
*/ | ||
float power(float base, float exponent) { | ||
return pow(base, exponent); | ||
} | ||
|
||
/** | ||
* Function that calculates the square root of a number | ||
* @num: the number | ||
* | ||
* Return: the square root of num | ||
*/ | ||
float squareRoot(float num) { | ||
return sqrt(num); | ||
} | ||
|
||
/** | ||
* Function that calculates the sine of an angle | ||
* @angle: the angle | ||
* | ||
* Return: the sine of the angle | ||
*/ | ||
float sine(float angle) { | ||
return sin(angle); | ||
} | ||
|
||
/** | ||
* Function that calculates the cosine of an angle | ||
* @angle: the angle | ||
* | ||
* Return: the cosine of the angle | ||
*/ | ||
float cosine(float angle) { | ||
return cos(angle); | ||
} |
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,15 @@ | ||
#ifndef CALCUL_H | ||
#define CALCUL_H | ||
|
||
|
||
/**prototypes*/ | ||
float add(float num1, float num2); | ||
float subtract(float num1, float num2); | ||
float multiply(float num1, float num2); | ||
float divide(float num1, float num2); | ||
float power(float base, float exponent); | ||
float squareRoot(float num); | ||
float sine(float angle); | ||
float cosine(float angle); | ||
|
||
#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,51 @@ | ||
#include <stdio.h> | ||
#include "calcul.h" | ||
|
||
int main(void) { | ||
float num1, num2; | ||
char operator; | ||
|
||
printf("Enter number one: "); | ||
scanf("%f", &num1); | ||
|
||
printf("Enter number two: "); | ||
scanf("%f", &num2); | ||
|
||
printf("Enter an operator (+, -, *, /, ^, r, s, c): "); | ||
scanf(" %c", &operator); | ||
|
||
float result; | ||
switch (operator) { | ||
case '+': | ||
result = add(num1, num2); | ||
break; | ||
case '-': | ||
result = subtract(num1, num2); | ||
break; | ||
case '*': | ||
result = multiply(num1, num2); | ||
break; | ||
case '/': | ||
result = divide(num1, num2); | ||
break; | ||
case '^': | ||
result = power(num1, num2); | ||
break; | ||
case 'r': | ||
result = squareRoot(num1); | ||
break; | ||
case 's': | ||
result = sine(num1); | ||
break; | ||
case 'c': | ||
result = cosine(num1); | ||
break; | ||
default: | ||
printf("Invalid operator\n"); | ||
return 1; | ||
} | ||
|
||
printf("Result: %.2f\n", result); | ||
|
||
return 0; | ||
} |