Skip to content

Commit

Permalink
Calcucator using C and Python
Browse files Browse the repository at this point in the history
  • Loading branch information
ActuaryEmma committed Jul 26, 2023
1 parent 87055a9 commit f8afb47
Show file tree
Hide file tree
Showing 11 changed files with 386 additions and 0 deletions.
1 change: 1 addition & 0 deletions actuaryemma/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Calculator Demo Project using C and Python
10 changes: 10 additions & 0 deletions actuaryemma/c/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Created different fucntions for int, float and double
1) add function : add two integer, float or double values (add.c)
2) sub function: subtract two integer, float or double values (sub.c)
3) mul functions: mul two integer, float or double values (mul.c)
4) division functions : division two integer, float or double values (division.c)
5) header file: define all my functions and macros (cal.h)
6) Main file - where I called my functions (cal.c)
7) validate.c - checks the input is correct according to the datatype else through an error

since C does not support overloading,, I implemented _Generic keyword
33 changes: 33 additions & 0 deletions actuaryemma/c/add.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "cal.h"
/**
* sum - add two numbers
* @a: first number
* @b: second number
* Return: sum of two numbers
*/
int sum(int a, int b)
{
return (a + b);
}

/**
* sum_0 - add two numbers
* @a: first number
* @b: second number
* Return: sum of two numbers
*/
float sum_0(float a, float b)
{
return (a + b);
}

/**
* sum_1 - add two numbers
* @a: first number
* @b: second number
* Return: sum of two numbers
*/
double sum_1(double a, double b)
{
return (a + b);
}
Binary file added actuaryemma/c/cal
Binary file not shown.
160 changes: 160 additions & 0 deletions actuaryemma/c/cal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
#include <stdio.h>
#include "cal.h"
#include <string.h>
#define sum(a, b) _Generic((a), int: sum, float: sum_0, double: sum_1)(a, b)
#define sub(a, b) _Generic((a), int: sub, float: sub_0, double: sub_1)(a, b)
#define mul(a, b) _Generic((a), int: mul, float: mul_0, double: mul_1)(a, b)
#define division(a, b) _Generic((a), int: division, float: division_0, double: division_1)(a, b)
int main()
{
int result = 0, a, b;
float c, d;
double e, f;
char operation;

int status = 0;

char data_types[10];

while (1)
{
printf("Enter the first data type (int, float, or double): ");
scanf("%s", data_types);

if (strcmp(data_types, "int") == 0)
{
if (validate_input("Enter the first number: ", "%d", &a) == 1)
{
if (validate_input("Enter the second number: ", "%d", &b) == 1)
status = 1;
break;
}
}
else if (strcmp(data_types, "float") == 0)
{
if (validate_input("Enter the first number: ", "%f", &c) == 1)
{
if (validate_input("Enter the second number: ", "%f", &d) == 1)
{
status = 1;
break;
}
}
}
else if (strcmp(data_types, "double") == 0)
{
if (validate_input("Enter the first number: ", "%le", &e) == 1)
{
if (validate_input("Enter the second number: ", "%le", &f) == 1)
{
status = 1;
break;
}
}
}

else
{
printf("Invalid data type. Please enter either int, double or float\n");
while (getchar() != '\n');
}
}

while (1)
{
printf("Enter one of these operations +, -, *, /: ");
if (scanf(" %c", &operation) != 1)
{
printf("Error: Invalid input for the operator.\n");
while (getchar() != '\n');
}
else
{
if (operation != '+' && operation != '-' && operation != '*' && operation != '/')
{
printf("Error: Invalid operation.\n");
}
else
{
break;
}
}
}

if (operation == '+')
{
if (strcmp(data_types, "int") == 0)
{
result = sum(a, b);
printf("The sum is: %d\n", result);
}
else if (strcmp(data_types, "float") == 0)
{
float res = sum_0(c, d);
printf("The sum is: %f\n", res);
}
else if (strcmp(data_types, "double") == 0)
{
double res = sum_1(e, f);
printf("The sum is: %lf\n", res);
}
}
else if (operation == '-')
{
if (strcmp(data_types, "int") == 0)
{
result = sub(a, b);
printf("The difference is: %d\n", result);
}
else if (strcmp(data_types, "float") == 0)
{
float res = sub_0(c, d);
printf("The difference is: %f\n", res);
}
else if (strcmp(data_types, "double") == 0)
{
double res = sub_1(e, f);
printf("The difference is: %lf\n", res);
}
}
else if (operation == '*')
{
if (strcmp(data_types, "int") == 0)
{
result = mul(a, b);
printf("The product is: %d\n", result);
}
else if (strcmp(data_types, "float") == 0)
{
float res = mul_0(c, d);
printf("The product is: %f\n", res);
}
else if (strcmp(data_types, "double") == 0)
{
double res = mul_1(e, f);
printf("The product is: %lf\n", res);
}
}
else if (operation == '/')
{
if (strcmp(data_types, "int") == 0)
{
result = division(a, b);
if (result != 0)
printf("The division is: %d\n", result);
}
else if (strcmp(data_types, "float") == 0)
{
float res = division_0(c, d);
if (res != 0.0)
printf("The division is: %f\n", res);
}
else if (strcmp(data_types, "double") == 0)
{
double res = division_1(e, f);
if (res != 0.0)
printf("The division is: %lf\n", res);
}
}

}
20 changes: 20 additions & 0 deletions actuaryemma/c/cal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef CAL_H
#define CAL_H
#include <stdio.h>
#include <stdlib.h>

