Browse Source

Merge branch 'main' into mathespieledev

remotes/origin/factorialdev
Lennart Goetze 11 months ago
parent
commit
fda1723677
  1. 98
      src/temperatur.c
  2. 15
      src/temperatur.h
  3. 68
      test/test_temperatur.c

98
src/temperatur.c

@ -0,0 +1,98 @@
#include <stdio.h>
#include <string.h>
#include "temperatur.h"
float CzuF(float tempC){
return (tempC * 9/5) + 32;
}
float CzuK(float tempC){
return tempC + 273.15;
}
float FzuC(float tempF){
return (tempF - 32) * 5/9;
}
float FzuK(float tempF){
return (tempF - 32) * 5/9 + 273.15;
}
float KzuC(float tempK){
float tmp = tempK - 273.15;
//Variable auf 2 Nachkommastellen begrenzen
return ((int)(tmp * 100 + 0.5) / 100.0);
}
float KzuF(float tempK){
float tmp = (tempK - 273.15) * 9/5 + 32;
//Variable auf 2 Nachkommastellen begrenzen
return ((int)(tmp * 100 + .5) / 100.0);
}
const char* getFirstUnit(int input){
if(input < 3)
return "Celsius";
else if(input < 5)
return "Fahrenheit";
else if(input < 7)
return "Kelvin";
return "";
}
const char* getSecondUnit(int input){
if(input == 1 || input == 6)
return "° Fahrenheit";
else if(input == 2 || input == 4)
return " Kelvin";
else if(input == 3 || input == 5)
return "° Celsius";
return "";
}
int run_TemperaturRechner(){
float var1, var2;
int var3;
printf("\nTemperaturkonverter (Celsius, Fahrenheit, Kelvin)\nAchten Sie darauf, Kommazahlen mit einem Punkt anzugeben!\n\n");
while(1){
printf("Waehlen Sie: \n<1> Celsius zu Fahrenheit\n<2> Celsius zu Kelvin\n<3> Fahrenheit zu Celsius\n<4> Fahrenheit zu Kelvin\n<5> Kelvin zu Celsius\n<6> Kelvin zu Fahrenheit\n<7> Zum beenden\nAuswahl: ");
scanf("%d", &var3);
if(var3 < 1 || var3 > 7){
printf("Eingabe nicht zulaessig!");
break;
}
else if(var3 == 7)
break;
printf("\nGeben Sie die Temperatur in %s ein: ", getFirstUnit(var3));
scanf("%f", &var1);
switch(var3){
case 1:
var2 = CzuF(var1);
break;
case 2:
var2 = CzuK(var1);
break;
case 3:
var2 = FzuC(var1);
break;
case 4:
var2 = FzuK(var1);
break;
case 5:
var2 = KzuC(var1);
break;
case 6:
var2 = KzuF(var1);
break;
}
printf(" = %.2f%s\n\n", var2, getSecondUnit(var3));
}
return 0;
}

15
src/temperatur.h

@ -0,0 +1,15 @@
#ifndef TEMPERATUR_H
#define TEMPERATUR_H
float CzuF(float tempC);
float CzuK(float tempC);
float FzuC(float tempF);
float FzuK(float tempF);
float KzuC(float tempK);
float KzuF(float tempK);
const char* getFirstUnit(int input);
const char* getSecondUnit(int input);
#endif //TEMPERATUR_H

68
test/test_temperatur.c

@ -0,0 +1,68 @@
#ifdef TEST
#include "unity.h"
#include "temperatur.h"
void setUp(void)
{
}
void tearDown(void)
{
}
void test_temperaturRechner_CzuF(void)
{
float result = CzuF(25);
float expected = 77;
TEST_ASSERT_EQUAL(expected, result);
}
void test_temperaturRechner_CzuK(void)
{
float result = CzuK(25);
float expected = 298.15;
TEST_ASSERT_EQUAL(expected, result);
}
void test_temperaturRechner_FzuC(void)
{
float result = FzuC(77);
float expected = 25;
TEST_ASSERT_EQUAL(expected, result);
}
void test_temperaturRechner_FzuK(void)
{
float result = FzuK(77);
float expected = 298.15;
TEST_ASSERT_EQUAL(expected, result);
}
void test_temperaturRechner_KzuC(void)
{
float result = KzuC(298.15);
float expected = 25;
TEST_ASSERT_EQUAL(expected, result);
}
void test_temperaturRechner_KzuF(void)
{
float result = KzuF(298.15);
float expected = 77;
TEST_ASSERT_EQUAL(expected, result);
}
void test_returnUnit_first(void)
{
const char *result = getFirstUnit(1);
char expected[] = "Celsius";
TEST_ASSERT_EQUAL_STRING(expected, result);
}
void test_returnUnit_second(void)
{
const char *result = getSecondUnit(2);
char expected[] = " Kelvin";
TEST_ASSERT_EQUAL_STRING(expected, result);
}
#endif
Loading…
Cancel
Save