From b286688bded4247f21379de7ad8bb83013ba49a8 Mon Sep 17 00:00:00 2001 From: Sandro Welte Date: Thu, 8 Feb 2024 17:24:17 +0100 Subject: [PATCH] added meter to foot and cm to inch converter --- src/convert_m_to_ft.c | 23 +++++++++++++++++++++++ src/convert_m_to_ft.h | 8 ++++++++ 2 files changed, 31 insertions(+) create mode 100644 src/convert_m_to_ft.c create mode 100644 src/convert_m_to_ft.h diff --git a/src/convert_m_to_ft.c b/src/convert_m_to_ft.c new file mode 100644 index 0000000..021c6b3 --- /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; // Centimeters to inches + } else { + printf("Invalid units or conversion not supported.\n"); + result = -1; // Error code + } + + return result; +} + diff --git a/src/convert_m_to_ft.h b/src/convert_m_to_ft.h new file mode 100644 index 0000000..6b9317c --- /dev/null +++ b/src/convert_m_to_ft.h @@ -0,0 +1,8 @@ + +#ifndef THEADMIRALS_CONVERT_M_TO_FT_H +#define THEADMIRALS_CONVERT_M_TO_FT_H + + +float convert_length(float value, char from_unit, char to_unit); + +#endif //THEADMIRALS_CONVERT_M_TO_FT_H