diff --git a/src/arithmeticAddition.c b/src/arithmeticAddition.c new file mode 100644 index 0000000..46a2e37 --- /dev/null +++ b/src/arithmeticAddition.c @@ -0,0 +1,35 @@ +#include "arithmeticAddition.h" +#include +#include +#include + +// addition for two integer inputs (no decimal number) +int* addition_integer(int num1, int num2) { + if ((num2 > 0 && num1 > INT_MAX - num2) || (num2 < 0 && num1 < INT_MIN - num2)) { + return NULL; + } + int* result = malloc(sizeof(int)); + *result = num1 + num2; + return result; +} + +// addition for two double inputs (decimal number) +double* addition_double(double num1, double num2) { + double* result = malloc(sizeof(double)); + *result = num1+num2; + return result; +} + +// addition for two float inputs (decimal number) +float* addition_float(float num1, float num2) { + float* result = malloc(sizeof(float)); + *result = num1+num2; + return result; +} + +// addition for two long inputs (no decimal number) +long* addition_long(long num1, long num2) { + long* result = malloc(sizeof(long)); + *result = num1+num2; + return result; +} \ No newline at end of file diff --git a/src/arithmeticAddition.h b/src/arithmeticAddition.h new file mode 100644 index 0000000..3bad5ba --- /dev/null +++ b/src/arithmeticAddition.h @@ -0,0 +1,12 @@ +#ifndef THEADMIRALS_ARITHMETICADDITION_H +#define THEADMIRALS_ARITHMETICADDITION_H + +int* addition_integer(int, int); + +double* addition_double(double, double); + +float* addition_float(float, float); + +long* addition_long(long, long); + +#endif //THEADMIRALS_ARITHMETICADDITION_H diff --git a/src/arithmeticDivision.c b/src/arithmeticDivision.c new file mode 100644 index 0000000..c29e7dd --- /dev/null +++ b/src/arithmeticDivision.c @@ -0,0 +1,48 @@ +#include "arithmeticDivision.h" +#include +#include +#include + +int* division_integer(int dividend, int divisor) { + // division with 0 would cause errors + if(divisor == 0) { + return NULL; + } + // Overflow protection + if (dividend == INT_MIN && divisor == -1) { + return NULL; + } + int* result = malloc(sizeof(int)); + *result = dividend / divisor; + return result; +} + +long* division_long(long dividend, long divisor) { + // division with 0 would cause errors + if(divisor == 0) { + return NULL; + } + long* result = malloc(sizeof(long)); + *result = dividend / divisor; + return result; +} + +double* division_double(double dividend, double divisor) { + // division with 0 would cause errors + if(divisor == 0) { + return NULL; + } + double* result = malloc(sizeof(double )); + *result = dividend / divisor; + return result; +} + +float* division_float(float dividend, float divisor) { + // division with 0 would cause errors + if(divisor == 0) { + return NULL; + } + float* result = malloc(sizeof(float)); + *result = dividend / divisor; + return result; +} \ No newline at end of file diff --git a/src/arithmeticDivision.h b/src/arithmeticDivision.h new file mode 100644 index 0000000..9ae1a34 --- /dev/null +++ b/src/arithmeticDivision.h @@ -0,0 +1,12 @@ +#ifndef THEADMIRALS_ARITHMETICDIVISION_H +#define THEADMIRALS_ARITHMETICDIVISION_H + +int* division_integer(int, int); + +long* division_long(long, long); + +double* division_double(double, double); + +float* division_float(float, float); + +#endif //THEADMIRALS_ARITHMETICDIVISION_H diff --git a/src/arithmeticMultiplication_Double.c b/src/arithmeticMultiplication_Double.c new file mode 100644 index 0000000..c33d245 --- /dev/null +++ b/src/arithmeticMultiplication_Double.c @@ -0,0 +1,12 @@ +#include "stdlib.h" +#include "arithmeticMultiplication_Double.h" + + +double* multiplication_double(double a, double b) { + double* result = (double*)malloc(sizeof(double)); + if (result == NULL) { + return NULL; + } + *result = a * b; // we multiply a times b and the result gets saved in *result + return result; +} diff --git a/src/arithmeticMultiplication_Double.h b/src/arithmeticMultiplication_Double.h new file mode 100644 index 0000000..0642564 --- /dev/null +++ b/src/arithmeticMultiplication_Double.h @@ -0,0 +1,14 @@ + + +#ifndef THEADMIRALS_ARITHMETICMULTIPLICATION_DOUBLE_H +#define THEADMIRALS_ARITHMETICMULTIPLICATION_DOUBLE_H + + + + + +double* multiplication_double(double num1, double num2); + + + +#endif //THEADMIRALS_ARITHMETICMULTIPLICATION_DOUBLE_H diff --git a/src/arithmeticMultiplication_Float.c b/src/arithmeticMultiplication_Float.c new file mode 100644 index 0000000..547b9d7 --- /dev/null +++ b/src/arithmeticMultiplication_Float.c @@ -0,0 +1,12 @@ +#include "arithmeticMultiplication_Float.h" +#include + +float* multiplication_float(float a, float b) { + float* result = (float*)malloc(sizeof(float)); + if (result == NULL) { + return NULL; + } + *result = a * b; + return result; +} + diff --git a/src/arithmeticMultiplication_Float.h b/src/arithmeticMultiplication_Float.h new file mode 100644 index 0000000..48ff7a8 --- /dev/null +++ b/src/arithmeticMultiplication_Float.h @@ -0,0 +1,13 @@ + +#ifndef THEADMIRALS_ARITHMETICMULTIPLICATION_FLOAT_H +#define THEADMIRALS_ARITHMETICMULTIPLICATION_FLOAT_H + + + + + + +float* multiplication_float(float num1, float num2); + + +#endif //THEADMIRALS_ARITHMETICMULTIPLICATION_FLOAT_H diff --git a/src/arithmeticMultiplication_Int.c b/src/arithmeticMultiplication_Int.c new file mode 100644 index 0000000..a00b05d --- /dev/null +++ b/src/arithmeticMultiplication_Int.c @@ -0,0 +1,13 @@ +#include "arithmeticMultiplication_Int.h" +#include + +int* multiplication_integer(int a, int b) { + int *result = (int*)malloc(sizeof(int)); + if (result == NULL) { + return NULL; + } + *result = a * b; + + return result; +} + diff --git a/src/arithmeticMultiplication_Int.h b/src/arithmeticMultiplication_Int.h new file mode 100644 index 0000000..4222d1f --- /dev/null +++ b/src/arithmeticMultiplication_Int.h @@ -0,0 +1,8 @@ + + +#ifndef THEADMIRALS_ARITHMETICMULTIPLICATION_INT_H +#define THEADMIRALS_ARITHMETICMULTIPLICATION_INT_H + +int* multiplication_integer(int, int); + +#endif //THEADMIRALS_ARITHMETICMULTIPLICATION_INT_H diff --git a/src/arithmeticSubtraction.c b/src/arithmeticSubtraction.c new file mode 100644 index 0000000..339431b --- /dev/null +++ b/src/arithmeticSubtraction.c @@ -0,0 +1,24 @@ +#include "arithmeticSubtraction.h" + +#include + +// arithmetic subtraction for integer +int* subtraction_integer(int a, int b) { + int* result= malloc(sizeof (int)); + *result=a - b; + return result; +} + +// arithmetic subtraction for float +float* subtraction_float(float a, float b) { + float* result= malloc(sizeof (float)); + *result=a - b; + return result; +} + +// arithmetic subtraction for double +double* subtraction_double(double a, double b) { + double* result= malloc(sizeof (double)); + *result=a - b; + return result; +} diff --git a/src/arithmeticSubtraction.h b/src/arithmeticSubtraction.h new file mode 100644 index 0000000..44ea27b --- /dev/null +++ b/src/arithmeticSubtraction.h @@ -0,0 +1,9 @@ + +#ifndef THEADMIRALS_ARITHMETICSUBTRACTION_H +#define THEADMIRALS_ARITHMETICSUBTRACTION_H + +int* subtraction_integer(int a, int b); +float* subtraction_float(float a, float b); +double* subtraction_double(double a, double b); + +#endif //THEADMIRALS_ARITHMETICSUBTRACTION_H diff --git a/src/calculateFactorial.c b/src/calculateFactorial.c new file mode 100644 index 0000000..bd7ae94 --- /dev/null +++ b/src/calculateFactorial.c @@ -0,0 +1,23 @@ + + +#include "calculateFactorial.h" + +// Function for Factorial with integer +int calculateFactorial_integer(int n) { + if (n < 0) { + + } else if (n == 0 || n == 1) { + + return 1; + } else { + + int result = 1; + for (int i = 2; i <= n; ++i) { + result *= i; + } + return result; + } + return 0; + +} + diff --git a/src/calculateFactorial.h b/src/calculateFactorial.h new file mode 100644 index 0000000..f48849b --- /dev/null +++ b/src/calculateFactorial.h @@ -0,0 +1,8 @@ + + +#ifndef THEADMIRALS_CALCULATEFACTORIAL_H +#define THEADMIRALS_CALCULATEFACTORIAL_H + +int calculateFactorial_integer(int n); + +#endif //THEADMIRALS_CALCULATEFACTORIAL_H diff --git a/src/convert_CM_in_M.c b/src/convert_CM_in_M.c new file mode 100644 index 0000000..825caca --- /dev/null +++ b/src/convert_CM_in_M.c @@ -0,0 +1,7 @@ + +#include "convert_CM_in_M.h" +#include +//convert length for float cm into meter +float cm_to_meter(float cm) { + return cm / 100.0; +} diff --git a/src/convert_CM_in_M.h b/src/convert_CM_in_M.h new file mode 100644 index 0000000..49b0383 --- /dev/null +++ b/src/convert_CM_in_M.h @@ -0,0 +1,7 @@ + +#ifndef THEADMIRALS_CONVERT_CM_IN_M_H +#define THEADMIRALS_CONVERT_CM_IN_M_H + +float cm_to_meter(float cm); + +#endif //THEADMIRALS_CONVERT_CM_IN_M_H diff --git a/src/convert_C_to_F.c b/src/convert_C_to_F.c new file mode 100644 index 0000000..ddbdd3e --- /dev/null +++ b/src/convert_C_to_F.c @@ -0,0 +1,19 @@ +#include "stdio.h" +#include "convert_C_to_F.h" + +float convert_temperature(float value, char from_unit, char to_unit) { + float result; + + if (from_unit == 'c' && to_unit == 'f') { + result = (value * 9 / 5) + 32; // Celsius to Fahrenheit + } else if (from_unit == 'f' && to_unit == 'c') { + result = (value - 32) * 5 / 9; // Fahrenheit to Celsius + } else { + printf("Invalid units or conversion not supported.\n"); + result = -1; // Error code + } + + return result; +} + + diff --git a/src/convert_C_to_F.h b/src/convert_C_to_F.h new file mode 100644 index 0000000..3981936 --- /dev/null +++ b/src/convert_C_to_F.h @@ -0,0 +1,7 @@ + +#ifndef THEADMIRALS_CONVERT_C_TO_F_H +#define THEADMIRALS_CONVERT_C_TO_F_H + +float convert_temperature(float value, char from_unit, char to_unit); + +#endif //THEADMIRALS_CONVERT_C_TO_F_H diff --git a/src/convert_M_in_KM.c b/src/convert_M_in_KM.c new file mode 100644 index 0000000..4980a96 --- /dev/null +++ b/src/convert_M_in_KM.c @@ -0,0 +1,6 @@ +#include "convert_M_in_KM.h" + +//convert length for double metre into kilometre +double meter_to_kilometer(double meter) { + return meter / 1000.0; +} diff --git a/src/convert_M_in_KM.h b/src/convert_M_in_KM.h new file mode 100644 index 0000000..9dc3232 --- /dev/null +++ b/src/convert_M_in_KM.h @@ -0,0 +1,7 @@ + +#ifndef THEADMIRALS_CONVERT_M_IN_KM_H +#define THEADMIRALS_CONVERT_M_IN_KM_H + +double meter_to_kilometer(double meter); + +#endif //THEADMIRALS_CONVERT_M_IN_KM_H diff --git a/src/convert_cm_in_dm.c b/src/convert_cm_in_dm.c new file mode 100644 index 0000000..05d87e3 --- /dev/null +++ b/src/convert_cm_in_dm.c @@ -0,0 +1,7 @@ + +#include "convert_cm_in_dm.h" +#include +//convert length for double cm into dm +double cm_to_dm(double cm) { + return cm / 10.0; +} diff --git a/src/convert_cm_in_dm.h b/src/convert_cm_in_dm.h new file mode 100644 index 0000000..eafecd9 --- /dev/null +++ b/src/convert_cm_in_dm.h @@ -0,0 +1,7 @@ + +#ifndef THEADMIRALS_CONVERT_CM_TO_DM_H +#define THEADMIRALS_CONVERT_CM_TO_DM_H + +double cm_to_dm(double cm); + +#endif //THEADMIRALS_CONVERT_CM_TO_DM_H diff --git a/src/convert_g_to_mg.c b/src/convert_g_to_mg.c new file mode 100644 index 0000000..ceba263 --- /dev/null +++ b/src/convert_g_to_mg.c @@ -0,0 +1,6 @@ + +#include "convert_g_to_mg.h" + +double g_to_mg(double grams) { + return grams * 1000.0; +} diff --git a/src/convert_g_to_mg.h b/src/convert_g_to_mg.h new file mode 100644 index 0000000..e764c7e --- /dev/null +++ b/src/convert_g_to_mg.h @@ -0,0 +1,17 @@ + +#ifndef THEADMIRALS_CONVERT_G_TO_MG_H +#define THEADMIRALS_CONVERT_G_TO_MG_H + + + +// Convert grams to milligrams +// Parameters: +// grams: weight in grams +// Returns: +// The equivalent weight in milligrams + +double g_to_mg(double grams); + + + +#endif //THEADMIRALS_CONVERT_G_TO_MG_H diff --git a/src/convert_kg_to_g.c b/src/convert_kg_to_g.c new file mode 100644 index 0000000..2ce2914 --- /dev/null +++ b/src/convert_kg_to_g.c @@ -0,0 +1,6 @@ + +#include "convert_kg_to_g.h" + +double kg_to_gram(double kilograms) { + return kilograms * 1000.0; +} diff --git a/src/convert_kg_to_g.h b/src/convert_kg_to_g.h new file mode 100644 index 0000000..15745dd --- /dev/null +++ b/src/convert_kg_to_g.h @@ -0,0 +1,18 @@ + + +#ifndef THEADMIRALS_CONVERT_KG_TO_G_H +#define THEADMIRALS_CONVERT_KG_TO_G_H + + + +// Convert kilograms to grams +// Parameters: +// kilograms: weight in kilograms +// Returns: +// The equivalent weight in grams + +double kg_to_gram(double kilograms); + + + +#endif //THEADMIRALS_CONVERT_KG_TO_G_H diff --git a/src/convert_kg_to_ton.c b/src/convert_kg_to_ton.c new file mode 100644 index 0000000..3803dce --- /dev/null +++ b/src/convert_kg_to_ton.c @@ -0,0 +1,6 @@ + +#include "convert_kg_to_ton.h" + +double kg_to_tons(double kilograms) { + return kilograms / 1000.0; +} diff --git a/src/convert_kg_to_ton.h b/src/convert_kg_to_ton.h new file mode 100644 index 0000000..032dc7f --- /dev/null +++ b/src/convert_kg_to_ton.h @@ -0,0 +1,7 @@ +#ifndef THEADMIRALS_CONVERT_KG_TO_TON_H +#define THEADMIRALS_CONVERT_KG_TO_TON_H + +// Convert kilograms to tons +double kg_to_tons(double kilograms); + +#endif //THEADMIRALS_CONVERT_KG_TO_TON_H diff --git a/src/convert_kph_to_mps.c b/src/convert_kph_to_mps.c new file mode 100644 index 0000000..ddb0da4 --- /dev/null +++ b/src/convert_kph_to_mps.c @@ -0,0 +1,11 @@ +#include "convert_kph_to_mps.h" + +double converter_kph_to_mps(double kph){ + double speed = kph / 3.6; + return speed; +} + +double converter_mps_to_kph(double mps){ + double speed = mps * 3.6; + return speed; +} \ No newline at end of file diff --git a/src/convert_kph_to_mps.h b/src/convert_kph_to_mps.h new file mode 100644 index 0000000..c4a8592 --- /dev/null +++ b/src/convert_kph_to_mps.h @@ -0,0 +1,7 @@ +#ifndef THEADMIRALS_CONVERT_KPH_TO_MPS_H +#define THEADMIRALS_CONVERT_KPH_TO_MPS_H + +double converter_kph_to_mps(double kph); +double converter_mps_to_kph(double mps); + +#endif //THEADMIRALS_CONVERT_KPH_TO_MPS_H diff --git a/src/convert_m_to_ft.c b/src/convert_m_to_ft.c new file mode 100644 index 0000000..e488352 --- /dev/null +++ b/src/convert_m_to_ft.c @@ -0,0 +1,23 @@ + +#include "convert_m_to_ft.h" +#include + +float convert_length(float value, char from_unit, char to_unit) { + float result; + + if (from_unit == 'm' && to_unit == 'f') { + result = value * 3.281; // Meters to feet + } else if (from_unit == 'f' && to_unit == 'm') { + result = value / 3.281; // Feet to meters + } else if (from_unit == 'i' && to_unit == 'c') { + result = value * 2.54; // Inches to centimeters + } else if (from_unit == 'c' && to_unit == 'i') { + result = value / 2.54; + } else { + printf("Invalid units or conversion not supported.\n"); + result = -1; // Error code + } + + return result; +} +// function converts between cm to inches and vice versa, and between m to ft and vice versa diff --git a/src/convert_m_to_ft.h b/src/convert_m_to_ft.h new file mode 100644 index 0000000..3c99725 --- /dev/null +++ b/src/convert_m_to_ft.h @@ -0,0 +1,15 @@ + +#ifndef THEADMIRALS_CONVERT_M_TO_FT_H +#define THEADMIRALS_CONVERT_M_TO_FT_H + +// Convert length from one unit to another +// Parameters: +// value: the value to be converted +// from_unit: the unit to convert from ('m' for meters, 'c' for centimeters, 'i' for inches) +// to_unit: the unit to convert to ('m' for meters, 'c' for centimeters, 'i' for inches) +// Returns: +// The converted length value + +float convert_length(float value, char from_unit, char to_unit); + +#endif //THEADMIRALS_CONVERT_M_TO_FT_H diff --git a/src/convert_time.c b/src/convert_time.c new file mode 100644 index 0000000..40a9403 --- /dev/null +++ b/src/convert_time.c @@ -0,0 +1,49 @@ +#include "convert_time.h" + +// Converts Seconds to Minutes +double converter_sec_to_min(double sec){ + double time = sec / 60; + return time; +} + +// Converts Minutes to Seconds +double converter_min_to_sec(double min){ + double time = min * 60; + return time; +} + +// Converts Hours to Minutes +double converter_hour_to_min(double hour){ + double min = hour * 60; + return min; +} + +// Converts Minutes To Hours +double converter_min_to_hour(double min){ + double hours = min / 60; + return hours; +} + +// Converts Hours in Seconds +double converter_hour_in_sec(double hour){ + double secs = hour * 60 * 60; + return secs; +} + +// Converts Seconds in Hours +double converter_sec_in_hour(double sec){ + double hours = sec / 60 / 60; + return hours; +} + +// Converts Days in Years +double converter_days_to_years(double days){ + double years = days / 365; + return years; +} + +// convert Years in days +double converter_years_to_days(double years){ + double amount = years * 365; + return amount; +} \ No newline at end of file diff --git a/src/convert_time.h b/src/convert_time.h new file mode 100644 index 0000000..ac2ec74 --- /dev/null +++ b/src/convert_time.h @@ -0,0 +1,13 @@ +#ifndef THEADMIRALS_CONVERT_TIME_H +#define THEADMIRALS_CONVERT_TIME_H + +double converter_sec_to_min(double sec); +double converter_min_to_sec(double min); +double converter_hour_to_min(double hour); +double converter_min_to_hour(double min); +double converter_hour_in_sec(double hour); +double converter_sec_in_hour(double sec); +double converter_days_to_years(double days); +double converter_years_to_days(double years); + +#endif //THEADMIRALS_CONVERT_TIME_H diff --git a/src/convert_ton_to_kg.c b/src/convert_ton_to_kg.c new file mode 100644 index 0000000..d1653d8 --- /dev/null +++ b/src/convert_ton_to_kg.c @@ -0,0 +1,7 @@ + +#include "convert_ton_to_kg.h" + + +double tons_to_kg(double tons) { + return tons * 1000.0; +} diff --git a/src/convert_ton_to_kg.h b/src/convert_ton_to_kg.h new file mode 100644 index 0000000..9591ea6 --- /dev/null +++ b/src/convert_ton_to_kg.h @@ -0,0 +1,17 @@ + + +#ifndef THEADMIRALS_CONVERT_TON_TO_KG_H +#define THEADMIRALS_CONVERT_TON_TO_KG_H + +// Convert tons to kilograms +// Parameters: +// tons: weight in tons +// Returns: +// The equivalent weight in kilograms + + +double tons_to_kg(double tons); + + + +#endif //THEADMIRALS_CONVERT_TON_TO_KG_H diff --git a/src/convert_ton_to_mt.c b/src/convert_ton_to_mt.c new file mode 100644 index 0000000..574d21e --- /dev/null +++ b/src/convert_ton_to_mt.c @@ -0,0 +1,7 @@ + +#include "convert_ton_to_mt.h" + + +double tons_to_megatons(double tons) { + return tons / 1000000.0; +} diff --git a/src/convert_ton_to_mt.h b/src/convert_ton_to_mt.h new file mode 100644 index 0000000..331c20b --- /dev/null +++ b/src/convert_ton_to_mt.h @@ -0,0 +1,13 @@ + +#ifndef THEADMIRALS_CONVERT_TON_TO_MT_H +#define THEADMIRALS_CONVERT_TON_TO_MT_H + + + + +double tons_to_megatons(double tons); + + + + +#endif //THEADMIRALS_CONVERT_TON_TO_MT_H diff --git a/src/exponentials.c b/src/exponentials.c new file mode 100644 index 0000000..389997b --- /dev/null +++ b/src/exponentials.c @@ -0,0 +1,9 @@ +#include "exponentials.h" +#include +#include + +double* exponentials_double(int base, int exponent) { + double* result = malloc(sizeof(double)); + *result = pow(base, exponent); + return result; +} \ No newline at end of file diff --git a/src/exponentials.h b/src/exponentials.h new file mode 100644 index 0000000..49ab5b9 --- /dev/null +++ b/src/exponentials.h @@ -0,0 +1,6 @@ +#ifndef THEADMIRALS_EXPONENTIALS_H +#define THEADMIRALS_EXPONENTIALS_H + +double* exponentials_double(int, int); + +#endif //THEADMIRALS_EXPONENTIALS_H diff --git a/src/inputHandler.c b/src/inputHandler.c new file mode 100644 index 0000000..199d2dd --- /dev/null +++ b/src/inputHandler.c @@ -0,0 +1,58 @@ +#include "inputHandler.h" +#include + +// return operation id for a specific symbol +int getOperationIdBySymbol(char symbol) { + int id = 0; + switch (symbol) { + case '+': + return 1; + case '-': + return 2; + case '/': + return 3; + case '*': + return 4; + case '^': + return 5; + case '%': + return 6; + case '!': + return 7; + } + return id; +} + +// returns operation symbol for a specific operation id +char getOperationSymbolById(int id) { + char symbol = ' '; + switch (id) { + case 1: + return '+'; + case 2: + return '-'; + case 3: + return '/'; + case 4: + return '*'; + case 5: + return '^'; + case 6: + return '%'; + case 7: + return '!'; + } + return symbol; +} + +// Checking if operation id is available +int isOperationIdValid(int id) { + if(id < 1 || id > 7) return 0; + return 1; +} + +int isNumberTooBig(int number) { + if(number < 200) return 0; + //printf("This number is too big. Please choose a smaller one...\n"); + return 1; +} \ No newline at end of file diff --git a/src/inputHandler.h b/src/inputHandler.h new file mode 100644 index 0000000..372f9d0 --- /dev/null +++ b/src/inputHandler.h @@ -0,0 +1,12 @@ +#ifndef THEADMIRALS_INPUTHANDLER_H +#define THEADMIRALS_INPUTHANDLER_H + +int getOperationIdBySymbol(char); + +char getOperationSymbolById(int); + +int isOperationIdValid(int); + +int isNumberTooBig(int); + +#endif //THEADMIRALS_INPUTHANDLER_H diff --git a/src/logarithmicFunctions.c b/src/logarithmicFunctions.c new file mode 100644 index 0000000..e940cb5 --- /dev/null +++ b/src/logarithmicFunctions.c @@ -0,0 +1,18 @@ +#include "logarithmicFunctions.h" +#include +#include +#include + +double* logarithm_two_integer(int base, int num){ + if(base <= 1 || num <= 0){ + return NULL; + } + + double* result = (double*)malloc(sizeof(double)); + if (result == NULL) { + return NULL; + } + + *result = log(num) / log(base); + return result; +} diff --git a/src/logarithmicFunctions.h b/src/logarithmicFunctions.h new file mode 100644 index 0000000..c13f29d --- /dev/null +++ b/src/logarithmicFunctions.h @@ -0,0 +1,6 @@ +#ifndef THEADMIRALS_LOGARITHMICFUNCTIONS_H +#define THEADMIRALS_LOGARITHMICFUNCTIONS_H + +double* logarithm_two_integer(int base, int num); + +#endif //THEADMIRALS_LOGARITHMICFUNCTIONS_H diff --git a/src/main.c b/src/main.c index 86d4137..56241ed 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,38 @@ #include +#include +#include "operationHandler.h" + +char buffer[100]; int main() { + printf("Please enter the id of a specific operation...\n1. addition\n2. subtraction\n3. multiplication\n4. division\n"); + + // input for math operation as integer + int input; + scanf("%d", &input); + + // check if operation input is valid + if(!checkOperationInput(input)) { + printf("Invalid operation id\n"); + return 0; + } + + printf("\nPlease enter the first and the second number separated by a space...\n"); + + // loop to enter numbers for calculation + while(fgets(buffer, 100, stdin)) { + buffer[strcspn(buffer, "\n")] = '\0'; + if (strlen(buffer) > 0) { + break; + } + } + + // extracting numbers from input + int* result = evaluateInput(buffer, input); + + if(result == NULL) { + printf("\nInvalid formatting. Two numbers need to be separated by a space\n"); + return 0; + } + printf("\nResult: %d", *result); } \ No newline at end of file diff --git a/src/operationHandler.c b/src/operationHandler.c new file mode 100644 index 0000000..df85a49 --- /dev/null +++ b/src/operationHandler.c @@ -0,0 +1,122 @@ +#include "operationHandler.h" +#include +#include +#include +#include +#include + +// checking integer input as operation id +bool checkOperationInput(int input) { + switch (input) { + case 4: case 3: case 2: case 1: + return true; + } + return false; +} + +// full input process +int* evaluateInput(char* input, int operation) { + // check if formatting is correct + if(!containsTwoNumbers(input)) return NULL; + int firstNumber = extractFirstNumber(input); + // origin string "input" has been edited + int secondNumber = atoi(input); + int* result = malloc(sizeof(int)); + switch (operation) { + case 1: + *result = firstNumber + secondNumber; + break; + case 2: + *result = firstNumber - secondNumber; + break; + case 3: + *result = firstNumber * secondNumber; + break; + case 4: + *result = firstNumber / secondNumber; + break; + } + return result; +} + +// formatting check of string +bool containsTwoNumbers(const char* str) { + int numbersCount = 0; + bool hasSpace = false; + bool isInNumber = false; + + for (int i = 0; str[i] != '\0'; i++) { + if (isdigit(str[i])) { + if (!isInNumber) { + isInNumber = true; + numbersCount++; + } + } else if (str[i] == ' ' && isInNumber) { + hasSpace = true; + isInNumber = false; + } else if (str[i] == ' ' && !isInNumber) { + hasSpace = false; + } else { + hasSpace = false; + isInNumber = false; + } + } + return (numbersCount == 2 && hasSpace); +} + +// extracting of first number and removing from string +int extractFirstNumber(char* str) { + int number = 0; + bool isInNumber = false; + int endIndex = 0; + + for (int i = 0; str[i] != '\0'; i++) { + if (isdigit(str[i])) { + if (!isInNumber) { + isInNumber = true; + number = str[i] - '0'; + } else { + number = number * 10 + (str[i] - '0'); + } + } else if (str[i] == ' ' && isInNumber) { + endIndex = i; + break; + } else { + isInNumber = false; + } + } + + int index = 0; + char temp[strlen(str)-endIndex+1]; + for(int i = endIndex+1; i < strlen(str); i++) { + temp[index++] = str[i]; + } + temp[index] = '\0'; + strcpy(str, temp); + + return number; +} + +void outputInteger(int output) { + outputTemplate(); + printf("Result: %d", output); +} + +void outputLong(long output) { + outputTemplate(); + printf("Result: %ld", output); +} + +void outputDouble(double output) { + outputTemplate(); + printf("Result: %lf", output); +} + +void outputFloat(float output) { + outputTemplate(); + printf("Result: %f", output); +} + +void outputTemplate() { + printf("###############################\n-> Calculator\n#################################"); +} \ No newline at end of file diff --git a/src/operationHandler.h b/src/operationHandler.h new file mode 100644 index 0000000..ad14eb2 --- /dev/null +++ b/src/operationHandler.h @@ -0,0 +1,23 @@ +#ifndef THEADMIRALS_OPERATIONHANDLER_H +#define THEADMIRALS_OPERATIONHANDLER_H +#include + +bool checkOperationInput(int); + +int* evaluateInput(char*, int); + +bool containsTwoNumbers(const char*); + +int extractFirstNumber(char*); + +void outputInteger(int); + +void outputDouble(double); + +void outputFloat(float); + +void outputLong(long); + +void outputTemplate(); + +#endif //THEADMIRALS_OPERATIONHANDLER_H diff --git a/src/trigonometricFunctions.c b/src/trigonometricFunctions.c new file mode 100644 index 0000000..db262a0 --- /dev/null +++ b/src/trigonometricFunctions.c @@ -0,0 +1,24 @@ + +#include "trigonometricFunctions.h" +#include "math.h" +#include +#include +//function for trigonometric +//Function 1 for double +double* calculate_sin_double(double angle) { + double* result= malloc(sizeof (double)); + *result=sin(angle); + return result; +} +//Function 2 for double +double* calculate_cos_double(double angle) { + double* result= malloc(sizeof (double)); + *result=cos(angle); + return result; +} +//Function 3 for double +double* calculate_tan_double(double angle) { + double* result= malloc(sizeof (double)); + *result=tan(angle); + return result; +} diff --git a/src/trigonometricFunctions.h b/src/trigonometricFunctions.h new file mode 100644 index 0000000..add1a8a --- /dev/null +++ b/src/trigonometricFunctions.h @@ -0,0 +1,9 @@ + +#ifndef THEADMIRALS_TRIGONOMETRICFUNCTIONS_H +#define THEADMIRALS_TRIGONOMETRICFUNCTIONS_H + +double* calculate_sin_double(double angle); +double* calculate_cos_double(double angle); +double* calculate_tan_double(double angle); + +#endif //THEADMIRALS_TRIGONOMETRICFUNCTIONS_H diff --git a/team.md b/team.md index 9389aab..bb39ebd 100644 --- a/team.md +++ b/team.md @@ -1,3 +1,5 @@ - Eric Bagus, fdai7812 +- fdai7812, fdai7812 - Leon Wolf, fdai7845 -- Sandro Welte, fdai7728 \ No newline at end of file +- Sandro Welte, fdai7728 +- Jonas Zitzmann, fdai7791 \ No newline at end of file diff --git a/test/test_arithmeticAddition.c b/test/test_arithmeticAddition.c new file mode 100644 index 0000000..5e261b2 --- /dev/null +++ b/test/test_arithmeticAddition.c @@ -0,0 +1,45 @@ +#include "../src/arithmeticAddition.h" +#include "unity.h" +#include "limits.h" + +void setUp(void) { + // set stuff up here +} + +void tearDown(void) { + // clean stuff up here +} + +void test_arithmeticAddition_numberplusnumberequalsnumber(void) { + int expectedResult = 8; + int* result; + result = addition_integer(5, 3); + TEST_ASSERT_EQUAL_INT(expectedResult, *result); +} + +void test_arithmeticAddition_numberplusmaxintegervalueequalsnull(void) { + int* result; + result = addition_integer(INT_MAX, 1); + TEST_ASSERT_NULL(result); +} + +void test_arithmeticAddition_doublenumbercalculationequalscorrectdoublenumber(void) { + double expectedResult = 5.500000; + double* result; + result = addition_double(3.5, 2.0); + TEST_ASSERT_EQUAL_DOUBLE(expectedResult, *result); +} + +void test_arithmeticAddition_longnumbercalculationequalscorrectlong(void) { + long expectedResult = 10; + long* result; + result = addition_long(5, 5); + TEST_ASSERT_EQUAL_INT(expectedResult, *result); +} + +void test_arithmeticAddition_floatnumbercalculationequalsfloat(void) { + float expectedResult = 5.5; + float* result; + result = addition_float(3.5, 2.0); + TEST_ASSERT_EQUAL_FLOAT(expectedResult, *result); +} diff --git a/test/test_arithmeticDivision.c b/test/test_arithmeticDivision.c new file mode 100644 index 0000000..0ae60eb --- /dev/null +++ b/test/test_arithmeticDivision.c @@ -0,0 +1,40 @@ +#include "../src/arithmeticDivision.h" +#include "unity.h" +#include "limits.h" + +void setUp(void) { + // set stuff up here +} + +void tearDown(void) { + // clean stuff up here +} + + +void test_arithmeticDivision_numberdividedbynumberequalsnumber(void) { + int expectedResult = 2; + int* result; + result = division_integer(14, 7); + TEST_ASSERT_EQUAL_INT(expectedResult, *result); +} + +void test_arithmeticDivision_longdividedbylongequalscorrectlong(void) { + long expectedResult = 5; + long* result; + result = division_long(10, 2); + TEST_ASSERT_EQUAL_INT(expectedResult, *result); +} + +void test_arithmeticDivsion_floatdividedbyfloatequalscorrectfloat(void) { + float expectedResult = 5.0; + float* result; + result = division_float(10.0, 2.0); + TEST_ASSERT_EQUAL_FLOAT(expectedResult, *result); +} + +void test_arithmeticDivision_doubledividedbydoublequalscorrectdouble(void) { + double expectedResult = 5.0; + double* result; + result = division_double(10.0, 2.0); + TEST_ASSERT_EQUAL_DOUBLE(expectedResult, *result); +} diff --git a/test/test_arithmeticMultiplication_Int.c b/test/test_arithmeticMultiplication_Int.c new file mode 100644 index 0000000..b5fb385 --- /dev/null +++ b/test/test_arithmeticMultiplication_Int.c @@ -0,0 +1,21 @@ +#include "../src/arithmeticMultiplication_Int.h" +#include "unity.h" +#include "limits.h" + +void setUp(void) { + // set stuff up here +} + +void tearDown(void) { + // clean stuff up here +} + +void test_arithmeticMultiplication_numbertimesnumberequalsnumber(void) { + int expectedResult = 10; + int* result; + result = multiplication_integer(2,5); + TEST_ASSERT_EQUAL_INT(expectedResult, *result); +} + + + diff --git a/test/test_arithmeticSubtraction.c b/test/test_arithmeticSubtraction.c new file mode 100644 index 0000000..007b72b --- /dev/null +++ b/test/test_arithmeticSubtraction.c @@ -0,0 +1,31 @@ +#include "../src/arithmeticSubtraction.h" +#include "unity.h" +#include "limits.h" + +void setUp(void) { + // set stuff up here +} + +void tearDown(void) { + // clean stuff up here +} +void test_arithmeticSubtraction_subractionoftwonumbers(void) { + int expectedResult = 7; + int* result; + result = subtraction_integer(14, 7); + TEST_ASSERT_EQUAL_INT(expectedResult, *result); +} +void test_arithmeticSubtraction_subractionoftwonumberswithfloat(void) { + float expectedResult = 7; + float* result; + result = subtraction_float(14, 7); + TEST_ASSERT_EQUAL_FLOAT(expectedResult, *result); +} +void test_arithmeticSubtraction_subractionoftwonumberswithdouble(void) { + double expectedResult = 7; + double* result; + result = subtraction_double(14, 7); + TEST_ASSERT_EQUAL_DOUBLE (expectedResult, *result); +} + + diff --git a/test/test_convert_m_to_ft.c b/test/test_convert_m_to_ft.c new file mode 100644 index 0000000..98ba87e --- /dev/null +++ b/test/test_convert_m_to_ft.c @@ -0,0 +1,28 @@ + +#include "../src/convert_m_to_ft.h" +#include "unity.h" +#include "limits.h" + +void setUp(void) { + // Set up resources here if needed +} + +void tearDown(void) { + // Clean up resources here if needed +} + +void test_convert_length(void) { + float value = 10.0; + char from_unit = 'm'; + char to_unit = 'f'; + + // Perform the conversion + float result = convert_length(value, from_unit, to_unit); + + // Define the expected result (10 meters to feet is approximately 32.81 feet) + float expectedResult = 32.81; + + // Assert the result + TEST_ASSERT_EQUAL_FLOAT(expectedResult, result); +} + diff --git a/test/test_exponentials.c b/test/test_exponentials.c new file mode 100644 index 0000000..6266ac7 --- /dev/null +++ b/test/test_exponentials.c @@ -0,0 +1,18 @@ +#include "../src/exponentials.h" +#include "unity.h" +#include "limits.h" + +void setUp(void) { + // set stuff up here +} + +void tearDown(void) { + // clean stuff up here +} + +void test_exponentials_calculatenumfrombaseandexponent(void) { + double expectedResult = 32.000000; + double* result; + result = exponentials_double(2, 5); + TEST_ASSERT_EQUAL_DOUBLE(expectedResult, *result); +} \ No newline at end of file diff --git a/test/test_inputHandler.c b/test/test_inputHandler.c new file mode 100644 index 0000000..4b4f874 --- /dev/null +++ b/test/test_inputHandler.c @@ -0,0 +1,39 @@ +#include "../src/inputHandler.h" +#include "unity.h" + +void setUp(void) { + // set stuff up here +} + +void tearDown(void) { + // clean stuff up here +} + + +void test_inputHandler_returntwoforinputminus(void) { + int expectedResult = 2; + int result; + result = getOperationIdBySymbol('-'); + TEST_ASSERT_EQUAL_INT(expectedResult, result); +} + +void test_inputHandler_returnoneforinputplus(void) { + int expectedResult = 1; + int result; + result = getOperationIdBySymbol('+'); + TEST_ASSERT_EQUAL_INT(expectedResult, result); +} + +void test_inputHandler_returnzeroforinvalidinput(void) { + int expectedResult = 0; + int result; + result = isOperationIdValid(0); + TEST_ASSERT_EQUAL_INT(expectedResult, result); +} + +void test_inputHandler_returnoneifinputtoobig(void) { + int expectedResult = 1; + int result; + result = isNumberTooBig(300); + TEST_ASSERT_EQUAL_INT(expectedResult, result); +} \ No newline at end of file diff --git a/test/test_logarithmicFunctions.c b/test/test_logarithmicFunctions.c new file mode 100644 index 0000000..512696e --- /dev/null +++ b/test/test_logarithmicFunctions.c @@ -0,0 +1,18 @@ +#include "../src/logarithmicFunctions.h" +#include "unity.h" +#include "limits.h" + +void setUp(void) { + // set stuff up here +} + +void tearDown(void) { + // clean stuff up here +} + +void test_logarithmicFunctions_logarithmiccalculation(void){ + double expectedResult = 3.000000; + double* result; + result = logarithm_two_integer(2,8); + TEST_ASSERT_EQUAL_DOUBLE(expectedResult, *result); +} \ No newline at end of file diff --git a/test/test_operationHandler.c b/test/test_operationHandler.c new file mode 100644 index 0000000..cadef23 --- /dev/null +++ b/test/test_operationHandler.c @@ -0,0 +1,56 @@ +#include "../src/operationHandler.h" +#include "unity.h" + +void setUp(void) { + // set stuff up here +} + +void tearDown(void) { + // clean stuff up here +} + +void test_operationHandler_truereturnvaluewithvalidinput(void) { + int expectedResult = 1; + int result1 = checkOperationInput(1); + int result2 = checkOperationInput(2); + int result3 = checkOperationInput(3); + int result4 = checkOperationInput(4); + TEST_ASSERT_EQUAL_INT(expectedResult, result1); + TEST_ASSERT_EQUAL_INT(expectedResult, result2); + TEST_ASSERT_EQUAL_INT(expectedResult, result3); + TEST_ASSERT_EQUAL_INT(expectedResult, result4); +} + +void test_operationHandler_falsereturnvaluewithinvalidinput(void) { + int expectedResult = 0; + int result = checkOperationInput(8); + TEST_ASSERT_EQUAL_INT(expectedResult, result); +} + +void test_operationHandler_truereturnvaluewithformattedinput(void) { + int expectedResult = 1; + const char str[] = {'1', '4', ' ', '5', '6', '\0'}; + int result = containsTwoNumbers(str); + TEST_ASSERT_EQUAL_INT(expectedResult, result); +} + +void test_operationHandler_falsereturnvaluewithwronginput(void) { + int expectedResult = 0; + const char str[] = {'5', '6', '\0'}; + int result = containsTwoNumbers(str); + TEST_ASSERT_EQUAL_INT(expectedResult, result); +} + +void test_operationHandler_extractingFirstNumber(void) { + int expectedResult = 48; + char str[] = {'4', '8', ' ', '5', '\0'}; + int result = extractFirstNumber(str); + TEST_ASSERT_EQUAL_INT(expectedResult, result); +} + +void test_operationHandler_removefirstnumberfromoriginalstring(void) { + char expected[] = {'5', '\0'}; + char str[] = {'4', '8', ' ', '5', '\0'}; + extractFirstNumber(str); + TEST_ASSERT_EQUAL_STRING(expected, str); +}