Skip to content

Instantly share code, notes, and snippets.

@abderraouf-adjal
Created December 17, 2016 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abderraouf-adjal/a2d9e62274a72ae5fa40167bea354089 to your computer and use it in GitHub Desktop.
Save abderraouf-adjal/a2d9e62274a72ae5fa40167bea354089 to your computer and use it in GitHub Desktop.
Conversion of units of temperature
/* Conversion of units of temperature */
/*
* Copyright (c) 2014 Abderraouf Adjal
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/* to compile: gcc -std=c99 -O2 main.c -o tuc */
#include <stdio.h>
#include <string.h> /* strcmp() */
#include <sysexits.h> /* EX_OK, EX_USAGE, EX_DATAERR */
#include <stdlib.h> /* atof() */
int is_valid_celsius_temperature(float c);
int is_valid_kelvin_temperature(float k);
int is_valid_fahrenheit_temperature(float f);
float celsius_to_fahrenheit(float c);
float celsius_to_kelvin(float c);
float fahrenheit_to_celsius(float f);
float fahrenheit_to_kelvin(float f);
float kelvin_to_celsius(float k);
float kelvin_to_fahrenheit(float k);
int main(int argc, char **argv)
{
float temperature;
if (argc != 4)
{
if (argc != 1)
{
fprintf(stderr, "Invalid command line usage.\n");
}
fprintf(stderr, "Usage: %s IN_UNIT OUT_UNIT VALUE\n", argv[0]);
return EX_USAGE;
}
temperature = (float)atof(argv[3]);
if (!strcmp(argv[1], "c") || !strcmp(argv[1], "C"))
{ /* from celsius */
if (is_valid_celsius_temperature(temperature))
{
if (!strcmp(argv[2], "c") || !strcmp(argv[2], "C"))
{
printf("%.2f\n", temperature);
}
else if (!strcmp(argv[2], "k") || !strcmp(argv[2], "K"))
{
printf("%.2f\n", celsius_to_kelvin(temperature));
}
else if (!strcmp(argv[2], "f") || !strcmp(argv[2], "F"))
{
printf("%.2f\n", celsius_to_fahrenheit(temperature));
}
else
{ /* Invalid cmd usage */
fprintf(stderr, "Invalid command line usage.\nUsage: %s IN_UNIT OUT_UNIT VALUE\n", argv[0]);
return EX_USAGE;
}
}
else
{
fprintf(stderr, "Invalid temperature.\n");
return EX_DATAERR;
}
}
else if (!strcmp(argv[1], "k") || !strcmp(argv[1], "K"))
{ /* from kelvin */
if (is_valid_kelvin_temperature(temperature))
{
if (!strcmp(argv[2], "k") || !strcmp(argv[2], "K"))
{
printf("%.2f\n", temperature);
}
else if (!strcmp(argv[2], "c") || !strcmp(argv[2], "C"))
{
printf("%.2f\n", kelvin_to_celsius(temperature));
}
else if (!strcmp(argv[2], "f") || !strcmp(argv[2], "F"))
{
printf("%.2f\n", kelvin_to_fahrenheit(temperature));
}
else
{ /* Invalid cmd usage */
fprintf(stderr, "Invalid command line usage.\nUsage: %s IN_UNIT OUT_UNIT VALUE\n", argv[0]);
return EX_USAGE;
}
}
else
{
fprintf(stderr, "Invalid temperature.\n");
return EX_DATAERR;
}
}
else if (!strcmp(argv[1], "f") || !strcmp(argv[1], "F"))
{ /* from fahrenheit */
if (is_valid_fahrenheit_temperature(temperature))
{
if (!strcmp(argv[2], "f") || !strcmp(argv[2], "F"))
{
printf("%.2f\n", temperature);
}
else if (!strcmp(argv[2], "c") || !strcmp(argv[2], "C"))
{
printf("%.2f\n", fahrenheit_to_celsius(temperature));
}
else if (!strcmp(argv[2], "k") || !strcmp(argv[2], "K"))
{
printf("%.2f\n", fahrenheit_to_kelvin(temperature));
}
else
{ /* Invalid cmd usage */
fprintf(stderr, "Invalid command line usage.\nUsage: %s IN_UNIT OUT_UNIT VALUE\n", argv[0]);
return EX_USAGE;
}
}
else
{
fprintf(stderr, "Invalid temperature.\n");
return EX_DATAERR;
}
}
else
{ /* Invalid cmd usage */
fprintf(stderr, "Invalid command line usage.\nUsage: %s IN_UNIT OUT_UNIT VALUE\n", argv[0]);
return EX_USAGE;
}
return EX_OK;
}
/**
* return 0 if the temperature is invalid;
* return 1 if the temperature is valid;
*/
int is_valid_celsius_temperature(float c)
{
return ((c < (- 273.15) || c > 49727.0) ? 0 : 1);
}
int is_valid_kelvin_temperature(float k)
{
return ((k < 0.0 || k > 50000.0) ? 0 : 1);
}
int is_valid_fahrenheit_temperature(float f)
{
return ((f < (- 459.67) || f > 89541.0) ? 0 : 1);
}
float celsius_to_fahrenheit(float c)
{
return (c * (9.0/5.0) + 32.0);
}
float celsius_to_kelvin(float c)
{
return (c + 273.15);
}
float fahrenheit_to_celsius(float f)
{
return ((f - 32.0) * (5.0/9.0));
}
float fahrenheit_to_kelvin(float f)
{
return ((f + 459.67) * (5.0/9.0));
}
float kelvin_to_celsius(float k)
{
return (k - 273.15);
}
float kelvin_to_fahrenheit(float k)
{
return (k * (9.0/5.0) - 459.67);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment