Browse Source

Merge branch 'develop' into 'main'

develop in main

See merge request fdai7812/theadmirals!2
main
fdai7812 11 months ago
parent
commit
d58f182e26
  1. 12
      src/arithmeticAddition.c
  2. 31
      src/arithmeticDivision.c
  3. 6
      src/arithmeticDivision.h
  4. 2
      src/arithmeticMultiplication_Double.c
  5. 2
      src/arithmeticMultiplication_Float.c
  6. 4
      src/arithmeticMultiplication_Int.c
  7. 10
      src/arithmeticSubtraction.c
  8. 7
      src/calculateFactorial.c
  9. 2
      src/convert_CM_in_M.c
  10. 5
      src/convert_M_in_KM.c
  11. 7
      src/convert_cm_in_dm.c
  12. 7
      src/convert_cm_in_dm.h
  13. 6
      src/convert_g_to_mg.c
  14. 17
      src/convert_g_to_mg.h
  15. 6
      src/convert_kg_to_g.c
  16. 18
      src/convert_kg_to_g.h
  17. 2
      src/convert_kg_to_ton.c
  18. 6
      src/convert_kg_to_ton.h
  19. 4
      src/convert_m_to_ft.c
  20. 7
      src/convert_m_to_ft.h
  21. 2
      src/convert_time.c
  22. 2
      src/convert_ton_to_kg.c
  23. 6
      src/convert_ton_to_kg.h
  24. 7
      src/convert_ton_to_mt.c
  25. 13
      src/convert_ton_to_mt.h
  26. 58
      src/inputHandler.c
  27. 12
      src/inputHandler.h
  28. 8
      src/main.c
  29. 25
      src/operationHandler.c
  30. 10
      src/operationHandler.h
  31. 7
      src/trigonometricFunctions.c
  32. 1
      team.md
  33. 7
      test/test_operationHandler.c

12
src/arithmeticAddition.c

@ -12,20 +12,20 @@ int* addition_integer(int num1, int num2) {
return result; return result;
} }
double* addition_double(double number1, double number2) {
double* addition_double(double num1, double num2) {
double* result = malloc(sizeof(double)); double* result = malloc(sizeof(double));
*result = number1+number2;
*result = num1+num2;
return result; return result;
} }
float* addition_float(float number1, float number2) {
float* addition_float(float num1, float num2) {
float* result = malloc(sizeof(float)); float* result = malloc(sizeof(float));
*result = number1+number2;
*result = num1+num2;
return result; return result;
} }
long* addition_long(long number1, long number2) {
long* addition_long(long num1, long num2) {
long* result = malloc(sizeof(long)); long* result = malloc(sizeof(long));
*result = number1+number2;
*result = num1+num2;
return result; return result;
} }

31
src/arithmeticDivision.c

