diff --git a/src/arithmeticAddition.c b/src/arithmeticAddition.c index 8f93738..173c438 100644 --- a/src/arithmeticAddition.c +++ b/src/arithmeticAddition.c @@ -12,20 +12,20 @@ int* addition_integer(int num1, int num2) { return result; } -double* addition_double(double number1, double number2) { +double* addition_double(double num1, double num2) { double* result = malloc(sizeof(double)); - *result = number1+number2; + *result = num1+num2; return result; } -float* addition_float(float number1, float number2) { +float* addition_float(float num1, float num2) { float* result = malloc(sizeof(float)); - *result = number1+number2; + *result = num1+num2; return result; } -long* addition_long(long number1, long number2) { +long* addition_long(long num1, long num2) { long* result = malloc(sizeof(long)); - *result = number1+number2; + *result = num1+num2; return result; } \ No newline at end of file diff --git a/src/arithmeticDivision.c b/src/arithmeticDivision.c index 7cdfc7e..c29e7dd 100644 --- a/src/arithmeticDivision.c +++ b/src/arithmeticDivision.c @@ -4,6 +4,7 @@ #include int* division_integer(int dividend, int divisor) { + // division with 0 would cause errors if(divisor == 0) { return NULL; } @@ -14,4 +15,34 @@ int* division_integer(int dividend, int divisor) { 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 index e4080b7..9ae1a34 100644 --- a/src/arithmeticDivision.h +++ b/src/arithmeticDivision.h @@ -3,4 +3,10 @@ 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 index 3207551..c33d245 100644 --- a/src/arithmeticMultiplication_Double.c +++ b/src/arithmeticMultiplication_Double.c @@ -5,7 +5,7 @@ double* multiplication_double(double a, double b) { double* result = (double*)malloc(sizeof(double)); if (result == NULL) { - return NULL; // Handle memory allocation failure + return NULL; } *result = a * b; // we multiply a times b and the result gets saved in *result return result; diff --git a/src/arithmeticMultiplication_Float.c b/src/arithmeticMultiplication_Float.c index 8e063f2..547b9d7 100644 --- a/src/arithmeticMultiplication_Float.c +++ b/src/arithmeticMultiplication_Float.c @@ -4,7 +4,7 @@ float* multiplication_float(float a, float b) { float* result = (float*)malloc(sizeof(float)); if (result == NULL) { - return NULL; // Handle memory allocation failure + return NULL; } *result = a * b; return result; diff --git a/src/arithmeticMultiplication_Int.c b/src/arithmeticMultiplication_Int.c index 1ad1a19..a00b05d 100644 --- a/src/arithmeticMultiplication_Int.c +++ b/src/arithmeticMultiplication_Int.c @@ -1,15 +1,11 @@ - - #include "arithmeticMultiplication_Int.h" #include int* multiplication_integer(int a, int b) { int *result = (int*)malloc(sizeof(int)); if (result == NULL) { - // Handle memory allocation failure return NULL; } - *result = a * b; return result; diff --git a/src/arithmeticSubtraction.c b/src/arithmeticSubtraction.c index ed08d04..bbabad9 100644 --- a/src/arithmeticSubtraction.c +++ b/src/arithmeticSubtraction.c @@ -1,20 +1,22 @@ - #include "arithmeticSubtraction.h" #include -//arithmetic Subtraction specification integer + +// arithmetic subtraction integer int* subtraction_integer(int a, int b) { int* result= malloc(sizeof (int)); *result=a - b; return result; } -//arithmetic Subtraction specification float + +// arithmetic subtraction float float* subtraction_float(float a, float b) { float* result= malloc(sizeof (float)); *result=a - b; return result; } -//arithmetic Subtraction specification double + +// arithmetic subtraction double double* subtraction_double(double a, double b) { double* result= malloc(sizeof (double)); *result=a - b; diff --git a/src/calculateFactorial.c b/src/calculateFactorial.c index 0c6cbf0..bd7ae94 100644 --- a/src/calculateFactorial.c +++ b/src/calculateFactorial.c @@ -1,8 +1,8 @@ #include "calculateFactorial.h" -//Factorial -// Function for Factorial + +// Function for Factorial with integer int calculateFactorial_integer(int n) { if (n < 0) { @@ -17,4 +17,7 @@ int calculateFactorial_integer(int n) { } return result; } + return 0; + } + diff --git a/src/convert_CM_in_M.c b/src/convert_CM_in_M.c index bdc42ea..825caca 100644 --- a/src/convert_CM_in_M.c +++ b/src/convert_CM_in_M.c @@ -1,7 +1,7 @@ #include "convert_CM_in_M.h" #include -//convert length +//convert length for float cm into meter float cm_to_meter(float cm) { return cm / 100.0; } diff --git a/src/convert_M_in_KM.c b/src/convert_M_in_KM.c index 966872e..bf56016 100644 --- a/src/convert_M_in_KM.c +++ b/src/convert_M_in_KM.c @@ -1,7 +1,6 @@ - #include "convert_M_in_KM.h" -#include -//convert length + +// convert length for double metre into kilometre double meter_to_kilometer(double meter) { return meter / 1000.0; } 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 index 51cea7a..3803dce 100644 --- a/src/convert_kg_to_ton.c +++ b/src/convert_kg_to_ton.c @@ -2,5 +2,5 @@ #include "convert_kg_to_ton.h" double kg_to_tons(double kilograms) { - return kilograms / 1000.0; // 1 ton = 1000 kilograms + return kilograms / 1000.0; } diff --git a/src/convert_kg_to_ton.h b/src/convert_kg_to_ton.h index 01f9369..cc38ea6 100644 --- a/src/convert_kg_to_ton.h +++ b/src/convert_kg_to_ton.h @@ -5,6 +5,12 @@ +// Convert kilograms to tons +// Parameters: +// kilograms: weight in kilograms +// Returns: +// The equivalent weight in tons + double kg_to_tons(double kilograms); diff --git a/src/convert_m_to_ft.c b/src/convert_m_to_ft.c index 021c6b3..e488352 100644 --- a/src/convert_m_to_ft.c +++ b/src/convert_m_to_ft.c @@ -12,7 +12,7 @@ float convert_length(float value, char from_unit, char to_unit) { } 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; // Centimeters to inches + result = value / 2.54; } else { printf("Invalid units or conversion not supported.\n"); result = -1; // Error code @@ -20,4 +20,4 @@ float convert_length(float value, char from_unit, char to_unit) { 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 index 6b9317c..3c99725 100644 --- a/src/convert_m_to_ft.h +++ b/src/convert_m_to_ft.h @@ -2,6 +2,13 @@ #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); diff --git a/src/convert_time.c b/src/convert_time.c index 86d93ca..2114697 100644 --- a/src/convert_time.c +++ b/src/convert_time.c @@ -36,11 +36,13 @@ double converter_sec_in_hour(double sec){ return time; } +// Converts Days in Years double converter_days_to_years(double days){ double time = days / 365; return time; } +// convert Years in days double converter_years_to_days(double years){ double amount = years * 365; return amount; diff --git a/src/convert_ton_to_kg.c b/src/convert_ton_to_kg.c index 44202e2..d1653d8 100644 --- a/src/convert_ton_to_kg.c +++ b/src/convert_ton_to_kg.c @@ -3,5 +3,5 @@ double tons_to_kg(double tons) { - return tons * 1000.0; // 1 ton = 1000 kilograms + return tons * 1000.0; } diff --git a/src/convert_ton_to_kg.h b/src/convert_ton_to_kg.h index 3ad55e5..9591ea6 100644 --- a/src/convert_ton_to_kg.h +++ b/src/convert_ton_to_kg.h @@ -3,6 +3,12 @@ #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); 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/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/main.c b/src/main.c index befd311..56241ed 100644 --- a/src/main.c +++ b/src/main.c @@ -6,14 +6,20 @@ 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) { @@ -21,7 +27,9 @@ int main() { } } + // 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; diff --git a/src/operationHandler.c b/src/operationHandler.c index 6fb44b8..df85a49 100644 --- a/src/operationHandler.c +++ b/src/operationHandler.c @@ -3,6 +3,7 @@ #include #include #include +#include // checking integer input as operation id bool checkOperationInput(int input) { @@ -94,4 +95,28 @@ int extractFirstNumber(char* str) { 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 index 58ca89a..ad14eb2 100644 --- a/src/operationHandler.h +++ b/src/operationHandler.h @@ -10,4 +10,14 @@ 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 index 0a9a956..6dbdfaf 100644 --- a/src/trigonometricFunctions.c +++ b/src/trigonometricFunctions.c @@ -3,20 +3,21 @@ #include "math.h" #include #include - +//function for trigonometric +//Function 1 double double* calculate_sin_double(double angle) { double* result= malloc(sizeof (double)); *result=sin(angle); return result; } - +//Function 2 double double* calculate_cos_double(double angle) { double* result= malloc(sizeof (double)); *result=cos(angle); return result; } - +//Function 3 double double* calculate_tan_double(double angle) { double* result= malloc(sizeof (double)); diff --git a/team.md b/team.md index 317f693..bb39ebd 100644 --- a/team.md +++ b/team.md @@ -1,4 +1,5 @@ - Eric Bagus, fdai7812 +- fdai7812, fdai7812 - Leon Wolf, fdai7845 - Sandro Welte, fdai7728 - Jonas Zitzmann, fdai7791 \ No newline at end of file diff --git a/test/test_operationHandler.c b/test/test_operationHandler.c index 1cdfb1d..cadef23 100644 --- a/test/test_operationHandler.c +++ b/test/test_operationHandler.c @@ -46,4 +46,11 @@ void test_operationHandler_extractingFirstNumber(void) { char str[] = {'4', '8', ' ', '5', '\0'}; int result = extractFirstNumber(str); TEST_ASSERT_EQUAL_INT(expectedResult, result); -} \ No newline at end of file +} + +void test_operationHandler_removefirstnumberfromoriginalstring(void) { + char expected[] = {'5', '\0'}; + char str[] = {'4', '8', ' ', '5', '\0'}; + extractFirstNumber(str); + TEST_ASSERT_EQUAL_STRING(expected, str); +}