int sum(int a, int b);
float sum_0(float a, float b);
double sum_1(double a, double b);
int sub(int a, int b);
float sub_0(float a, float b);
double sub_1(double a, double b);
int mul(int a, int b);
float mul_0(float a, float b);
double mul_1(double a, double b);
int division(int a, int b);
float division_0(float a, float b);
double division_1(double a, double b);
int validate_input(const char *prompt, const char *format, void *data);

#endif
49 changes: 49 additions & 0 deletions actuaryemma/c/division.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "cal.h"

/**
* division - divide two int numbers
* @a: first number
* @b: second number
* Return: division of two numbers
*/
int division(int a, int b)
{
if (b == 0)
{
printf("Error: Division by zero is not allowed.\n");
return (0);
}
return (a / b);
}

/**
* division_0 - divide two float numbers
* @a: first number
* @b: second number
* Return: division of two numbers
*/
float division_0(float a, float b)
{
if (b == 0)
{
printf("Error: Division by zero is not allowed.\n");
return (0);
}
return (a / b);
}

/**
* division_1 - divide two double numbers
* @a: first number
* @b: second number
* Return: division of two numbers
*/
double division_1(double a, double b)
{
if (b == 0)
{
printf("Error: Division by zero is not allowed.\n");
return (0);
}
return (a / b);
}
35 changes: 35 additions & 0 deletions actuaryemma/c/mul.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "cal.h"

/**
* mul - multiply two int numbers
* @a: first number
* @b: second number
* Return: product of two numbers
*/

int mul(int a, int b)
{
return (a * b);
}

/**
* mul_0 - multiply two float numbers
* @a: first number
* @b: second number
* Return: product of two numbers
*/
float mul_0(float a, float b)
{
return (a * b);
}

/**
* mul_1 - multiply two double numbers
* @a: first number
* @b: second number
* Return: product of two numbers
*/
double mul_1(double a, double b)
{
return (a * b);
}
34 changes: 34 additions & 0 deletions actuaryemma/c/sub.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "cal.h"

/**
* sub - subtract two int numbers
* @a: first number
* @b: second number
* Return: difference of two numbers
*/
int sub(int a, int b)
{
return (a - b);
}

/**
* sub_0 - subtract two float numbers
* @a: first number
* @b: second number
* Return: difference of two numbers
*/
float sub_0(float a, float b)
{
return (a - b);
}

/**
* sub_1 - add two double numbers
* @a: first number
* @b: second number
* Return: difference of two numbers
*/
double sub_1(double a, double b)
{
return (a - b);
}
12 changes: 12 additions & 0 deletions actuaryemma/c/validate.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "cal.h"

int validate_input(const char *prompt, const char *format, void *data)
{
int valid_input;
while (printf("%s", prompt) && (valid_input = scanf(format, data)) != 1)
{
printf("Error: Invalid input.\n");
while (getchar() != '\n');
}
return valid_input;
}
32 changes: 32 additions & 0 deletions actuaryemma/python/cal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
print("\nSelect an operation to perform either 1, 2, 3 or 4:")
print("1. ADD")
print("2. SUBTRACT")
print("3. MULTIPLY")
print("4. DIVIDE")
print()

operation = input()
if operation == "1":
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
total = int(num1) + int(num2)
print("The sum of {} and {} is {}".format(num1, num2, total))
elif operation == "2":
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
sub = int(num1) - int(num2)
print("The difference of {} and {} is {}".format(num1, num2, sub))
elif operation == "3":
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
product = int(num1) * int(num2)
print("The product of {} and {} is {}".format(num1, num2, product))
elif operation == "4":
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
total = int(num1) / int(num2)
print("The division of {} and {} is {}".format(num1, num2, total))

else:
print("Ivalid Entry")

0 comments on commit f8afb47

Please sign in to comment.