@ -4,6 +4,7 @@
#include <stdlib.h> #include <stdlib.h>
int* division_integer(int dividend, int divisor) { int* division_integer(int dividend, int divisor) {
// division with 0 would cause errors
if(divisor == 0) { if(divisor == 0) {
return NULL; return NULL;
} }
@ -15,3 +16,33 @@ int* division_integer(int dividend, int divisor) {
*result = dividend / divisor; *result = dividend / divisor;
return result; 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;
}

6
src/arithmeticDivision.h

@ -3,4 +3,10 @@
int* division_integer(int, int); int* division_integer(int, int);
long* division_long(long, long);
double* division_double(double, double);
float* division_float(float, float);
#endif //THEADMIRALS_ARITHMETICDIVISION_H #endif //THEADMIRALS_ARITHMETICDIVISION_H

2
src/arithmeticMultiplication_Double.c

@ -5,7 +5,7 @@
double* multiplication_double(double a, double b) { double* multiplication_double(double a, double b) {
double* result = (double*)malloc(sizeof(double)); double* result = (double*)malloc(sizeof(double));
if (result == NULL) { 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 *result = a * b; // we multiply a times b and the result gets saved in *result
return result; return result;

2
src/arithmeticMultiplication_Float.c

@ -4,7 +4,7 @@
float* multiplication_float(float a, float b) { float* multiplication_float(float a, float b) {
float* result = (float*)malloc(sizeof(float)); float* result = (float*)malloc(sizeof(float));
if (result == NULL) { if (result == NULL) {
return NULL; // Handle memory allocation failure
return NULL;
} }
*result = a * b; *result = a * b;
return result; return result;

4
src/arithmeticMultiplication_Int.c

@ -1,15 +1,11 @@
#include "arithmeticMultiplication_Int.h" #include "arithmeticMultiplication_Int.h"
#include <stdlib.h> #include <stdlib.h>
int* multiplication_integer(int a, int b) { int* multiplication_integer(int a, int b) {
int *result = (int*)malloc(sizeof(int)); int *result = (int*)malloc(sizeof(int));
if (result == NULL) { if (result == NULL) {
// Handle memory allocation failure
return NULL; return NULL;
} }
*result = a * b; *result = a * b;
return result; return result;

10
src/arithmeticSubtraction.c

@ -1,20 +1,22 @@
#include "arithmeticSubtraction.h" #include "arithmeticSubtraction.h"
#include <stdlib.h> #include <stdlib.h>
//arithmetic Subtraction specification integer
// arithmetic subtraction integer
int* subtraction_integer(int a, int b) { int* subtraction_integer(int a, int b) {
int* result= malloc(sizeof (int)); int* result= malloc(sizeof (int));
*result=a - b; *result=a - b;
return result; return result;
} }
//arithmetic Subtraction specification float
// arithmetic subtraction float
float* subtraction_float(float a, float b) { float* subtraction_float(float a, float b) {
float* result= malloc(sizeof (float)); float* result= malloc(sizeof (float));
*result=a - b; *result=a - b;
return result; return result;
} }
//arithmetic Subtraction specification double
// arithmetic subtraction double
double* subtraction_double(double a, double b) { double* subtraction_double(double a, double b) {
double* result= malloc(sizeof (double)); double* result= malloc(sizeof (double));
*result=a - b; *result=a - b;

7
src/calculateFactorial.c

@ -1,8 +1,8 @@
#include "calculateFactorial.h" #include "calculateFactorial.h"
//Factorial
// Function for Factorial
// Function for Factorial with integer
int calculateFactorial_integer(int n) { int calculateFactorial_integer(int n) {
if (n < 0) { if (n < 0) {
@ -17,4 +17,7 @@ int calculateFactorial_integer(int n) {
} }
return result; return result;
} }
return 0;
} }

2
src/convert_CM_in_M.c

@ -1,7 +1,7 @@
#include "convert_CM_in_M.h" #include "convert_CM_in_M.h"
#include <stdio.h> #include <stdio.h>
//convert length
//convert length for float cm into meter
float cm_to_meter(float cm) { float cm_to_meter(float cm) {
return cm / 100.0; return cm / 100.0;
} }

5
src/convert_M_in_KM.c

@ -1,7 +1,6 @@
#include "convert_M_in_KM.h" #include "convert_M_in_KM.h"
#include <stdio.h>
//convert length
// convert length for double metre into kilometre
double meter_to_kilometer(double meter) { double meter_to_kilometer(double meter) {
return meter / 1000.0; return meter / 1000.0;
} }

7
src/convert_cm_in_dm.c

@ -0,0 +1,7 @@
#include "convert_cm_in_dm.h"
#include <stdio.h>
//convert length for double cm into dm
double cm_to_dm(double cm) {
return cm / 10.0;
}

7
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

6
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;
}

17
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

6
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;
}

18
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

2
src/convert_kg_to_ton.c

@ -2,5 +2,5 @@
#include "convert_kg_to_ton.h" #include "convert_kg_to_ton.h"
double kg_to_tons(double kilograms) { double kg_to_tons(double kilograms) {
return kilograms / 1000.0; // 1 ton = 1000 kilograms
return kilograms / 1000.0;
} }

6
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); double kg_to_tons(double kilograms);

4
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') { } else if (from_unit == 'i' && to_unit == 'c') {
result = value * 2.54; // Inches to centimeters result = value * 2.54; // Inches to centimeters
} else if (from_unit == 'c' && to_unit == 'i') { } else if (from_unit == 'c' && to_unit == 'i') {
result = value / 2.54; // Centimeters to inches
result = value / 2.54;
} else { } else {
printf("Invalid units or conversion not supported.\n"); printf("Invalid units or conversion not supported.\n");
result = -1; // Error code result = -1; // Error code
@ -20,4 +20,4 @@ float convert_length(float value, char from_unit, char to_unit) {
return result; return result;
} }
// function converts between cm to inches and vice versa, and between m to ft and vice versa

7
src/convert_m_to_ft.h

@ -2,6 +2,13 @@
#ifndef THEADMIRALS_CONVERT_M_TO_FT_H #ifndef THEADMIRALS_CONVERT_M_TO_FT_H
#define 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); float convert_length(float value, char from_unit, char to_unit);

2
src/convert_time.c

@ -36,11 +36,13 @@ double converter_sec_in_hour(double sec){
return time; return time;
} }
// Converts Days in Years
double converter_days_to_years(double days){ double converter_days_to_years(double days){
double time = days / 365; double time = days / 365;
return time; return time;
} }
// convert Years in days
double converter_years_to_days(double years){ double converter_years_to_days(double years){
double amount = years * 365; double amount = years * 365;
return amount; return amount;

2
src/convert_ton_to_kg.c

@ -3,5 +3,5 @@
double tons_to_kg(double tons) { double tons_to_kg(double tons) {
return tons * 1000.0; // 1 ton = 1000 kilograms
return tons * 1000.0;
} }

6
src/convert_ton_to_kg.h

@ -3,6 +3,12 @@
#ifndef THEADMIRALS_CONVERT_TON_TO_KG_H #ifndef THEADMIRALS_CONVERT_TON_TO_KG_H
#define 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); double tons_to_kg(double tons);

7
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;
}

13
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

58
src/inputHandler.c

@ -0,0 +1,58 @@
#include "inputHandler.h"
#include <stdio.h>
// 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;
}

12
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

8
src/main.c

@ -6,14 +6,20 @@ char buffer[100];
int main() { int main() {
printf("Please enter the id of a specific operation...\n1. addition\n2. subtraction\n3. multiplication\n4. division\n"); 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; int input;
scanf("%d", &input); scanf("%d", &input);
// check if operation input is valid
if(!checkOperationInput(input)) { if(!checkOperationInput(input)) {
printf("Invalid operation id\n"); printf("Invalid operation id\n");
return 0; return 0;
} }
printf("\nPlease enter the first and the second number separated by a space...\n"); 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)) { while(fgets(buffer, 100, stdin)) {
buffer[strcspn(buffer, "\n")] = '\0'; buffer[strcspn(buffer, "\n")] = '\0';
if (strlen(buffer) > 0) { if (strlen(buffer) > 0) {
@ -21,7 +27,9 @@ int main() {
} }
} }
// extracting numbers from input
int* result = evaluateInput(buffer, input); int* result = evaluateInput(buffer, input);
if(result == NULL) { if(result == NULL) {
printf("\nInvalid formatting. Two numbers need to be separated by a space\n"); printf("\nInvalid formatting. Two numbers need to be separated by a space\n");
return 0; return 0;

25
src/operationHandler.c

@ -3,6 +3,7 @@
#include <ctype.h> #include <ctype.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h>
// checking integer input as operation id // checking integer input as operation id
bool checkOperationInput(int input) { bool checkOperationInput(int input) {
@ -95,3 +96,27 @@ int extractFirstNumber(char* str) {
return number; 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#################################");
}

10
src/operationHandler.h

@ -10,4 +10,14 @@ bool containsTwoNumbers(const char*);
int extractFirstNumber(char*); int extractFirstNumber(char*);
void outputInteger(int);
void outputDouble(double);
void outputFloat(float);
void outputLong(long);
void outputTemplate();
#endif //THEADMIRALS_OPERATIONHANDLER_H #endif //THEADMIRALS_OPERATIONHANDLER_H

7
src/trigonometricFunctions.c

@ -3,20 +3,21 @@
#include "math.h" #include "math.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
//function for trigonometric
//Function 1 double
double* calculate_sin_double(double angle) { double* calculate_sin_double(double angle) {
double* result= malloc(sizeof (double)); double* result= malloc(sizeof (double));
*result=sin(angle); *result=sin(angle);
return result; return result;
} }
//Function 2 double
double* calculate_cos_double(double angle) { double* calculate_cos_double(double angle) {
double* result= malloc(sizeof (double)); double* result= malloc(sizeof (double));
*result=cos(angle); *result=cos(angle);
return result; return result;
} }
//Function 3 double
double* calculate_tan_double(double angle) { double* calculate_tan_double(double angle) {
double* result= malloc(sizeof (double)); double* result= malloc(sizeof (double));

1
team.md

@ -1,4 +1,5 @@
- Eric Bagus, fdai7812 - Eric Bagus, fdai7812
- fdai7812, fdai7812
- Leon Wolf, fdai7845 - Leon Wolf, fdai7845
- Sandro Welte, fdai7728 - Sandro Welte, fdai7728
- Jonas Zitzmann, fdai7791 - Jonas Zitzmann, fdai7791

7
test/test_operationHandler.c

@ -47,3 +47,10 @@ void test_operationHandler_extractingFirstNumber(void) {
int result = extractFirstNumber(str); int result = extractFirstNumber(str);
TEST_ASSERT_EQUAL_INT(expectedResult, result); 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);
}
Loading…
Cancel
Save