Menu
Coddy logo textTech

Wszystko razem

Lekcja 10 z 10 w kursie Konwerter temperatur w C++ w Coddy.

challenge icon

Wyzwanie

Łatwy

Teraz połączmy wszystko w całość. Mamy wybór użytkownika, temperaturę i funkcje konwertujące.

Wyświetl odpowiedni wynik na podstawie danych wprowadzonych przez użytkownika w następującym formacie:

Temperature in X: Y

Gdzie X to nazwa skali temperatury (Celsjusz, kelwin lub Fahrenheit), a Y to przeliczona wartość.

Sprawdź przypadki testowe, aby zobaczyć przykłady!

Spróbuj swoich sił

#include <iostream>
using namespace std;

float celsiusToFahrenheit(float celsius) {
    return (celsius * 9.0 / 5.0) + 32.0;
}

double celsiusToKelvin(double celsius) {
    return celsius + 273.15;
}

double fahrenheitToCelsius(double fahrenheit) {
    return (fahrenheit - 32.0) * 5.0 / 9.0;
}

double fahrenheitToKelvin(double fahrenheit) {
    return (fahrenheit + 459.67) * 5.0 / 9.0;
}

double kelvinToCelsius(double kelvin) {
    return kelvin - 273.15;
}

double kelvinToFahrenheit(double kelvin) {
    return kelvin * 9.0 / 5.0 - 459.67;
}

int main() {
    int choice;
    double temperature;

    cout << "Temperature Converter\n";
    cout << "1. Celsius to Fahrenheit\n";
    cout << "2. Celsius to Kelvin\n";
    cout << "3. Fahrenheit to Celsius\n";
    cout << "4. Fahrenheit to Kelvin\n";
    cout << "5. Kelvin to Celsius\n";
    cout << "6. Kelvin to Fahrenheit\n";
    
    cout << "Enter your choice (1-6):\n";
    cin >> choice;

    cout << "Enter the temperature:\n";
    cin >> temperature;
    
    return 0;
}

Wszystkie lekcje w sekcji Konwerter temperatur w C++

Poćwicz samodzielnie: Kompilator C++ online